X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=blobdiff_plain;f=lib%2Fpython%2FComponents%2Fconfig.py;h=0fd6e7327aaaafdc5951ee6936bd1988e8ed6752;hp=4da004e1bfa37c0862014ec66a3e65fde1bbe224;hb=e128ab84e720068a36530a0dab1426f893697247;hpb=1c9b6ea7f091be3c8be45dc21317af0adcc92c10 diff --git a/lib/python/Components/config.py b/lib/python/Components/config.py index 4da004e..0fd6e73 100644 --- a/lib/python/Components/config.py +++ b/lib/python/Components/config.py @@ -26,6 +26,7 @@ import copy # class ConfigElement(object): def __init__(self): + object.__init__(self) self.saved_value = None self.save_disabled = False @@ -70,10 +71,21 @@ class ConfigElement(object): for x in self.notifiers: x(self) - def addNotifier(self, notifier): + def addNotifier(self, notifier, initial_call = True): assert callable(notifier), "notifiers must be callable" self.notifiers.append(notifier) + # CHECKME: + # do we want to call the notifier + # - at all when adding it? (yes, though optional) + # - when the default is active? (yes) + # - when no value *yet* has been set, + # because no config has ever been read (currently yes) + # (though that's not so easy to detect. + # the entry could just be new.) + if initial_call: + notifier(self) + def disableSave(self): self.save_disabled = True @@ -125,7 +137,10 @@ class ConfigSelection(ConfigElement): else: assert False, "ConfigSelection choices must be dict or list!" - assert len(self.choices), "you can't have an empty configselection" + #assert len(self.choices), "you can't have an empty configselection" + if len(self.choices) == 0: + self.choices = [""] + self.description[""] = "" if default is None: default = self.choices[0] @@ -145,11 +160,18 @@ class ConfigSelection(ConfigElement): self.changed() def tostring(self, val): - return (val, ','.join(self.choices)) + return val def getValue(self): return self._value + def setCurrentText(self, text): + i = self.choices.index(self.value) + del self.description[self.choices[i]] + self.choices[i] = text + self.description[text] = text + self._value = text + value = property(getValue, setValue) def getIndex(self): @@ -165,9 +187,6 @@ class ConfigSelection(ConfigElement): self.value = self.choices[(i + nchoices - 1) % nchoices] elif key == KEY_RIGHT: self.value = self.choices[(i + 1) % nchoices] - elif key == KEY_TIMEOUT: - self.timeout() - return def getMulti(self, selected): return ("text", self.description[self.value]) @@ -371,7 +390,7 @@ class ConfigSequence(ConfigElement): if self.censor_char == "": value += ("%0" + str(len(str(self.limits[num][1]))) + "d") % i else: - value += (self.censorChar * len(str(self.limits[num][1]))) + value += (self.censor_char * len(str(self.limits[num][1]))) num += 1 # only mark cursor when we are selected @@ -404,10 +423,12 @@ class ConfigPosition(ConfigSequence): class ConfigClock(ConfigSequence): def __init__(self, default): - ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = default) + import time + t = time.localtime(default) + ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = [t.tm_hour, t.tm_min]) class ConfigInteger(ConfigSequence): - def __init__(self, default, limits): + def __init__(self, default, limits = (0, 10000000000)): ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default) # you need to override this to do input validation @@ -426,9 +447,16 @@ class ConfigInteger(ConfigSequence): def tostring(self, value): return str(value) -class ConfigPIN(ConfigSequence): +class ConfigPIN(ConfigInteger): def __init__(self, default, len = 4, censor = ""): - ConfigSequence.__init__(self, seperator = ":", limits = [(0, (10**len)-1)], censor_char = censor, default = [default]) + assert isinstance(default, int), "ConfigPIN default must be an integer" + if default == -1: + default = "aaaa" + ConfigSequence.__init__(self, seperator = ":", limits = [(0, (10**len)-1)], censor_char = censor, default = default) + self.len = len + + def getLength(self): + return self.len class ConfigFloat(ConfigSequence): def __init__(self, default, limits): @@ -473,7 +501,7 @@ class ConfigText(ConfigElement, NumericalTextInput): self.text = self.text.ljust(len(self.text) + 1) elif key in KEY_NUMBERS: number = self.getKey(getKeyNumber(key)) - self.text = self.text[0:self.marked_pos] + str(number) + self.text[self.marked_pos + 1:] + self.text = self.text[0:self.marked_pos] + unicode(number) + self.text[self.marked_pos + 1:] elif key == KEY_TIMEOUT: self.timeout() return @@ -551,17 +579,17 @@ class ConfigSatlist(ConfigSelection): def __init__(self, list, default = None): if default is not None: default = str(default) - if list == [ ]: - list = [0, "N/A"] ConfigSelection.__init__(self, choices = [(str(orbpos), desc) for (orbpos, desc) in list], default = default) def getOrbitalPosition(self): + if self.value == "": + return None return int(self.value) orbital_position = property(getOrbitalPosition) # nothing. -class ConfigDummy(ConfigSelection): +class ConfigNothing(ConfigSelection): def __init__(self): ConfigSelection.__init__(self, choices = [""]) @@ -621,8 +649,8 @@ class ConfigSubList(list, object): saved_value = property(getSavedValue, setSavedValue) def append(self, item): - list.append(self, item) i = str(len(self)) + list.append(self, item) if i in self.stored_values: item.saved_value = self.stored_values[i] item.load() @@ -687,6 +715,7 @@ class ConfigSubsection(object): 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] @@ -701,6 +730,9 @@ class ConfigSubsection(object): 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): @@ -771,7 +803,8 @@ class Config(ConfigSubsection): # we inherit from ConfigSubsection, so ... #object.__setattr__(self, "saved_value", tree["config"]) - self.setSavedValue(tree["config"]) + if "config" in tree: + self.setSavedValue(tree["config"]) def saveToFile(self, filename): f = open(filename, "w") @@ -787,7 +820,7 @@ config = Config() config.misc = ConfigSubsection() class ConfigFile: - CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "config2") + CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "settings") def load(self): try: @@ -799,8 +832,23 @@ class ConfigFile: config.save() config.saveToFile(self.CONFIG_FILE) + def __resolveValue(self, pickles, cmap): + if cmap.has_key(pickles[0]): + if len(pickles) > 1: + return self.__resolveValue(pickles[1:], cmap[pickles[0]].content.items) + else: + return str(cmap[pickles[0]].value) + return None + def getResolvedKey(self, key): - return None # FIXME + names = key.split('.') + if len(names) > 1: + if names[0] == "config": + ret=self.__resolveValue(names[1:], config.content.items) + if ret and len(ret): + return ret + print "getResolvedKey", key, "failed !! (Typo??)" + return "" def NoSave(element): element.disableSave()