summaryrefslogtreecommitdiffstats
path: root/pyfirstaidkit/utils
diff options
context:
space:
mode:
authorMartin Sivak <msivak@redhat.com>2008-05-28 10:31:05 +0200
committerMartin Sivak <msivak@redhat.com>2008-05-28 10:32:14 +0200
commitfb95875f6daffe7b9c6fbea69cf7c89246ea7985 (patch)
tree92c33693848d26c9bc58bc8ff00b2700a304fd49 /pyfirstaidkit/utils
parent66a081a3c9d155e0cbd22180f4beebc62e052ae1 (diff)
downloadfirstaidkit-fb95875f6daffe7b9c6fbea69cf7c89246ea7985.tar.gz
firstaidkit-fb95875f6daffe7b9c6fbea69cf7c89246ea7985.tar.xz
firstaidkit-fb95875f6daffe7b9c6fbea69cf7c89246ea7985.zip
Backup values
Diffstat (limited to 'pyfirstaidkit/utils')
-rw-r--r--pyfirstaidkit/utils/backup.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/pyfirstaidkit/utils/backup.py b/pyfirstaidkit/utils/backup.py
index f6b092c..f68b154 100644
--- a/pyfirstaidkit/utils/backup.py
+++ b/pyfirstaidkit/utils/backup.py
@@ -23,6 +23,7 @@ import os
import shutil
import hashlib
import weakref
+import cPickle as pickle
class BackupException(Exception):
pass
@@ -92,8 +93,36 @@ class FileBackupStore(BackupStoreInterface):
self._data[name] = (stored, path)
return True
+ def backupValue(self, value, name):
+ if self._data.has_key(name):
+ raise BackupException("Named backup %s already in the backup store %s!" % (name,self._id))
+ stored = hashlib.sha224(name).hexdigest()
+
+ f = open(os.path.join(self._path, stored), "wb")
+ pickle.dump(value, f, pickle.HIGHEST_PROTOCOL)
+ f.close()
+
+ self._data[name] = (stored, None)
+
+ return True
+
+ def restoreValue(self, name):
+ stored, origin = self._data[name]
+ if origin is not None:
+ raise BackupException("Named backup %s is not a value object!" % (name,))
+
+
+ f = open(os.path.join(self._path, stored), "rb")
+ val = pickle.load(f)
+ f.close()
+
+ return val
+
def restoreName(self, name, path = None):
stored, origin = self._data[name]
+ if origin is None:
+ raise BackupException("Named backup %s is not a filesystem object!" % (name,))
+
assert self._origin[name]==origin
if path is None:
@@ -130,6 +159,11 @@ class FileBackupStore(BackupStoreInterface):
shutil.rmtree(stored)
else:
os.unlink(stored)
+
+ del self._data[name]
+ if origin is not None:
+ del self._origin[origin]
+
return True
def cleanup(self):