← Back to team overview

configglue team mailing list archive

[Merge] lp:~ricardokirkner/configglue/849937-raw-dict into lp:configglue

 

Ricardo Kirkner has proposed merging lp:~ricardokirkner/configglue/849937-raw-dict into lp:configglue.

Requested reviews:
  Configglue developers (configglue)
Related bugs:
  Bug #849937 in configglue: "Unable to specify raw strings for multi-level DictOption"
  https://bugs.launchpad.net/configglue/+bug/849937

For more details, see:
https://code.launchpad.net/~ricardokirkner/configglue/849937-raw-dict/+merge/77619
-- 
https://code.launchpad.net/~ricardokirkner/configglue/849937-raw-dict/+merge/77619
Your team Configglue developers is requested to review the proposed merge of lp:~ricardokirkner/configglue/849937-raw-dict into lp:configglue.
=== modified file 'configglue/schema.py'
--- configglue/schema.py	2011-07-30 14:45:45 +0000
+++ configglue/schema.py	2011-09-29 22:45:27 +0000
@@ -332,7 +332,7 @@
     def _get_default(self):
         return None
 
-    def parse(self, value):
+    def parse(self, value, raw=False, interpolate=True):
         """Parse the given value."""
         raise NotImplementedError()
 
@@ -350,7 +350,7 @@
     def _get_default(self):
         return False
 
-    def parse(self, value, raw=False):
+    def parse(self, value, raw=False, interpolate=True):
         """Parse the given value.
 
         If *raw* is *True*, return the value unparsed.
@@ -376,7 +376,7 @@
     def _get_default(self):
         return 0
 
-    def parse(self, value, raw=False):
+    def parse(self, value, raw=False, interpolate=True):
         """Parse the given value.
 
         If *raw* is *True*, return the value unparsed.
@@ -435,7 +435,7 @@
     def _get_default(self):
         return []
 
-    def parse(self, value, parser=None, raw=False):
+    def parse(self, value, parser=None, raw=False, interpolate=True):
         """Parse the given value.
 
         A *parser* object is used to parse individual list items.
@@ -504,7 +504,7 @@
     def _get_default(self):
         return '' if not self.null else None
 
-    def parse(self, value, raw=False):
+    def parse(self, value, raw=False, interpolate=True):
         """Parse the given value.
 
         If *raw* is *True*, return the value unparsed.
@@ -555,7 +555,7 @@
     def _get_default(self):
         return ()
 
-    def parse(self, value, raw=False):
+    def parse(self, value, raw=False, interpolate=True):
         """Parse the given value.
 
         If *raw* is *True*, return the value unparsed.
@@ -626,7 +626,7 @@
             default[key] = value.default
         return default
 
-    def parse(self, value, parser, raw=False):
+    def parse(self, value, parser, raw=False, interpolate=True):
         """Parse the given value.
 
         A *parser* object is used to parse individual dict items.
@@ -649,7 +649,7 @@
                 nested = self.get_extra_sections(name, parser)
                 parser.extra_sections.update(set(nested))
 
-            parsed = dict(parser.items(value))
+            parsed = dict(parser.items(value, raw=not interpolate))
 
         result = {}
         # parse config items according to spec
@@ -663,12 +663,16 @@
                 # parse it using the default item parser
                 option = self.item
 
-            # parse option
-            kwargs = {}
-            if option.require_parser:
-                kwargs['parser'] = parser
-            if not raw:
-                value = option.parse(value, **kwargs)
+            if not option.validate(value):
+                # parse option
+                kwargs = {
+                    'raw': raw,
+                    'interpolate': interpolate and option.raw,
+                }
+                if option.require_parser:
+                    kwargs['parser'] = parser
+                if not raw:
+                    value = option.parse(value, **kwargs)
             result[key] = value
 
         # fill in missing items with default values

=== modified file 'configglue/tests/test_schema.py'
--- configglue/tests/test_schema.py	2011-07-30 14:24:21 +0000
+++ configglue/tests/test_schema.py	2011-09-29 22:45:27 +0000
@@ -936,6 +936,48 @@
         parsed = schema.foo.parse('mydict', parser, True)
         self.assertEqual(parsed, expected)
 
+    def test_parse_json_raw_with_interpolation_marks(self):
+        """Test DictOption parse json using raw=True when data has interpolation marks."""
+        class MySchema(Schema):
+            class logging(Section):
+                formatters = self.cls(raw=True, item=self.cls())
+
+        config = StringIO(textwrap.dedent("""
+            [logging]
+            formatters = {"sample": {"format": "%(name)s"}}
+            """))
+        expected = {'sample': {'format': '%(name)s'}}
+
+        schema = MySchema()
+        parser = SchemaConfigParser(schema)
+        parser.readfp(config)
+        parsed = parser.values('logging')['formatters']
+        self.assertEqual(parsed, expected)
+
+    def test_parse_no_json_raw_with_interpolation_marks(self):
+        """Test DictOption parse non-json using raw=True when data has interpolation marks."""
+        class MySchema(Schema):
+            class logging(Section):
+                formatters = self.cls(raw=True, item=self.cls())
+
+        config = StringIO(textwrap.dedent("""
+            [logging]
+            formatters = logging_formatters
+
+            [logging_formatters]
+            sample = sample_formatter
+
+            [sample_formatter]
+            format = %(name)s
+            """))
+        expected = {'sample': {'format': '%(name)s'}}
+
+        schema = MySchema()
+        parser = SchemaConfigParser(schema)
+        parser.readfp(config)
+        parsed = parser.values('logging')['formatters']
+        self.assertEqual(parsed, expected)
+
     def test_parse_invalid_key_in_parsed(self):
         """Test DictOption parse with an invalid key in the config."""
         class MySchema(Schema):


Follow ups