Merge pull request #2288 from kived/config-upgrade

add upgrade method to ConfigParser
This commit is contained in:
Akshay Arora 2014-07-12 19:37:34 +05:30
commit de74dc48a0
1 changed files with 18 additions and 1 deletions

View File

@ -376,6 +376,12 @@ class ConfigParser(PythonConfigParser, object):
self._do_callbacks(section, option, value)
return ret
def setall(self, section, keyvalues):
'''Set a lot of keys/values in one section at the same time.
'''
for key, value in keyvalues.items():
self.set(section, key, value)
def get(self, section, option, **kwargs):
value = PythonConfigParser.get(self, section, option, **kwargs)
if PY2:
@ -384,7 +390,7 @@ class ConfigParser(PythonConfigParser, object):
return value
def setdefaults(self, section, keyvalues):
'''Set a lot of keys/values in one section at the same time.
'''Set a lot of keys/value defaults in one section at the same time.
'''
self.adddefaultsection(section)
for key, value in keyvalues.items():
@ -437,6 +443,17 @@ class ConfigParser(PythonConfigParser, object):
return False
return True
def update_config(self, filename, overwrite=False):
'''Upgrade the configuration based on a new default config file.
Overwrite any existing values if overwrite is True.
'''
pcp = PythonConfigParser()
pcp.read(filename)
confset = self.setall if overwrite else self.setdefaults
for section in pcp.sections():
confset(section, dict(pcp.items(section)))
self.write()
@staticmethod
def _register_named_property(name, widget_ref, *largs):
''' Called by the ConfigParserProperty to register a property which