changeset 63:e7005b75ef8d

still refactorin
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 28 Mar 2012 08:29:50 -0700
parents 81996be938bb
children a8013bd3126a
files configuration/configuration.py
diffstat 1 files changed, 57 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/configuration.py	Tue Mar 27 20:17:26 2012 -0700
+++ b/configuration/configuration.py	Wed Mar 28 08:29:50 2012 -0700
@@ -182,7 +182,7 @@
 
     options = {} # configuration basis
 
-    def __init__(self, configuration_providers=configuration_providers, types=types, **parser_args):
+    def __init__(self, configuration_providers=configuration_providers, configuration_provider_option=None, types=types, dump='--dump', **parser_args):
 
         # setup configuration
         self.config = {}
@@ -205,6 +205,18 @@
         optparse.OptionParser.__init__(self, **parser_args)
         self.parsed = set()
         self.optparse_options(self)
+        # add option(s) for configuration_providers
+        if configuration_provider_option:
+            self.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:
+            self.add_option(dump, dest='dump',
+                            help="dump configuration to a file; Formats: %s" % formats)
+
 
     ### methods for iteration
     ### TODO: make this a real iterator
@@ -294,7 +306,6 @@
         """
         return OptionParser for this Configuration instance
         - 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
         """
@@ -317,22 +328,9 @@
 
         # 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")
+            parser.add_option(configuration_provider_option,
+                              dest='load', action='append',
+                              help="load configuration from a file")
 
         # add an option for dumping
         formats = self.formats()
@@ -342,6 +340,47 @@
 
         return parser
 
+    def parse_args(self, *args, **kw):
+        options, args = optparse.OptionParser.parse_args(self, *args, **kw)
+
+        # get CLI configuration options
+        cli_config = dict([(key, value) for key, value in options.__dict__.items()
+                           if key in self.options and 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
+
     def parse(self, args=sys.argv[1:], parser=None, configuration_provider_option=None):
         """
         parse configuration including command line options