Code improvements to the cache

* Catch the right exceptions where possible
* Move class vars to instance vars
* Prefer 'with' statement for files
This commit is contained in:
Bas Stottelaar
2014-09-14 22:26:20 +02:00
parent ca084f1216
commit 2e6c8e6537
+35 -39
View File
@@ -42,24 +42,22 @@ class Cache(object):
path_to_art_cache = os.path.join(headphones.CACHE_DIR, 'artwork') path_to_art_cache = os.path.join(headphones.CACHE_DIR, 'artwork')
id = None
id_type = None # 'artist' or 'album' - set automatically depending on whether ArtistID or AlbumID is passed
query_type = None # 'artwork','thumb' or 'info' - set automatically
artwork_files = []
thumb_files = []
artwork_errors = False
artwork_url = None
thumb_errors = False
thumb_url = None
info_summary = None
info_content = None
def __init__(self): def __init__(self):
pass self.id = None
self.id_type = None # 'artist' or 'album' - set automatically depending on whether ArtistID or AlbumID is passed
self.query_type = None # 'artwork','thumb' or 'info' - set automatically
self.artwork_files = []
self.thumb_files = []
self.artwork_errors = False
self.artwork_url = None
self.thumb_errors = False
self.thumb_url = None
self.info_summary = None
self.info_content = None
def _findfilesstartingwith(self,pattern,folder): def _findfilesstartingwith(self,pattern,folder):
files = [] files = []
@@ -125,9 +123,9 @@ class Cache(object):
return thumb_url return thumb_url
def get_artwork_from_cache(self, ArtistID=None, AlbumID=None): def get_artwork_from_cache(self, ArtistID=None, AlbumID=None):
''' """
Pass a musicbrainz id to this function (either ArtistID or AlbumID) Pass a musicbrainz id to this function (either ArtistID or AlbumID)
''' """
self.query_type = 'artwork' self.query_type = 'artwork'
@@ -151,9 +149,9 @@ class Cache(object):
return None return None
def get_thumb_from_cache(self, ArtistID=None, AlbumID=None): def get_thumb_from_cache(self, ArtistID=None, AlbumID=None):
''' """
Pass a musicbrainz id to this function (either ArtistID or AlbumID) Pass a musicbrainz id to this function (either ArtistID or AlbumID)
''' """
self.query_type = 'thumb' self.query_type = 'thumb'
@@ -215,7 +213,7 @@ class Cache(object):
try: try:
image_url = data['artist']['image'][-1]['#text'] image_url = data['artist']['image'][-1]['#text']
except Exception: except (KeyError, IndexError):
logger.debug('No artist image found') logger.debug('No artist image found')
image_url = None image_url = None
@@ -233,7 +231,7 @@ class Cache(object):
try: try:
image_url = data['album']['image'][-1]['#text'] image_url = data['album']['image'][-1]['#text']
except Exception: except (KeyError, IndexError):
logger.debug('No album image found on last.fm') logger.debug('No album image found on last.fm')
image_url = None image_url = None
@@ -260,17 +258,17 @@ class Cache(object):
try: try:
self.info_summary = data['artist']['bio']['summary'] self.info_summary = data['artist']['bio']['summary']
except Exception: except KeyError:
logger.debug('No artist bio summary found') logger.debug('No artist bio summary found')
self.info_summary = None self.info_summary = None
try: try:
self.info_content = data['artist']['bio']['content'] self.info_content = data['artist']['bio']['content']
except Exception: except KeyError:
logger.debug('No artist bio found') logger.debug('No artist bio found')
self.info_content = None self.info_content = None
try: try:
image_url = data['artist']['image'][-1]['#text'] image_url = data['artist']['image'][-1]['#text']
except Exception: except KeyError:
logger.debug('No artist image found') logger.debug('No artist image found')
image_url = None image_url = None
@@ -288,17 +286,17 @@ class Cache(object):
try: try:
self.info_summary = data['album']['wiki']['summary'] self.info_summary = data['album']['wiki']['summary']
except Exception: except KeyError:
logger.debug('No album summary found') logger.debug('No album summary found')
self.info_summary = None self.info_summary = None
try: try:
self.info_content = data['album']['wiki']['content'] self.info_content = data['album']['wiki']['content']
except Exception: except KeyError:
logger.debug('No album infomation found') logger.debug('No album infomation found')
self.info_content = None self.info_content = None
try: try:
image_url = data['album']['image'][-1]['#text'] image_url = data['album']['image'][-1]['#text']
except Exception: except KeyError:
logger.debug('No album image link found') logger.debug('No album image link found')
image_url = None image_url = None
@@ -358,10 +356,9 @@ class Cache(object):
artwork_path = os.path.join(self.path_to_art_cache, self.id + '.' + helpers.today() + ext) artwork_path = os.path.join(self.path_to_art_cache, self.id + '.' + helpers.today() + ext)
try: try:
f = open(artwork_path, 'wb') with open(artwork_path, 'wb') as f:
f.write(artwork) f.write(artwork)
f.close() except IOError as e:
except Exception, e:
logger.error('Unable to write to the cache dir: %s', e) logger.error('Unable to write to the cache dir: %s', e)
self.artwork_errors = True self.artwork_errors = True
self.artwork_url = image_url self.artwork_url = image_url
@@ -375,7 +372,7 @@ class Cache(object):
if not os.path.isdir(self.path_to_art_cache): if not os.path.isdir(self.path_to_art_cache):
try: try:
os.makedirs(self.path_to_art_cache) os.makedirs(self.path_to_art_cache)
except Exception, e: except Exception as e:
logger.error('Unable to create artwork cache dir. Error: %s' + e) logger.error('Unable to create artwork cache dir. Error: %s' + e)
self.thumb_errors = True self.thumb_errors = True
self.thumb_url = thumb_url self.thumb_url = thumb_url
@@ -384,17 +381,16 @@ class Cache(object):
for thumb_file in self.thumb_files: for thumb_file in self.thumb_files:
try: try:
os.remove(thumb_file) os.remove(thumb_file)
except: except Exception as e:
logger.error('Error deleting file from the cache: %s', thumb_file) logger.error('Error deleting file from the cache: %s', thumb_file)
ext = os.path.splitext(image_url)[1] ext = os.path.splitext(image_url)[1]
thumb_path = os.path.join(self.path_to_art_cache, 'T_' + self.id + '.' + helpers.today() + ext) thumb_path = os.path.join(self.path_to_art_cache, 'T_' + self.id + '.' + helpers.today() + ext)
try: try:
f = open(thumb_path, 'wb') with open(thumb_path, 'wb') as f:
f.write(artwork) f.write(artwork)
f.close() except IOError as e:
except Exception, e:
logger.error('Unable to write to the cache dir: %s', e) logger.error('Unable to write to the cache dir: %s', e)
self.thumb_errors = True self.thumb_errors = True
self.thumb_url = image_url self.thumb_url = image_url