Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 540:33856ae1cc0b
_split_ns() and _split_filters() are now module-globals
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Tue, 28 Dec 2021 19:22:28 +0100 |
| parents | 9546d38cd3f8 |
| children | 25b61f0a1958 |
comparison
equal
deleted
inserted
replaced
| 539:9546d38cd3f8 | 540:33856ae1cc0b |
|---|---|
| 343 return tuple([unquote(p) for p in varname.split(_HIER_SEPARATOR)]) | 343 return tuple([unquote(p) for p in varname.split(_HIER_SEPARATOR)]) |
| 344 else: | 344 else: |
| 345 return tuple() | 345 return tuple() |
| 346 | 346 |
| 347 | 347 |
| 348 def _split_ns(s): | |
| 349 ns, sep, rest = s.partition(_NS_SEPARATOR) | |
| 350 if sep: | |
| 351 return (unquote(ns), rest) | |
| 352 else: | |
| 353 return (None, ns) | |
| 354 | |
| 355 | |
| 356 def _split_filters(s): | |
| 357 name, sep, filters = s.partition(_FILTER_SEPARATOR) | |
| 358 if sep: | |
| 359 filters = filters.strip() | |
| 360 if filters: | |
| 361 return (name.rstrip(), | |
| 362 filters.split(_FILTER_SEPARATOR)) | |
| 363 else: | |
| 364 return (name.rstrip(), []) | |
| 365 else: | |
| 366 return (name, []) | |
| 367 | |
| 368 | |
| 348 class Configuration(CoercingMethodsMixin, _AttributeDict): | 369 class Configuration(CoercingMethodsMixin, _AttributeDict): |
| 349 | 370 |
| 350 """The configuration dictionary with attribute support or | 371 """The configuration dictionary with attribute support or |
| 351 variable substitution. | 372 variable substitution. |
| 352 | 373 |
| 521 the default namespace. | 542 the default namespace. |
| 522 | 543 |
| 523 See also :func:`.quote`. | 544 See also :func:`.quote`. |
| 524 | 545 |
| 525 """ | 546 """ |
| 526 varns, varname = self._split_ns(varname) | 547 varns, varname = _split_ns(varname) |
| 527 if not varns: | 548 if not varns: |
| 528 return self.getvarl(*pathstr2path(varname), default=default) | 549 return self.getvarl(*pathstr2path(varname), default=default) |
| 529 else: | 550 else: |
| 530 return self.getvarl(varname, namespace=varns, default=default) | 551 return self.getvarl(varname, namespace=varns, default=default) |
| 531 | 552 |
| 634 and filters are applied. | 655 and filters are applied. |
| 635 | 656 |
| 636 For more details see chapter :ref:`variable-interpolation`. | 657 For more details see chapter :ref:`variable-interpolation`. |
| 637 | 658 |
| 638 """ | 659 """ |
| 639 varns, varname = self._split_ns(varname) | 660 varns, varname = _split_ns(varname) |
| 640 try: | 661 try: |
| 641 if not varns: | 662 if not varns: |
| 642 return self.substitute_variables_in_obj( | 663 return self.substitute_variables_in_obj( |
| 643 self.getvarl(*pathstr2path(varname))) | 664 self.getvarl(*pathstr2path(varname))) |
| 644 else: | 665 else: |
| 657 Caching is currently not allowed when namespaces are used. | 678 Caching is currently not allowed when namespaces are used. |
| 658 | 679 |
| 659 Currently used by :meth:`~.interpolate_variables`. | 680 Currently used by :meth:`~.interpolate_variables`. |
| 660 | 681 |
| 661 """ | 682 """ |
| 662 varns, varname = self._split_ns(varname) | 683 varns, varname = _split_ns(varname) |
| 663 if not varns: | 684 if not varns: |
| 664 # no namespace -> cacheable | 685 # no namespace -> cacheable |
| 665 return ( | 686 return ( |
| 666 self.substitute_variables_in_obj( | 687 self.substitute_variables_in_obj( |
| 667 self.getvarl(*pathstr2path(varname))), | 688 self.getvarl(*pathstr2path(varname))), |
| 691 if default is _MARKER: | 712 if default is _MARKER: |
| 692 raise KeyError( | 713 raise KeyError( |
| 693 "none of the given variables found: %r" % (varnames,)) | 714 "none of the given variables found: %r" % (varnames,)) |
| 694 else: | 715 else: |
| 695 return default | 716 return default |
| 696 | |
| 697 def _split_ns(self, s): | |
| 698 ns, sep, rest = s.partition(_NS_SEPARATOR) | |
| 699 if sep: | |
| 700 return (unquote(ns), rest) | |
| 701 else: | |
| 702 return (None, ns) | |
| 703 | |
| 704 def _split_filters(self, s): | |
| 705 name, sep, filters = s.partition(_FILTER_SEPARATOR) | |
| 706 if sep: | |
| 707 filters = filters.strip() | |
| 708 if filters: | |
| 709 return (name.rstrip(), | |
| 710 filters.split(_FILTER_SEPARATOR)) | |
| 711 else: | |
| 712 return (name.rstrip(), []) | |
| 713 else: | |
| 714 return (name, []) | |
| 715 | 717 |
| 716 def _lookupvar(self, *path): | 718 def _lookupvar(self, *path): |
| 717 """Lookup a variable within a hierarchy. | 719 """Lookup a variable within a hierarchy. |
| 718 | 720 |
| 719 :raise KeyError: An unexisting `path` raises a `KeyError` | 721 :raise KeyError: An unexisting `path` raises a `KeyError` |
| 838 res_append(s[rest:start]) | 840 res_append(s[rest:start]) |
| 839 end = s.find(_ENDTOK, start) | 841 end = s.find(_ENDTOK, start) |
| 840 if end < 0: | 842 if end < 0: |
| 841 rest = start | 843 rest = start |
| 842 break | 844 break |
| 843 varname, filters = self._split_filters( | 845 varname, filters = _split_filters( |
| 844 s[start+2:end]) # noqa: E226 | 846 s[start+2:end]) # noqa: E226 |
| 845 try: | 847 try: |
| 846 varvalue, cacheable = self._getvar_s_with_cache_info(varname) | 848 varvalue, cacheable = self._getvar_s_with_cache_info(varname) |
| 847 except KeyError: | 849 except KeyError: |
| 848 cacheable = True | 850 cacheable = True |
| 920 raise ValueError("one of `rootpath' or `root' must be given") | 922 raise ValueError("one of `rootpath' or `root' must be given") |
| 921 if rootpath is not None and not isinstance(rootpath, (list, tuple)): | 923 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
| 922 raise TypeError("`rootpath' must be a list or a tuple") | 924 raise TypeError("`rootpath' must be a list or a tuple") |
| 923 if root is not None: | 925 if root is not None: |
| 924 # convert to path | 926 # convert to path |
| 925 varns, varname = self._split_ns(root) | 927 varns, varname = _split_ns(root) |
| 926 if varns: | 928 if varns: |
| 927 raise ValueError( | 929 raise ValueError( |
| 928 "jailed configurations do not support namespaces") | 930 "jailed configurations do not support namespaces") |
| 929 rootpath = pathstr2path(root) | 931 rootpath = pathstr2path(root) |
| 930 jc = _JailedConfiguration(*rootpath) | 932 jc = _JailedConfiguration(*rootpath) |
| 1140 raise ValueError("one of `rootpath' or `root' must be given") | 1142 raise ValueError("one of `rootpath' or `root' must be given") |
| 1141 if rootpath is not None and not isinstance(rootpath, (list, tuple)): | 1143 if rootpath is not None and not isinstance(rootpath, (list, tuple)): |
| 1142 raise TypeError("`rootpath' must be a list or a tuple") | 1144 raise TypeError("`rootpath' must be a list or a tuple") |
| 1143 if root is not None: | 1145 if root is not None: |
| 1144 # convert to path | 1146 # convert to path |
| 1145 varns, varname = self._base._split_ns(root) | 1147 varns, varname = _split_ns(root) |
| 1146 if varns: | 1148 if varns: |
| 1147 raise ValueError( | 1149 raise ValueError( |
| 1148 "sub-jails do not support namespaces") | 1150 "sub-jails do not support namespaces") |
| 1149 rootpath = pathstr2path(varname) | 1151 rootpath = pathstr2path(varname) |
| 1150 if self._path: | 1152 if self._path: |
