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
def _load(self, context: ILoadingContext) -> Any: if not context.expect_scalar(): return UNDEFINED current_node = context.current_node() string_value = current_node.value return self._convert(context, string_value)
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))
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
def _get_file(self, context: ILoadingContext) -> Optional[Path]: node = context.current_node() file_path = Path(node.value) if file_path.is_absolute(): return file_path for root in self._get_roots(context): path = root / file_path if path.is_file(): return path return None
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
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)
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
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)
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)
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)
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
def load(self, context: ILoadingContext, field: IBaseField) -> Node: return context.current_node()