Mercurial > hg > configuration
changeset 50:113e8c2f7cab
start to hook up deserializers
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 27 Mar 2012 14:01:25 -0700 |
parents | 09fbc09455d4 |
children | fb133bc3bed1 |
files | configuration/config.py |
diffstat | 1 files changed, 39 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/configuration/config.py Tue Mar 27 13:00:14 2012 -0700 +++ b/configuration/config.py Tue Mar 27 14:01:25 2012 -0700 @@ -88,6 +88,9 @@ configuration_providers.append(YAML()) +# TODO: add a configuration provider for taking command-line arguments +# from a file + __all__.extend([i.__class__.__name__ for i in configuration_providers]) ### optparse interface @@ -269,9 +272,8 @@ def parser(self, configuration_provider_option=None, dump='--dump', **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) + - configuration_provider_options : option for configuration files + (or '-' for taking from the extensions names) - dump : option for dumping configuration - parser_args : arguments to the OptionParser constructor """ @@ -292,7 +294,24 @@ parser.parsed = set() self.optparse_options(parser) - # TODO: add option(s) for configuration_providers + # add option(s) for configuration_providers + if configuration_provider_option: + if configuration_provider_option == '-': + raise NotImplementedError(""" +The arguments will be interspersed. Will need to be more clever to get this +to work properly. +""") + + # take option from configuration_provider extensions + for format in self.formats(): + parser.add_option('--%s' % format, + dest='load_%s' % format, + action='append', + help="load configuration from a %s file" % format) + else: + parser.add_option(configuration_provider_option, + dest='load', action='append', + help="load configuration from a file") # add an option for dumping formats = self.formats() @@ -303,7 +322,11 @@ return parser def parse(self, args=sys.argv[1:], parser=None, configuration_provider_option=None): - """parse configuration including command line options""" + """ + parse configuration including command line options + - args: command line arguments to parse (default: system arguments) + - parser + """ # parse arguments if parser is None: @@ -319,6 +342,15 @@ cli_config = dict([(key, value) for key, value in cli_config.items() if key in parser.parsed]) + # deserialize configuration + configuration_files = getattr(options, 'load', []) + missing = [i for i in configuration_files + if not os.path.exists(i)] + if missing: + parser.error("Missing files: %s" % ', '.join(missing)) + for f in configuration_files: + pass + # generate configuration self(cli_config) @@ -378,6 +410,8 @@ """load configuration from a file""" # TODO: allow file object vs file name + assert os.path.exists(filename) + # get the format if not format: format = self.filename2format(filename)