comparison configmix/config.py @ 411:3c95faa91dad

Optimize the creation of a jailed config. Allow to skip the binding of the base in case the caller wants to rebind just after creation anyways.
author Franz Glasner <f.glasner@feldmann-mg.com>
date Fri, 19 Nov 2021 15:52:01 +0100
parents 5286df5aeefe
children 816327e178b0
comparison
equal deleted inserted replaced
410:dd46257d2b22 411:3c95faa91dad
652 res.append(p[9:]) 652 res.append(p[9:])
653 else: 653 else:
654 raise ValueError("unknown quote syntax string: {}".format(s)) 654 raise ValueError("unknown quote syntax string: {}".format(s))
655 return ''.join(res) 655 return ''.join(res)
656 656
657 def jailed(self, rootpath=None, root=None): 657 def jailed(self, rootpath=None, root=None, bind_root=True):
658 """Return a "jailed" configuration of the current configuration. 658 """Return a "jailed" configuration of the current configuration.
659 659
660 :param rootpath: a sequence of strings that shall emcompass 660 :param rootpath: a sequence of strings that shall emcompass
661 the chroot-like jail of the returned 661 the chroot-like jail of the returned
662 configuration 662 configuration
663 :type rootpath: list or tuple 663 :type rootpath: list or tuple
664 :param str root: a string path expression that shall encompass 664 :param str root: a string path expression that shall encompass
665 the chroot-like jail of the returned configuration 665 the chroot-like jail of the returned configuration
666 :param bool bind_root: if you do a :meth:`~.rebind` just after
667 creation of a jailed config you can set
668 `bind_root` to `False`; otherwise use
669 the default
666 :return: a jailed (aka restricted) configuration 670 :return: a jailed (aka restricted) configuration
667 :rtype: _JailedConfiguration 671 :rtype: _JailedConfiguration
668 672
669 Exactly one of `rootpath` or `root` must be given. 673 Exactly one of `rootpath` or `root` must be given.
670 674
681 "jailed configurations do not support namespaces") 685 "jailed configurations do not support namespaces")
682 rootpath = [ 686 rootpath = [
683 self.unquote(p) for p in root.split( 687 self.unquote(p) for p in root.split(
684 self._HIER_SEPARATOR) 688 self._HIER_SEPARATOR)
685 ] 689 ]
686 690 jc = _JailedConfiguration(*rootpath)
687 return _JailedConfiguration(self, *rootpath) 691 if bind_root:
692 jc.rebind(self)
693 return jc
688 694
689 695
690 class _JailedConfiguration(object): 696 class _JailedConfiguration(object):
691 697
692 """A jailed and restricted variant of :class:`Configuration`. 698 """A jailed and restricted variant of :class:`Configuration`.
712 __slots__ = ("_base", "_path", "_pathstr") 718 __slots__ = ("_base", "_path", "_pathstr")
713 719
714 is_jail = True 720 is_jail = True
715 """Flag to show that this is a jail for another configuration""" 721 """Flag to show that this is a jail for another configuration"""
716 722
717 def __init__(self, config, *path): 723 def __init__(self, *path):
718 super(_JailedConfiguration, self).__init__() 724 super(_JailedConfiguration, self).__init__()
719 self._path = path 725 self._path = path
720 if path: 726 if path:
721 self._pathstr = \ 727 self._pathstr = \
722 Configuration._HIER_SEPARATOR.join( 728 Configuration._HIER_SEPARATOR.join(
723 [Configuration.quote(p) for p in path]) \ 729 [Configuration.quote(p) for p in path]) \
724 + Configuration._HIER_SEPARATOR 730 + Configuration._HIER_SEPARATOR
725 else: 731 else:
726 self._pathstr = '' 732 self._pathstr = ''
727 self.rebind(config)
728 733
729 @property 734 @property
730 def base(self): 735 def base(self):
731 """Ask for the base (aka parent) configuration""" 736 """Ask for the base (aka parent) configuration"""
732 return self._base 737 return self._base