From 2add9fa264df0f97cabea823734c2bb967f2b5ac Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Tue, 1 Apr 2014 14:33:34 +0200 Subject: [PATCH] First attempt to replace logger methods with lazy evaluating version Forgot to format the message --- Headphones.py | 4 +-- headphones/__init__.py | 30 +++++++++----------- headphones/albumart.py | 2 +- headphones/api.py | 4 +-- headphones/cache.py | 64 +++++++++++++++++++++--------------------- headphones/db.py | 6 ++-- headphones/helpers.py | 26 ++++++++--------- headphones/importer.py | 8 +++--- headphones/lastfm.py | 23 ++++++++------- headphones/logger.py | 2 +- headphones/webstart.py | 2 +- 11 files changed, 84 insertions(+), 87 deletions(-) diff --git a/Headphones.py b/Headphones.py index 5b4a42de..f982b2e9 100755 --- a/Headphones.py +++ b/Headphones.py @@ -146,7 +146,7 @@ def main(): # Force the http port if neccessary if args.port: http_port = args.port - logger.info('Using forced port: %i' % http_port) + logger.info('Using forced port: %i', http_port) else: http_port = int(headphones.HTTP_PORT) @@ -176,7 +176,7 @@ def main(): except KeyboardInterrupt: headphones.SIGNAL = 'shutdown' else: - logger.info('Received signal: ' + headphones.SIGNAL) + logger.info('Received signal: %d', headphones.SIGNAL) if headphones.SIGNAL == 'shutdown': headphones.shutdown() elif headphones.SIGNAL == 'restart': diff --git a/headphones/__init__.py b/headphones/__init__.py index 76563a03..ff7f8622 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -306,7 +306,7 @@ def check_setting_int(config, cfg_name, item_name, def_val): except: config[cfg_name] = {} config[cfg_name][item_name] = my_val - logger.debug(item_name + " -> " + str(my_val)) + logger.debug("%s -> %s", item_name, my_val) return my_val ################################################################################ @@ -323,10 +323,7 @@ def check_setting_str(config, cfg_name, item_name, def_val, log=True): config[cfg_name] = {} config[cfg_name][item_name] = my_val - if log: - logger.debug(item_name + " -> " + my_val) - else: - logger.debug(item_name + " -> ******") + logger.debug("%s -> %s", item_name, my_val if log else "******") return my_val def initialize(): @@ -702,7 +699,7 @@ def initialize(): try: os.makedirs(CACHE_DIR) except OSError: - logger.error('Could not create cache dir. Check permissions of datadir: ' + DATA_DIR) + logger.error('Could not create cache dir. Check permissions of datadir: %s', DATA_DIR) # Sanity check for search interval. Set it to at least 6 hours if SEARCH_INTERVAL < 360: @@ -714,7 +711,7 @@ def initialize(): try: dbcheck() except Exception, e: - logger.error("Can't connect to the database: %s" % e) + logger.error("Can't connect to the database: %s", e) # Get the currently installed version - returns None, 'win32' or the git hash # Also sets INSTALL_TYPE variable to 'win', 'git' or 'source' @@ -751,7 +748,7 @@ def daemonize(): if pid != 0: sys.exit(0) except OSError, e: - raise RuntimeError("1st fork failed: %s [%d]" % (e.strerror, e.errno)) + raise RuntimeError("1st fork failed: %s [%d]", e.strerror, e.errno) os.setsid() @@ -765,7 +762,7 @@ def daemonize(): if pid != 0: sys.exit(0) except OSError, e: - raise RuntimeError("2nd fork failed: %s [%d]" % (e.strerror, e.errno)) + raise RuntimeError("2nd fork failed: %s [%d]", e.strerror, e.errno) dev_null = file('/dev/null', 'r') os.dup2(dev_null.fileno(), sys.stdin.fileno()) @@ -782,8 +779,9 @@ def daemonize(): logger.info('Daemonized to PID: %s' % pid) if CREATEPID: - logger.info("Writing PID " + pid + " to " + str(PIDFILE)) - file(PIDFILE, 'w').write("%s\n" % pid) + logger.info("Writing PID %d to %s", pid, PIDFILE) + with file(PIDFILE, 'w') as fp: + fp.write("%s\n" % pid) def launch_browser(host, port, root): @@ -798,7 +796,7 @@ def launch_browser(host, port, root): try: webbrowser.open('%s://%s:%i%s' % (protocol, host, port, root)) except Exception, e: - logger.error('Could not launch browser: %s' % e) + logger.error('Could not launch browser: %s', e) def config_write(): @@ -1082,7 +1080,7 @@ def start(): def sig_handler(signum=None, frame=None): if type(signum) != type(None): - logger.info("Signal %i caught, saving and exiting..." % int(signum)) + logger.info("Signal %i caught, saving and exiting...", signum) shutdown() def dbcheck(): @@ -1301,10 +1299,10 @@ def shutdown(restart=False, update=False): try: versioncheck.update() except Exception, e: - logger.warn('Headphones failed to update: %s. Restarting.' % e) + logger.warn('Headphones failed to update: %s. Restarting.', e) if CREATEPID : - logger.info ('Removing pidfile %s' % PIDFILE) + logger.info ('Removing pidfile %s', PIDFILE) os.remove(PIDFILE) if restart: @@ -1313,7 +1311,7 @@ def shutdown(restart=False, update=False): popen_list += ARGS if '--nolaunch' not in popen_list: popen_list += ['--nolaunch'] - logger.info('Restarting Headphones with ' + str(popen_list)) + logger.info('Restarting Headphones with %s', popen_list) subprocess.Popen(popen_list, cwd=os.getcwd()) os._exit(0) diff --git a/headphones/albumart.py b/headphones/albumart.py index ee437bb2..88406858 100644 --- a/headphones/albumart.py +++ b/headphones/albumart.py @@ -44,7 +44,7 @@ def getCachedArt(albumid): artwork = urllib2.urlopen(artwork_path, timeout=20).read() return artwork except: - logger.warn("Unable to open url: " + artwork_path) + logger.warn("Unable to open url: %s", artwork_path) return None else: artwork = open(artwork_path, "r").read() diff --git a/headphones/api.py b/headphones/api.py index a689ec1d..49405b6b 100644 --- a/headphones/api.py +++ b/headphones/api.py @@ -78,8 +78,8 @@ class Api(object): def fetchData(self): - if self.data == 'OK': - logger.info('Recieved API command: ' + self.cmd) + if self.data == 'OK': + logger.info('Recieved API command: %s', self.cmd) methodToCall = getattr(self, "_" + self.cmd) result = methodToCall(**self.kwargs) if 'callback' not in self.kwargs: diff --git a/headphones/cache.py b/headphones/cache.py index 78cd0c47..25acb19d 100644 --- a/headphones/cache.py +++ b/headphones/cache.py @@ -218,12 +218,12 @@ class Cache(object): } url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params) - logger.debug('Retrieving artist information from: ' + url) + logger.debug('Retrieving artist information from: %s', url) try: result = urllib2.urlopen(url, timeout=20).read() except: - logger.warn('Could not open url: ' + url) + logger.warn('Could not open url: %s', url) return if result: @@ -231,18 +231,18 @@ class Cache(object): try: data = simplejson.JSONDecoder().decode(result) except: - logger.warn('Could not parse data from url: ' + url) + logger.warn('Could not parse data from url: %s', url) return try: image_url = data['artist']['image'][-1]['#text'] except Exception: - logger.debug('No artist image found on url: ' + url) + logger.debug('No artist image found on url: %s', url) image_url = None thumb_url = self._get_thumb_url(data) if not thumb_url: - logger.debug('No artist thumbnail image found on url: ' + url) + logger.debug('No artist thumbnail image found on url: %s', url) else: @@ -255,12 +255,12 @@ class Cache(object): } url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params) - logger.debug('Retrieving album information from: ' + url) + logger.debug('Retrieving album information from: %s', url) try: result = urllib2.urlopen(url, timeout=20).read() except: - logger.warn('Could not open url: ' + url) + logger.warn('Could not open url: %s', url) return if result: @@ -268,19 +268,19 @@ class Cache(object): try: data = simplejson.JSONDecoder().decode(result) except: - logger.warn('Could not parse data from url: ' + url) + logger.warn('Could not parse data from url: %s', url) return try: image_url = data['artist']['image'][-1]['#text'] except Exception: - logger.debug('No artist image found on url: ' + url) + logger.debug('No artist image found on url: %s', url) image_url = None thumb_url = self._get_thumb_url(data) if not thumb_url: - logger.debug('No artist thumbnail image found on url: ' + url) + logger.debug('No artist thumbnail image found on url: %s', url) image_dict = {'artwork' : image_url, 'thumbnail' : thumb_url } return image_dict @@ -301,12 +301,12 @@ class Cache(object): } url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params) - logger.debug('Retrieving artist information from: ' + url) + logger.debug('Retrieving artist information from: %s', url) try: result = urllib2.urlopen(url, timeout=20).read() except: - logger.warn('Could not open url: ' + url) + logger.warn('Could not open url: %s', url) return if result: @@ -314,27 +314,27 @@ class Cache(object): try: data = simplejson.JSONDecoder().decode(result) except: - logger.warn('Could not parse data from url: ' + url) + logger.warn('Could not parse data from url: %s', url) return try: self.info_summary = data['artist']['bio']['summary'] except Exception: - logger.debug('No artist bio summary found on url: ' + url) + logger.debug('No artist bio summary found on url: %s', url) self.info_summary = None try: self.info_content = data['artist']['bio']['content'] except Exception: - logger.debug('No artist bio found on url: ' + url) + logger.debug('No artist bio found on url: %s', url) self.info_content = None try: image_url = data['artist']['image'][-1]['#text'] except Exception: - logger.debug('No artist image found on url: ' + url) + logger.debug('No artist image found on url: %s', url) image_url = None thumb_url = self._get_thumb_url(data) if not thumb_url: - logger.debug('No artist thumbnail image found on url: ' + url) + logger.debug('No artist thumbnail image found on url: %s', url) else: @@ -349,39 +349,39 @@ class Cache(object): url = "http://ws.audioscrobbler.com/2.0/?" + urllib.urlencode(params) - logger.debug('Retrieving artist information from: ' + url) + logger.debug('Retrieving artist information from: %s', url) try: result = urllib2.urlopen(url, timeout=20).read() except: - logger.warn('Could not open url: ' + url) + logger.warn('Could not open url: %s', url) return if result: try: data = simplejson.JSONDecoder().decode(result) except: - logger.warn('Could not parse data from url: ' + url) + logger.warn('Could not parse data from url: %s', url) return try: self.info_summary = data['album']['wiki']['summary'] except Exception: - logger.debug('No album summary found from: ' + url) + logger.debug('No album summary found from: %s', url) self.info_summary = None try: self.info_content = data['album']['wiki']['content'] except Exception: - logger.debug('No album infomation found from: ' + url) + logger.debug('No album infomation found from: %s', url) self.info_content = None try: image_url = data['album']['image'][-1]['#text'] except Exception: - logger.debug('No album image link found on url: ' + url) + logger.debug('No album image link found on url: %s', url) image_url = None thumb_url = self._get_thumb_url(data) if not thumb_url: - logger.debug('No album thumbnail image found on url: ' + url) + logger.debug('No album thumbnail image found on url: %s', url) #Save the content & summary to the database no matter what if we've opened up the url if self.id_type == 'artist': @@ -414,7 +414,7 @@ class Cache(object): try: artwork = urllib2.urlopen(image_url, timeout=20).read() except Exception, e: - logger.error('Unable to open url "' + image_url + '". Error: ' + str(e)) + logger.error('Unable to open url "%s". Error: %s', image_url, e) artwork = None if artwork: @@ -424,7 +424,7 @@ class Cache(object): try: os.makedirs(self.path_to_art_cache) except Exception, e: - logger.error('Unable to create artwork cache dir. Error: ' + str(e)) + logger.error('Unable to create artwork cache dir. Error: %s', e) self.artwork_errors = True self.artwork_url = image_url @@ -433,7 +433,7 @@ class Cache(object): try: os.remove(artwork_file) except: - logger.error('Error deleting file from the cache: ' + artwork_file) + logger.error('Error deleting file from the cache: %s', artwork_file) ext = os.path.splitext(image_url)[1] @@ -443,7 +443,7 @@ class Cache(object): f.write(artwork) f.close() except Exception, e: - logger.error('Unable to write to the cache dir: ' + str(e)) + logger.error('Unable to write to the cache dir: %s', e) self.artwork_errors = True self.artwork_url = image_url @@ -453,7 +453,7 @@ class Cache(object): try: artwork = urllib2.urlopen(thumb_url, timeout=20).read() except Exception, e: - logger.error('Unable to open url "' + thumb_url + '". Error: ' + str(e)) + logger.error('Unable to open url "%s". Error: %s', thumb_url, e) artwork = None if artwork: @@ -463,7 +463,7 @@ class Cache(object): try: os.makedirs(self.path_to_art_cache) except Exception, e: - logger.error('Unable to create artwork cache dir. Error: ' + str(e)) + logger.error('Unable to create artwork cache dir. Error: %s' + e) self.thumb_errors = True self.thumb_url = thumb_url @@ -472,7 +472,7 @@ class Cache(object): try: os.remove(thumb_file) except: - logger.error('Error deleting file from the cache: ' + thumb_file) + logger.error('Error deleting file from the cache: %s', thumb_file) ext = os.path.splitext(image_url)[1] @@ -482,7 +482,7 @@ class Cache(object): f.write(artwork) f.close() except Exception, e: - logger.error('Unable to write to the cache dir: ' + str(e)) + logger.error('Unable to write to the cache dir: %s', e) self.thumb_errors = True self.thumb_url = image_url diff --git a/headphones/db.py b/headphones/db.py index c0b9bba3..7068ba0e 100644 --- a/headphones/db.py +++ b/headphones/db.py @@ -73,14 +73,14 @@ class DBConnection: break except sqlite3.OperationalError, e: if "unable to open database file" in e.message or "database is locked" in e.message: - logger.warn('Database Error: %s' % e) + logger.warn('Database Error: %s', e) attempt += 1 time.sleep(1) else: - logger.error('Database error: %s' % e) + logger.error('Database error: %s', e) raise except sqlite3.DatabaseError, e: - logger.error('Fatal Error executing %s :: %s' % (query, e)) + logger.error('Fatal Error executing %s :: %s', query, e) raise return sqlResult diff --git a/headphones/helpers.py b/headphones/helpers.py index 0bf10c9b..3cd14e3c 100644 --- a/headphones/helpers.py +++ b/headphones/helpers.py @@ -341,7 +341,7 @@ def extract_song_data(s): year = match.group("year") return (name, album, year) else: - logger.info("Couldn't parse " + s + " into a valid default format") + logger.info("Couldn't parse %s into a valid default format", s) #newzbin default format pattern = re.compile(r'(?P.*?)\s\-\s(?P.*?)\s\((?P\d+?\))', re.VERBOSE) @@ -352,7 +352,7 @@ def extract_song_data(s): year = match.group("year") return (name, album, year) else: - logger.info("Couldn't parse " + s + " into a valid Newbin format") + logger.info("Couldn't parse %s into a valid Newbin format", s) return (name, album, year) def smartMove(src, dest, delete=True): @@ -363,7 +363,7 @@ def smartMove(src, dest, delete=True): filename = os.path.basename(src) if os.path.isfile(os.path.join(dest, filename)): - logger.info('Destination file exists: %s' % os.path.join(dest, filename).decode(headphones.SYS_ENCODING, 'replace')) + logger.info('Destination file exists: %s', os.path.join(dest, filename).decode(headphones.SYS_ENCODING, 'replace')) title = os.path.splitext(filename)[0] ext = os.path.splitext(filename)[1] i = 1 @@ -372,12 +372,12 @@ def smartMove(src, dest, delete=True): if os.path.isfile(os.path.join(dest, newfile)): i += 1 else: - logger.info('Renaming to %s' % newfile) - try: + logger.info('Renaming to %s', newfile) + try: os.rename(src, os.path.join(source_dir, newfile)) filename = newfile except Exception, e: - logger.warn('Error renaming %s: %s' % (src.decode(headphones.SYS_ENCODING, 'replace'), str(e).decode(headphones.SYS_ENCODING, 'replace'))) + logger.warn('Error renaming %s: %s', src.decode(headphones.SYS_ENCODING, 'replace'), e) break try: @@ -387,7 +387,7 @@ def smartMove(src, dest, delete=True): shutil.copy(os.path.join(source_dir, filename), os.path.join(dest, filename)) return True except Exception, e: - logger.warn('Error moving file %s: %s' % (filename.decode(headphones.SYS_ENCODING, 'replace'), str(e).decode(headphones.SYS_ENCODING, 'replace'))) + logger.warn('Error moving file %s: %s', filename.decode(headphones.SYS_ENCODING, 'replace'), e) ######################### #Sab renaming functions # @@ -453,12 +453,12 @@ def create_https_certificates(ssl_cert, ssl_key): Create self-signed HTTPS certificares and store in paths 'ssl_cert' and 'ssl_key' """ from headphones import logger - + try: - from OpenSSL import crypto #@UnresolvedImport - from lib.certgen import createKeyPair, createCertRequest, createCertificate, TYPE_RSA, serial #@UnresolvedImport + from OpenSSL import crypto + from lib.certgen import createKeyPair, createCertRequest, createCertificate, TYPE_RSA, serial except: - logger.warn(u"pyopenssl module missing, please install for https access") + logger.warn("pyOpenSSL module missing, please install to enable HTTPS") return False # Create the CA Certificate @@ -475,8 +475,8 @@ def create_https_certificates(ssl_cert, ssl_key): try: open(ssl_key, 'w').write(crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)) open(ssl_cert, 'w').write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert)) - except: - logger.error(u"Error creating SSL key and certificate") + except Exception, e: + logger.error("Error creating SSL key and certificate: %s", e) return False return True diff --git a/headphones/importer.py b/headphones/importer.py index ec2b19aa..3b4179af 100644 --- a/headphones/importer.py +++ b/headphones/importer.py @@ -521,10 +521,10 @@ def addReleaseById(rid): try: release_dict = mb.getRelease(rid) except Exception, e: - logger.info('Unable to get release information for Release: ' + str(rid) + " " + str(e)) + logger.info('Unable to get release information for Release %s: %s', rid, e) return if not release_dict: - logger.info('Unable to get release information for Release: ' + str(rid) + " no dict") + logger.info('Unable to get release information for Release %s: no dict', rid) return rgid = release_dict['rgid'] @@ -664,7 +664,7 @@ def getHybridRelease(fullreleaselist): '2xVinyl': '2', 'Vinyl': '2', 'CD': '0', - 'Cassette': '3', + 'Cassette': '3', '2xCD': '1', 'Digital Media': '0' } @@ -681,7 +681,7 @@ def getHybridRelease(fullreleaselist): format = 3 try: - country = int(countries[release['Country']]) + country = int(countries[release['Country']]) except: country = 3 diff --git a/headphones/lastfm.py b/headphones/lastfm.py index e7c89821..dcb6207b 100644 --- a/headphones/lastfm.py +++ b/headphones/lastfm.py @@ -82,7 +82,7 @@ def getSimilar(): myDB.action('''DELETE from lastfmcloud''') for tuple in top_list: artist_name, artist_mbid = tuple[0] - count = tuple[1] + count = tuple[1] myDB.action('INSERT INTO lastfmcloud VALUES( ?, ?, ?)', [artist_name, artist_mbid, count]) def getArtists(): @@ -96,7 +96,7 @@ def getArtists(): else: username = headphones.LASTFM_USERNAME - logger.info("Starting Last.FM artists import with username '%s'" % username) + logger.info("Starting Last.FM artists import with username '%s'", username) url = 'http://ws.audioscrobbler.com/2.0/?method=library.getartists&limit=10000&api_key=%s&user=%s' % (api_key, username) data = urllib2.urlopen(url, timeout=20).read() @@ -108,7 +108,7 @@ def getArtists(): return artists = d.getElementsByTagName("artist") - logger.info("Fetched %d artists from Last.FM" % len(artists)) + logger.info("Fetched %d artists from Last.FM", len(artists)) artistlist = [] @@ -129,7 +129,7 @@ def getArtists(): for artistid in artistlist: importer.addArtisttoDB(artistid) - logger.info("Imported %d new artists from Last.FM" % len(artistid)) + logger.info("Imported %d new artists from Last.FM", len(artistid)) def getTagTopArtists(tag, limit=50): myDB = db.DBConnection() @@ -141,7 +141,7 @@ def getTagTopArtists(tag, limit=50): try: d = minidom.parseString(data) except: - logger.error("Could not parse artist list from last.fm data") + logger.error("Could not parse artist list from Last.FM data") return artists = d.getElementsByTagName("artist") @@ -205,6 +205,7 @@ def getAlbumDescription(rgid, artist, album): myDB.upsert("descriptions", newValueDict, controlValueDict) except: + logger.exception("Unhandled exception") return def getAlbumDescriptionOld(rgid, releaselist): @@ -213,7 +214,7 @@ def getAlbumDescriptionOld(rgid, releaselist): because I may use it to fetch and cache album art """ - myDB = db.DBConnection() + myDB = db.DBConnection() result = myDB.select('SELECT Summary from descriptions WHERE ReleaseGroupID=?', [rgid]) if result: @@ -240,15 +241,13 @@ def getAlbumDescriptionOld(rgid, releaselist): summary = node.data for node in contentnode: content = node.data - + controlValueDict = {'ReleaseGroupID': rgid} newValueDict = {'ReleaseID': mbid, 'Summary': summary, 'Content': content} - myDB.upsert("descriptions", newValueDict, controlValueDict) + myDB.upsert("descriptions", newValueDict, controlValueDict) break - except: - continue - - + logger.exception("Unhandled exception") + continue \ No newline at end of file diff --git a/headphones/logger.py b/headphones/logger.py index 5541a814..f81ff405 100644 --- a/headphones/logger.py +++ b/headphones/logger.py @@ -34,7 +34,7 @@ class LogListHandler(logging.Handler): """ def emit(self, record): - headphones.LOG_LIST.insert(0, (helpers.now(), record.msg, record.levelname, record.threadName)) + headphones.LOG_LIST.insert(0, (helpers.now(), self.format(record), record.levelname, record.threadName)) def initLogger(verbose=1): """ diff --git a/headphones/webstart.py b/headphones/webstart.py index a30d3694..efc25617 100644 --- a/headphones/webstart.py +++ b/headphones/webstart.py @@ -62,7 +62,7 @@ def initialize(options={}): else: protocol = "http" - logger.info(u"Starting Headphones on " + protocol + "://" + str(options['http_host']) + ":" + str(options['http_port']) + "/") + logger.info("Starting Headphones on %s://%s:%d/", protocol, options['http_host'], options['http_port']) cherrypy.config.update(options_dict) conf = {