comparison configmix/config.py @ 610:764d4185c76a

C-implementations of Configuration.getvarl_s() and Configuration.getvar()
author Franz Glasner <fzglas.hg@dom66.de>
date Wed, 12 Jan 2022 00:44:02 +0100
parents e55a42144ba9
children 4499e9b4855d
comparison
equal deleted inserted replaced
609:9ad860d6ddc9 610:764d4185c76a
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_getvarl, _fast_getvarl_s,
37 _fast_getvar, _fast_getvar_s,
37 _fast_interpolate_variables, 38 _fast_interpolate_variables,
38 _sync_MISSING, _sync_MARKER) 39 _sync_MISSING, _sync_MARKER)
39 except ImportError: 40 except ImportError:
40 fast_unquote = None 41 fast_unquote = None
41 fast_quote = None 42 fast_quote = None
42 fast_pathstr2path = None 43 fast_pathstr2path = None
43 _fast_split_ns = None 44 _fast_split_ns = None
44 _fast_split_filters = None 45 _fast_split_filters = None
45 _fast_getvarl = None 46 _fast_getvarl = None
47 _fast_getvarl_s = None
48 _fast_getvar = None
46 _fast_getvar_s = None 49 _fast_getvar_s = None
47 _fast_interpolate_variables = None 50 _fast_interpolate_variables = None
48 _sync_MISSING = None 51 _sync_MISSING = None
49 52
50 53
605 raise KeyError( 608 raise KeyError(
606 "none of the given variables found: %r" % (paths,)) 609 "none of the given variables found: %r" % (paths,))
607 else: 610 else:
608 return default 611 return default
609 612
610 def getvar(self, varname, default=_MARKER): 613 def py_getvar(self, varname, default=_MARKER):
611 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including 614 """Get a variable of the form ``[ns:][[key1.]key2.]name`` - including
612 variables from other namespaces. 615 variables from other namespaces.
613 616
614 No variable interpolation is done and no filters are applied. 617 No variable interpolation is done and no filters are applied.
615 618
622 varns, varname = _split_ns(varname) 625 varns, varname = _split_ns(varname)
623 if not varns: 626 if not varns:
624 return self.getvarl(*pathstr2path(varname), default=default) 627 return self.getvarl(*pathstr2path(varname), default=default)
625 else: 628 else:
626 return self.getvarl(varname, namespace=varns, default=default) 629 return self.getvarl(varname, namespace=varns, default=default)
630
631 if _fast_getvar:
632
633 def fast_getvar(self, varname, default=_MARKER):
634 return _fast_getvar(self, varname, default);
635
636 getvar = fast_getvar
637
638 else:
639
640 getvar = py_getvar
627 641
628 def getkeys(self, varname): 642 def getkeys(self, varname):
629 """Yield all the keys of a variable value. 643 """Yield all the keys of a variable value.
630 644
631 :rtype: A generator 645 :rtype: A generator
654 raise KeyError( 668 raise KeyError(
655 "none of the given variables found: %r" % (varnames,)) 669 "none of the given variables found: %r" % (varnames,))
656 else: 670 else:
657 return default 671 return default
658 672
659 def getvarl_s(self, *path, **kwds): 673 def py_getvarl_s(self, *path, **kwds):
660 """Get a variable - including variables from other namespaces. 674 """Get a variable - including variables from other namespaces.
661 675
662 `path` and `namespace` are interpreted as in 676 `path` and `namespace` are interpreted as in
663 :meth:`.getvarl`. But variables will be interpolated 677 :meth:`.getvarl`. But variables will be interpolated
664 recursively within the variable values and filters are 678 recursively within the variable values and filters are
675 except KeyError: 689 except KeyError:
676 if default is _MARKER: 690 if default is _MARKER:
677 raise 691 raise
678 else: 692 else:
679 return default 693 return default
694
695 if _fast_getvarl_s:
696
697 def fast_getvarl_s(self, *path, **kwds):
698 return _fast_getvarl_s(self, path, **kwds)
699
700 getvarl_s = fast_getvarl_s
701
702 else:
703
704 getvarl_s = py_getvarl_s
680 705
681 def getfirstvarl_s(self, *paths, **kwds): 706 def getfirstvarl_s(self, *paths, **kwds):
682 """A variant of :meth:`~.getfirstvarl` that does variable 707 """A variant of :meth:`~.getfirstvarl` that does variable
683 interpolation. 708 interpolation.
684 709