# HG changeset patch # User Jeff Hammel # Date 1332872373 25200 # Node ID f09982d47b3c9b312f3b07a00e08ce49c5cf3d35 # Parent 75886253ee7371a6f3c911a8fbe8b3674dc4d906 add --dump option diff -r 75886253ee73 -r f09982d47b3c configuration/config.py --- 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: diff -r 75886253ee73 -r f09982d47b3c tests/unit.py --- a/tests/unit.py Tue Mar 27 11:07:51 2012 -0700 +++ b/tests/unit.py Tue Mar 27 11:19:33 2012 -0700 @@ -96,5 +96,7 @@ 'activeTests': ['ts']} args1 = ['-e', '/opt/bin/firefox'] + # + if __name__ == '__main__': unittest.main()