changeset 81:f3b2563b2a91

move to a class-based architecture
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 17 Apr 2012 11:36:01 -0700
parents 35416ad14098
children 490687e70723
files configuration/configuration.py
diffstat 1 files changed, 64 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/configuration/configuration.py	Mon Apr 09 15:47:08 2012 -0700
+++ b/configuration/configuration.py	Tue Apr 17 11:36:01 2012 -0700
@@ -127,76 +127,82 @@
 #     def take_action(self, ...):
 #         """do something appropriate based on type"""
 
-def base_cli(name, value):
+class BaseCLI(object):
     """base_cli for all option types"""
 
-    # CLI arguments
-    args = value.get('flags', ['--%s' % name])
-    if not args:
-        # No CLI interface
-        return (), {}
+    def __call__(self, name, value):
+        """return args, kwargs needed to instantiate an optparse option"""
+
+        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
+        kw = {'dest': name}
+        help = value.get('help', name)
+        if 'default' in value:
+            kw['default'] = value['default']
+            help += ' [DEFAULT: %s]' % value['default']
+        kw['help'] = help
+        kw['action'] = 'store'
+        return args, kw
 
-        help += ' [DEFAULT: %s]' % value['default']
-    kw['help'] = help
-    kw['action'] = 'store'
-    return args, kw
+class BoolCLI(BaseCLI):
+
+    def __call__(self, name, value):
 
-def bool_cli(name, value):
-
-    # preserve the default values
-    help = value.get('help')
-    flags = value.get('flags')
+        # preserve the default values
+        help = value.get('help')
+        flags = value.get('flags')
 
-    args, kw = base_cli(name, value)
-    kw['help'] = help # reset
-    if value.get('default'):
-        kw['action'] = 'store_false'
-        if not flags:
-            args = ['--no-%s' % name]
-        if not help:
-            kw['help'] = 'disable %s' % name
-    else:
-        kw['action'] = 'store_true'
-        if not help:
-            kw['help'] = 'enable %s' % name
-    return args, kw
+        args, kw = BaseCLI.__call__(self, name, value)
+        kw['help'] = help # reset
+        if value.get('default'):
+            kw['action'] = 'store_false'
+            if not flags:
+                args = ['--no-%s' % name]
+            if not help:
+                kw['help'] = 'disable %s' % name
+        else:
+            kw['action'] = 'store_true'
+            if not help:
+                kw['help'] = 'enable %s' % name
+        return args, kw
 
-def list_cli(name, value):
-    args, kw = base_cli(name, value)
+class ListCLI(BaseCLI):
+
+    def __call__(self, name, value):
+        args, kw = BaseCLI.__call__(self, name, value)
 
-    # TODO: could use 'extend'
-    # - http://hg.mozilla.org/build/mozharness/file/5f44ba08f4be/mozharness/base/config.py#l41
+        # 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
+        # TODO: what about nested types?
+        kw['action'] = 'append'
+        return args, kw
+
+class IntCLI(BaseCLI):
 
-def int_cli(name, value):
-    args, kw = base_cli(name, value)
-    kw['type'] = 'int'
-    return args, kw
+    def __call__(self, name, value):
+        args, kw = BaseCLI.__call__(self, name, value)
+        kw['type'] = 'int'
+        return args, kw
 
-def float_cli(name, value):
-    args, kw = base_cli(name, value)
-    kw['type'] = 'float'
-    return args, kw
+class FloatCLI(BaseCLI):
+
+    def __call__(self, name, value):
+        args, kw = BaseCLI.__call__(self, name, value)
+        kw['type'] = 'float'
+        return args, kw
 
 # TODO: 'dict'-type cli interface
 
-types = {bool:  bool_cli,
-         int:   int_cli,
-         float: float_cli,
-         list:  list_cli,
-         None:  base_cli} # default
-__all__ += [i.__name__ for i in types.values()]
+types = {bool:  BoolCLI(),
+         int:   IntCLI(),
+         float: FloatCLI(),
+         list:  ListCLI(),
+         None:  BaseCLI()} # default
+__all__ += [i.__class__.__name__ for i in types.values()]
 
 class Configuration(optparse.OptionParser):
     """declarative configuration object"""
@@ -325,6 +331,8 @@
             parser.add_option(*args, **kw)
 
     def parse_args(self, *args, **kw):
+
+        self.parsed = set()
         options, args = optparse.OptionParser.parse_args(self, *args, **kw)
 
         # get CLI configuration options