+ if _COVERDIR and os.path.isdir(_COVERDIR):
+ self.prime_cache()
+
+ def prime_cache(self):
+ coverdir = _COVERDIR
+ covermatch = re.compile(r'(\d+)\-(\d+)\.jpg')
+
+ prev_covers = os.listdir(coverdir)
+
+ if len(prev_covers) > _CACHED_COVERS:
+ import random
+ dropn = len(prev_covers) - _CACHED_COVERS
+ todrop = random.sample(prev_covers, dropn)
+ log.warning("Deleting from cache: %s", todrop)
+ for d in todrop:
+ m = covermatch.match(d)
+ if m:
+ try:
+ os.unlink(os.path.join(coverdir, d))
+ except OSError, e:
+ log.exception('unlinking failed')
+
+ for fil in os.listdir(coverdir):
+ fl = os.path.join(coverdir, fil)
+ m = covermatch.match(fil)
+ if m and os.path.isfile(fl):
+ self._covers[(int(m.group(1)), int(m.group(2)))] = fl