changeset 432:b96f49c9c76b

Proper "repr()" for a jailed configuration: put the root path into the output
author Franz Glasner <fzglas.hg@dom66.de>
date Thu, 09 Dec 2021 22:51:10 +0100
parents 49cadb78c45a
children 8408662b5b1b
files configmix/compat.py configmix/config.py tests/test.py
diffstat 3 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/compat.py	Thu Dec 09 22:50:26 2021 +0100
+++ b/configmix/compat.py	Thu Dec 09 22:51:10 2021 +0100
@@ -15,7 +15,8 @@
            "native_os_str_to_text",
            "u",
            "u2fs",
-           "uchr"]
+           "uchr",
+           "n"]
 
 
 import sys
@@ -71,6 +72,12 @@
     def uchr(n):
         return unichr(n)      # noqa: F821
 
+    def n(s, encoding="utf-8"):
+        if isinstance(s, str):
+            return s
+        else:
+            return s.encode(encoding)
+
 else:
 
     def text_to_native_os_str(s, encoding=None):
@@ -103,3 +110,9 @@
 
     def uchr(n):
         return chr(n)
+
+    def n(s, encoding="utf-8"):
+        if isinstance(s, str):
+            return s
+        else:
+            return s.decode(encoding)
--- a/configmix/config.py	Thu Dec 09 22:50:26 2021 +0100
+++ b/configmix/config.py	Thu Dec 09 22:51:10 2021 +0100
@@ -28,7 +28,7 @@
     from urlparse import urlsplit
 
 from .variables import lookup_varns, lookup_filter
-from .compat import u, uchr
+from .compat import u, uchr, n
 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER
 
 
@@ -894,3 +894,7 @@
         if bind_root:
             sjc.rebind(self._base)
         return sjc
+
+    def __repr__(self):
+        r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path))
+        return r
--- a/tests/test.py	Thu Dec 09 22:50:26 2021 +0100
+++ b/tests/test.py	Thu Dec 09 22:51:10 2021 +0100
@@ -17,7 +17,7 @@
 import configmix.json
 import configmix.py
 import configmix.toml
-from configmix.compat import u
+from configmix.compat import u, PY2
 
 
 TESTDATADIR = os.path.join(
@@ -1535,6 +1535,27 @@
             set([u"key1", u"key2", u"tree1"]),
             set(jcfg.getkeys(u"")))
 
+    def test_repr_empty_rootpath(self):
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py"))
+        jcfg = cfg.jailed(rootpath=tuple())
+
+        self.assertEqual(
+            r"_JailedConfiguration(rootpath=())",
+            repr(jcfg))
+
+    def test_repr_non_empty_rootpath(self):
+        cfg = configmix.load(os.path.join(TESTDATADIR, "conf10.py"))
+        jcfg = cfg.jailed(rootpath=(u"tree1", u"tree2"))
+
+        if PY2:
+            self.assertEqual(
+                r"_JailedConfiguration(rootpath=(u'tree1', u'tree2'))",
+                repr(jcfg))
+        else:
+            self.assertEqual(
+                r"_JailedConfiguration(rootpath=('tree1', 'tree2'))",
+                repr(jcfg))
+
 
 if __name__ == "__main__":
     unittest.main()