X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Futil%2Fstorage.py;h=9b682064eed12f71068bf94e4738ce2ab6cb3581;hb=fd395e1944dcd49f10a4d5b27ce4983ad389fb96;hp=95ed8f58bff7d83d205e0b898d2107c6fce30135;hpb=f13173726f8382eef380f1e754f24dd2b126a77b;p=sfa.git diff --git a/sfa/util/storage.py b/sfa/util/storage.py index 95ed8f58..9b682064 100644 --- a/sfa/util/storage.py +++ b/sfa/util/storage.py @@ -1,9 +1,6 @@ -### $Id$ -### $URL$ - import os +from sfa.util.xml import XML -from sfa.util.rspec import RecordSpec class SimpleStorage(dict): """ @@ -12,62 +9,61 @@ class SimpleStorage(dict): """ db_filename = None type = 'dict' - - def __init__(self, db_filename, db = {}): + def __init__(self, db_filename, db=None): + if db is None: + db = {} dict.__init__(self, db) self.db_filename = db_filename - + def load(self): if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename): db_file = open(self.db_filename, 'r') dict.__init__(self, eval(db_file.read())) elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename): - raise IOError, '%s exists but is not a file. please remove it and try again' \ - % self.db_filename + raise IOError('%s exists but is not a file. please remove it and try again' + % self.db_filename) else: self.write() self.load() - + def write(self): - db_file = open(self.db_filename, 'w') + db_file = open(self.db_filename, 'w') db_file.write(str(self)) db_file.close() - + def sync(self): self.write() + class XmlStorage(SimpleStorage): """ Handles storing and loading python dictionaries. The storage file created is a xml representation of the python dictionary. - """ + """ db_filename = None type = 'xml' def load(self): """ Parse an xml file and store it as a dict - """ - data = RecordSpec() + """ if os.path.exists(self.db_filename) and os.path.isfile(self.db_filename): - data.parseFile(self.db_filename) - dict.__init__(self, data.toDict()) + xml = XML(self.db_filename) + dict.__init__(self, xml.todict()) elif os.path.exists(self.db_filename) and not os.path.isfile(self.db_filename): - raise IOError, '%s exists but is not a file. please remove it and try again' \ - % self.db_filename + raise IOError('%s exists but is not a file. please remove it and try again' + % self.db_filename) else: self.write() self.load() def write(self): - data = RecordSpec() - data.parseDict(self) + xml = XML() + xml.parseDict(self) db_file = open(self.db_filename, 'w') db_file.write(data.toprettyxml()) db_file.close() def sync(self): self.write() - -