2 from sfa.util.xml import XML
3 class SimpleStorage(dict):
5 Handles storing and loading python dictionaries. The storage file created
6 is a string representation of the native python dictionary.
11 def __init__(self, db_filename, db = {}):
13 dict.__init__(self, db)
14 self.db_filename = db_filename
17 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
18 db_file = open(self.db_filename, 'r')
19 dict.__init__(self, eval(db_file.read()))
20 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
21 raise IOError, '%s exists but is not a file. please remove it and try again' \
28 db_file = open(self.db_filename, 'w')
29 db_file.write(str(self))
35 class XmlStorage(SimpleStorage):
37 Handles storing and loading python dictionaries. The storage file created
38 is a xml representation of the python dictionary.
45 Parse an xml file and store it as a dict
47 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
48 xml = XML(self.db_filename)
49 dict.__init__(self, xml.todict())
50 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
51 raise IOError, '%s exists but is not a file. please remove it and try again' \
60 db_file = open(self.db_filename, 'w')
61 db_file.write(data.toprettyxml())