* 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.
This commit is contained in:
Thomas Vander Stichele
2009-05-24 18:13:16 +00:00
parent feec95e768
commit 59e8310135
3 changed files with 67 additions and 1 deletions

View File

@@ -1,3 +1,12 @@
2009-05-24 Thomas Vander Stichele <thomas at apestaart dot org>
* 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 <thomas at apestaart dot org>
* morituri/image/table.py:

View File

@@ -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

View File

@@ -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)