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

Loading…
Cancel
Save