From: Ralf Ertzinger Date: Thu, 27 Feb 2014 22:18:17 +0000 (+0100) Subject: Integrate default values into ConfigParser X-Git-Tag: 0.2.98.python3.1~13 X-Git-Url: https://git.camperquake.de/gitweb.cgi?p=time-slider.git;a=commitdiff_plain;h=f0c72d33c4c8c10b0a0e1a245d6eb499d01e1f93;hp=b3414cd23fce8e0f1d7678a5a0dc20cacfae9ec3 Integrate default values into ConfigParser --- diff --git a/usr/share/time-slider/lib/time_slider/linux/timesliderconfig.py b/usr/share/time-slider/lib/time_slider/linux/timesliderconfig.py index 453abc6..03961af 100644 --- a/usr/share/time-slider/lib/time_slider/linux/timesliderconfig.py +++ b/usr/share/time-slider/lib/time_slider/linux/timesliderconfig.py @@ -68,24 +68,29 @@ default_properties = { }, } +class MyConfigParser(ConfigParser.ConfigParser): + def __init__(self): + ConfigParser.ConfigParser.__init__(self) + + for section, content in default_properties.iteritems(): + if not self.has_section(section): + self.add_section(section) + for k,v in content.iteritems(): + self.set(section, k, v) + class Config: def __init__(self): - self.config = ConfigParser.ConfigParser() + self.config = MyConfigParser() self.config.read(configfile) def get(self, section, option): try: result = self.config.get(section, option) - sys.stderr.write('CONFIG: FILE section %s, option %s with value %s\n' % (section, option, result)) + sys.stderr.write('CONFIG: GET section %s, option %s with value %s\n' % (section, option, result)) return result except (ConfigParser.NoOptionError, ConfigParser.NoSectionError): - try: - result = default_properties[section][option] - sys.stderr.write('CONFIG: DEFAULT section %s, option %s with value %s\n' % (section, option, result)) - return result - except KeyError: - sys.stderr.write('CONFIG: NOTFOUND section %s, option %s\n' % (section, option)) - return '' + sys.stderr.write('CONFIG: NOTFOUND section %s, option %s\n' % (section, option)) + return '' def sections(self): return self.config.sections()