[SCM] morituri/master: add a TableCache.

js at users.alioth.debian.org js at users.alioth.debian.org
Sun Oct 19 20:10:00 UTC 2014


The following commit has been merged in the master branch:
commit d4c81126d57a0c48bf96ef165e7a6ec8ceef7ba1
Author: Thomas Vander Stichele <thomas (at) apestaart (dot) org>
Date:   Sun Feb 3 22:19:11 2013 +0100

    add a TableCache.

diff --git a/morituri/common/cache.py b/morituri/common/cache.py
index f8424d0..b1cdb06 100644
--- a/morituri/common/cache.py
+++ b/morituri/common/cache.py
@@ -27,6 +27,8 @@ import tempfile
 import shutil
 
 from morituri.result import result
+from morituri.common import directory
+
 from morituri.extern.log import log
 
 
@@ -188,4 +190,49 @@ class ResultCache(log.Loggable):
         paths = glob.glob(os.path.join(self._path, '*.pickle'))
 
         return [os.path.splitext(os.path.basename(path))[0] for path in paths]
-        
+
+
+class TableCache(log.Loggable):
+
+    """
+    I read and write entries to and from the cache of tables.
+
+    If no path is specified, the cache will write to the current cache
+    directory and read from all possible cache directories (to allow for
+    pre-0.2.1 cddbdiscid-keyed entries).
+    """
+
+    def __init__(self, path=None):
+        if not path:
+            d = directory.Directory()
+            self._path = d.getCache('table')
+            self._readPaths = d.getReadCaches('table')
+        else:
+            self._path = path
+            self._readPaths = [path, ]
+
+        self._pcache = PersistedCache(self._path)
+        self._readPCaches = [PersistedCache(p) for p in self._readPaths]
+
+    def get(self, cddbdiscid, mbdiscid):
+        # Before 0.2.1, we only saved by cddbdiscid, and had collisions
+        # mbdiscid collisions are a lot less likely
+        for pcache in self._readPCaches:
+            ptable = pcache.get('mbdiscid.' + mbdiscid)
+            if ptable.object:
+                break
+
+        if not ptable.object:
+            for pcache in self._readPCaches:
+                ptable = pcache.get(cddbdiscid)
+                if ptable.object:
+                    if ptable.object.getMusicBrainzDiscId() != mbdiscid:
+                        self.debug('cached table is for different mb id %r' % (
+                            ptable.object.getMusicBrainzDiscId()))
+                    ptable.object = None
+
+        if not ptable.object:
+            # get an empty persistable from the writable location
+            ptable = self._pcache.get('mbdiscid.' + mbdiscid)
+
+        return ptable

-- 
morituri packaging



More information about the pkg-multimedia-commits mailing list