# HG changeset patch # User Franz Glasner # Date 1521901753 -3600 # Node ID b35837427a7aac4ce777e76018b6820697fddc41 # Parent 1c2f8a96dec208c2a5d342bf3433e0be428f6f35 Add an "add_varns()" function to add new namespaces for variables. Use the new function to register all builtin namespaces. diff -r 1c2f8a96dec2 -r b35837427a7a configmix/variables.py --- a/configmix/variables.py Fri Mar 23 17:43:36 2018 +0100 +++ b/configmix/variables.py Sat Mar 24 15:29:13 2018 +0100 @@ -59,30 +59,49 @@ return default -_varns_registry = { - "ENV": _envlookup, - "OS": _oslookup, - "PY": _pylookup -} +_varns_registry = {} +"""Namespace registry""" + + +def add_varns(name, fn): + """Register a new variable namespace `name` and it's implementing + function `fn` + + """ + _varns_registry[name] = fn def lookup_varns(name): + """Lookup the variable namespace `name` and return it's implementing + function + + :param str name: the namespace name + :returns: the implementing function + :exception KeyError: if the namespace `name` doesn't exist + + """ return _varns_registry[_normalized(name)] _filter_registry = {} +"""Filter registry""" def add_filter(name, fn): - """Register a variable filter with name `name` and implementation `fn` + """Register a variable filter function with name `name` and + implementation `fn` """ _filter_registry[_normalized(name)] = fn def lookup_filter(name): - """Lookup a variable filter with name `name` and return it's implementation - function + """Lookup a variable filter with name `name` and return it's + implementation function + + :param str name: the logical filter name + :returns: the implementing filter function + :exception KeyError: if the filter cannot be found """ return _filter_registry[_normalized(name)] @@ -183,3 +202,9 @@ def upper_impl(config, v): """Implementation of the `upper` filter function""" return v.upper() + + +# Register the default namespaces +add_varns("ENV", _envlookup) +add_varns("OS", _oslookup) +add_varns("PY", _pylookup)