changeset 325:ab33d51f3412

By default quote the "#" character also because it is typically a comment in all the configuration file formats. Test quoting the "#" also
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 06 May 2021 20:55:43 +0200
parents 87424f4754d1
children b7abfbfe806d
files configmix/config.py tests/data/quoting.yml tests/test.py
diffstat 3 files changed, 25 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/config.py	Thu May 06 20:55:19 2021 +0200
+++ b/configmix/config.py	Thu May 06 20:55:43 2021 +0200
@@ -71,6 +71,7 @@
     _ENDTOK_REF = _ENDTOK
     _DOT = u(b'.')
     _QUOTE = u(b'%')
+    _COMMENT = u(b'#')
 
     def getvarl(self, *names, **kwds):
         """Get a variable where the hierarchy is given in `names` as sequence
@@ -374,14 +375,15 @@
 
     @classmethod
     def quote(klass, s):
-        """Quote a key to protect all dangerous chars: ``%``, ``.``, ``:``
-        and ``|``
+        """Quote a key to protect all dangerous chars: ``%``, ``.``, ``:``,
+        ``#`` and ``|``
 
         """
         qc = klass._QUOTE
         s = s.replace(qc, qc + "x25")
         s = s.replace(klass._DOT, qc + "x2e")
         s = s.replace(klass._NS_SEPARATOR, qc + "x3a")
+        s = s.replace(klass._COMMENT, qc + "x23")
         return s.replace(klass._FILTER_SEPARATOR, qc + "x7c")
 
     @classmethod
--- a/tests/data/quoting.yml	Thu May 06 20:55:19 2021 +0200
+++ b/tests/data/quoting.yml	Thu May 06 20:55:43 2021 +0200
@@ -5,15 +5,16 @@
 %YAML 1.1
 ---
 
-':|%.':
+'#:|%.':
   '.':
     ':':
       '%':
         '|':
-          '/': 'value'
+          '/':
+            '#': 'value'
 
 events:
   'qc-2021.1-5G-summit':
     name: "5G Summit"
-    xname: '{{%x3a%x7c%x25%x2e.%u002e.%U0000003a.%x25.%x7c./}}'
-    xref: '{{ref:#%u003a%x7c%U00000025%x2e.%x2e.%x3a.%x25.%x7c./}}'
+    xname: '{{%x23%x3a%x7c%x25%x2e.%u002e.%U0000003a.%x25.%x7c./.%x23}}'
+    xref: '{{ref:#%x23%u003a%x7c%U00000025%x2e.%x2e.%x3a.%x25.%x7c./}}'
--- a/tests/test.py	Thu May 06 20:55:19 2021 +0200
+++ b/tests/test.py	Thu May 06 20:55:43 2021 +0200
@@ -727,38 +727,38 @@
     def test_getvar(self):
         self.assertEqual(
             "value",
-            self._cfg.getvar("%x3a%x7c%x25%x2e.%x2e.%x3a.%x25.%x7c./"))
+            self._cfg.getvar("%x23%x3a%x7c%x25%x2e.%x2e.%x3a.%x25.%x7c./.%x23"))
         self.assertEqual(
             "value",
             self._cfg.getvar(
-                "%u003a%u007c%u0025%u002e.%u002e.%u003a.%u0025.%u007c./"))
+                "%u0023%u003a%u007c%u0025%u002e.%u002e.%u003a.%u0025.%u007c./.%u0023"))
         self.assertEqual(
             "value",
             self._cfg.getvar(
-                "%U0000003a%U0000007c%U00000025%U0000002e.%U0000002e.%U0000003a.%U00000025.%U0000007c./"))
+                "%U00000023%U0000003a%U0000007c%U00000025%U0000002e.%U0000002e.%U0000003a.%U00000025.%U0000007c./.%U00000023"))
 
     def test_getvar_s(self):
         self.assertEqual(
             "value",
-            self._cfg.getvar_s("%x3a%x7c%x25%x2e.%x2e.%x3a.%x25.%x7c./"))
+            self._cfg.getvar_s("%x23%x3a%x7c%x25%x2e.%x2e.%x3a.%x25.%x7c./.%x23"))
         self.assertEqual(
             "value",
             self._cfg.getvar_s(
-                "%u003a%u007c%u0025%u002e.%u002e.%u003a.%u0025.%u007c./"))
+                "%u0023%u003a%u007c%u0025%u002e.%u002e.%u003a.%u0025.%u007c./.%u0023"))
         self.assertEqual(
             "value",
             self._cfg.getvar_s(
-                "%U0000003a%U0000007c%U00000025%U0000002e.%U0000002e.%U0000003a.%U00000025.%U0000007c./"))
+                "%U00000023%U0000003a%U0000007c%U00000025%U0000002e.%U0000002e.%U0000003a.%U00000025.%U0000007c./.%U00000023"))
 
     def test_getvarl(self):
         self.assertEqual(
             "value",
-            self._cfg.getvarl(":|%.", ".", ":", "%", "|", "/"))
+            self._cfg.getvarl("#:|%.", ".", ":", "%", "|", "/", "#"))
 
     def test_getvarl_s(self):
         self.assertEqual(
             "value",
-            self._cfg.getvarl_s(":|%.", ".", ":", "%", "|", "/"))
+            self._cfg.getvarl_s("#:|%.", ".", ":", "%", "|", "/", "#"))
 
     def test_interpolation1(self):
         self.assertEqual(
@@ -770,10 +770,16 @@
             "value",
             self._cfg.getvar_s("events.qc-2021%x2e1-5G-summit.xname"))
 
-    def test_reference(self):
+    def test_reference1(self):
+        self.assertTrue(
+            isinstance(
+                self._cfg.getvar_s("events.qc-2021%x2e1-5G-summit.xref"),
+                dict))
+
+    def test_reference2(self):
         self.assertEqual(
             "value",
-            self._cfg.getvar_s("events.qc-2021%x2e1-5G-summit.xref"))
+            self._cfg.getvar_s("events.qc-2021%x2e1-5G-summit.xref.%x23"))
 
 
 if __name__ == "__main__":