예제 #1
0
    def _get_roots(self, context: ILoadingContext) -> Iterator[Path]:
        """Return the configured root directories."""
        if self._allow_relative:
            current_location = context.current_location()
            if current_location is not None:
                file_path = Path(current_location)
                parent = file_path.parent
                if parent.is_dir():
                    yield parent

        config = context.get_config(PathHandler.Config)
        if config is not None:
            for root in config.roots:
                yield root
예제 #2
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        node = context.current_node()
        tag = node.tag[1:]  # Remove trailing !
        match = self._compiled_pattern.match(tag)

        # The pattern should match already if we're here
        assert match is not None

        config = context.get_config(IfHandler.Config)
        flag = match.group("flag")

        if not config.is_defined(flag):
            return UNDEFINED

        # We need to return a copy of the node and erase the tag to avoid
        # the infinite recursion that would happen if we return a node with
        # an if tag still defined on it
        node_copy = copy(node)
        node_copy.tag = ""

        return context.load(field, node_copy)
예제 #3
0
    def _load(self, context: ILoadingContext) -> Any:
        if not context.expect_mapping():
            return UNDEFINED

        config = context.get_config(ObjectField.Config)
        node = context.current_node()
        tag = str(node.tag)
        if tag.startswith("!type"):
            splitted_tag = tag.split(":")
            if len(splitted_tag) != 2:
                context.error(ErrorCode.BAD_TYPE_TAG_FORMAT, _TYPE_FORMAT_MSG,
                              tag)
                return None
            type_name = splitted_tag[1]
            obj = config.create(type_name, context)
        else:
            obj = self._object_class()

        if obj is None:
            return UNDEFINED

        return _load(obj, context, config)