comparison configmix/config.py @ 603:e55a42144ba9

C-implementations for Configuration.getvarl() and Configuration.getvar_s()
author Franz Glasner <fzglas.hg@dom66.de>
date Tue, 11 Jan 2022 02:50:17 +0100
parents f454889e41fa
children 764d4185c76a
comparison
equal deleted inserted replaced
602:a2fff0d93d83 603:e55a42144ba9
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_getvarl, _fast_getvar_s,
36 _fast_interpolate_variables, 37 _fast_interpolate_variables,
37 _sync_MISSING) 38 _sync_MISSING, _sync_MARKER)
38 except ImportError: 39 except ImportError:
39 fast_unquote = None 40 fast_unquote = None
40 fast_quote = None 41 fast_quote = None
41 fast_pathstr2path = None 42 fast_pathstr2path = None
42 _fast_split_ns = None 43 _fast_split_ns = None
43 _fast_split_filters = None 44 _fast_split_filters = None
45 _fast_getvarl = None
46 _fast_getvar_s = None
44 _fast_interpolate_variables = None 47 _fast_interpolate_variables = None
45 _sync_MISSING = None 48 _sync_MISSING = None
46 49
47 50
48 _MARKER = object() 51 _MARKER = object()
501 def values(self): 504 def values(self):
502 """Values without interpolation""" 505 """Values without interpolation"""
503 for k in self: 506 for k in self:
504 yield self.getitem_ns(k) 507 yield self.getitem_ns(k)
505 508
506 def getvarl(self, *path, **kwds): 509 def py_getvarl(self, *path, **kwds):
507 """Get a variable where the hierarchy is given in `path` as sequence 510 """Get a variable where the hierarchy is given in `path` as sequence
508 and the namespace is given in the `namespace` keyword argument. 511 and the namespace is given in the `namespace` keyword argument.
509 512
510 No variable interpolation is done and no filters are applied. 513 No variable interpolation is done and no filters are applied.
511 514
528 raise KeyError("Variable %r not found" % (path,)) 531 raise KeyError("Variable %r not found" % (path,))
529 else: 532 else:
530 return default 533 return default
531 else: 534 else:
532 return varvalue 535 return varvalue
536
537 if _fast_getvarl:
538
539 def fast_getvarl(self, *args, **kwds):
540 return _fast_getvarl(self, args, **kwds)
541
542 getvarl = fast_getvarl
543
544 else:
545 getvarl = py_getvarl
533 546
534 def getkeysl(self, *path, **kwds): 547 def getkeysl(self, *path, **kwds):
535 """Yield the keys of a variable value. 548 """Yield the keys of a variable value.
536 549
537 :rtype: A generator 550 :rtype: A generator
707 raise KeyError( 720 raise KeyError(
708 "none of the given variables found: %r" % (paths,)) 721 "none of the given variables found: %r" % (paths,))
709 else: 722 else:
710 return default 723 return default
711 724
712 def getvar_s(self, varname, default=_MARKER): 725 def py_getvar_s(self, varname, default=_MARKER):
713 """Get a variable - including variables from other namespaces. 726 """Get a variable - including variables from other namespaces.
714 727
715 `varname` is interpreted as in :meth:`.getvar`. But variables 728 `varname` is interpreted as in :meth:`.getvar`. But variables
716 will be interpolated recursively within the variable values 729 will be interpolated recursively within the variable values
717 and filters are applied. 730 and filters are applied.
731 if default is _MARKER: 744 if default is _MARKER:
732 raise 745 raise
733 else: 746 else:
734 return default 747 return default
735 748
736 def _getvar_s_with_cache_info(self, varname): 749 if _fast_getvar_s:
750
751 def fast_getvar_s(self, varname, default=_MARKER):
752 return _fast_getvar_s(self, varname, default)
753
754 getvar_s = fast_getvar_s
755
756 else:
757
758 getvar_s = py_getvar_s
759
760 def _py_getvar_s_with_cache_info(self, varname):
737 """Internal variant of :meth:`~.getvar_s` that returns some information 761 """Internal variant of :meth:`~.getvar_s` that returns some information
738 whether caching of interpolated values is allowed 762 whether caching of interpolated values is allowed
739 763
740 Caching is currently not allowed when namespaces are used. 764 Caching is currently not allowed when namespaces are used.
741 765
907 rest = start 931 rest = start
908 break 932 break
909 varname, filters = _split_filters( 933 varname, filters = _split_filters(
910 s[start+2:end]) # noqa: E226 934 s[start+2:end]) # noqa: E226
911 try: 935 try:
912 varvalue, cacheable = self._getvar_s_with_cache_info(varname) 936 varvalue, cacheable = self._py_getvar_s_with_cache_info(varname)
913 except KeyError: 937 except KeyError:
914 cacheable = True 938 cacheable = True
915 if NONE_FILTER in filters: 939 if NONE_FILTER in filters:
916 varvalue = None 940 varvalue = None
917 elif EMPTY_FILTER in filters: 941 elif EMPTY_FILTER in filters:
1239 return r 1263 return r
1240 1264
1241 1265
1242 if _sync_MISSING: 1266 if _sync_MISSING:
1243 _sync_MISSING(_MISSING) 1267 _sync_MISSING(_MISSING)
1268 _sync_MARKER(_MARKER)