changeset 708:e692216f8756

Allow also "," characters to act as a separator within a filter-chain. The first filter is introduced by a "|", optional other ones may be introduced by a "|" or a ",".
author Franz Glasner <f.glasner@feldmann-mg.com>
date Tue, 15 Aug 2023 11:14:51 +0200
parents 10fbc23b4dba
children 115de6fe420e
files configmix/_speedups.c configmix/config.py docs/introduction.rst tests/test.py
diffstat 4 files changed, 29 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/configmix/_speedups.c	Tue Aug 15 10:41:22 2023 +0200
+++ b/configmix/_speedups.c	Tue Aug 15 11:14:51 2023 +0200
@@ -684,7 +684,12 @@
     }
 
     if (direction == 1) {
-        tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR, -1);
+        if (PySequence_Contains(filters, sstate->FILTER_SEPARATOR_2)) {
+            tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR_2, -1);
+        }
+        else {
+            tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR, -1);
+        }
     }
     else {
         tmp = PyUnicode_Split(filters, sstate->FILTER_SEPARATOR_2, -1);
--- a/configmix/config.py	Tue Aug 15 10:41:22 2023 +0200
+++ b/configmix/config.py	Tue Aug 15 11:14:51 2023 +0200
@@ -451,7 +451,10 @@
         filters = filters.strip()
         if filters:
             if direction == 1:
-                return (name, filters.split(_FILTER_SEPARATOR))
+                if _FILTER_SEPARATOR_2 in filters:
+                    return (name, filters.split(_FILTER_SEPARATOR_2))
+                else:
+                    return (name, filters.split(_FILTER_SEPARATOR))
             else:
                 return (name, filters.split(_FILTER_SEPARATOR_2))
         else:
--- a/docs/introduction.rst	Tue Aug 15 10:41:22 2023 +0200
+++ b/docs/introduction.rst	Tue Aug 15 11:14:51 2023 +0200
@@ -460,9 +460,14 @@
 
     {{[namespace:]variable[|filter[|filter...]]}}
 
+or::
+
+    {{[namespace:]variable[|filter[,filter...]]}}
+
 I.e.: between double curly braces an optional `namespace` name followed by
-a colon ``:``, the `variable` and then zero or more filters, each one
-introduced by a pipe symbol ``|``.
+a colon ``:``, the `variable` and then zero or more filters, the first one
+introduced by a pipe symbol ``|`` the following ones introduced by a
+comma ``,`` or a pipe symbol ``|``. The comma ``,`` should be preferred.
 
 Variables are expanded *lately* at runtime -- exactly when calling
 :py:meth:`.Configuration.getvar_s`,
@@ -474,13 +479,20 @@
           *must* be quoted (see :ref:`quoting`) when using variable
           interpolation syntax.
 
+.. note:: Commata ``,`` and pipe symbols ``|`` are not allowed within
+          filter names.
+
 
 Filter functions
 ~~~~~~~~~~~~~~~~
 
 Interpolated values can be processed through a series of filter functions::
 
-    {{my.variable|filter1|filter2}}
+    {{my.variable|filter1|filter2|filter3}}
+
+or::
+
+   {{my.variable|filter1,filter2,filter3}}
 
 Available filter functions are:
 
--- a/tests/test.py	Tue Aug 15 10:41:22 2023 +0200
+++ b/tests/test.py	Tue Aug 15 11:14:51 2023 +0200
@@ -2229,6 +2229,10 @@
         self.assertEqual((u"the-varname", [u"Empty", u"None"]),
                          self.split_filters(u"the-varname|Empty|None", 1))
 
+    def test_split_filters_many_alt(self):
+        self.assertEqual((u"the-varname", [u"Empty", u"None"]),
+                         self.split_filters(u"the-varname|Empty,None", 1))
+
     def test_None_filter_single(self):
         cfg = configmix.load()
         x = getattr(cfg, self.interpolate_meth)(u"{{non-existing|None}}")