Browse Source

Minor refactorisation of cache_retrieved

pull/165/head
Oliverpool 9 years ago
parent
commit
9a31a21c01
  1. 37
      patacrep/songs/__init__.py

37
patacrep/songs/__init__.py

@ -98,10 +98,10 @@ class Song:
def __init__(self, subpath, config, *, datadir=None): def __init__(self, subpath, config, *, datadir=None):
if datadir is None: if datadir is None:
self.datadir = "" self.datadir = ""
# Only songs in datadirs may be cached
self.use_cache = False self.use_cache = False
else: else:
self.datadir = datadir self.datadir = datadir
# Only songs in datadirs are cached
self.use_cache = ('_cache' in config) self.use_cache = ('_cache' in config)
self.fullpath = os.path.join(self.datadir, subpath) self.fullpath = os.path.join(self.datadir, subpath)
@ -145,27 +145,26 @@ class Song:
def _cache_retrieved(self): def _cache_retrieved(self):
"""If relevant, retrieve self from the cache.""" """If relevant, retrieve self from the cache."""
if self.use_cache: if self.use_cache and os.path.exists(self.cached_name):
self._filehash = hashlib.md5( self._filehash = hashlib.md5(
open(self.fullpath, 'rb').read() open(self.fullpath, 'rb').read()
).hexdigest() ).hexdigest()
if os.path.exists(self.cached_name): try:
try: cached = pickle.load(open(
cached = pickle.load(open( self.cached_name,
self.cached_name, 'rb',
'rb', ))
)) if (
if ( cached['_filehash'] == self._filehash
cached['_filehash'] == self._filehash and cached['_version'] == self.CACHE_VERSION
and cached['_version'] == self.CACHE_VERSION ):
): for attribute in self.cached_attributes:
for attribute in self.cached_attributes: setattr(self, attribute, cached[attribute])
setattr(self, attribute, cached[attribute]) return True
return True except: # pylint: disable=bare-except
except: # pylint: disable=bare-except LOGGER.warning("Could not use cached version of {}.".format(
LOGGER.warning("Could not use cached version of {}.".format( self.fullpath
self.fullpath ))
))
return False return False
def _write_cache(self): def _write_cache(self):

Loading…
Cancel
Save