From 59e831013546ff8c983d674b7c7552d01debcf29 Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sun, 24 May 2009 18:13:16 +0000 Subject: [PATCH] * morituri/common/common.py: Add a persisted cache so that we can store pickles on discs. Automatically delete them if the class version is newer than the object's. * morituri/rip/cd.py: Use it. --- ChangeLog | 9 +++++++++ morituri/common/common.py | 40 +++++++++++++++++++++++++++++++++++++++ morituri/rip/cd.py | 19 ++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index a8f6aff..efaf915 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-05-24 Thomas Vander Stichele + + * morituri/common/common.py: + Add a persisted cache so that we can store pickles on discs. + Automatically delete them if the class version is newer than + the object's. + * morituri/rip/cd.py: + Use it. + 2009-05-23 Thomas Vander Stichele * morituri/image/table.py: diff --git a/morituri/common/common.py b/morituri/common/common.py index 7c7896a..3fea509 100644 --- a/morituri/common/common.py +++ b/morituri/common/common.py @@ -136,3 +136,43 @@ class Persister(object): handle = open(self._path) import pickle self.object = pickle.load(handle) + + def delete(self): + self.object = None + os.unlink(self._path) + + +class PersistedCache(object): + """ + I wrap a directory of persisted objects. + """ + + path = None + + def __init__(self, path): + self.path = path + try: + os.makedirs(self.path) + except OSError, e: + if e.errno != 17: # FIXME + raise + + + def _getPath(self, key): + return os.path.join(self.path, '%s.pickle' % key) + + def get(self, key): + """ + Returns the persister for the given key. + """ + persister = Persister(self._getPath(key)) + if persister.object: + if hasattr(persister.object, 'version'): + o = persister.object + if o.version < o.__class__.version: + print 'object needs upgrade' + persister.delete() + + return persister + + diff --git a/morituri/rip/cd.py b/morituri/rip/cd.py index 04aca4b..0c57f8e 100644 --- a/morituri/rip/cd.py +++ b/morituri/rip/cd.py @@ -119,6 +119,20 @@ def musicbrainz(discid): return metadata def getPath(outdir, template, metadata, i): + """ + Based on the template, get a complete path for the given track, + minus extension. + Also works for the disc name, using disc variables for the template. + + @param outdir: the directory where to write the files + @type outdir: str + @param template: the template for writing the file + @type template: str + @param metadata: + @type metadata: L{DiscMetadata} + @param i: track number (0 for HTOA) + @type i: int + """ # returns without extension v = {} @@ -208,7 +222,10 @@ class Rip(logcommand.LogCommand): responses = cache.retrieve(url) # now, read the complete index table, which is slower - ptable = common.Persister(self.options.table_pickle or None) + path = os.path.join(os.path.expanduser('~'), '.morituri', 'cache', + 'table') + pcache = common.PersistedCache(path) + ptable = pcache.get(ittoc.getCDDBDiscId()) if not ptable.object: t = cdrdao.ReadTableTask(device=self.parentCommand.options.device) function(runner, t)