+ 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():
+ if val.saved_value is not None:
+ res[str(key)] = val.saved_value
+ 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():
+ if val.saved_value is not None:
+ res[key] = val.saved_value
+ 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
+
+class ConfigSet(ConfigElement):
+ def __init__(self, choices, default = []):
+ ConfigElement.__init__(self)
+ self.choices = []
+ self.description = {}
+ if isinstance(choices, list):
+ choices.sort()
+ for x in choices:
+ if isinstance(x, tuple):
+ self.choices.append(x[0])
+ self.description[x[0]] = str(x[1])
+ else:
+ self.choices.append(x)
+ self.description[x] = str(x)
+ else:
+ assert False, "ConfigSet choices must be a list!"
+ if len(self.choices) == 0:
+ self.choices = [""]
+ self.description[""] = ""
+ if default is None:
+ default = []
+ self.pos = -1
+ default.sort()
+ self.default = default
+ self.value = default+[]
+
+ def toggleChoice(self, choice):
+ if choice in self.value:
+ self.value.remove(choice)
+ else:
+ self.value.append(choice)
+ self.value.sort()
+
+ def handleKey(self, key):
+ if key in KEY_NUMBERS + [KEY_DELETE, KEY_BACKSPACE]:
+ if self.pos != -1:
+ self.toggleChoice(self.choices[self.pos])
+ elif key == KEY_LEFT:
+ self.pos -= 1
+ if self.pos < -1:
+ self.pos = len(self.choices)-1
+ elif key == KEY_RIGHT:
+ self.pos += 1
+ if self.pos >= len(self.choices):
+ self.pos = -1
+ elif key in [KEY_HOME, KEY_END]:
+ self.pos = -1
+
+ def genString(self, lst):
+ res = ""
+ for x in lst:
+ res += self.description[x]+" "
+ return res
+
+ def getText(self):
+ return self.genString(self.value)
+
+ def getMulti(self, selected):
+ if not selected or self.pos == -1:
+ return ("text", self.genString(self.value))
+ else:
+ tmp = self.value+[]
+ ch = self.choices[self.pos]
+ mem = ch in self.value
+ if not mem:
+ tmp.append(ch)
+ tmp.sort()
+ ind = tmp.index(ch)
+ val1 = self.genString(tmp[:ind])
+ val2 = " "+self.genString(tmp[ind+1:])
+ if mem:
+ chstr = " "+self.description[ch]+" "
+ else:
+ chstr = "("+self.description[ch]+")"
+ return ("mtext", val1+chstr+val2, range(len(val1),len(val1)+len(chstr)))
+
+ def onDeselect(self, session):
+ self.pos = -1
+ self.changed()