← Back to team overview

configglue team mailing list archive

[Merge] lp:~configglue/configglue/schemaconfig-integration-fixes into lp:configglue/schemaconfig-integration

 

Ricardo Kirkner has proposed merging lp:~configglue/configglue/schemaconfig-integration-fixes into lp:configglue/schemaconfig-integration.

Requested reviews:
  Configglue developers (configglue)


fixes most unit tests (the unicode test is left failing for further discussion, as it seems to fail on lucid but work on maverick)
-- 
https://code.launchpad.net/~configglue/configglue/schemaconfig-integration-fixes/+merge/31792
Your team Configglue developers is requested to review the proposed merge of lp:~configglue/configglue/schemaconfig-integration-fixes into lp:configglue/schemaconfig-integration.
=== modified file 'configglue/inischema/glue.py'
--- configglue/inischema/glue.py	2010-08-04 12:38:07 +0000
+++ configglue/inischema/glue.py	2010-08-04 21:26:01 +0000
@@ -32,7 +32,7 @@
         return parser.optionxform(section +'-'+ option)
     
 
-def configglue(fileobj, *filenames, **kwargs):
+def _configglue(fileobj, *filenames, **kwargs):
     """Populate an OptionParser with options and defaults taken from a
     series of files.
 
@@ -90,6 +90,6 @@
     return op, options, args
 
 from configglue.pyschema import schemaconfigglue, ini2schema
-def configglue(fileobj, args=None):
-    return schemaconfigglue(ini2schema(fileobj),
-                            argv=args)
+def configglue(fileobj, *filenames, **kwargs):
+    args = kwargs.pop('args', None)
+    return schemaconfigglue(ini2schema(fileobj), argv=args)

=== modified file 'configglue/pyschema/__init__.py'
--- configglue/pyschema/__init__.py	2010-07-31 01:15:59 +0000
+++ configglue/pyschema/__init__.py	2010-08-04 21:26:01 +0000
@@ -32,7 +32,8 @@
 
     parser2option = {'unicode': options.StringConfigOption,
                      'int': options.IntConfigOption,
-                     'bool': options.BoolConfigOption}
+                     'bool': options.BoolConfigOption,
+                     'lines': options.LinesConfigOption}
 
     class MySchema(Schema):
         pass
@@ -45,6 +46,7 @@
             setattr(MySchema, section_name, section)
         for option_name in p.options(section_name):
             option = p.get(section_name, option_name)
+
             parser = option.attrs.pop('parser', 'unicode')
             parser_args = option.attrs.pop('parser.args', '').split()
             parser_fun = getattr(parsers, parser, None)
@@ -52,9 +54,23 @@
                 parser_fun = getattr(__builtin__, parser, None)
             if parser_fun is None:
                 parser_fun = lambda x: x
+
+            attrs = {}
+            option_help = option.attrs.pop('help', None)
+            if option_help is not None:
+                attrs['help'] = option_help
+            if not option.is_empty:
+                attrs['default'] = parser_fun(option.value, *parser_args)
+            option_action = option.attrs.pop('action', None)
+            if option_action is not None:
+                attrs['action'] = option_action
+
             klass = parser2option.get(parser, options.StringConfigOption)
-            setattr(section, option_name,
-                    klass(default=parser_fun(option.value, *parser_args)))
+            if parser == 'lines':
+                instance = klass(options.StringConfigOption(), **attrs)
+            else:
+                instance = klass(**attrs)
+            setattr(section, option_name, instance)
 
     return SchemaConfigParser(MySchema())
 
@@ -69,6 +85,9 @@
             return option.name
         return option.section.name + '_' + option.name
 
+    def opt_name(option):
+        return long_name(option).replace('-', '_')
+
     if op is None:
         op = OptionParser()
     if argv is None:
@@ -81,17 +100,17 @@
         else:
             og = op.add_option_group(section.name)
         for option in section.options():
-            attrs = {}
+            kwargs = {}
             if option.help:
-                attrs['help'] = option.help
-            default = parser.get(section.name, option.name)
-            og.add_option('--' + long_name(option),
-                          default=default, **attrs)
+                kwargs['help'] = option.help
+            kwargs['default'] = parser.get(section.name, option.name)
+            kwargs['action'] = option.action
+            og.add_option('--' + long_name(option), **kwargs)
     options, args = op.parse_args(argv)
 
     for section in schema.sections():
         for option in section.options():
-            value = getattr(options, long_name(option))
+            value = getattr(options, opt_name(option))
             if parser.get(section.name, option.name) != value:
                 # the value has been overridden by an argument;
                 # update it.
@@ -142,7 +161,7 @@
     require_parser = False
 
     def __init__(self, name='', raw=False, default=NO_DEFAULT, fatal=False, help='',
-                 section=None):
+                 section=None, action='store'):
         self.name = name
         self.raw = raw
         self.fatal = fatal
@@ -151,6 +170,7 @@
         self.default = default
         self.help = help
         self.section = section
+        self.action = action
 
     def __eq__(self, other):
         try:

=== modified file 'configglue/pyschema/options.py'
--- configglue/pyschema/options.py	2010-07-31 01:15:59 +0000
+++ configglue/pyschema/options.py	2010-08-04 21:26:01 +0000
@@ -84,9 +84,9 @@
         return items
 
     def __init__(self, item, raw=False, default=NO_DEFAULT, fatal=False,
-        help='', remove_duplicates=False):
+        help='', action='store', remove_duplicates=False):
         super(LinesConfigOption, self).__init__(raw=raw, default=default,
-                                                fatal=fatal, help=help)
+            fatal=fatal, help=help, action=action)
         self.item = item
         self.require_parser = item.require_parser
         self.raw = item.raw
@@ -109,10 +109,10 @@
         return unicode(value)
 
     def __init__(self, raw=False, default=NO_DEFAULT, fatal=False, null=False,
-                 help=''):
+                 help='', action='store'):
         self.null = null
         super(StringConfigOption, self).__init__(raw=raw, default=default,
-                                                 fatal=fatal, help=help)
+            fatal=fatal, help=help, action=action)
 
 
 class TupleConfigOption(ConfigOption):
@@ -122,9 +122,10 @@
     constructor argument.
     """
 
-    def __init__(self, length=0, raw=False, default=NO_DEFAULT, fatal=False, help=''):
+    def __init__(self, length=0, raw=False, default=NO_DEFAULT, fatal=False,
+                 help='', action='store'):
         super(TupleConfigOption, self).__init__(raw=raw, default=default,
-                                                fatal=fatal, help=help)
+            fatal=fatal, help=help, action=action)
         self.length = length
 
     def _get_default(self):
@@ -160,8 +161,8 @@
     require_parser = True
 
     def __init__(self, spec=None, strict=False, raw=False,
-                 default=NO_DEFAULT, fatal=False,
-                 help='', item=None):
+                 default=NO_DEFAULT, fatal=False, help='', action='store',
+                 item=None):
         if spec is None:
             spec = {}
         if item is None:
@@ -170,7 +171,7 @@
         self.strict = strict
         self.item = item
         super(DictConfigOption, self).__init__(raw=raw, default=default,
-                                               fatal=fatal, help=help)
+            fatal=fatal, help=help, action=action)
 
     def _get_default(self):
         default = {}

=== modified file 'tests/inischema/test_glue.py'
--- tests/inischema/test_glue.py	2010-08-04 12:38:07 +0000
+++ tests/inischema/test_glue.py	2010-08-04 21:26:01 +0000
@@ -28,18 +28,22 @@
     """ Base class to keep common set-up """
     def setUp(self):
         self.file = StringIO(self.ini)
+        self.old_sys_argv = sys.argv
+        sys.argv = ['']
+
+    def tearDown(self):
+        sys.argv = self.old_sys_argv
 
 class TestGlue(TestBase):
     ini = '''
 [blah]
-foo.default = 3
 foo.help = yadda yadda yadda
          yadda
 foo.metavar = FOO
 foo.parser = int
 foo = 2
 '''
-    arg = '--blah-foo'
+    arg = '--blah_foo'
     opt = 'blah_foo'
     val = 2
 
@@ -52,7 +56,7 @@
         parser, options, args = configglue(self.file,
                                            args=['', self.arg + '=5'])
         self.assertEqual(vars(options),
-                         {self.opt: 5})
+                         {self.opt: '5'})
     def test_help_is_displayed(self):
         sys.stdout = StringIO()
         try:
@@ -78,11 +82,11 @@
 class TestNoValue(TestGlue):
     ini = '''
 [blah]
-foo.default = 3
 foo.help = yadda yadda yadda
          yadda
 foo.metavar = FOO
 foo.parser = int
+foo = 3
 '''
     val = 3
 
@@ -96,11 +100,11 @@
     ini = '[x]\na.help=hi\n'
     def test_empty(self):
         parser, options, args = configglue(self.file)
-        self.assertEqual(options.x_a, None)
+        self.assertEqual(options.x_a, '')
 
     def test_accepts_args_and_filenames(self):
         parser, options, args = configglue(self.file, 'dummy',
-                                           args=['', '--x-a=1'])
+                                           args=['', '--x_a=1'])
         self.assertEqual(options.x_a, '1')
 
 class TestGlueBool(TestBase):
@@ -133,7 +137,7 @@
 '''
     def test_nothing(self):
         parser, options, args = configglue(self.file)
-        self.assertEqual(options.foo, None)
+        self.assertEqual(options.foo, [])
 
     def test_no_append(self):
         parser, options, args = configglue(self.file)