# HG changeset patch # User Jeff Hammel # Date 1332865856 25200 # Node ID 79aca36abd9a64446191823fb6451292b28fdfdc # Parent b7d6a90b0bb5fcec9cf26c79ffbac7a9cd315d5a fill in defaults if not present diff -r b7d6a90b0bb5 -r 79aca36abd9a configuration/config.py --- a/configuration/config.py Mon Mar 26 20:31:12 2012 -0700 +++ b/configuration/config.py Tue Mar 27 09:30:56 2012 -0700 @@ -4,6 +4,7 @@ multi-level unified configuration """ +import copy import sys import optparse @@ -196,7 +197,14 @@ """add items to configuration and check it""" for config in args: self.add(config) - self.validate() # validate total configuration + + # add defaults if not present + for key, value in self.options.items(): + if 'default' in value and key not in self.config: + self.config[key] = value['default'] + + # validate total configuration + self.validate() # TODO: configuration should be locked after this is called def add(self, config, check=True): @@ -268,13 +276,36 @@ if format in provider.extensions: return provider - def serialize(self, filename, format=None): - """serialize configuration to a file""" + def filename2format(self, filename): + extension = os.path.splitext(filename)[-1] + return extension.rstrip('.') or None + + def serialize(self, filename, format=None, full=False): + """ + serialize configuration to a file + - filename: path of file to serialize to + - format: format of configuration provider + - full: whether to serialize non-set optional strings [TODO] + """ # TODO: allow file object vs file name + if not format: + format = self.filename2format(filename) + if not format: + raise Exception('Please specify a format') + # TODO: more specific exception type + + provider = self.configuration_provider(format) + + config = copy.deepcopy(self.config) + + provider.write(config, filename) + def deserialize(self, filename, format=None): """load configuration from a file""" # TODO: allow file object vs file name if not format: extension = os.path.splitext(filename)[-1] + + raise NotImplementedError("TODO") diff -r b7d6a90b0bb5 -r 79aca36abd9a tests/unit.py --- a/tests/unit.py Mon Mar 26 20:31:12 2012 -0700 +++ b/tests/unit.py Tue Mar 27 09:30:56 2012 -0700 @@ -53,6 +53,11 @@ # serialize to a temporary file filename = tempfile.mktemp() + config = {'browser_path': '/home/jhammel/bin/firefox', + 'activeTests': ['ts']} + example(config) + config['test_timeout'] = 1200 # default + self.assertEqual(config, example.config) if os.path.exists(filename): os.remove(filename)