Browse Source

Move the filehash computation into its own (cached) method

pull/165/head
Oliverpool 9 years ago
parent
commit
4ae7e13326
  1. 16
      patacrep/songs/__init__.py

16
patacrep/songs/__init__.py

@ -88,7 +88,6 @@ class Song:
"unprefixed_titles", "unprefixed_titles",
"cached", "cached",
"data", "data",
"subpath",
"lang", "lang",
"authors", "authors",
"_filehash", "_filehash",
@ -106,6 +105,7 @@ class Song:
self.fullpath = os.path.join(self.datadir, subpath) self.fullpath = os.path.join(self.datadir, subpath)
self.subpath = subpath self.subpath = subpath
self._filehash = None
self.encoding = config["encoding"] self.encoding = config["encoding"]
self.default_lang = config["lang"] self.default_lang = config["lang"]
self.config = config self.config = config
@ -143,19 +143,25 @@ class Song:
"""Name of the file used for the cache""" """Name of the file used for the cache"""
return cached_name(self.datadir, self.subpath) return cached_name(self.datadir, self.subpath)
def _cache_retrieved(self): @property
"""If relevant, retrieve self from the cache.""" def filehash(self):
if self.use_cache and os.path.exists(self.cached_name): """Compute (and cache) the md5 hash of the file"""
if self._filehash is None:
self._filehash = hashlib.md5( self._filehash = hashlib.md5(
open(self.fullpath, 'rb').read() open(self.fullpath, 'rb').read()
).hexdigest() ).hexdigest()
return self._filehash
def _cache_retrieved(self):
"""If relevant, retrieve self from the cache."""
if self.use_cache and 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:

Loading…
Cancel
Save