* morituri/common/common.py:
Create a Persister class to wrap a possible pickle path. * examples/trm.py: Use it.
This commit is contained in:
@@ -1,3 +1,10 @@
|
|||||||
|
2009-05-03 Thomas Vander Stichele <thomas at apestaart dot org>
|
||||||
|
|
||||||
|
* morituri/common/common.py:
|
||||||
|
Create a Persister class to wrap a possible pickle path.
|
||||||
|
* examples/trm.py:
|
||||||
|
Use it.
|
||||||
|
|
||||||
2009-05-03 Thomas Vander Stichele <thomas at apestaart dot org>
|
2009-05-03 Thomas Vander Stichele <thomas at apestaart dot org>
|
||||||
|
|
||||||
* morituri/program/cdparanoia.py:
|
* morituri/program/cdparanoia.py:
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import gobject
|
|||||||
gobject.threads_init()
|
gobject.threads_init()
|
||||||
import gtk
|
import gtk
|
||||||
|
|
||||||
from morituri.common import checksum, task, taskgtk
|
from morituri.common import checksum, task, taskgtk, common
|
||||||
|
|
||||||
def gtkmain(runner, taskk):
|
def gtkmain(runner, taskk):
|
||||||
runner.connect('stop', lambda _: gtk.main_quit())
|
runner.connect('stop', lambda _: gtk.main_quit())
|
||||||
@@ -48,9 +48,8 @@ def climain(runner, taskk):
|
|||||||
runner.run(taskk)
|
runner.run(taskk)
|
||||||
|
|
||||||
class Listener(object):
|
class Listener(object):
|
||||||
def __init__(self, path):
|
def __init__(self, persister):
|
||||||
self.path = path
|
self._persister = persister
|
||||||
self.trms = {}
|
|
||||||
|
|
||||||
def progressed(self, task, value):
|
def progressed(self, task, value):
|
||||||
pass
|
pass
|
||||||
@@ -62,14 +61,9 @@ class Listener(object):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def stopped(self, task):
|
def stopped(self, task):
|
||||||
self.trms[task.path] = task.trm
|
self._persister.object[task.path] = task.trm
|
||||||
print task.path, task.trm
|
print task.path, task.trm
|
||||||
|
self._persister.persist()
|
||||||
(fd, path) = tempfile.mkstemp(suffix='.morituri')
|
|
||||||
handle = os.fdopen(fd, 'wb')
|
|
||||||
pickle.dump(self.trms, handle, 2)
|
|
||||||
handle.close()
|
|
||||||
shutil.move(path, self.path)
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
@@ -98,19 +92,12 @@ def main(argv):
|
|||||||
|
|
||||||
mtask = task.MultiCombinedTask()
|
mtask = task.MultiCombinedTask()
|
||||||
listener = None
|
listener = None
|
||||||
trms = {}
|
|
||||||
if options.pickle:
|
|
||||||
listener = Listener(options.pickle)
|
|
||||||
print 'Opening pickle %s' % options.pickle
|
|
||||||
handle = open(options.pickle)
|
|
||||||
try:
|
|
||||||
trms = pickle.load(handle)
|
|
||||||
except Exception, e:
|
|
||||||
sys.stderr.write(
|
|
||||||
"Pickle file '%s' cannot be loaded.\n" % options.pickle)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
handle.close()
|
ptrms = common.Persister(options.pickle or None, {})
|
||||||
|
if options.pickle:
|
||||||
|
listener = Listener(ptrms)
|
||||||
|
print 'Using pickle %s' % options.pickle
|
||||||
|
trms = ptrms.object
|
||||||
|
|
||||||
for path in paths:
|
for path in paths:
|
||||||
path = path.rstrip()
|
path = path.rstrip()
|
||||||
|
|||||||
@@ -20,6 +20,10 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with morituri. If not, see <http://www.gnu.org/licenses/>.
|
# along with morituri. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import shutil
|
||||||
|
|
||||||
def msfToFrames(msf):
|
def msfToFrames(msf):
|
||||||
"""
|
"""
|
||||||
Converts a string value in MM:SS:FF to frames
|
Converts a string value in MM:SS:FF to frames
|
||||||
@@ -57,3 +61,71 @@ def framesToHMSF(frames):
|
|||||||
h = frames / 75 / 60 / 60
|
h = frames / 75 / 60 / 60
|
||||||
|
|
||||||
return "%02d:%02d:%02d.%02d" % (h, m, s, f)
|
return "%02d:%02d:%02d.%02d" % (h, m, s, f)
|
||||||
|
|
||||||
|
class Persister(object):
|
||||||
|
"""
|
||||||
|
I wrap an optional pickle to persist an object to disk.
|
||||||
|
|
||||||
|
Instantiate me with a path to automatically unpickle the object.
|
||||||
|
Call persist to store the object to disk; it will get stored if it
|
||||||
|
changed from the on-disk object.
|
||||||
|
|
||||||
|
@ivar object: the persistent object
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, path=None, default=None):
|
||||||
|
"""
|
||||||
|
If path is not given, the object will not be persisted.
|
||||||
|
This allows code to transparently deal with both persisted and
|
||||||
|
non-persisted objects, since the persist method will just end up
|
||||||
|
doing nothing.
|
||||||
|
"""
|
||||||
|
self._path = path
|
||||||
|
self.object = None
|
||||||
|
|
||||||
|
self._unpickle(default)
|
||||||
|
|
||||||
|
def persist(self, object=None):
|
||||||
|
"""
|
||||||
|
Persist the given object, if we have a persistence path and the
|
||||||
|
object changed.
|
||||||
|
|
||||||
|
If object is not given, re-persist our object, always.
|
||||||
|
If object is given, only persist if it was changed.
|
||||||
|
"""
|
||||||
|
# don't pickle if it's already ok
|
||||||
|
if object and object == self.object:
|
||||||
|
return
|
||||||
|
|
||||||
|
# don't pickle if there is no path
|
||||||
|
if not self._path:
|
||||||
|
return
|
||||||
|
|
||||||
|
# default to pickling our object again
|
||||||
|
if object is None:
|
||||||
|
object = self.object
|
||||||
|
|
||||||
|
# pickle
|
||||||
|
self.object = object
|
||||||
|
(fd, path) = tempfile.mkstemp(suffix='.morituri.pickle')
|
||||||
|
handle = os.fdopen(fd, 'wb')
|
||||||
|
print 'pickle'
|
||||||
|
import pickle
|
||||||
|
pickle.dump(object, handle, 2)
|
||||||
|
handle.close()
|
||||||
|
# do an atomic move
|
||||||
|
shutil.move(path, self._path)
|
||||||
|
|
||||||
|
def _unpickle(self, default=None):
|
||||||
|
self.object = default
|
||||||
|
|
||||||
|
if not self._path:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not os.path.exists(self._path):
|
||||||
|
return None
|
||||||
|
|
||||||
|
handle = open(self._path)
|
||||||
|
import pickle
|
||||||
|
print 'unpickle'
|
||||||
|
self.object = pickle.load(handle)
|
||||||
|
|||||||
Reference in New Issue
Block a user