inital checkin of storage class
authorTony Mack <tmack@cs.princeton.edu>
Tue, 3 Feb 2009 22:47:02 +0000 (22:47 +0000)
committerTony Mack <tmack@cs.princeton.edu>
Tue, 3 Feb 2009 22:47:02 +0000 (22:47 +0000)
geni/util/storage.py [new file with mode: 0644]

diff --git a/geni/util/storage.py b/geni/util/storage.py
new file mode 100644 (file)
index 0000000..ec220b6
--- /dev/null
@@ -0,0 +1,25 @@
+
+class SimpleStorage(dict):
+
+    db_filename = None
+    types = ['dict', 'tabbed', 'text', 'shell']
+
+    def __init__(self, db_filename, db = {}, type = 'dict'):
+
+        if type not in self.types:
+            raise Exception, "Invalid type %s, must be in %s" % (type, self.types)
+        self.type = type
+        dict.__init__(self, db)
+        self.db_filename = db_filename
+    
+    def load(self):
+        db_file = open(self.db_filename, 'r')
+        dict.__init__(self, eval(db_file.read()))    
+    def write(self):
+        db_file = open(self.db_filename, 'w')  
+        db_file.write(str(self)
+        db_file.close()
+    
+    def sync(self):
+        self.write()