+ def save(self):
+ for x in self.values():
+ x.save()
+
+ def load(self):
+ for x in self.values():
+ x.load()
+
+ def getSavedValue(self):
+ res = {}
+ for (key, val) in self.items():
+ sv = val.saved_value
+ if sv is not None:
+ res[str(key)] = sv
+ return res
+
+ def setSavedValue(self, values):
+ self.stored_values = dict(values)
+ for (key, val) in self.items():
+ if str(key) in self.stored_values:
+ val = self.stored_values[str(key)]
+
+ saved_value = property(getSavedValue, setSavedValue)
+
+ def __setitem__(self, key, item):
+ dict.__setitem__(self, key, item)
+ if str(key) in self.stored_values:
+ item.saved_value = self.stored_values[str(key)]
+ item.load()
+
+ def dict(self):
+ return self
+
+# Like the classes above, just with a more "native"
+# syntax.
+#
+# some evil stuff must be done to allow instant
+# loading of added elements. this is why this class
+# is so complex.
+#
+# we need the 'content' because we overwrite
+# __setattr__.
+# If you don't understand this, try adding
+# __setattr__ to a usual exisiting class and you will.
+class ConfigSubsection(object):
+ def __init__(self):
+ object.__init__(self)
+ self.__dict__["content"] = ConfigSubsectionContent()
+ self.content.items = { }
+ self.content.stored_values = { }
+
+ def __setattr__(self, name, value):
+ if name == "saved_value":
+ return self.setSavedValue(value)
+ assert isinstance(value, ConfigSubsection) or isinstance(value, ConfigElement) or isinstance(value, ConfigSubList) or isinstance(value, ConfigSubDict), "ConfigSubsections can only store ConfigSubsections, ConfigSubLists, ConfigSubDicts or ConfigElements"
+ self.content.items[name] = value
+ if name in self.content.stored_values:
+ #print "ok, now we have a new item,", name, "and have the following value for it:", self.content.stored_values[name]
+ value.saved_value = self.content.stored_values[name]
+ value.load()
+
+ def __getattr__(self, name):
+ return self.content.items[name]
+
+ def getSavedValue(self):
+ res = self.content.stored_values
+ for (key, val) in self.content.items.items():
+ sv = val.saved_value
+ if sv is not None:
+ res[key] = sv
+ elif key in res:
+ del res[key]
+ return res
+
+ def setSavedValue(self, values):
+ values = dict(values)
+
+ self.content.stored_values = values
+
+ for (key, val) in self.content.items.items():
+ if key in values:
+ val.saved_value = values[key]
+
+ saved_value = property(getSavedValue, setSavedValue)
+
+ def save(self):
+ for x in self.content.items.values():
+ x.save()
+
+ def load(self):
+ for x in self.content.items.values():
+ x.load()
+
+ def dict(self):
+ return self.content.items
+
+# the root config object, which also can "pickle" (=serialize)
+# down the whole config tree.
+#
+# we try to keep non-existing config entries, to apply them whenever
+# a new config entry is added to a subsection
+# also, non-existing config entries will be saved, so they won't be
+# lost when a config entry disappears.
+class Config(ConfigSubsection):
+ def __init__(self):
+ ConfigSubsection.__init__(self)
+
+ def pickle_this(self, prefix, topickle, result):
+ for (key, val) in topickle.items():
+ name = prefix + "." + key
+
+ if isinstance(val, dict):
+ self.pickle_this(name, val, result)
+ elif isinstance(val, tuple):
+ result.append(name + "=" + val[0]) # + " ; " + val[1])
+ else:
+ result.append(name + "=" + val)
+
+ def pickle(self):
+ result = [ ]
+ self.pickle_this("config", self.saved_value, result)
+ return '\n'.join(result) + "\n"
+
+ def unpickle(self, lines):
+ tree = { }
+ for l in lines:
+ if not len(l) or l[0] == '#':
+ continue
+
+ n = l.find('=')
+ val = l[n+1:].strip()
+
+ names = l[:n].split('.')
+# if val.find(' ') != -1:
+# val = val[:val.find(' ')]
+
+ base = tree
+
+ for n in names[:-1]:
+ base = base.setdefault(n, {})
+
+ base[names[-1]] = val
+
+ # we inherit from ConfigSubsection, so ...
+ #object.__setattr__(self, "saved_value", tree["config"])
+ if "config" in tree:
+ self.setSavedValue(tree["config"])
+
+ def saveToFile(self, filename):
+ f = open(filename, "w")
+ f.write(self.pickle())
+ f.close()