- pass
-
-class configElement:
- def datafromFile(self, control, data):
- if control == ConfigSlider:
- return int(data);
- elif control == configSelection:
- return int(data);
- elif control == configSequence:
- list = [ ]
- part = data.split(self.vals[0])
- for x in part:
- list.append(int(x))
- return list
- else:
- return ""
-
- def datatoFile(self, control, data):
- if control == ConfigSlider:
- return str(data);
- elif control == configSelection:
- return str(data);
- elif control == configSequence:
- value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
-# just in case you don't understand the above, here an equivalent:
-# value = ""
-# for i in data:
-# if value !="":
-# value += self.vals[0]
-# value += str(i)
- return value
- else:
- return ""
-
- def loadData(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)
+ 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():
+ if val.saved_value is not None:
+ res[key] = val.saved_value
+ 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.setSavedValue(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()
+
+# 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"])
+ self.setSavedValue(tree["config"])
+
+ def saveToFile(self, filename):
+ f = open(filename, "w")
+ f.write(self.pickle())
+ f.close()
+
+ def loadFromFile(self, filename):
+ f = open(filename, "r")
+ self.unpickle(f.readlines())
+ f.close()
+
+config = Config()
+config.misc = ConfigSubsection()
+
+class ConfigFile:
+ CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "config2")
+
+ def load(self):