def _ordered_dict(loader: SafeLineLoader, node: yaml.nodes.MappingNode) -> OrderedDict: """Load YAML mappings into an ordered dictionary to preserve key order.""" loader.flatten_mapping(node) nodes = loader.construct_pairs(node) seen = {} # type: Dict for (key, _), (child_node, _) in zip(nodes, node.value): line = child_node.start_mark.line try: hash(key) except TypeError: fname = getattr(loader.stream, 'name', '') raise yaml.MarkedYAMLError( context="invalid key: \"{}\"".format(key), context_mark=yaml.Mark(fname, 0, line, -1, None, None)) if key in seen: fname = getattr(loader.stream, 'name', '') first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None) second_mark = yaml.Mark(fname, 0, line, -1, None, None) raise yaml.MarkedYAMLError( context="duplicate key: \"{}\"".format(key), context_mark=first_mark, problem_mark=second_mark, ) seen[key] = line processed = OrderedDict(nodes) setattr(processed, '__config_file__', loader.name) setattr(processed, '__line__', node.start_mark.line) return processed
def construct_yaml_map(self, node): self.custom_flatten_mapping(node) nodes = self.custom_construct_pairs(node) seen = {} for (key, _), nv in zip(nodes, node.value): if isinstance(nv, yaml.ScalarNode): line = nv.start_mark.line else: line = nv[0].start_mark.line try: hash(key) except TypeError: raise yaml.MarkedYAMLError( context="invalid key: \"{}\"".format(key), context_mark=yaml.Mark(self.name, 0, line, -1, None, None)) if key in seen: raise yaml.MarkedYAMLError( context="duplicate key: \"{}\"".format(key), context_mark=yaml.Mark(self.name, 0, line, -1, None, None)) seen[key] = line return OrderedDict(nodes)
def _ordered_dict(loader, node): """Load YAML mappings into an ordered dictionary to preserve key order.""" custom_flatten_mapping(loader, node) nodes = custom_construct_pairs(loader, node) seen = {} for (key, _), nv in zip(nodes, node.value): if isinstance(nv, yaml.ScalarNode): line = nv.start_mark.line else: line = nv[0].start_mark.line try: hash(key) except TypeError: fname = getattr(loader.stream, 'name', '') raise yaml.MarkedYAMLError( context="invalid key: \"{}\"".format(key), context_mark=yaml.Mark(fname, 0, line, -1, None, None)) if key in seen: fname = getattr(loader.stream, 'name', '') raise pi4homeError(u'YAML file {} contains duplicate key "{}". ' u'Check lines {} and {}.'.format( fname, key, seen[key], line)) seen[key] = line return _add_reference(OrderedDict(nodes), loader, node)
def _ordered_dict(loader: SafeLineLoader, node: yaml.nodes.MappingNode) -> OrderedDict: """Load YAML mappings into an ordered dictionary to preserve key order.""" loader.flatten_mapping(node) nodes = loader.construct_pairs(node) seen: Dict = {} for (key, _), (child_node, _) in zip(nodes, node.value): line = child_node.start_mark.line try: hash(key) except TypeError: fname = getattr(loader.stream, "name", "") raise yaml.MarkedYAMLError( context=f'invalid key: "{key}"', context_mark=yaml.Mark(fname, 0, line, -1, None, None), ) if key in seen: fname = getattr(loader.stream, "name", "") _LOGGER.error( 'YAML file %s contains duplicate key "%s". ' "Check lines %d and %d.", fname, key, seen[key], line, ) seen[key] = line return _add_reference(OrderedDict(nodes), loader, node)
def _ordered_dict(loader: SafeLineLoader, node: yaml.nodes.MappingNode) -> OrderedDict: """Load YAML mappings into an ordered dictionary to preserve key order.""" loader.flatten_mapping(node) nodes = loader.construct_pairs(node) seen = {} # type: Dict min_line = None for (key, _), (node, _) in zip(nodes, node.value): line = getattr(node, '__line__', 'unknown') if line != 'unknown' and (min_line is None or line < min_line): min_line = line if key in seen: fname = getattr(loader.stream, 'name', '') first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None) second_mark = yaml.Mark(fname, 0, line, -1, None, None) raise yaml.MarkedYAMLError( context="duplicate key: \"{}\"".format(key), context_mark=first_mark, problem_mark=second_mark, ) seen[key] = line processed = OrderedDict(nodes) setattr(processed, '__config_file__', loader.name) setattr(processed, '__line__', min_line) return processed
def _ordered_dict(loader: LoaderType, node: yaml.nodes.MappingNode) -> OrderedDict: """Load YAML mappings into an ordered dictionary to preserve key order.""" loader.flatten_mapping(node) nodes = loader.construct_pairs(node) seen: dict = {} for (key, _), (child_node, _) in zip(nodes, node.value): line = child_node.start_mark.line try: hash(key) except TypeError as exc: fname = loader.get_stream_name() raise yaml.MarkedYAMLError( context=f'invalid key: "{key}"', context_mark=yaml.Mark(fname, 0, line, -1, None, None), # type: ignore[arg-type] ) from exc if key in seen: fname = loader.get_stream_name() _LOGGER.warning( 'YAML file %s contains duplicate key "%s". Check lines %d and %d', fname, key, seen[key], line, ) seen[key] = line return _add_reference(OrderedDict(nodes), loader, node)
def construct_secret(self, node): secrets = _load_yaml_internal(self._rel_path(SECRET_YAML)) if node.value not in secrets: raise yaml.MarkedYAMLError(f"Secret '{node.value}' not defined", node.start_mark) val = secrets[node.value] _SECRET_VALUES[str(val)] = node.value return val
def extract_file_vars(node): fields = self.construct_yaml_map(node) file = fields.get("file") if file is None: raise yaml.MarkedYAMLError("Must include 'file'", node.start_mark) vars = fields.get("vars") if vars: vars = {k: str(v) for k, v in vars.items()} return file, vars
def construct_secret(self, node): secrets = _load_yaml_internal(self._rel_path(SECRET_YAML)) if node.value not in secrets: raise yaml.MarkedYAMLError( context=u"Secret '{}' not defined".format(node.value), context_mark=node.start_mark) val = secrets[node.value] _SECRET_VALUES[text_type(val)] = node.value return val
def construct_env_var(self, node): args = node.value.split() # Check for a default value if len(args) > 1: return os.getenv(args[0], " ".join(args[1:])) if args[0] in os.environ: return os.environ[args[0]] raise yaml.MarkedYAMLError( f"Environment variable '{node.value}' not defined", node.start_mark )
def create_class(loader, node): """Create class instance from yaml node. """ kwargs = {} if isinstance(node, yaml.MappingNode): kwargs = loader.construct_mapping(node) class_name = [v for v in kwargs.keys() if kwargs[v] is None][0] else: class_name = loader.construct_scalar(node) try: # pylint: disable=invalid-name ObjClass = class_from_string(class_name) except Exception: # pragma: no cover raise yaml.MarkedYAMLError(f"`{class_name}` not found", node.start_mark) obj = ObjClass(**kwargs) return obj
def _ordered_dict(loader, node): """Load YAML mappings into an ordered dict to preserve key order.""" loader.flatten_mapping(node) nodes = loader.construct_pairs(node) seen = {} for (key, _), (node, _) in zip(nodes, node.value): line = getattr(node, '__line__', 'unknown') if key in seen: fname = getattr(loader.stream, 'name', '') first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None) second_mark = yaml.Mark(fname, 0, line, -1, None, None) raise yaml.MarkedYAMLError( context="duplicate key: \"{}\"".format(key), context_mark=first_mark, problem_mark=second_mark, ) seen[key] = line return OrderedDict(nodes)