← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/default-name into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/default-name into lp:configglue.

Requested reviews:
  Configglue developers (configglue)

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/default-name/+merge/52412

This branch adds some consistency to ConfigSection and ConfigOption objects by
making sure the 'name' attribute is the first attribute in the constructor,
and that all objects have their name set upon Schema initialization.
-- 
https://code.launchpad.net/~ricardokirkner/configglue/default-name/+merge/52412
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/default-name into lp:configglue.
=== modified file 'configglue/inischema/glue.py'
--- configglue/inischema/glue.py	2011-01-10 12:45:09 +0000
+++ configglue/inischema/glue.py	2011-03-07 13:22:15 +0000
@@ -70,7 +70,7 @@
         if section_name == '__main__':
             section = MySchema
         else:
-            section = ConfigSection()
+            section = ConfigSection(name=section_name)
             setattr(MySchema, section_name, section)
         for option_name in p.options(section_name):
             option = p.get(section_name, option_name)
@@ -83,7 +83,7 @@
             if parser_fun is None:
                 parser_fun = lambda x: x
 
-            attrs = {}
+            attrs = {'name': option_name}
             option_help = option.attrs.pop('help', None)
             if option_help is not None:
                 attrs['help'] = option_help
@@ -95,7 +95,7 @@
 
             klass = parser2option.get(parser, StringConfigOption)
             if parser == 'lines':
-                instance = klass(StringConfigOption(), **attrs)
+                instance = klass(item=StringConfigOption(), **attrs)
             else:
                 instance = klass(**attrs)
             setattr(section, option_name, instance)

=== modified file 'configglue/pyschema/schema.py'
--- configglue/pyschema/schema.py	2010-12-18 22:32:43 +0000
+++ configglue/pyschema/schema.py	2011-03-07 13:22:15 +0000
@@ -82,25 +82,36 @@
     def __init__(self):
         self.includes = LinesConfigOption(item=StringConfigOption())
         self._sections = {}
-        defaultSection = None
-        for attname in super_vars(self.__class__):
-            att = getattr(self, attname)
-            if isinstance(att, ConfigSection):
-                att.name = attname
-                self._sections[attname] = att
-                for optname in super_vars(att):
-                    opt = getattr(att, optname)
-                    if isinstance(opt, ConfigOption):
-                        opt.name = optname
-                        opt.section = att
-            elif isinstance(att, ConfigOption):
-                if defaultSection is None:
-                    defaultSection = ConfigSection()
-                    defaultSection.name = '__main__'
-                    self._sections['__main__'] = defaultSection
-                att.name = attname
-                att.section = defaultSection
-                setattr(defaultSection, attname, att)
+        # add section and options to the schema
+        for key in super_vars(self.__class__):
+            value = getattr(self, key)
+            self._add_item(key, value)
+
+    def _add_item(self, name, item):
+        """Add a top-level item to the schema."""
+        if not isinstance(item, (ConfigSection, ConfigOption)):
+            return
+
+        item.name = name
+        if isinstance(item, ConfigSection):
+            self._add_section(name, item)
+        elif isinstance(item, ConfigOption):
+            self._add_option(name, item)
+
+    def _add_section(self, name, section):
+        """Add a top-level section to the schema."""
+        self._sections[name] = section
+        items = super_vars(section).items()
+        options = ((k, v) for (k, v) in items if isinstance(v, ConfigOption))
+        for opt_name, opt in options:
+            opt.name = opt_name
+            opt.section = section
+
+    def _add_option(self, name, option):
+        """Add a top-level option to the schema."""
+        section = self._sections.setdefault('__main__', ConfigSection(name='__main__'))
+        option.section = section
+        setattr(section, name, option)
 
     def __eq__(self, other):
         return (self._sections == other._sections and
@@ -330,10 +341,10 @@
             items = filtered_items
         return items
 
-    def __init__(self, item, raw=False, default=NO_DEFAULT, fatal=False,
-        help='', action='store', remove_duplicates=False):
-        super(LinesConfigOption, self).__init__(raw=raw, default=default,
-            fatal=fatal, help=help, action=action)
+    def __init__(self, name='', item=None, raw=False, default=NO_DEFAULT,
+        fatal=False, help='', action='store', remove_duplicates=False):
+        super(LinesConfigOption, self).__init__(name=name, raw=raw,
+            default=default, fatal=fatal, help=help, action=action)
         self.item = item
         self.require_parser = item.require_parser
         self.raw = item.raw
@@ -356,11 +367,11 @@
 
         return unicode(value)
 
-    def __init__(self, raw=False, default=NO_DEFAULT, fatal=False, null=False,
-                 help='', action='store'):
+    def __init__(self, name='', raw=False, default=NO_DEFAULT, fatal=False,
+        null=False, help='', action='store'):
         self.null = null
-        super(StringConfigOption, self).__init__(raw=raw, default=default,
-            fatal=fatal, help=help, action=action)
+        super(StringConfigOption, self).__init__(name=name, raw=raw,
+            default=default, fatal=fatal, help=help, action=action)
 
 
 class TupleConfigOption(ConfigOption):
@@ -370,10 +381,10 @@
     constructor argument.
     """
 
-    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, action=action)
+    def __init__(self, name='', length=0, raw=False, default=NO_DEFAULT,
+        fatal=False, help='', action='store'):
+        super(TupleConfigOption, self).__init__(name=name, raw=raw,
+            default=default, fatal=fatal, help=help, action=action)
         self.length = length
 
     def _get_default(self):
@@ -408,7 +419,7 @@
     """
     require_parser = True
 
-    def __init__(self, spec=None, strict=False, raw=False,
+    def __init__(self, name='', spec=None, strict=False, raw=False,
                  default=NO_DEFAULT, fatal=False, help='', action='store',
                  item=None):
         if spec is None:
@@ -418,8 +429,8 @@
         self.spec = spec
         self.strict = strict
         self.item = item
-        super(DictConfigOption, self).__init__(raw=raw, default=default,
-            fatal=fatal, help=help, action=action)
+        super(DictConfigOption, self).__init__(name=name, raw=raw,
+            default=default, fatal=fatal, help=help, action=action)
 
     def _get_default(self):
         default = {}

=== modified file 'tests/pyschema/test_parser.py'
--- tests/pyschema/test_parser.py	2010-12-18 22:32:43 +0000
+++ tests/pyschema/test_parser.py	2011-03-07 13:22:15 +0000
@@ -311,7 +311,7 @@
 
     def test_get_interpolation_keys_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'a': IntConfigOption()})
+            foo = DictConfigOption(spec={'a': IntConfigOption()})
         config = StringIO("[__noschema__]\nbar=4\n[__main__]\nfoo=mydict\n[mydict]\na=%(bar)s")
         expected = ('mydict', set([]))
 
@@ -355,7 +355,7 @@
 
     def test_interpolate_parse_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'a': IntConfigOption()})
+            foo = DictConfigOption(spec={'a': IntConfigOption()})
         config = StringIO("[__noschema__]\nbar=4\n[__main__]\nfoo=mydict\n[mydict]\na=%(bar)s")
         expected = {'__main__': {'foo': {'a': 4}}}
 
@@ -525,7 +525,7 @@
 
     def test_extra_sections(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption()})
+            foo = DictConfigOption(spec={'bar': IntConfigOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1")
         parser = SchemaConfigParser(MySchema())
@@ -539,7 +539,7 @@
     def test_multiple_extra_sections(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption({'bar': IntConfigOption()}))
+                item=DictConfigOption(spec={'bar': IntConfigOption()}))
 
         config = StringIO('[__main__]\nfoo=d1\n    d2\n    d3\n'
                           '[d1]\nbar=1\n[d2]\nbar=2\n[d3]\nbar=3')
@@ -589,9 +589,10 @@
 
     def test_multi_file_dict_config(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption(),
-                                    'baz': IntConfigOption()},
-                                   strict=True)
+            foo = DictConfigOption(spec={
+                'bar': IntConfigOption(),
+                'baz': IntConfigOption(),
+            }, strict=True)
         config1 = StringIO('[__main__]\nfoo=mydict\n[mydict]\nbar=1\nbaz=1')
         config2 = StringIO('[mydict]\nbaz=2')
         expected_values = {'__main__': {'foo': {'bar': 1, 'baz': 2}}}
@@ -604,9 +605,10 @@
     def test_multi_file_dict_list_config(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption({'bar': IntConfigOption(),
-                                       'baz': IntConfigOption()},
-                                      strict=True))
+                item=DictConfigOption(spec={
+                    'bar': IntConfigOption(),
+                    'baz': IntConfigOption(),
+                }, strict=True))
 
         config1 = StringIO('[__main__]\nfoo=mydict\n[mydict]\nbar=1\nbaz=1')
         expected_values = {'__main__': {'foo': [{'bar': 1, 'baz': 1}]}}
@@ -855,7 +857,7 @@
 
     def test_extra_sections(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption()})
+            foo = DictConfigOption(spec={'bar': IntConfigOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1")
         parser = SchemaConfigParser(MySchema())
@@ -886,7 +888,7 @@
 
     def test_extra_sections_with_nested_dicts_strict(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': DictConfigOption()}, strict=True)
+            foo = DictConfigOption(spec={'bar': DictConfigOption()}, strict=True)
 
         config = StringIO("""
 [__main__]
@@ -996,7 +998,7 @@
     def test_multiple_extra_sections(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption({'bar': IntConfigOption()}))
+                item=DictConfigOption(spec={'bar': IntConfigOption()}))
 
         config = StringIO('[__main__]\nfoo=d1\n    d2\n    d3\n'
                           '[d1]\nbar=1\n[d2]\nbar=2\n[d3]\nbar=3')

=== modified file 'tests/pyschema/test_schema.py'
--- tests/pyschema/test_schema.py	2010-12-18 22:32:43 +0000
+++ tests/pyschema/test_schema.py	2011-03-07 13:22:15 +0000
@@ -334,7 +334,7 @@
 
 class TestTupleConfigOption(unittest.TestCase):
     def test_init(self):
-        opt = TupleConfigOption(2)
+        opt = TupleConfigOption(length=2)
         self.assertEqual(opt.length, 2)
 
     def test_init_no_length(self):
@@ -373,7 +373,7 @@
         self.assertRaises(ValueError, parser.values)
 
     def test_default(self):
-        opt = TupleConfigOption(2)
+        opt = TupleConfigOption(length=2)
         self.assertEqual(opt.default, ())
 
 
@@ -384,11 +384,11 @@
         self.assertEqual(opt.strict, False)
 
         spec = {'a': IntConfigOption(), 'b': BoolConfigOption()}
-        opt = DictConfigOption(spec)
+        opt = DictConfigOption(spec=spec)
         self.assertEqual(opt.spec, spec)
         self.assertEqual(opt.strict, False)
 
-        opt = DictConfigOption(spec, strict=True)
+        opt = DictConfigOption(spec=spec, strict=True)
         self.assertEqual(opt.spec, spec)
         self.assertEqual(opt.strict, True)
 
@@ -414,10 +414,11 @@
 
     def test_parse_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': StringConfigOption(),
-                                    'baz': IntConfigOption(),
-                                    'bla': BoolConfigOption(),
-                                    })
+            foo = DictConfigOption(spec={
+                'bar': StringConfigOption(),
+                'baz': IntConfigOption(),
+                'bla': BoolConfigOption(),
+            })
         config = StringIO("""[__main__]
 foo = mydict
 [mydict]
@@ -438,10 +439,11 @@
 
     def test_parse_raw(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': StringConfigOption(),
-                                    'baz': IntConfigOption(),
-                                    'bla': BoolConfigOption(),
-                                    })
+            foo = DictConfigOption(spec={
+                'bar': StringConfigOption(),
+                'baz': IntConfigOption(),
+                'bla': BoolConfigOption(),
+            })
         config = StringIO("""[__main__]
 foo = mydict
 [mydict]
@@ -457,7 +459,7 @@
 
     def test_parse_invalid_key_in_parsed(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption()})
+            foo = DictConfigOption(spec={'bar': IntConfigOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbaz=2")
         expected_values = {'__main__': {'foo': {'bar': 0, 'baz': '2'}}}
@@ -467,8 +469,9 @@
 
     def test_parse_invalid_key_in_spec(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption(),
-                                    'baz': IntConfigOption(fatal=True)})
+            foo = DictConfigOption(spec={
+                'bar': IntConfigOption(),
+                'baz': IntConfigOption(fatal=True)})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         parser = SchemaConfigParser(MySchema())
@@ -476,12 +479,12 @@
         self.assertRaises(ValueError, parser.parse_all)
 
     def test_default(self):
-        opt = DictConfigOption({})
+        opt = DictConfigOption(spec={})
         self.assertEqual(opt.default, {})
 
     def test_parse_no_strict_missing_args(self):
         class MySchema(Schema):
-            foo = DictConfigOption({'bar': IntConfigOption()})
+            foo = DictConfigOption(spec={'bar': IntConfigOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]")
         expected_values = {'__main__': {'foo': {'bar': 0}}}
@@ -520,7 +523,7 @@
     def test_parse_strict(self):
         class MySchema(Schema):
             spec = {'bar': IntConfigOption()}
-            foo = DictConfigOption(spec, strict=True)
+            foo = DictConfigOption(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         expected_values = {'__main__': {'foo': {'bar': 2}}}
@@ -532,7 +535,7 @@
         class MySchema(Schema):
             spec = {'bar': IntConfigOption(),
                     'baz': IntConfigOption()}
-            foo = DictConfigOption(spec, strict=True)
+            foo = DictConfigOption(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         expected_values = {'__main__': {'foo': {'bar': 2, 'baz': 0}}}
@@ -543,7 +546,7 @@
     def test_parse_strict_extra_vars(self):
         class MySchema(Schema):
             spec = {'bar': IntConfigOption()}
-            foo = DictConfigOption(spec, strict=True)
+            foo = DictConfigOption(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2\nbaz=3")
         parser = SchemaConfigParser(MySchema())
@@ -554,11 +557,12 @@
 class TestLinesOfDictConfigOption(unittest.TestCase):
     def test_parse_lines_of_dict(self):
         class MySchema(Schema):
-            foo = LinesConfigOption(
-                        DictConfigOption({'bar': StringConfigOption(),
-                                          'baz': IntConfigOption(),
-                                          'bla': BoolConfigOption(),
-                                          }))
+            foo = LinesConfigOption(item=DictConfigOption(
+                spec={
+                    'bar': StringConfigOption(),
+                    'baz': IntConfigOption(),
+                    'bla': BoolConfigOption(),
+                }))
         config = StringIO("""[__main__]
 foo = mylist0
       mylist1
@@ -590,9 +594,9 @@
                     }
         spec = {'name': StringConfigOption(),
                 'size': IntConfigOption(),
-                'options': DictConfigOption(innerspec)}
+                'options': DictConfigOption(spec=innerspec)}
         class MySchema(Schema):
-            foo = DictConfigOption(spec)
+            foo = DictConfigOption(spec=spec)
         config = StringIO("""[__main__]
 foo = outerdict
 [outerdict]