changeset 697:57fe110c50c8

Implement a new "SYS" namespace with "prefix", "base_prefix" and "platform"
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 14 Jun 2023 01:11:01 +0200
parents 8648e0641d60
children 3a9d661d33b5
files CHANGES.txt configmix/variables.py docs/introduction.rst tests/test.py
diffstat 4 files changed, 62 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.txt	Mon Jun 12 09:28:36 2023 +0200
+++ b/CHANGES.txt	Wed Jun 14 01:11:01 2023 +0200
@@ -12,6 +12,14 @@
 Pre-1.0 Series
 --------------
 
+n/a
+~~~
+
+- **[feature]**
+  Implement a new ``SYS`` variable namespace with ``prefix``, ``base_prefix``
+  and ``platform`` as current content.
+
+
 v0.21.3 (2023-06-12)
 ~~~~~~~~~~~~~~~~~~~~
 
--- a/configmix/variables.py	Mon Jun 12 09:28:36 2023 +0200
+++ b/configmix/variables.py	Wed Jun 14 01:11:01 2023 +0200
@@ -15,6 +15,7 @@
 
 import os
 import platform
+import sys
 from functools import wraps
 
 from .compat import PY2, native_os_str_to_text, text_to_native_os_str, u
@@ -48,6 +49,27 @@
             return default
 
 
+def _syslookup(name, default=_MARKER):
+    """Lookup some variables from Python's :mod:`sys` module"""
+    if name == "prefix":
+        return native_os_str_to_text(sys.prefix)
+    elif name == "base_prefix":
+        val = getattr(sys, name, _MARKER)
+        if val is _MARKER:
+            if default is _MARKER:
+                raise KeyError("key %r not found in the namespace" % name)
+            else:
+                return default
+        return native_os_str_to_text(val)
+    elif name == "platform":
+        return native_os_str_to_text(sys.platform)
+    else:
+        if default is _MARKER:
+            raise KeyError("key %r not found in the namespace" % name)
+        else:
+            return default
+
+
 def _pylookup(name, default=_MARKER):
     """Lookup Python specific information"""
     if name == "version":
@@ -270,6 +292,7 @@
 add_varns("ENV", _envlookup)
 add_varns("OS", _oslookup)
 add_varns("PY", _pylookup)
+add_varns("SYS", _syslookup)
 try:
     from .extras import aws
 except ImportError:
--- a/docs/introduction.rst	Mon Jun 12 09:28:36 2023 +0200
+++ b/docs/introduction.rst	Wed Jun 14 01:11:01 2023 +0200
@@ -372,12 +372,30 @@
          Contains the current node's computername (or whatever
          :py:func:`platform.node` returns)
 
-4. The namespace ``ENV``
+4. The namespace ``SYS``
+
+   Available functions:
+
+     ``prefix``
+         Contains the content of the current running Python's
+         :py:data:`sys.prefix`.
+
+     ``base_prefix``
+         Contains the content of the current running Python's
+         :py:data:`sys.base_prefix`.
+
+         Raises :py:class:`KeyError` if the attribute is not available.
+
+     ``platform``
+         Contains the content of the current running Python's
+         :py:data:`sys.platform`.
+
+5. The namespace ``ENV``
 
    This namespace contains all the environment variables as they are
    available from :py:data:`os.environ`.
 
-5. The namespace ``PY``
+6. The namespace ``PY``
 
    Contains selected values from the running Python:
 
@@ -394,7 +412,7 @@
      ``implementation``
          The return value of :py:func:`platform.python_implementation`
 
-6. The namespace ``AWS``
+7. The namespace ``AWS``
 
    Contains some metadata for AWS instances when running from within
    AWS:
--- a/tests/test.py	Mon Jun 12 09:28:36 2023 +0200
+++ b/tests/test.py	Wed Jun 14 01:11:01 2023 +0200
@@ -4,6 +4,7 @@
 import platform
 import io
 import os
+import sys
 
 from _test_context import TESTDATADIR
 
@@ -287,6 +288,13 @@
         self.assertEqual(u(os.getcwd()), cfg.getvar("OS:cwd"))
         self.assertEqual(u(platform.python_version()),
                          cfg.getvar_s("PY:version"))
+        self.assertEqual(u(sys.prefix), cfg.getvar_s("SYS:prefix"))
+        self.assertEqual(u(sys.platform), cfg.getvar_s("SYS:platform"))
+        if hasattr(sys, "base_prefix"):
+            self.assertEqual(u(sys.base_prefix),
+                             cfg.getvar_s("SYS:base_prefix"))
+        else:
+            self.assertRaises(KeyError, cfg.getvar_s, "SYS:base_prefix")
 
     def test03_namespace_l(self):
         cfg = self._load(
@@ -296,6 +304,8 @@
         self.assertEqual(u(os.getcwd()), cfg.getvarl("cwd", namespace="OS"))
         self.assertEqual(u(platform.python_version()),
                          cfg.getvarl_s("version", namespace="PY"))
+        self.assertEqual(u(sys.prefix),
+                         cfg.getvarl_s("prefix", namespace="SYS"))
 
     def test04_no_filter(self):
         cfg = self._load(