# HG changeset patch # User Jeff Hammel # Date 1332780306 25200 # Node ID e00afe2c83bf5c5ec201a5fbe599f4cf973b7376 # Parent c782d750fd6dbb5f1f18170800726f14fff02490 stubbing configuration parser diff -r c782d750fd6d -r e00afe2c83bf configuration/config.py --- 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) diff -r c782d750fd6d -r e00afe2c83bf tests/example.py --- 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() - + diff -r c782d750fd6d -r e00afe2c83bf tests/unit.py --- 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()