Mercurial > hg > configuration
changeset 43:f09982d47b3c
add --dump option
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 27 Mar 2012 11:19:33 -0700 |
parents | 75886253ee73 |
children | b3db38b14567 |
files | configuration/config.py tests/unit.py |
diffstat | 2 files changed, 27 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/configuration/config.py Tue Mar 27 11:07:51 2012 -0700 +++ b/configuration/config.py Tue Mar 27 11:19:33 2012 -0700 @@ -248,12 +248,13 @@ continue parser.add_option(*args, **kw) - def parser(self, configuration_provider_option=None, **parser_args): + 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) + - dump : option for dumping configuration - parser_args : arguments to the OptionParser constructor """ if 'description' not in parser_args: @@ -269,6 +270,15 @@ parser_args['formatter'] = PlainDescriptionFormatter() parser = optparse.OptionParser(**parser_args) self.optparse_options(parser) + + # TODO: add option(s) for configuration_providers + + # add an option for dumping + formats = self.formats() + if formats and dump: + parser.add_option(dump, dest='dump', + help="dump configuration to a file [Formats: %s]" % formats) + return parser def parse(self, args=sys.argv[1:], parser=None, configuration_provider_option=None): @@ -286,11 +296,25 @@ # generate configuration self(cli_config) + # dump configuration, if specified + dump = getattr(options, 'dump') + if dump: + # TODO: have a way of specifying format other than filename + self.serialize(dump) + # return parsed arguments return options, args ### serialization/deserialization + def formats(self): + """formats for deserialization""" + retval = [] + for provider in self.configuration_providers: + if provider.extensions and hasattr(provider, 'write'): + retval.append(provider.extensions[0]) + return retval + def configuration_provider(self, format): """configuration provider guess for a given filename""" for provider in self.configuration_providers: