Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/variables.py @ 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 | f6f2dc7cf053 |
| children | e2e8d21b4122 |
comparison
equal
deleted
inserted
replaced
| 105:1c2f8a96dec2 | 106:b35837427a7a |
|---|---|
| 57 raise KeyError("variable %r not found in namespace PY" % name) | 57 raise KeyError("variable %r not found in namespace PY" % name) |
| 58 else: | 58 else: |
| 59 return default | 59 return default |
| 60 | 60 |
| 61 | 61 |
| 62 _varns_registry = { | 62 _varns_registry = {} |
| 63 "ENV": _envlookup, | 63 """Namespace registry""" |
| 64 "OS": _oslookup, | 64 |
| 65 "PY": _pylookup | 65 |
| 66 } | 66 def add_varns(name, fn): |
| 67 """Register a new variable namespace `name` and it's implementing | |
| 68 function `fn` | |
| 69 | |
| 70 """ | |
| 71 _varns_registry[name] = fn | |
| 67 | 72 |
| 68 | 73 |
| 69 def lookup_varns(name): | 74 def lookup_varns(name): |
| 75 """Lookup the variable namespace `name` and return it's implementing | |
| 76 function | |
| 77 | |
| 78 :param str name: the namespace name | |
| 79 :returns: the implementing function | |
| 80 :exception KeyError: if the namespace `name` doesn't exist | |
| 81 | |
| 82 """ | |
| 70 return _varns_registry[_normalized(name)] | 83 return _varns_registry[_normalized(name)] |
| 71 | 84 |
| 72 | 85 |
| 73 _filter_registry = {} | 86 _filter_registry = {} |
| 87 """Filter registry""" | |
| 74 | 88 |
| 75 | 89 |
| 76 def add_filter(name, fn): | 90 def add_filter(name, fn): |
| 77 """Register a variable filter with name `name` and implementation `fn` | 91 """Register a variable filter function with name `name` and |
| 92 implementation `fn` | |
| 78 | 93 |
| 79 """ | 94 """ |
| 80 _filter_registry[_normalized(name)] = fn | 95 _filter_registry[_normalized(name)] = fn |
| 81 | 96 |
| 82 | 97 |
| 83 def lookup_filter(name): | 98 def lookup_filter(name): |
| 84 """Lookup a variable filter with name `name` and return it's implementation | 99 """Lookup a variable filter with name `name` and return it's |
| 85 function | 100 implementation function |
| 101 | |
| 102 :param str name: the logical filter name | |
| 103 :returns: the implementing filter function | |
| 104 :exception KeyError: if the filter cannot be found | |
| 86 | 105 |
| 87 """ | 106 """ |
| 88 return _filter_registry[_normalized(name)] | 107 return _filter_registry[_normalized(name)] |
| 89 | 108 |
| 90 | 109 |
| 181 | 200 |
| 182 @filter("upper") | 201 @filter("upper") |
| 183 def upper_impl(config, v): | 202 def upper_impl(config, v): |
| 184 """Implementation of the `upper` filter function""" | 203 """Implementation of the `upper` filter function""" |
| 185 return v.upper() | 204 return v.upper() |
| 205 | |
| 206 | |
| 207 # Register the default namespaces | |
| 208 add_varns("ENV", _envlookup) | |
| 209 add_varns("OS", _oslookup) | |
| 210 add_varns("PY", _pylookup) |
