comparison configmix/config.py @ 554:36d7aa000435

Implement a C-version of Configuration.interpolate_variables
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 03 Jan 2022 00:11:41 +0100
parents 9d2bd411f5c5
children f454889e41fa
comparison
equal deleted inserted replaced
553:9d2bd411f5c5 554:36d7aa000435
30 from .variables import lookup_varns, lookup_filter 30 from .variables import lookup_varns, lookup_filter
31 from .compat import u, uchr, n, str_and_u, PY2 31 from .compat import u, uchr, n, str_and_u, PY2
32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER 32 from .constants import REF_NAMESPACE, NONE_FILTER, EMPTY_FILTER
33 try: 33 try:
34 from ._speedups import (fast_unquote, fast_quote, fast_pathstr2path, 34 from ._speedups import (fast_unquote, fast_quote, fast_pathstr2path,
35 _fast_split_ns, _fast_split_filters) 35 _fast_split_ns, _fast_split_filters,
36 _fast_interpolate_variables,
37 _sync_MISSING)
36 except ImportError: 38 except ImportError:
37 fast_unquote = None 39 fast_unquote = None
38 fast_quote = None 40 fast_quote = None
39 fast_pathstr2path = None 41 fast_pathstr2path = None
40 _fast_split_ns = None 42 _fast_split_ns = None
41 _fast_split_filters = None 43 _fast_split_filters = None
44 _fast_interpolate_variables = None
45 _sync_MISSING = None
42 46
43 47
44 _MARKER = object() 48 _MARKER = object()
45 _MISSING = object() 49 _MISSING = object()
46 50
871 for i in obj: 875 for i in obj:
872 newset.add(self.substitute_variables_in_obj(i)) 876 newset.add(self.substitute_variables_in_obj(i))
873 else: 877 else:
874 return obj 878 return obj
875 879
876 def interpolate_variables(self, s): 880 def py_interpolate_variables(self, s):
877 """Expand all variables in the single string `s`""" 881 """Expand all variables in the single string `s`"""
878 len_s = len(s) 882 len_s = len(s)
879 if len_s < 4: 883 if len_s < 4:
880 return s 884 return s
881 start = s.find(_STARTTOK, 0) 885 start = s.find(_STARTTOK, 0)
941 res = _EMPTY_STR.join(res) 945 res = _EMPTY_STR.join(res)
942 if use_cache: 946 if use_cache:
943 self.__interpolation_cache[s] = res 947 self.__interpolation_cache[s] = res
944 return res 948 return res
945 949
950 if _fast_interpolate_variables:
951
952 def fast_interpolate_variables(self, s):
953 return _fast_interpolate_variables(
954 self, s, self.__interpolation_cache)
955
956 interpolate_variables = fast_interpolate_variables
957
958 else:
959
960 interpolate_variables = py_interpolate_variables
961
946 def _apply_filters(self, filters, value): 962 def _apply_filters(self, filters, value):
947 for name in filters: 963 for name in filters:
948 try: 964 try:
949 filterfn = lookup_filter(name) 965 filterfn = lookup_filter(name)
950 except KeyError: 966 except KeyError:
1219 return sjc 1235 return sjc
1220 1236
1221 def __repr__(self): 1237 def __repr__(self):
1222 r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path)) 1238 r = "_JailedConfiguration(rootpath=%s)" % n(repr(self._path))
1223 return r 1239 return r
1240
1241
1242 if _sync_MISSING:
1243 _sync_MISSING(_MISSING)