Mercurial > hgrepos > Python > libs > ConfigMix
changeset 352:2b209bdf6995
Implement the "Empty" filter.
This is like the "None" filter but returning an empty string instead of None.
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Thu, 24 Jun 2021 19:26:53 +0200 |
| parents | efbf7ba40287 |
| children | a7491f835cb0 |
| files | configmix/config.py configmix/variables.py tests/test.py |
| diffstat | 3 files changed, 19 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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))
--- 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)
--- 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):
