2 from sfa.util.xml import XML
5 class SimpleStorage(dict):
7 Handles storing and loading python dictionaries. The storage file created
8 is a string representation of the native python dictionary.
13 def __init__(self, db_filename, db=None):
16 dict.__init__(self, db)
17 self.db_filename = db_filename
20 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
21 db_file = open(self.db_filename, 'r')
22 dict.__init__(self, eval(db_file.read()))
23 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
24 raise IOError('%s exists but is not a file. please remove it and try again'
31 db_file = open(self.db_filename, 'w')
32 db_file.write(str(self))
39 class XmlStorage(SimpleStorage):
41 Handles storing and loading python dictionaries. The storage file created
42 is a xml representation of the python dictionary.
49 Parse an xml file and store it as a dict
51 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
52 xml = XML(self.db_filename)
53 dict.__init__(self, xml.todict())
54 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
55 raise IOError('%s exists but is not a file. please remove it and try again'
64 db_file = open(self.db_filename, 'w')
65 db_file.write(data.toprettyxml())