renaming the toplevel geni/ package into sfa/
[sfa.git] / sfa / util / storage.py
diff --git a/sfa/util/storage.py b/sfa/util/storage.py
new file mode 100644 (file)
index 0000000..95ed8f5
--- /dev/null
@@ -0,0 +1,73 @@
+### $Id$
+### $URL$
+
+import os
+
+from sfa.util.rspec import RecordSpec
+
+class SimpleStorage(dict):
+    """
+    Handles storing and loading python dictionaries. The storage file created
+    is a string representation of the native python dictionary.
+    """
+    db_filename = None
+    type = 'dict'
+    
+    def __init__(self, db_filename, db = {}):
+
+        dict.__init__(self, db)
+        self.db_filename = db_filename
+    
+    def load(self):
+        if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
+            db_file = open(self.db_filename, 'r')
+            dict.__init__(self, eval(db_file.read()))
+        elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
+            raise IOError, '%s exists but is not a file. please remove it and try again' \
+                           % self.db_filename
+        else:
+            self.write()
+            self.load()
+    def write(self):
+        db_file = open(self.db_filename, 'w')  
+        db_file.write(str(self))
+        db_file.close()
+    
+    def sync(self):
+        self.write()
+
+class XmlStorage(SimpleStorage):
+    """
+    Handles storing and loading python dictionaries. The storage file created
+    is a xml representation of the python dictionary.
+    """ 
+    db_filename = None
+    type = 'xml'
+
+    def load(self):
+        """
+        Parse an xml file and store it as a dict
+        """ 
+        data = RecordSpec()
+        if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
+            data.parseFile(self.db_filename)
+            dict.__init__(self, data.toDict())
+        elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
+            raise IOError, '%s exists but is not a file. please remove it and try again' \
+                           % self.db_filename
+        else:
+            self.write()
+            self.load()
+
+    def write(self):
+        data = RecordSpec()
+        data.parseDict(self)
+        db_file = open(self.db_filename, 'w')
+        db_file.write(data.toprettyxml())
+        db_file.close()
+
+    def sync(self):
+        self.write()
+
+