Ejemplo n.º 1
0
def _load(obj: Any, context: ILoadingContext,
          config: ObjectField.Config) -> Any:
    fields = config.get_fields(obj)

    node = context.current_node()
    set_fields = set()

    for name_node, value_node in node.value:
        field_name = context.load(StringField(), name_node)
        if field_name is UNDEFINED:
            continue

        field_name = name_node.value
        set_fields.add(field_name)
        if field_name not in fields:
            context.error(ErrorCode.FIELD_NOT_DECLARED,
                          _("Field {} is not declared."), field_name)
            continue

        field = fields[field_name]
        field_value = context.load(field, value_node)
        if field_value is UNDEFINED:
            continue

        setattr(obj, field_name, field_value)

    if _validate(obj, fields, set_fields, context, config):
        post_load = config.get_hook(obj, "post_load")
        if post_load is not None:
            post_load()

        return obj

    return UNDEFINED
Ejemplo n.º 2
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        """See Resolver.resolve for usage."""
        if not context.expect_scalar(
            _("import / try-import must be set on a scalar node")
        ):
            return UNDEFINED

        file_path = self._get_file(context)
        if file_path is None:
            node = context.current_node()
            if node.tag == "!import":
                context.error(
                    ErrorCode.IMPORT_NOT_FOUND,
                    _("Unable to find {} in any of the configured directories"),
                    node.value,
                )

            return UNDEFINED

        file_yaml_node = self._load_file(context, file_path)

        if file_yaml_node is None:
            return UNDEFINED

        return context.load(field, file_yaml_node, str(file_path))
Ejemplo n.º 3
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()
        assert isinstance(node, SequenceNode)

        for child in node.value:
            result = context.load(field, child)
            if result is not UNDEFINED:
                return result

        return UNDEFINED
Ejemplo n.º 4
0
    def _load(self, context: ILoadingContext) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()
        result = []
        for item_node in node.value:
            item = context.load(self._item_field, item_node)
            if item is UNDEFINED:
                continue

            result.append(item)

        return result
Ejemplo n.º 5
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        if not context.expect_scalar(
            _("!env must be set on a string node containing the variable name.")
        ):
            return UNDEFINED

        node = context.current_node()
        var_name = node.value

        if var_name not in environ:
            return UNDEFINED

        fake_node = ScalarNode("", environ[var_name], node.start_mark, node.end_mark)

        return context.load(field, fake_node)
Ejemplo n.º 6
0
    def _load(self, context: ILoadingContext) -> Any:
        node = context.current_node()
        if not context.expect_mapping():
            return UNDEFINED

        result = {}
        for key_node, value_node in node.value:
            assert isinstance(key_node, ScalarNode)
            key = key_node.value

            item = context.load(self._item_field, value_node)
            if item is UNDEFINED:
                continue

            result[key] = item

        return result
Ejemplo n.º 7
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        """See Resolver.resolve for usage."""
        if not context.expect_scalar(_("glob must be set on a scalar node")):
            return UNDEFINED

        node = context.current_node()
        glob = node.value
        result = []
        for root in self._get_roots(context):
            for path in root.glob(glob):
                if not path.is_file():
                    continue

                content = self._load_file(context, path)

                if content is not None:
                    result.append(content)

        fake_node = SequenceNode("", result, node.start_mark, node.end_mark)
        return context.load(field, fake_node)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
    def load(self, context: ILoadingContext, field: IBaseField) -> Any:
        if not context.expect_sequence():
            return UNDEFINED

        node = context.current_node()

        result: Optional[Union[Dict[str, Any], List[Any]]] = None
        for child in node.value:
            child_result = context.load(field, child)
            if child_result is UNDEFINED:
                continue

            if isinstance(child_result, dict):
                if result is None:
                    result = {}

                assert isinstance(result, dict)
                result.update(child_result)

            elif isinstance(child_result, list):
                if result is None:
                    result = []

                assert isinstance(result, list)
                result += child_result

            else:
                msg = _("Trying to merge invalid object {}, expected dict or "
                        "list")
                context.error(ErrorCode.VALUE_ERROR, msg, result)

        # If nothing it's to merge, return UNDEFINED
        if result is None:
            return UNDEFINED

        return result
Ejemplo n.º 10
0
 def _load(self, context: ILoadingContext) -> None:
     context.load(_ChildField(), _get_dummy_node())