sections.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:configmanager 作者: jbasko 项目源码 文件源码
def _get_item_or_section(self, key, handle_not_found=True):
        """
        This method must NOT be called from outside the Section class.

        Do not override this method.

        If handle_not_found is set to False, hooks won't be called.
        This is needed when checking key existence -- the whole
        purpose of key existence checking is to avoid errors (and error handling).
        """
        if isinstance(key, six.string_types):
            if self.settings.str_path_separator in key:
                return self._get_item_or_section(key.split(self.settings.str_path_separator))

            if key.endswith('_') and keyword.iskeyword(key[:-1]):
                key = key[:-1]

            if key in self._tree:
                resolution = self._tree[key]
            else:
                if handle_not_found:
                    result = self.dispatch_event(self.hooks.not_found, name=key, section=self)
                    if result is not None:
                        resolution = result
                    else:
                        raise NotFound(key, section=self)
                else:
                    raise NotFound(key, section=self)

        elif isinstance(key, (tuple, list)) and len(key) > 0:
            if len(key) == 1:
                resolution = self._get_item_or_section(key[0], handle_not_found=handle_not_found)
            else:
                resolution = self._get_item_or_section(
                    key[0], handle_not_found=handle_not_found
                )._get_item_or_section(key[1:], handle_not_found=handle_not_found)
        else:
            raise TypeError('Expected either a string or a tuple as key, got {!r}'.format(key))

        return resolution
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号