First attempt to replace logger methods with lazy evaluating version

Forgot to format the message
This commit is contained in:
Bas Stottelaar
2014-04-01 14:33:34 +02:00
parent 89aff6e199
commit 2add9fa264
11 changed files with 84 additions and 87 deletions

View File

@@ -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':

View File

@@ -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)

View File

@@ -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()

View File

@@ -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:

View File

@@ -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

View File

@@ -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

View File

@@ -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<name>.*?)\s\-\s(?P<album>.*?)\s\((?P<year>\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

View File

@@ -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

View File

@@ -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

View File

@@ -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):
"""

View File

@@ -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 = {