← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/refactor-options-dictoption into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/refactor-options-dictoption into lp:configglue with lp:~ricardokirkner/configglue/refactor-options-booloption as a prerequisite.

Requested reviews:
  Configglue developers (configglue)

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/refactor-options-dictoption/+merge/64446
-- 
https://code.launchpad.net/~ricardokirkner/configglue/refactor-options-dictoption/+merge/64446
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/refactor-options-dictoption into lp:configglue.
=== modified file 'configglue/pyschema/parser.py'
--- configglue/pyschema/parser.py	2011-06-13 18:55:58 +0000
+++ configglue/pyschema/parser.py	2011-06-13 18:55:58 +0000
@@ -29,7 +29,7 @@
     NoSectionError,
 )
 
-from configglue.pyschema.schema import DictConfigOption
+from configglue.pyschema.schema import DictOption
 
 
 __all__ = [
@@ -327,9 +327,9 @@
                 kwargs = {'parser': self}
 
             # hook to save extra sections
-            is_dict_option = isinstance(option_obj, DictConfigOption)
+            is_dict_option = isinstance(option_obj, DictOption)
             is_dict_lines_option = (hasattr(option_obj, 'item') and
-                isinstance(option_obj.item, DictConfigOption))
+                isinstance(option_obj.item, DictOption))
             is_default_value = unicode(option_obj.default) == value
 
             # avoid adding implicit sections for dict default value

=== modified file 'configglue/pyschema/schema.py'
--- configglue/pyschema/schema.py	2011-06-13 18:55:58 +0000
+++ configglue/pyschema/schema.py	2011-06-13 18:55:58 +0000
@@ -26,6 +26,7 @@
     'ConfigOption',
     'ConfigSection',
     'DictConfigOption',
+    'DictOption',
     'IntConfigOption',
     'IntOption',
     'LinesConfigOption',
@@ -216,7 +217,7 @@
     argument, parser, that should receive the whole SchemaConfigParser to
     do the parsing.  This is needed for config options that need to look at
     other parts of the config file to be able to carry out their parsing,
-    like DictConfigOptions.
+    like DictOption.
 
     If self.fatal == True, SchemaConfigParser's parse_all will raise an
     exception if no value for this option is provided in the configuration
@@ -473,7 +474,7 @@
         return isinstance(value, tuple)
 
 
-class DictConfigOption(ConfigOption):
+class DictOption(ConfigOption):
     """A ConfigOption that is parsed into a dictionary.
 
     In the configuration file you'll need to specify the name of a section,
@@ -498,7 +499,7 @@
         self.spec = spec
         self.strict = strict
         self.item = item
-        super(DictConfigOption, self).__init__(name=name, raw=raw,
+        super(DictOption, self).__init__(name=name, raw=raw,
             default=default, fatal=fatal, help=help, action=action)
 
     def _get_default(self):
@@ -558,9 +559,9 @@
         sections = []
         for option in parser.options(section):
             option_obj = self.spec.get(option, self.item)
-            is_dict_item = isinstance(option_obj, DictConfigOption)
+            is_dict_item = isinstance(option_obj, DictOption)
             is_dict_lines_item = (hasattr(option_obj, 'item') and
-                isinstance(option_obj.item, DictConfigOption))
+                isinstance(option_obj.item, DictOption))
 
             if is_dict_item:
                 base = option_obj
@@ -601,3 +602,7 @@
 
 class BoolConfigOption(BoolOption):
     __metaclass__ = DeprecatedOption
+
+
+class DictConfigOption(DictOption):
+    __metaclass__ = DeprecatedOption

=== modified file 'doc/howto/custom-schema-options.rst'
--- doc/howto/custom-schema-options.rst	2011-06-13 18:55:58 +0000
+++ doc/howto/custom-schema-options.rst	2011-06-13 18:55:58 +0000
@@ -36,20 +36,20 @@
 :meth:`~configglue.pyschema.schema.ConfigOption.__init__` method of
 :class:`~configglue.pyschema.schema.ConfigOption` (or your parent class).
 
-In our example, we'll call our option ``UpperCaseDictConfigOption``. (It's a
+In our example, we'll call our option ``UpperCaseDictOption``. (It's a
 good idea to call your :class:`~configglue.pyschema.schema.ConfigOption`
 subclass ``<Something>ConfigOption``, so it's easily identifiable as a
 :class:`~configglue.pyschema.schema.ConfigOption` subclass.) It behaves
-mostly like a :class:`~configglue.pyschema.schema.DictConfigOption`, so we'll
+mostly like a :class:`~configglue.pyschema.schema.DictOption`, so we'll
 subclass from that::
 
     from configglue import pyschema
 
-    class UpperCaseDictConfigOption(pyschema.DictConfigOption):
-        """ A DictConfigOption with all upper-case keys. """
+    class UpperCaseDictOption(pyschema.DictOption):
+        """ A DictOption with all upper-case keys. """
 
         def parse(self, section, parser=None, raw=False):
-            parsed = super(UpperCaseDictConfigOption, self).parse(
+            parsed = super(UpperCaseDictOption, self).parse(
                 section, parser, raw)
             result = {}
             for k, v in parsed.items():
@@ -57,7 +57,7 @@
             return result
 
 
-Our ``UpperCaseDictConfigOption`` will represent a dictionary with all-uppercase
+Our ``UpperCaseDictOption`` will represent a dictionary with all-uppercase
 keys.
 
 So, let's assume we have a configuration file (see documentation on 
@@ -73,7 +73,7 @@
 and a schema like::
 
     class MySchema(pyschema.Schema):
-        mydict = UpperCaseDictConfigOption()
+        mydict = UpperCaseDictOption()
 
 When parsing this configuration file, the parser will contain the following
 value for the ``mydict`` attribute::
@@ -82,6 +82,6 @@
 
 .. note::
     Note that the dictionary values are strings because we didn't specify an
-    item type for the ``UpperCaseDictConfigOption``, and so it defaulted to
+    item type for the ``UpperCaseDictOption``, and so it defaulted to
     :class:`~configglue.pyschema.schema.StringOption`.
 

=== modified file 'doc/ref/schemas/options.rst'
--- doc/ref/schemas/options.rst	2011-06-13 18:55:58 +0000
+++ doc/ref/schemas/options.rst	2011-06-13 18:55:58 +0000
@@ -167,14 +167,14 @@
 
     If not 0, the tuple has to have exactly this number of elements.
 
-``DictConfigOption``
+``DictOption``
 --------------------
 
-.. class:: DictConfigOption([spec=None, strict=False, item=None, **attributes])
+.. class:: DictOption([spec=None, strict=False, item=None, **attributes])
 
 A dictionary.
 
-.. attribute:: DictConfigOption.spec
+.. attribute:: DictOption.spec
 
     *Optional*.
 
@@ -182,14 +182,14 @@
     are instances of a subclass of
     :class:`~configglue.pyschema.schema.ConfigOption`.
 
-.. attribute:: DictConfigOption.strict
+.. attribute:: DictOption.strict
 
     *Optional*.
 
     If ``True``, no keys will be allowed other than those specified
-    in the :attr:`~DictConfigOption.spec`.
+    in the :attr:`~DictOption.spec`.
 
-.. attribute:: DictConfigOption.item
+.. attribute:: DictOption.item
 
     *Optional*.
 

=== modified file 'doc/topics/config-file.rst'
--- doc/topics/config-file.rst	2011-06-13 18:55:58 +0000
+++ doc/topics/config-file.rst	2011-06-13 18:55:58 +0000
@@ -88,9 +88,9 @@
 Dictionaries
 ------------
 
-For specifying the value of a :class:`~configglue.pyschema.schema.DictConfigOption`,
+For specifying the value of a :class:`~configglue.pyschema.schema.DictOption`,
 a special syntax convention was defined. The value of a 
-:class:`~configglue.pyschema.schema.DictConfigOption` is the name of a section
+:class:`~configglue.pyschema.schema.DictOption` is the name of a section
 describing the structure of that dictionary.
 
 For example, given the configuration file::
@@ -104,7 +104,7 @@
 and the schema::
 
     class MySchema(pyschema.Schema):
-        my_dict = pyschema.DictConfigOption(
+        my_dict = pyschema.DictOption(
             spec={'foo': pyschema.IntOption(),
                   'bar': pyschema.BoolOption()})
 

=== modified file 'tests/pyschema/test_parser.py'
--- tests/pyschema/test_parser.py	2011-06-13 18:55:58 +0000
+++ tests/pyschema/test_parser.py	2011-06-13 18:55:58 +0000
@@ -44,7 +44,7 @@
 from configglue.pyschema.schema import (
     BoolOption,
     ConfigSection,
-    DictConfigOption,
+    DictOption,
     IntOption,
     LinesConfigOption,
     Schema,
@@ -323,7 +323,7 @@
 
     def test_get_interpolation_keys_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'a': IntOption()})
+            foo = DictOption(spec={'a': IntOption()})
         config = StringIO(textwrap.dedent("""
             [__noschema__]
             bar=4
@@ -390,7 +390,7 @@
 
     def test_interpolate_parse_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'a': IntOption()})
+            foo = DictOption(spec={'a': IntOption()})
         config = StringIO(textwrap.dedent("""
             [__noschema__]
             bar=4
@@ -574,7 +574,7 @@
 
     def test_extra_sections(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'bar': IntOption()})
+            foo = DictOption(spec={'bar': IntOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1")
         parser = SchemaConfigParser(MySchema())
@@ -588,7 +588,7 @@
     def test_multiple_extra_sections(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption(spec={'bar': IntOption()}))
+                item=DictOption(spec={'bar': IntOption()}))
 
         config = StringIO('[__main__]\nfoo=d1\n    d2\n    d3\n'
                           '[d1]\nbar=1\n[d2]\nbar=2\n[d3]\nbar=3')
@@ -638,7 +638,7 @@
 
     def test_multi_file_dict_config(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={
+            foo = DictOption(spec={
                 'bar': IntOption(),
                 'baz': IntOption(),
             }, strict=True)
@@ -654,7 +654,7 @@
     def test_multi_file_dict_list_config(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption(spec={
+                item=DictOption(spec={
                     'bar': IntOption(),
                     'baz': IntOption(),
                 }, strict=True))
@@ -968,7 +968,7 @@
 
     def test_extra_sections(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'bar': IntOption()})
+            foo = DictOption(spec={'bar': IntOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=1")
         parser = SchemaConfigParser(MySchema())
@@ -979,7 +979,7 @@
 
     def test_extra_sections_when_dict_with_nested_dicts(self):
         class MySchema(Schema):
-            foo = DictConfigOption(item=DictConfigOption())
+            foo = DictOption(item=DictOption())
 
         config = StringIO("""
 [__main__]
@@ -999,7 +999,7 @@
 
     def test_extra_sections_with_nested_dicts_strict(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'bar': DictConfigOption()},
+            foo = DictOption(spec={'bar': DictOption()},
                 strict=True)
 
         config = StringIO("""
@@ -1021,7 +1021,7 @@
     def test_extra_sections_when_lines_dict_with_nested_dicts(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption(item=DictConfigOption()))
+                item=DictOption(item=DictOption()))
 
         config = StringIO("""
 [__main__]
@@ -1048,8 +1048,8 @@
 
     def test_extra_sections_when_dict_with_nested_lines_dicts(self):
         class MySchema(Schema):
-            foo = DictConfigOption(
-                item=LinesConfigOption(item=DictConfigOption()))
+            foo = DictOption(
+                item=LinesConfigOption(item=DictOption()))
 
         config = StringIO("""
 [__main__]
@@ -1073,8 +1073,8 @@
     def test_extra_sections_when_lines_dict_with_nested_lines_dicts(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption(
-                    item=LinesConfigOption(item=DictConfigOption())))
+                item=DictOption(
+                    item=LinesConfigOption(item=DictOption())))
 
         config = StringIO("""
 [__main__]
@@ -1108,7 +1108,7 @@
     def test_multiple_extra_sections(self):
         class MySchema(Schema):
             foo = LinesConfigOption(
-                item=DictConfigOption(spec={'bar': IntOption()}))
+                item=DictOption(spec={'bar': IntOption()}))
 
         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	2011-06-13 18:55:58 +0000
+++ tests/pyschema/test_schema.py	2011-06-13 18:55:58 +0000
@@ -26,6 +26,7 @@
     ConfigOption,
     ConfigSection,
     DictConfigOption,
+    DictOption,
     IntConfigOption,
     IntOption,
     LinesConfigOption,
@@ -470,7 +471,7 @@
 
     def test_remove_dict_duplicates(self):
         class MyOtherSchema(Schema):
-            foo = LinesConfigOption(item=DictConfigOption(),
+            foo = LinesConfigOption(item=DictOption(),
                                     remove_duplicates=True)
 
         schema = MyOtherSchema()
@@ -543,24 +544,26 @@
         self.assertEqual(opt.validate(0), False)
 
 
-class TestDictConfigOption(unittest.TestCase):
+class TestDictOption(unittest.TestCase):
+    cls = DictOption
+
     def test_init(self):
-        opt = DictConfigOption()
+        opt = self.cls()
         self.assertEqual(opt.spec, {})
         self.assertEqual(opt.strict, False)
 
         spec = {'a': IntOption(), 'b': BoolOption()}
-        opt = DictConfigOption(spec=spec)
+        opt = self.cls(spec=spec)
         self.assertEqual(opt.spec, spec)
         self.assertEqual(opt.strict, False)
 
-        opt = DictConfigOption(spec=spec, strict=True)
+        opt = self.cls(spec=spec, strict=True)
         self.assertEqual(opt.spec, spec)
         self.assertEqual(opt.strict, True)
 
     def test_get_extra_sections(self):
         class MySchema(Schema):
-            foo = DictConfigOption(item=DictConfigOption())
+            foo = self.cls(item=self.cls())
 
         config = StringIO("""
 [__main__]
@@ -574,13 +577,13 @@
         parser.readfp(config)
         expected = ['dict2']
 
-        opt = DictConfigOption(item=DictConfigOption())
+        opt = self.cls(item=self.cls())
         extra = opt.get_extra_sections('dict1', parser)
         self.assertEqual(extra, expected)
 
     def test_parse_dict(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={
+            foo = self.cls(spec={
                 'bar': StringOption(),
                 'baz': IntOption(),
                 'bla': BoolOption(),
@@ -604,7 +607,7 @@
 
     def test_parse_raw(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={
+            foo = self.cls(spec={
                 'bar': StringOption(),
                 'baz': IntOption(),
                 'bla': BoolOption(),
@@ -625,7 +628,7 @@
 
     def test_parse_invalid_key_in_parsed(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'bar': IntOption()})
+            foo = self.cls(spec={'bar': IntOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbaz=2")
         expected_values = {'__main__': {'foo': {'bar': 0, 'baz': '2'}}}
@@ -635,7 +638,7 @@
 
     def test_parse_invalid_key_in_spec(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={
+            foo = self.cls(spec={
                 'bar': IntOption(),
                 'baz': IntOption(fatal=True)})
 
@@ -645,12 +648,12 @@
         self.assertRaises(ValueError, parser.parse_all)
 
     def test_default(self):
-        opt = DictConfigOption(spec={})
+        opt = self.cls(spec={})
         self.assertEqual(opt.default, {})
 
     def test_parse_no_strict_missing_args(self):
         class MySchema(Schema):
-            foo = DictConfigOption(spec={'bar': IntOption()})
+            foo = self.cls(spec={'bar': IntOption()})
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]")
         expected_values = {'__main__': {'foo': {'bar': 0}}}
@@ -660,7 +663,7 @@
 
     def test_parse_no_strict_extra_args(self):
         class MySchema(Schema):
-            foo = DictConfigOption()
+            foo = self.cls()
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         expected_values = {'__main__': {'foo': {'bar': '2'}}}
@@ -670,8 +673,8 @@
 
     def test_parse_no_strict_with_item(self):
         class MySchema(Schema):
-            foo = DictConfigOption(
-                      item=DictConfigOption(
+            foo = self.cls(
+                      item=self.cls(
                           item=IntOption()))
         config = StringIO("""
 [__main__]
@@ -689,7 +692,7 @@
     def test_parse_strict(self):
         class MySchema(Schema):
             spec = {'bar': IntOption()}
-            foo = DictConfigOption(spec=spec, strict=True)
+            foo = self.cls(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         expected_values = {'__main__': {'foo': {'bar': 2}}}
@@ -701,7 +704,7 @@
         class MySchema(Schema):
             spec = {'bar': IntOption(),
                     'baz': IntOption()}
-            foo = DictConfigOption(spec=spec, strict=True)
+            foo = self.cls(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2")
         expected_values = {'__main__': {'foo': {'bar': 2, 'baz': 0}}}
@@ -712,7 +715,7 @@
     def test_parse_strict_extra_vars(self):
         class MySchema(Schema):
             spec = {'bar': IntOption()}
-            foo = DictConfigOption(spec=spec, strict=True)
+            foo = self.cls(spec=spec, strict=True)
 
         config = StringIO("[__main__]\nfoo=mydict\n[mydict]\nbar=2\nbaz=3")
         parser = SchemaConfigParser(MySchema())
@@ -720,18 +723,22 @@
         self.assertRaises(ValueError, parser.parse_all)
 
     def test_validate_dict(self):
-        opt = DictConfigOption()
+        opt = self.cls()
         self.assertEqual(opt.validate({}), True)
 
     def test_validate_nondict(self):
-        opt = DictConfigOption()
+        opt = self.cls()
         self.assertEqual(opt.validate(0), False)
 
 
-class TestLinesOfDictConfigOption(unittest.TestCase):
+class TestDictConfigOption(TestDictOption):
+    cls = DictConfigOption
+
+
+class TestLinesOfDictOption(unittest.TestCase):
     def test_parse_lines_of_dict(self):
         class MySchema(Schema):
-            foo = LinesConfigOption(item=DictConfigOption(
+            foo = LinesConfigOption(item=DictOption(
                 spec={
                     'bar': StringOption(),
                     'baz': IntOption(),
@@ -769,10 +776,10 @@
                     }
         spec = {'name': StringOption(),
                 'size': IntOption(),
-                'options': DictConfigOption(spec=innerspec)}
+                'options': DictOption(spec=innerspec)}
 
         class MySchema(Schema):
-            foo = DictConfigOption(spec=spec)
+            foo = DictOption(spec=spec)
 
         config = StringIO("""[__main__]
 foo = outerdict


Follow ups