Mercurial > hg > configuration
changeset 34:79aca36abd9a
fill in defaults if not present
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 27 Mar 2012 09:30:56 -0700 |
parents | b7d6a90b0bb5 |
children | 321fe58a9eae |
files | configuration/config.py tests/unit.py |
diffstat | 2 files changed, 39 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- 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")
--- 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)