Mercurial > hg > configuration
changeset 11:e00afe2c83bf
stubbing configuration parser
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 26 Mar 2012 09:45:06 -0700 |
parents | c782d750fd6d |
children | db43d30afcf5 |
files | configuration/config.py tests/example.py tests/unit.py |
diffstat | 3 files changed, 39 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/configuration/config.py Mon Mar 26 09:14:04 2012 -0700 +++ b/configuration/config.py Mon Mar 26 09:45:06 2012 -0700 @@ -49,6 +49,10 @@ self.config = {} self.configuration_providers = configuration_providers + def items(self): + # TODO: allow options to be a list of 2-tuples + return options.items() + def check(self, config): """check validity of configuration""" @@ -68,21 +72,43 @@ self.config.update(config) # TODO: option to extend; augment lists/dicts - def parser(self): - """return OptionParser""" + def parser(self, configuration_provider_option=None, **parser_args): + """ + return OptionParser for this Configuration instance + - configuration_provider_options : option for configuration files [TODO] + (also TODO: a special value that equates to the first file extension value + for the configuration_providers) + - parser_args : arguments to the OptionParser constructor + """ + if 'description' not in parser_args: + parser_args['description'] = getattr(self, '__doc__', '') + if 'formatter' not in parser_args: + class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): + """description formatter for console script entry point""" + def format_description(self, description): + if description: + return description.strip() + '\n' + else: + return '' + parser_args['formatter'] = PlainDescriptionFormatter() + parser = optparse.OptionParser(**parser_args) + for key, value in self.items(): + + # CLI arguments + args = value.get('flags', ['--%s' % key]) + if not args: + continue + + kw = {} + help = value.get('help', key) + # TODO: types + raise NotImplementedError("TODO") def main(args=sys.argv[:]): # parse command line options usage = '%prog [options]' - class PlainDescriptionFormatter(optparse.IndentedHelpFormatter): - """description formatter for console script entry point""" - def format_description(self, description): - if description: - return description.strip() + '\n' - else: - return '' parser = optparse.OptionParser(usage=usage, description=__doc__, formatter=PlainDescriptionFormatter()) options, args = parser.parse_args(args)
--- a/tests/example.py Mon Mar 26 09:14:04 2012 -0700 +++ b/tests/example.py Mon Mar 26 09:45:06 2012 -0700 @@ -15,4 +15,4 @@ if __name__ == '__main__': parser = ExampleConfiguration().parser() - +
--- a/tests/unit.py Mon Mar 26 09:14:04 2012 -0700 +++ b/tests/unit.py Mon Mar 26 09:45:06 2012 -0700 @@ -8,13 +8,15 @@ import sys import unittest +import example # example configuration to test + # globals here = os.path.dirname(os.path.abspath(__file__)) class configurationUnitTest(unittest.TestCase): def test_configuration(self): - pass + if __name__ == '__main__': unittest.main()