# HG changeset patch # User Franz Glasner # Date 1624555613 -7200 # Node ID 2b209bdf6995d570d4d4740ddd5868f347449090 # Parent efbf7ba40287a7df4456dcb222f40574228d10dc Implement the "Empty" filter. This is like the "None" filter but returning an empty string instead of None. diff -r efbf7ba40287 -r 2b209bdf6995 configmix/config.py --- a/configmix/config.py Thu Jun 24 19:23:06 2021 +0200 +++ b/configmix/config.py Thu Jun 24 19:26:53 2021 +0200 @@ -348,6 +348,9 @@ if "None" in filters: varvalue = self._apply_filters( filters, self.getvar_s(varname, default=None)) + elif "Empty" in filters: + varvalue = self._apply_filters( + filters, self.getvar_s(varname, default=u(""))) else: varvalue = self._apply_filters( filters, self.getvar_s(varname)) diff -r efbf7ba40287 -r 2b209bdf6995 configmix/variables.py --- a/configmix/variables.py Thu Jun 24 19:23:06 2021 +0200 +++ b/configmix/variables.py Thu Jun 24 19:26:53 2021 +0200 @@ -255,6 +255,17 @@ return v +@filter("Empty") +def Empty_filter_impl(config, v): + """Identity. + + The `Empty` filter is just a marker to not throw `KeyError` but return + the empty string. + + """ + return v + + # Register the default namespaces add_varns("ENV", _envlookup) add_varns("OS", _oslookup) diff -r efbf7ba40287 -r 2b209bdf6995 tests/test.py --- a/tests/test.py Thu Jun 24 19:23:06 2021 +0200 +++ b/tests/test.py Thu Jun 24 19:26:53 2021 +0200 @@ -417,6 +417,11 @@ x = cfg.expand_variable("A{{non-existing|None}}Z") self.assertEquals("AZ", x) + def test10_Empty_filtersingle(self): + cfg = self._load() + x = cfg.expand_variable("{{non-existing|Empty}}") + self.assertEquals("", x) + class T02LoadAndMerge(_T02MixinLoadAndMerge, unittest.TestCase):