changeset 21:0fe74db6a56c

a hacky way to do CLI types handlers
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 26 Mar 2012 14:45:36 -0700
parents dbfabe96187e
children f6c3f91af7f2
files configuration/config.py
diffstat 1 files changed, 46 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/config.py	Mon Mar 26 14:03:48 2012 -0700
+++ b/configuration/config.py	Mon Mar 26 14:45:36 2012 -0700
@@ -41,13 +41,55 @@
 
     configuration_providers.append(YAML)
 
+### plugins for option types
+### TODO: this could use a bit of thought
+def base_cli(name, value):
+    # CLI arguments
+    args = value.get('flags', ['--%s' % name])
+    if not args:
+        # No CLI interface
+        return (), {}
+
+    kw = {'dest': name}
+    help = value.get('help', name)
+    if 'default' in value:
+        kw['default'] = value['default']
+        # TODO: use default pattern a la
+        # - http://hg.mozilla.org/build/talos/file/c6013a2f09ce/talos/PerfConfigurator.py#l358
+        # - http://k0s.org/mozilla/hg/bzconsole/file/d5e88dadde69/bzconsole/command.py#l12
+
+        help += ' [DEFAULT: %s]' % value['default']
+    kw['help'] = help
+    kw['action'] = 'store'
+    return args, kw
+
+def bool_cli(name, value):
+    args, kw = base_cli(name, value)
+    if value.get('default'):
+        pass # TODO
+    return args, kw
+
+def list_cli(name, value):
+    args, kw = base_cli(name, value)
+
+    # TODO: could use 'extend'
+    # - http://hg.mozilla.org/build/mozharness/file/5f44ba08f4be/mozharness/base/config.py#l41
+
+    # TODO: what about nested types?
+    kw['action'] = 'append'
+    return args, kw
+
+types = {bool: bool_cli,
+         list: list_cli,
+         None: base_cli} # default
 
 class Configuration(object):
     options = {}
 
-    def __init__(self, configuration_providers=configuration_providers):
+    def __init__(self, configuration_providers=configuration_providers, types=types):
         self.config = {}
         self.configuration_providers = configuration_providers
+        self.types = types
 
     def items(self):
         # TODO: allow options to be a list of 2-tuples
@@ -75,41 +117,14 @@
     ### methods for optparse
     ### XXX could go in a subclass
 
-    @classmethod
-    def bool_cli(cls, value):
-        if value.get('default'):
-            pass
-    @classmethod
-    def list_cli(cls, value):
-        # TODO: could use 'extend'
-        # - http://hg.mozilla.org/build/mozharness/file/5f44ba08f4be/mozharness/base/config.py#l41
-        return dict(action='append')
-
-    types = {bool: bool_cli,
-             list: list_cli}
-
     def optparse_options(self, parser):
         """add optparse options to a OptionParser instance"""
         for key, value in self.items():
-            # TODO: move adding options to a separate function
-
-            # CLI arguments
-            args = value.get('flags', ['--%s' % key])
+            handler = self.types[value.get('type')]
+            args, kw = handler(key, value)
             if not args:
+                # No CLI interface
                 continue
-
-            kw = {'dest': key}
-            help = value.get('help', key)
-            if 'default' in value:
-
-                kw['default'] = value['default']
-                # TODO: use default pattern a la
-                # - http://hg.mozilla.org/build/talos/file/c6013a2f09ce/talos/PerfConfigurator.py#l358
-                # - http://k0s.org/mozilla/hg/bzconsole/file/d5e88dadde69/bzconsole/command.py#l12
-
-                help += ' [DEFAULT: %s]' % value['default']
-            kw['help'] = help
-            kw['action'] = 'store' # TODO: types
             parser.add_option(*args, **kw)
 
     def parser(self, configuration_provider_option=None, **parser_args):