2 from sfa.util.xml import XML
4 class SimpleStorage(dict):
6 Handles storing and loading python dictionaries. The storage file created
7 is a string representation of the native python dictionary.
12 def __init__(self, db_filename, db = None):
14 dict.__init__(self, db)
15 self.db_filename = db_filename
18 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
19 db_file = open(self.db_filename, 'r')
20 dict.__init__(self, eval(db_file.read()))
21 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
22 raise IOError, '%s exists but is not a file. please remove it and try again' \
29 db_file = open(self.db_filename, 'w')
30 db_file.write(str(self))
36 class XmlStorage(SimpleStorage):
38 Handles storing and loading python dictionaries. The storage file created
39 is a xml representation of the python dictionary.
46 Parse an xml file and store it as a dict
48 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
49 xml = XML(self.db_filename)
50 dict.__init__(self, xml.todict())
51 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
52 raise IOError, '%s exists but is not a file. please remove it and try again' \
61 db_file = open(self.db_filename, 'w')
62 db_file.write(data.toprettyxml())