Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/variables.py @ 305:f529ca46dd50
Implemented the "ref" namespace to get configuration tree references.
BUGS:
- Tests should be done more thoroughly and extensively
- Interaction of tree references and variable substitution should be
tested more properly
- Documentation is missing yet
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 26 Apr 2021 09:42:42 +0200 |
| parents | eed16a1ec8f3 |
| children | d7daec119383 |
comparison
equal
deleted
inserted
replaced
| 304:d8361dd70d2d | 305:f529ca46dd50 |
|---|---|
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 # :- | 2 # :- |
| 3 # :Copyright: (c) 2015-2020, Franz Glasner. All rights reserved. | 3 # :Copyright: (c) 2015-2021, Franz Glasner. All rights reserved. |
| 4 # :License: BSD-3-Clause. See LICENSE.txt for details. | 4 # :License: BSD-3-Clause. See LICENSE.txt for details. |
| 5 # :- | 5 # :- |
| 6 """Variable interpolation: implementation of namespaces and filters | 6 """Variable interpolation: implementation of namespaces and filters |
| 7 | 7 |
| 8 """ | 8 """ |
| 16 import os | 16 import os |
| 17 import platform | 17 import platform |
| 18 from functools import wraps | 18 from functools import wraps |
| 19 | 19 |
| 20 from .compat import PY2, native_os_str_to_text, u | 20 from .compat import PY2, native_os_str_to_text, u |
| 21 from .constants import REF_NAMESPACE | |
| 21 | 22 |
| 22 | 23 |
| 23 _MARKER = object() | 24 _MARKER = object() |
| 24 | 25 |
| 25 | 26 |
| 72 | 73 |
| 73 def add_varns(name, fn): | 74 def add_varns(name, fn): |
| 74 """Register a new variable namespace `name` and it's implementing | 75 """Register a new variable namespace `name` and it's implementing |
| 75 function `fn` | 76 function `fn` |
| 76 | 77 |
| 77 """ | 78 ..note:: This function checks that `name` is not the special |
| 79 namespace :data:`~configmix.constants.REF_NAMESPACE`. | |
| 80 | |
| 81 """ | |
| 82 if name == REF_NAMESPACE: | |
| 83 raise ValueError( | |
| 84 "the special namespace `%s' is not allowed here" % REF_NAMESPACE) | |
| 78 _varns_registry[name] = fn | 85 _varns_registry[name] = fn |
| 79 | 86 |
| 80 | 87 |
| 81 def lookup_varns(name): | 88 def lookup_varns(name): |
| 82 """Lookup the variable namespace `name` and return it's implementing | 89 """Lookup the variable namespace `name` and return it's implementing |
| 84 | 91 |
| 85 :param str name: the namespace name | 92 :param str name: the namespace name |
| 86 :returns: the implementing function | 93 :returns: the implementing function |
| 87 :exception KeyError: if the namespace `name` doesn't exist | 94 :exception KeyError: if the namespace `name` doesn't exist |
| 88 | 95 |
| 89 """ | 96 ..note:: This function checks that `name` is not the special |
| 97 namespace :data:`~configmix.constants.REF_NAMESPACE`. | |
| 98 | |
| 99 """ | |
| 100 if name == REF_NAMESPACE: | |
| 101 raise ValueError( | |
| 102 "the special namespace `%s' is not allowed here" % REF_NAMESPACE) | |
| 90 return _varns_registry[_normalized(name)] | 103 return _varns_registry[_normalized(name)] |
| 91 | 104 |
| 92 | 105 |
| 93 _filter_registry = {} | 106 _filter_registry = {} |
| 94 """Filter registry""" | 107 """Filter registry""" |
