3 from sfa.util.rspec import RecordSpec
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 = {}):
15 dict.__init__(self, db)
16 self.db_filename = db_filename
19 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
20 db_file = open(self.db_filename, 'r')
21 dict.__init__(self, eval(db_file.read()))
22 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
23 raise IOError, '%s exists but is not a file. please remove it and try again' \
30 db_file = open(self.db_filename, 'w')
31 db_file.write(str(self))
37 class XmlStorage(SimpleStorage):
39 Handles storing and loading python dictionaries. The storage file created
40 is a xml representation of the python dictionary.
47 Parse an xml file and store it as a dict
50 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
51 data.parseFile(self.db_filename)
52 dict.__init__(self, data.toDict())
53 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
54 raise IOError, '%s exists but is not a file. please remove it and try again' \
63 db_file = open(self.db_filename, 'w')
64 db_file.write(data.toprettyxml())