Mercurial > hgrepos > Python > libs > ConfigMix
comparison configmix/config.py @ 395:0b3ffc34fa5c
Begin a jailed configuration with access to a sub-tree of the original configuration
| author | Franz Glasner <f.glasner@feldmann-mg.com> |
|---|---|
| date | Thu, 18 Nov 2021 18:30:59 +0100 |
| parents | 44ef854da70a |
| children | a50f8fb16b05 |
comparison
equal
deleted
inserted
replaced
| 394:e05195ee869d | 395:0b3ffc34fa5c |
|---|---|
| 116 | 116 |
| 117 No variable interpolation is done and no filters are applied. | 117 No variable interpolation is done and no filters are applied. |
| 118 | 118 |
| 119 Quoting of anything in `paths` is *not* needed and wrong. | 119 Quoting of anything in `paths` is *not* needed and wrong. |
| 120 | 120 |
| 121 """ | 121 """ |
| 122 default = kwds.pop("default", _MARKER) | 122 default = kwds.pop("default", _MARKER) |
| 123 for path in paths: | 123 for path in paths: |
| 124 if isinstance(path, (list, tuple)): | 124 if isinstance(path, (list, tuple)): |
| 125 try: | 125 try: |
| 126 varvalue = self.getvarl(*path) | 126 varvalue = self.getvarl(*path) |
| 646 res.append(uchr(int(p[1:9], 16))) | 646 res.append(uchr(int(p[1:9], 16))) |
| 647 res.append(p[9:]) | 647 res.append(p[9:]) |
| 648 else: | 648 else: |
| 649 raise ValueError("unknown quote syntax string: {}".format(s)) | 649 raise ValueError("unknown quote syntax string: {}".format(s)) |
| 650 return ''.join(res) | 650 return ''.join(res) |
| 651 | |
| 652 def jailed(self, rootpath=None, root=None): | |
| 653 """Return a "jailed" configuration. | |
| 654 | |
| 655 """ | |
| 656 if rootpath is not None and root is not None: | |
| 657 raise ValueError("only one of `rootpath' or `root' can be given") | |
| 658 if rootpath is None and root is None: | |
| 659 raise ValueError("one of `rootpath' or `root' must be given") | |
| 660 if root is not None: | |
| 661 # convert to path | |
| 662 varns, varname = self._split_ns(root) | |
| 663 if varns: | |
| 664 raise ValueError( | |
| 665 "jailed configurations do not support namespaces") | |
| 666 rootpath = [ | |
| 667 self.unquote(p) for p in root.split( | |
| 668 self._HIER_SEPARATOR) | |
| 669 ] | |
| 670 | |
| 671 return _JailedConfiguration(self, *rootpath) | |
| 672 | |
| 673 | |
| 674 class _JailedConfiguration(object): | |
| 675 | |
| 676 """Chrooted into a subtree of the configuration -- no namespace support""" | |
| 677 | |
| 678 __slots__ = ("_base", "_path", "_pathstr" ) | |
| 679 | |
| 680 def __init__(self, config, *path): | |
| 681 super(_JailedConfiguration, self).__init__() | |
| 682 self._base = config | |
| 683 self._path = path | |
| 684 if path: | |
| 685 self._pathstr = \ | |
| 686 Configuration._HIER_SEPARATOR.join( | |
| 687 [Configuration.quote(p) for p in path]) \ | |
| 688 + Configuration._HIER_SEPARATOR | |
| 689 else: | |
| 690 self._pathstr = '' | |
| 691 # | |
| 692 # Early error out if the chroot does not exist but allow | |
| 693 # degenerated case if `self._path` is empty. | |
| 694 # | |
| 695 if self._path: | |
| 696 self._base.getvarl(*self._path) | |
| 697 | |
| 698 def getvarl(self, *path, **kwds): | |
| 699 return self._base.getvarl(*(self._path + path), **kwds) | |
| 700 | |
| 701 def getvarl_s(self, *path, **kwds): | |
| 702 return self._base.getvarl_s(*(self._path + path), **kwds) | |
| 703 | |
| 704 def getvar(self, varname, default=_MARKER): | |
| 705 return self._base.getvar(self._pathstr + varname, default=default) | |
| 706 | |
| 707 def getvar_s(self, varname, default=_MARKER): | |
| 708 return self._base.getvar_s(self._pathstr + varname, default=default) |
