[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] New code to abstract pickling portions of VoteFile



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria:/tmp/cvs-serv26827/lib/mixminion

Modified Files:
	Filestore.py 
Log Message:
New code to abstract pickling portions of VoteFile

Index: Filestore.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Filestore.py,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -d -r1.24 -r1.25
--- Filestore.py	12 Dec 2004 22:28:39 -0000	1.24
+++ Filestore.py	4 Jun 2005 13:46:01 -0000	1.25
@@ -26,7 +26,7 @@
 import whichdb
 
 from mixminion.Common import MixError, MixFatalError, secureDelete, LOG, \
-     createPrivateDir, readFile, replaceFile, tryUnlink
+     createPrivateDir, readFile, replaceFile, tryUnlink, writePickled
 from mixminion.Crypto import getCommonPRNG
 
 __all__ = [ "StringStore", "StringMetadataStore",
@@ -939,3 +939,46 @@
         self.cache = cache = {}
         for k in keys:
             cache[k] = cPickle.loads(self.db[k])
+
+class PickleCache:
+    """DOCDOC"""
+    def __init__(self, fname_base, fname_cache):
+        self._fname_base = fname_base
+        self._fname_cache = fname_cache
+        self._dirty = 0
+
+    def _setFromPickle(self, p):
+        raise NotImplemented
+
+    def _getForPickle(self, p):
+        raise NotImplemented
+
+    def _reload(self):
+        raise NotImplemented
+
+    def _loadFromCache(self):
+        # raise OSError or return false on can't/shouldn't load.
+        try:
+            cache_mtime = os.stat(self._fname_cache)[stat.ST_MTIME]
+            file_mtime = os.stat(self._fname_base)[stat.ST_MTIME]
+        except OSError:
+            return 0
+        if file_mtime >= cache_mtime:
+            return 0
+        try:
+            p = readPickled(self._fname_cache)
+        except (OSError, cPickle.UnpicklingError), _:
+            return 0
+        if not self._setFromPickle(p):
+            return 0
+        self._dirty = 0
+        return 1
+
+    def load(self):
+        if not self._loadFromCache():
+            self._reload()
+            self._dirty = 1
+
+    def save(self, mode):
+        writePickled(self._fname_cache, self._getForPickle(), mode=mode)
+        self._dirty = 0