summaryrefslogtreecommitdiffstats
path: root/module/config/ConfigParser.py
diff options
context:
space:
mode:
Diffstat (limited to 'module/config/ConfigParser.py')
-rw-r--r--module/config/ConfigParser.py130
1 files changed, 39 insertions, 91 deletions
diff --git a/module/config/ConfigParser.py b/module/config/ConfigParser.py
index 3cad67bc2..135e84bbc 100644
--- a/module/config/ConfigParser.py
+++ b/module/config/ConfigParser.py
@@ -17,25 +17,20 @@ ConfigData = namedtuple("ConfigData", "name type description default")
class ConfigParser:
"""
- Holds and manages the configuration + meta data for core and every user.
+ Holds and manages the configuration + meta data for config read from file.
"""
CONFIG = "pyload.conf"
- PLUGIN = "plugin.conf"
- def __init__(self):
- """Constructor"""
+ def __init__(self, config=None):
- # core config sections from pyload
- self.baseSections = []
+ if config: self.CONFIG = config
# Meta data information
self.config = OrderedDict()
# The actual config values
self.values = {}
- self.changeCB = None # callback when config value was changed
-
self.checkVersion()
self.loadDefault()
@@ -50,22 +45,21 @@ class ConfigParser:
# workaround conflict, with GUI (which also accesses the config) so try read in 3 times
for i in range(0, 3):
try:
- for conf in (self.CONFIG, self.PLUGIN):
- if exists(conf):
- f = open(conf, "rb")
- v = f.readline()
- f.close()
- v = v[v.find(":") + 1:].strip()
-
- if not v or int(v) < CONF_VERSION:
- f = open(conf, "wb")
- f.write("version: " + str(CONF_VERSION))
- f.close()
- print "Old version of %s deleted" % conf
- else:
- f = open(conf, "wb")
- f.write("version:" + str(CONF_VERSION))
+ if exists(self.CONFIG):
+ f = open(self.CONFIG, "rb")
+ v = f.readline()
+ f.close()
+ v = v[v.find(":") + 1:].strip()
+
+ if not v or int(v) < CONF_VERSION:
+ f = open(self.CONFIG, "wb")
+ f.write("version: " + str(CONF_VERSION))
f.close()
+ print "Old version of %s deleted" % self.CONFIG
+ else:
+ f = open(self.CONFIG, "wb")
+ f.write("version:" + str(CONF_VERSION))
+ f.close()
except Exception, ex:
e = ex
@@ -112,19 +106,13 @@ class ConfigParser:
def save(self):
"""saves config to filename"""
- # separate pyload and plugin conf
configs = []
- for c in (self.CONFIG, self.PLUGIN):
- f = open(c, "wb")
- configs.append(f)
- chmod(c, 0600)
- f.write("version: %i\n\n" % CONF_VERSION)
-
+ f = open(self.CONFIG, "wb")
+ configs.append(f)
+ chmod(self.CONFIG, 0600)
+ f.write("version: %i\n\n" % CONF_VERSION)
- # write on 2 files
for section, data in self.config.iteritems():
- f = configs[0] if section in self.baseSections else configs[1]
-
f.write("[%s]\n" % section)
for option, data in data.config.iteritems():
@@ -136,17 +124,21 @@ class ConfigParser:
f.write("\n")
- [f.close() for f in configs]
+ f.close()
def __getitem__(self, section):
"""provides dictionary like access: c['section']['option']"""
return Section(self, section)
+ def __contains__(self, section):
+ """ checks if parser contains section """
+ return section in self.config
+
def get(self, section, option):
- """get value"""
- if option in self.values[section]:
+ """get value or default"""
+ try:
return self.values[section][option]
- else:
+ except KeyError:
return self.config[section].config[option].default
def set(self, section, option, value, sync=True):
@@ -154,45 +146,27 @@ class ConfigParser:
data = self.config[section].config[option]
value = from_string(value, data.type)
+ old_value = self.get(section, option)
- # only save when different to default values
- if value != data.default or (option in self.values[section] and value != self.values[section][option]):
+ # only save when different values
+ if value != old_value:
+ if section not in self.values: self.values[section] = {}
self.values[section][option] = value
if sync:
- if self.changeCB: self.changeCB(section, option, value)
self.save()
+ return True
- def getPlugin(self, *args):
- """gets a value for a plugin"""
- ret = self.get(*args)
- print "Deprecated method getPlugin%s -> %s" % (str(args), ret)
- return ret
-
- def setPlugin(self, *args):
- """sets a value for a plugin"""
- print "Deprecated method setPlugin%s" % str(args)
- self.set(*args)
+ return False
def getMetaData(self, section, option):
""" get all config data for an option """
return self.config[section].config[option]
- def getBaseSections(self):
- for section, data in self.config.iteritems():
- if section in self.baseSections:
- yield section, data
- return
-
- def getPluginSections(self):
- for section, data in self.config.iteritems():
- if section not in self.baseSections:
- yield section, data
- return
+ def iterSections(self):
+ return self.config.iteritems()
- def addConfigSection(self, section, name, desc, long_desc, config, base=False):
+ def addConfigSection(self, section, name, desc, long_desc, config):
"""Adds a section to the config. `config` is a list of config tuples as used in plugin api defined as:
- Either (name, type, verbose_name, default_value) or
- (name, type, verbose_name, short_description, default_value)
The order of the config elements is preserved with OrderedDict
"""
d = OrderedDict()
@@ -200,23 +174,15 @@ class ConfigParser:
for entry in config:
if len(entry) == 5:
conf_name, type, conf_desc, conf_verbose, default = entry
- else: # config options without tooltip / description
+ else: # config options without description
conf_name, type, conf_desc, default = entry
conf_verbose = ""
d[conf_name] = ConfigData(gettext(conf_desc), type, gettext(conf_verbose), from_string(default, type))
- if base:
- if section not in self.baseSections: self.baseSections.append(section)
- elif section in self.baseSections:
- return # would overwrite base section
-
data = SectionTuple(gettext(name), gettext(desc), gettext(long_desc), d)
self.config[section] = data
- if section not in self.values:
- self.values[section] = {}
-
class Section:
"""provides dictionary like access for configparser"""
@@ -232,21 +198,3 @@ class Section:
def __setitem__(self, item, value):
"""setitem"""
self.parser.set(self.section, item, value)
-
-
-if __name__ == "__main__":
- pypath = ""
-
- from time import time
-
- a = time()
-
- c = ConfigParser()
-
- b = time()
-
- print "sec", b - a
-
- print c.config
-
- c.saveConfig(c.config, "user.conf")