changeset 106:b35837427a7a

Add an "add_varns()" function to add new namespaces for variables. Use the new function to register all builtin namespaces.
author Franz Glasner <hg@dom66.de>
date Sat, 24 Mar 2018 15:29:13 +0100
parents 1c2f8a96dec2
children 2ee042791197
files configmix/variables.py
diffstat 1 files changed, 33 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- 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)