diff options
Diffstat (limited to 'pyload/config')
-rw-r--r-- | pyload/config/ConfigParser.py | 36 | ||||
-rw-r--r-- | pyload/config/convert.py | 29 |
2 files changed, 39 insertions, 26 deletions
diff --git a/pyload/config/ConfigParser.py b/pyload/config/ConfigParser.py index bda3f7bd4..0f96fd8b9 100644 --- a/pyload/config/ConfigParser.py +++ b/pyload/config/ConfigParser.py @@ -1,21 +1,19 @@ # -*- coding: utf-8 -*- from __future__ import with_statement -from time import sleep from os.path import exists from gettext import gettext from new_collections import namedtuple, OrderedDict - from pyload.Api import Input, InputType from pyload.utils.fs import chmod from default import make_config -from convert import to_input, from_string +from convert import to_configdata, from_string CONF_VERSION = 2 SectionTuple = namedtuple("SectionTuple", "label description explanation config") -ConfigData = namedtuple("ConfigData", "label description input") + class ConfigParser: """ @@ -109,8 +107,10 @@ class ConfigParser: for option, data in data.config.iteritems(): value = self.get(section, option) - if type(value) == unicode: value = value.encode("utf8") - else: value = str(value) + if type(value) == unicode: + value = value.encode("utf8") + else: + value = str(value) f.write('%s = %s\n' % (option, value)) @@ -165,35 +165,19 @@ class ConfigParser: return self.config[section], self.values[section] if section in self.values else {} def addConfigSection(self, section, label, desc, expl, config): - """Adds a section to the config. `config` is a list of config tuples as used in plugin api defined as: + """Adds a section to the config. `config` is a list of config tuple as used in plugin api defined as: The order of the config elements is preserved with OrderedDict """ d = OrderedDict() for entry in config: - if len(entry) != 4: - raise ValueError("Config entry must be of length 4") - - # Values can have different roles depending on the two config formats - conf_name, type_label, label_desc, default_input = entry - - # name, label, desc, input - if isinstance(default_input, Input): - input = default_input - conf_label = type_label - conf_desc = label_desc - # name, type, label, default - else: - input = Input(to_input(type_label)) - input.default_value = from_string(default_input, input.type) - conf_label = label_desc - conf_desc = "" - - d[conf_name] = ConfigData(gettext(conf_label), gettext(conf_desc), input) + name, data = to_configdata(entry) + d[name] = data data = SectionTuple(gettext(label), gettext(desc), gettext(expl), d) self.config[section] = data + class Section: """provides dictionary like access for configparser""" diff --git a/pyload/config/convert.py b/pyload/config/convert.py index 7a110e0f3..59f814020 100644 --- a/pyload/config/convert.py +++ b/pyload/config/convert.py @@ -1,7 +1,14 @@ +# -*- coding: utf-8 -*- + +from gettext import gettext + +from new_collections import namedtuple from pyload.Api import Input, InputType from pyload.utils import decode, to_bool +ConfigData = namedtuple("ConfigData", "label description input") + # Maps old config formats to new values input_dict = { "int": InputType.Int, @@ -18,6 +25,28 @@ def to_input(typ): return input_dict.get(typ, InputType.Text) +def to_configdata(entry): + if len(entry) != 4: + raise ValueError("Config entry must be of length 4") + + # Values can have different roles depending on the two config formats + conf_name, type_label, label_desc, default_input = entry + + # name, label, desc, input + if isinstance(default_input, Input): + _input = default_input + conf_label = type_label + conf_desc = label_desc + # name, type, label, default + else: + _input = Input(to_input(type_label)) + _input.default_value = from_string(default_input, _input.type) + conf_label = label_desc + conf_desc = "" + + return conf_name, ConfigData(gettext(conf_label), gettext(conf_desc), _input) + + def from_string(value, typ=None): """ cast value to given type, unicode for strings """ |