Python yaml.MappingNode() Examples
The following are 25
code examples of yaml.MappingNode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
yaml
, or try the search function
.
Example #1
Source File: cfn_yaml.py From checkov with Apache License 2.0 | 6 votes |
def multi_constructor(loader, tag_suffix, node): """ Deal with !Ref style function format """ if tag_suffix not in UNCONVERTED_SUFFIXES: tag_suffix = '{}{}'.format(FN_PREFIX, tag_suffix) constructor = None if tag_suffix == 'Fn::GetAtt': constructor = construct_getatt elif isinstance(node, ScalarNode): constructor = loader.construct_scalar elif isinstance(node, SequenceNode): constructor = loader.construct_sequence elif isinstance(node, MappingNode): constructor = loader.construct_mapping else: raise 'Bad tag: !{}'.format(tag_suffix) return dict_node({tag_suffix: constructor(node)}, node.start_mark, node.end_mark)
Example #2
Source File: serializer.py From workload-automation with Apache License 2.0 | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, MappingNode): self.flatten_mapping(node) if not isinstance(node, MappingNode): raise ConstructorError(None, None, "expected a mapping node, but found %s" % node.id, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) if not isinstance(key, Hashable): raise ConstructorError("while constructing a mapping", node.start_mark, "found unhashable key", key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #3
Source File: base_entity.py From v20-python with MIT License | 6 votes |
def represent_odict(dump, tag, mapping, flow_style=None): """ Like BaseRepresenter.represent_mapping, but does not issue the sort(). """ value = [] node = yaml.MappingNode(tag, value, flow_style=flow_style) if dump.alias_key is not None: dump.represented_objects[dump.alias_key] = node best_style = True if hasattr(mapping, 'items'): mapping = mapping.items() for item_key, item_value in mapping: node_key = dump.represent_data(item_key) node_value = dump.represent_data(item_value) if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dump.default_flow_style is not None: node.flow_style = dump.default_flow_style else: node.flow_style = best_style return node
Example #4
Source File: specific_function.py From nxs-backup with GNU General Public License v3.0 | 6 votes |
def include(self, node): if isinstance(node, yaml.ScalarNode): return self.extractFile(self.construct_scalar(node)) elif isinstance(node, yaml.SequenceNode): result = [] for i in self.construct_sequence(node): i = general_function.get_absolute_path(i, self._root) for j in general_files_func.get_ofs(i): result += self.extractFile(j) return result elif isinstance(node, yaml.MappingNode): result = {} for k,v in self.construct_mapping(node).iteritems(): result[k] = self.extractFile(v) return result else: print ('Error:: unrecognised node type in !include statement') raise yaml.constructor.ConstructorError
Example #5
Source File: yamlutils.py From bazarr with GNU General Public License v3.0 | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: # pragma: no cover raise yaml.constructor.ConstructorError(None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as exc: # pragma: no cover raise yaml.constructor.ConstructorError('while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #6
Source File: representers.py From hokusai with MIT License | 6 votes |
def represent_odict(dump, tag, mapping, flow_style=None): """Like BaseRepresenter.represent_mapping, but does not issue the sort(). """ value = [] node = yaml.MappingNode(tag, value, flow_style=flow_style) if dump.alias_key is not None: dump.represented_objects[dump.alias_key] = node best_style = True if hasattr(mapping, 'items'): mapping = mapping.items() for item_key, item_value in mapping: node_key = dump.represent_data(item_key) node_value = dump.represent_data(item_value) if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dump.default_flow_style is not None: node.flow_style = dump.default_flow_style else: node.flow_style = best_style return node
Example #7
Source File: curriculum.py From MADRL with MIT License | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: raise yaml.constructor.ConstructorError(None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as exc: raise yaml.constructor.ConstructorError('while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #8
Source File: substages_parser.py From bootloader_instrumentation_suite with MIT License | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: raise yaml.constructor.ConstructorError(None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError, exc: raise yaml.constructor.ConstructorError('while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value
Example #9
Source File: translate_template.py From tacker with Apache License 2.0 | 6 votes |
def represent_odict(self, dump, tag, mapping, flow_style=None): value = [] node = yaml.MappingNode(tag, value, flow_style=flow_style) if dump.alias_key is not None: dump.represented_objects[dump.alias_key] = node best_style = True if hasattr(mapping, 'items'): mapping = mapping.items() for item_key, item_value in mapping: node_key = dump.represent_data(item_key) node_value = dump.represent_data(item_value) if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dump.default_flow_style is not None: node.flow_style = dump.default_flow_style else: node.flow_style = best_style return node
Example #10
Source File: utils.py From tacker with Apache License 2.0 | 6 votes |
def represent_odict(dump, tag, mapping, flow_style=None): value = [] node = yaml.MappingNode(tag, value, flow_style=flow_style) if dump.alias_key is not None: dump.represented_objects[dump.alias_key] = node best_style = True if hasattr(mapping, 'items'): mapping = mapping.items() for item_key, item_value in mapping: node_key = dump.represent_data(item_key) node_value = dump.represent_data(item_value) if not (isinstance(node_key, yaml.ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, yaml.ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dump.default_flow_style is not None: node.flow_style = dump.default_flow_style else: node.flow_style = best_style return node
Example #11
Source File: yaml_loader.py From aws-cfn-template-flip with Apache License 2.0 | 6 votes |
def multi_constructor(loader, tag_suffix, node): """ Deal with !Ref style function format """ if tag_suffix not in UNCONVERTED_SUFFIXES: tag_suffix = "{}{}".format(FN_PREFIX, tag_suffix) constructor = None if tag_suffix == "Fn::GetAtt": constructor = construct_getatt elif isinstance(node, yaml.ScalarNode): constructor = loader.construct_scalar elif isinstance(node, yaml.SequenceNode): constructor = loader.construct_sequence elif isinstance(node, yaml.MappingNode): constructor = loader.construct_mapping else: raise Exception("Bad tag: !{}".format(tag_suffix)) return ODict(( (tag_suffix, constructor(node)), ))
Example #12
Source File: yamlordereddictloader.py From python-yamlordereddictloader with MIT License | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: msg = 'expected a mapping node, but found %s' % node.id raise yaml.constructor.ConstructError(None, None, msg, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as err: raise yaml.constructor.ConstructError( 'while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % err, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #13
Source File: commands.py From cloud-custodian with Apache License 2.0 | 6 votes |
def construct_mapping(self, node, deep=False): if not isinstance(node, yaml.MappingNode): raise ConstructorError(None, None, "expected a mapping node, but found %s" % node.id, node.start_mark) key_set = set() for key_node, value_node in node.value: if not isinstance(key_node, yaml.ScalarNode): continue k = key_node.value if k in key_set: raise ConstructorError( "while constructing a mapping", node.start_mark, "found duplicate key", key_node.start_mark) key_set.add(k) return super(DuplicateKeyCheckLoader, self).construct_mapping(node, deep)
Example #14
Source File: yaml_ordered_dict.py From CityEnergyAnalyst with MIT License | 6 votes |
def construct_mapping(self, node, deep=False): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: raise yaml.constructor.ConstructorError(None, None, 'expected a mapping node, but found %s' % node.id, node.start_mark) mapping = OrderedDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as exc: raise yaml.constructor.ConstructorError('while constructing a mapping', node.start_mark, 'found unacceptable key (%s)' % exc, key_node.start_mark) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #15
Source File: serializer.py From rekall with GNU General Public License v2.0 | 6 votes |
def construct_mapping(cls, loader, node, deep=False): """Based on yaml.loader.BaseConstructor.construct_mapping.""" if not isinstance(node, yaml.MappingNode): raise yaml.loader.ConstructorError( None, None, "expected a mapping node, but found %s" % node.id, node.start_mark) mapping = OrderedYamlDict() for key_node, value_node in node.value: key = loader.construct_object(key_node, deep=deep) try: hash(key) except TypeError as exc: raise yaml.loader.ConstructorError( "while constructing a mapping", node.start_mark, "found unacceptable key (%s)" % exc, key_node.start_mark) value = loader.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #16
Source File: yaml_utils.py From rekall with GNU General Public License v2.0 | 6 votes |
def construct_mapping(cls, loader, node, deep=False): """Based on yaml.loader.BaseConstructor.construct_mapping.""" if not isinstance(node, yaml.MappingNode): raise yaml.loader.ConstructorError( None, None, "expected a mapping node, but found %s" % node.id, node.start_mark) mapping = OrderedYamlDict() for key_node, value_node in node.value: key = loader.construct_object(key_node, deep=deep) try: hash(key) except TypeError as exc: raise yaml.loader.ConstructorError( "while constructing a mapping", node.start_mark, "found unacceptable key (%s)" % exc, key_node.start_mark) value = loader.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #17
Source File: conftest.py From pycraft with MIT License | 5 votes |
def construct_mapping(self, node, deep=None): if isinstance(node, yaml.MappingNode): self.flatten_mapping(node) else: if hasattr(node, 'id') and hasattr(node, 'start_mark'): error = 'expected a mapping node, but found {}'.format(node.id) raise yaml.constructorConstructorError(None, None, error, node.start_mark) else: error = 'Invalid type for node variable: {} - {}'.format(type(node), node) raise TypeError(error) mapping = self.container() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError as e: raise yaml.constructorConstructorError( 'while constructing a mapping', node.start_mark, 'found unacceptable key ({})'.format(e), key_node.start_mark ) value = self.construct_object(value_node, deep=deep) mapping[key] = value return mapping
Example #18
Source File: support.py From StrategyEase-Python-SDK with MIT License | 5 votes |
def construct_odict(self, node): omap = OrderedDict() yield omap if not isinstance(node, yaml.SequenceNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a sequence, but found %s" % node.id, node.start_mark ) for subnode in node.value: if not isinstance(subnode, yaml.MappingNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a mapping of length 1, but found %s" % subnode.id, subnode.start_mark ) if len(subnode.value) != 1: raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a single mapping item, but found %d items" % len(subnode.value), subnode.start_mark ) key_node, value_node = subnode.value[0] key = self.construct_object(key_node) value = self.construct_object(value_node) omap[key] = value
Example #19
Source File: utfyaml.py From pykit with MIT License | 5 votes |
def represent_mapping(dumper, mapping, flow_style=None): # override to dump map with item in any types value = [] tag = u'tag:yaml.org,2002:map' node = MappingNode(tag, value, flow_style=flow_style) if dumper.alias_key is not None: dumper.represented_objects[dumper.alias_key] = node # there is the change # ex: [u'hello', '中国'].sort() raises an error # if hasattr(mapping, 'items'): # mapping = mapping.items() # mapping.sort() best_style = True for item_key, item_value in mapping.items(): node_key = dumper.represent_data(item_key) node_value = dumper.represent_data(item_value) if not (isinstance(node_key, ScalarNode) and not node_key.style): best_style = False if not (isinstance(node_value, ScalarNode) and not node_value.style): best_style = False value.append((node_key, node_value)) if flow_style is None: if dumper.default_flow_style is not None: node.flow_style = dumper.default_flow_style else: node.flow_style = best_style return node
Example #20
Source File: file_loader.py From cfn-sphere with Apache License 2.0 | 5 votes |
def handle_yaml_constructors(loader, suffix, node): """ Constructor method for PyYaml to handle cfn intrinsic functions specified as yaml tags """ function_mapping = { "!and": ("Fn::And", lambda x: x), "!base64": ("Fn::Base64", lambda x: x), "!condition": ("Condition", lambda x: x), "!equals": ("Fn::Equals", lambda x: x), "!findinmap": ("Fn::FindInMap", lambda x: x), "!getatt": ("Fn::GetAtt", lambda x: str(x).split(".", 1)), "!getazs": ("Fn::GetAZs", lambda x: x), "!if": ("Fn::If", lambda x: x), "!importvalue": ("Fn::ImportValue", lambda x: x), "!join": ("Fn::Join", lambda x: [x[0], x[1]]), "!not": ("Fn::Not", lambda x: x), "!or": ("Fn::Or", lambda x: x), "!ref": ("Ref", lambda x: x), "!select": ("Fn::Select", lambda x: x), "!split": ("Fn::Split", lambda x: x), "!sub": ("Fn::Sub", lambda x: x), } try: function, value_transformer = function_mapping[str(suffix).lower()] except KeyError as key: raise CfnSphereException( "Unsupported cfn intrinsic function tag found: {0}".format(key)) if isinstance(node, yaml.ScalarNode): value = loader.construct_scalar(node) elif isinstance(node, yaml.SequenceNode): value = loader.construct_sequence(node) elif isinstance(node, yaml.MappingNode): value = loader.construct_mapping(node) else: raise CfnSphereException( "Invalid yaml node found while handling cfn intrinsic function tags") return {function: value_transformer(value)}
Example #21
Source File: serializer.py From rekall with GNU General Public License v2.0 | 5 votes |
def to_yaml(cls, dumper, data): value = [] node = yaml.nodes.MappingNode(cls.yaml_tag, value) for key, item in data.items(): node_key = dumper.represent_data(key) node_value = dumper.represent_data(item) value.append((node_key, node_value)) return node
Example #22
Source File: yaml_utils.py From rekall with GNU General Public License v2.0 | 5 votes |
def represent_orderedyamldict(dumper, data): value = [] for item_key, item_value in list(data.items()): node_key = dumper.represent_data(item_key) if type(item_value) not in [ str, str, list, dict, OrderedYamlDict, bool, int, int]: raise AttributeError("Non representable yaml.") node_value = dumper.represent_data(item_value) value.append((node_key, node_value)) return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', value)
Example #23
Source File: yaml_utils.py From rekall with GNU General Public License v2.0 | 5 votes |
def to_yaml(cls, dumper, data): value = [] node = yaml.nodes.MappingNode(cls.yaml_tag, value) for key, item in data.items(): node_key = dumper.represent_data(key) node_value = dumper.represent_data(item) value.append((node_key, node_value)) return node
Example #24
Source File: yaml_odict.py From airspyone_firmware with GNU General Public License v2.0 | 4 votes |
def construct_odict(load, node): """This is the same as SafeConstructor.construct_yaml_omap(), except the data type is changed to OrderedDict() and setitem is used instead of append in the loop. >>> yaml.load(''' ... !!omap ... - foo: bar ... - mumble: quux ... - baz: gorp ... ''') OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')]) >>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''') OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')]) """ omap = OrderedDict() yield omap if not isinstance(node, yaml.SequenceNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a sequence, but found %s" % node.id, node.start_mark ) for subnode in node.value: if not isinstance(subnode, yaml.MappingNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a mapping of length 1, but found %s" % subnode.id, subnode.start_mark ) if len(subnode.value) != 1: raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a single mapping item, but found %d items" % len(subnode.value), subnode.start_mark ) key_node, value_node = subnode.value[0] key = load.construct_object(key_node) value = load.construct_object(value_node) omap[key] = value
Example #25
Source File: meta.py From Carnets with BSD 3-Clause "New" or "Revised" License | 4 votes |
def _construct_odict(load, node): """ Construct OrderedDict from !!omap in yaml safe load. Source: https://gist.github.com/weaver/317164 License: Unspecified This is the same as SafeConstructor.construct_yaml_omap(), except the data type is changed to OrderedDict() and setitem is used instead of append in the loop Examples -------- :: >>> yaml.load(''' # doctest: +SKIP ... !!omap ... - foo: bar ... - mumble: quux ... - baz: gorp ... ''') OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')]) >>> yaml.load('''!!omap [ foo: bar, mumble: quux, baz : gorp ]''') # doctest: +SKIP OrderedDict([('foo', 'bar'), ('mumble', 'quux'), ('baz', 'gorp')]) """ import yaml omap = OrderedDict() yield omap if not isinstance(node, yaml.SequenceNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, f"expected a sequence, but found {node.id}", node.start_mark) for subnode in node.value: if not isinstance(subnode, yaml.MappingNode): raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, f"expected a mapping of length 1, but found {subnode.id}", subnode.start_mark) if len(subnode.value) != 1: raise yaml.constructor.ConstructorError( "while constructing an ordered map", node.start_mark, "expected a single mapping item, but found {} items".format(len(subnode.value)), subnode.start_mark) key_node, value_node = subnode.value[0] key = load.construct_object(key_node) value = load.construct_object(value_node) omap[key] = value