def construct_ordered_mapping(self, node, deep=False): if not isinstance(node, yaml.MappingNode): raise yaml.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, collections.Hashable): raise yaml.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
def construct_mapping(self, node, deep=False): if not isinstance(node, yaml.MappingNode): msg = "expected a mapping node, but found %s" % node.id raise yaml.ConstructorError(None, None, msg, node.start_mark) mapping = LexicalScopeDict() for key_node, value_node in node.value: key = self.construct_object(key_node, deep=deep) try: hash(key) except TypeError, exc: msg = "while constructing a mapping" msg2 = "found unacceptable key (%s)" % exc raise yaml.ConstructorError(msg, node.start_mark, msg2, key_node.start_mark) value = self.construct_object(value_node, deep=deep) if isinstance(value, (LexicalScopeDict, LexicalScopeList)): value.parent = mapping mapping[key] = value
def construct_yaml_omap(self, node): omap = OrderedDict() yield omap if not isinstance(node, yaml.SequenceNode): raise yaml.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.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.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
def construct_sequence(self, node, deep=False): if not isinstance(node, yaml.SequenceNode): msg = "expected a sequence node, but found %s" % node.id raise yaml.ConstructorError(None, None, msg, node.start_mark) out = LexicalScopeList() for child in node.value: value = self.construct_object(child, deep=deep) if isinstance(value, (LexicalScopeDict, LexicalScopeList)): value.parent = out out.append(value) return out
def make_ordered_mapping(self, node, deep=False): if not isinstance(node, yaml.MappingNode): msg = 'node has to be an instance of yaml.MappingNode but it is {0}' raise yaml.ConstructorError(None, None, msg.format(type(node)), node.start_mark) kv = [(self.construct_object(key, deep=deep), self.construct_object(value, deep=deep)) for key, value in node.value] first_unhashable = next( (key for key, _ in kv if not isinstance(key, collections.Hashable)), None) if first_unhashable is not None: raise yaml.ConstructorError('During the construction of the mapping', node.start_mark, 'found at least one unhashable key', first_unhashable.start_mark) return OrderedDict(kv)