2 from geni.util.rspec import RecordSpec
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 = {}):
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
49 if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename):
50 data.parseFile(self.db_filename)
51 dict.__init__(self, data.toDict())
52 elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename):
53 raise IOError, '%s exists but is not a file. please remove it and try again' \
62 db_file = open(self.db_filename, 'w')
63 db_file.write(data.toprettyxml())