From cc76686009bb1fc5edc3aace124f408bcd3afb4f Mon Sep 17 00:00:00 2001 From: Ryan Pessa Date: Mon, 23 Jun 2014 17:26:18 -0500 Subject: [PATCH 1/2] add upgrade method to ConfigParser --- kivy/config.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/kivy/config.py b/kivy/config.py index 0d8b2c9c7..04d91efe6 100644 --- a/kivy/config.py +++ b/kivy/config.py @@ -426,6 +426,15 @@ class ConfigParser(PythonConfigParser, object): return False return True + def upgrade(self, default_config_file): + '''Upgrade the configuration based on a new default config file. + ''' + pcp = PythonConfigParser() + pcp.read(default_config_file) + for section in pcp.sections(): + self.setdefaults(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 From 504ac4b23baaf8b5b50560f4b4fa4eb5e9b2a556 Mon Sep 17 00:00:00 2001 From: Ryan Pessa Date: Tue, 24 Jun 2014 10:19:31 -0500 Subject: [PATCH 2/2] extend update_config to overwrite values --- kivy/config.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kivy/config.py b/kivy/config.py index 04d91efe6..146b56c74 100644 --- a/kivy/config.py +++ b/kivy/config.py @@ -365,6 +365,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: @@ -373,7 +379,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(): @@ -426,13 +432,15 @@ class ConfigParser(PythonConfigParser, object): return False return True - def upgrade(self, default_config_file): + 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(default_config_file) + pcp.read(filename) + confset = self.setall if overwrite else self.setdefaults for section in pcp.sections(): - self.setdefaults(section, dict(pcp.items(section))) + confset(section, dict(pcp.items(section))) self.write() @staticmethod