changeset 65:2a2e9aee5bc6

refactor completed and tests pass again
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 28 Mar 2012 08:37:01 -0700
parents a8013bd3126a
children f3b3f4046a8a
files configuration/configuration.py tests/unit.py
diffstat 2 files changed, 6 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/configuration.py	Wed Mar 28 08:33:54 2012 -0700
+++ b/configuration/configuration.py	Wed Mar 28 08:37:01 2012 -0700
@@ -302,44 +302,6 @@
                 continue
             parser.add_option(*args, **kw)
 
-    def parser(self, configuration_provider_option=None, dump='--dump', **parser_args):
-        """
-        return OptionParser for this Configuration instance
-        - configuration_provider_options : option for configuration files
-        - dump : option for dumping configuration
-        - 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_args.setdefault('option_class', ConfigurationOption)
-        parser = optparse.OptionParser(**parser_args)
-        parser.parsed = set()
-        self.optparse_options(parser)
-
-        # add option(s) for configuration_providers
-        if configuration_provider_option:
-            parser.add_option(configuration_provider_option,
-                              dest='load', action='append',
-                              help="load configuration from a file")
-
-        # 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_args(self, *args, **kw):
         options, args = optparse.OptionParser.parse_args(self, *args, **kw)
 
@@ -384,63 +346,6 @@
         # return parsed arguments
         return options, args
 
-    def parse(self, args=sys.argv[1:], parser=None, configuration_provider_option=None):
-        """
-        parse configuration including command line options
-        - args: command line arguments to parse (default: system arguments)
-        - parser: a parser instance
-        - config_provider_option: option for configuration files; if None,
-          will be taken from remaining args
-        """
-
-        # parse arguments
-        if parser is None:
-            parser = self.parser(configuration_provider_option=configuration_provider_option)
-        options, args = parser.parse_args(args)
-
-        # get CLI configuration options
-        cli_config = dict([(key, value) for key, value in options.__dict__.items()
-                           if key in self.options])
-        if hasattr(parser, 'parsed'):
-            # only use parsed arguments
-            # (though i'm not sure what to do with parser doesn't have the parsed attribute)
-            cli_config = dict([(key, value) for key, value in cli_config.items()
-                               if key in parser.parsed])
-
-        # deserialize configuration
-        configuration_files = getattr(options, 'load', args)
-        missing = [i for i in configuration_files
-                   if not os.path.exists(i)]
-        if missing:
-            parser.error("Missing files: %s" % ', '.join(missing))
-        config = []
-        for f in configuration_files:
-            try:
-                config.append(self.deserialize(f))
-            except BaseException, e:
-                parser.error(str(e))
-        config.append(cli_config)
-
-        missingvalues = None
-        try:
-            # generate configuration
-            self(*config)
-        except MissingValueException, missingvalues:
-            pass
-
-        # dump configuration, if specified
-        dump = getattr(options, 'dump')
-        if dump:
-            # TODO: have a way of specifying format other than filename
-            self.serialize(dump)
-
-        if missingvalues and not dump:
-            # XXX assuming if you don't have values you were just dumping
-            raise missingvalues
-
-        # return parsed arguments
-        return options, args
-
     ### serialization/deserialization
 
     def formats(self):
--- a/tests/unit.py	Wed Mar 28 08:33:54 2012 -0700
+++ b/tests/unit.py	Wed Mar 28 08:37:01 2012 -0700
@@ -28,7 +28,7 @@
         example = ExampleConfiguration()
 
         # parse command line arguments
-        options, args = example.parse(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox'])
+        options, args = example.parse_args(['-a', 'ts', '--develop', '-e', '/home/jhammel/bin/firefox'])
 
         # ensure that the options appropriately get set
         self.assertEqual(bool(args), False) # no arguments
@@ -80,7 +80,7 @@
 
         exception = None
         try:
-            example.parse(args=[])
+            example.parse_args(args=[])
         except Exception, exception:
             pass
         self.assertTrue(isinstance(exception, configuration.MissingValueException))
@@ -111,19 +111,19 @@
 
         # parse the json file
         example = ExampleConfiguration()
-        example.parse([json_file])
+        example.parse_args([json_file])
         self.assertEqual(example.config, json_config)
 
         # parse the json file with overrides
         example = ExampleConfiguration()
-        example.parse([json_file] + args1)
+        example.parse_args([json_file] + args1)
         config = json_config.copy()
         config['browser_path'] = '/opt/bin/firefox'
         self.assertEqual(example.config, config)
 
         # it shouldn't matter in which order the arguments are
         example = ExampleConfiguration()
-        example.parse(args1 + [json_file])
+        example.parse_args(args1 + [json_file])
         self.assertEqual(example.config, config)
 
         # Now a tricky case:
@@ -134,7 +134,7 @@
         self.assertEqual(json_config['test_timeout'], 60)
         self.assertEqual(config['test_timeout'], 60)
         # but we can override it back from the "command line"
-        example.parse(args1 + [json_file, '--test_timeout', '1200'])
+        example.parse_args(args1 + [json_file, '--test_timeout', '1200'])
         config['test_timeout'] = 1200
         self.assertEqual(example.config, config)