Browse Source

Fix and refactor caching functions

pull/165/head
Oliverpool 9 years ago
parent
commit
30af0bba20
  1. 25
      patacrep/songs/__init__.py

25
patacrep/songs/__init__.py

@ -96,13 +96,21 @@ class Song:
] ]
def __init__(self, subpath, config, *, datadir=None): def __init__(self, subpath, config, *, datadir=None):
self.datadir = datadir or "" if datadir is None:
self.datadir = ""
self.use_cache = False
else:
self.datadir = datadir
# Only songs in datadirs are cached
self.use_cache = ('_cache' in config)
self.fullpath = os.path.join(self.datadir, subpath) self.fullpath = os.path.join(self.datadir, subpath)
self.subpath = subpath
self.encoding = config["encoding"] self.encoding = config["encoding"]
self.default_lang = config["lang"] self.default_lang = config["lang"]
self.config = config self.config = config
if self.config['_cache'] and self._cache_retrieved(): if self._cache_retrieved():
return return
# Data extraction from the latex song # Data extraction from the latex song
@ -113,7 +121,6 @@ class Song:
self._parse() self._parse()
# Post processing of data # Post processing of data
self.subpath = subpath
self.unprefixed_titles = [ self.unprefixed_titles = [
unprefixed_title( unprefixed_title(
title, title,
@ -129,20 +136,18 @@ class Song:
# Cache management # Cache management
self._version = self.CACHE_VERSION self._version = self.CACHE_VERSION
if self.config['_cache']: self._write_cache()
self._write_cache()
def _cache_retrieved(self): def _cache_retrieved(self):
"""If relevant, retrieve self from the cache.""" """If relevant, retrieve self from the cache."""
if self.datadir: if self.use_cache:
# Only songs in datadirs are cached
self._filehash = hashlib.md5( self._filehash = hashlib.md5(
open(self.fullpath, 'rb').read() open(self.fullpath, 'rb').read()
).hexdigest() ).hexdigest()
if os.path.exists(cached_name(datadir, subpath)): if os.path.exists(cached_name(self.datadir, self.subpath)):
try: try:
cached = pickle.load(open( cached = pickle.load(open(
cached_name(datadir, subpath), cached_name(self.datadir, self.subpath),
'rb', 'rb',
)) ))
if ( if (
@ -160,7 +165,7 @@ class Song:
def _write_cache(self): def _write_cache(self):
"""If relevant, write a dumbed down version of self to the cache.""" """If relevant, write a dumbed down version of self to the cache."""
if self.datadir: if self.use_cache:
# Only songs in datadirs can be cached # Only songs in datadirs can be cached
cached = {} cached = {}
for attribute in self.cached_attributes: for attribute in self.cached_attributes:

Loading…
Cancel
Save