6 from sfa.util.rspec import RecordSpec
8 class SimpleStorage(dict):
10 Handles storing and loading python dictionaries. The storage file created
11 is a string representation of the native python dictionary.
16 def __init__(self, db_filename, db = {}):
18 dict.__init__(self, db)
19 self.db_filename = db_filename
22 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
23 db_file = open(self.db_filename, 'r')
24 dict.__init__(self, eval(db_file.read()))
25 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
26 raise IOError, '%s exists but is not a file. please remove it and try again' \
33 db_file = open(self.db_filename, 'w')
34 db_file.write(str(self))
40 class XmlStorage(SimpleStorage):
42 Handles storing and loading python dictionaries. The storage file created
43 is a xml representation of the python dictionary.
50 Parse an xml file and store it as a dict
53 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
54 data.parseFile(self.db_filename)
55 dict.__init__(self, data.toDict())
56 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
57 raise IOError, '%s exists but is not a file. please remove it and try again' \
66 db_file = open(self.db_filename, 'w')
67 db_file.write(data.toprettyxml())