def compose_sequence_node(self, anchor): # type: (Any) -> Any start_event = self.parser.get_event() tag = start_event.tag if tag is None or tag == u'!': tag = self.resolver.resolve(SequenceNode, None, start_event.implicit) node = SequenceNode(tag, [], start_event.start_mark, None, flow_style=start_event.flow_style, comment=start_event.comment, anchor=anchor) if anchor is not None: self.anchors[anchor] = node index = 0 while not self.parser.check_event(SequenceEndEvent): node.value.append(self.compose_node(node, index)) index += 1 end_event = self.parser.get_event() if node.flow_style is True and end_event.comment is not None: if node.comment is not None: print('Warning: unexpected end_event commment in sequence ' 'node {}'.format(node.flow_style)) node.comment = end_event.comment node.end_mark = end_event.end_mark self.check_end_doc_comment(end_event, node) return node
def represent_sequence_of_pairs(self, tag, sequence): value = [] for item_key, item_value in sequence: item_node = self.represent_mapping( "tag:yaml.org,2002:map", [(item_key, item_value)], ) value.append(item_node) return SequenceNode(tag, value, flow_style=False)
def revert_mappings(node): r""" Undo the effect of simplify_mappings. """ from ruamel.yaml.nodes import SequenceNode, MappingNode, ScalarNode if isinstance(node, MappingNode): if node.tag.startswith(YAML_TAG_REWRITE_MAP): tag = node.tag[len(YAML_TAG_REWRITE_MAP ):] or "tag:yaml.org,2002:seq" node = SequenceNode( tag=tag, value=[ MappingNode(tag="tag:yaml.org,2002:map", value=[ (ScalarNode(tag="tag:yaml.org,2002:str", value="key", start_mark=key.start_mark, end_mark=key.end_mark), revert_mappings(key)), (ScalarNode(tag="tag:yaml.org,2002:str", value="value", start_mark=value.start_mark, end_mark=value.end_mark), revert_mappings(value)), ], start_mark=key.start_mark, end_mark=value.end_mark) for (key, value) in node.value ], start_mark=node.start_mark, end_mark=node.end_mark, comment=node.comment, anchor=node.anchor) else: node.value = [(revert_mappings(key), revert_mappings(value)) for (key, value) in node.value] elif isinstance(node, SequenceNode): node.value = [revert_mappings(value) for value in node.value] return node
def compose_sequence_node(self, anchor): start_event = self.get_event() tag = start_event.tag if tag is None or tag == u'!': tag = self.resolve(SequenceNode, None, start_event.implicit) node = SequenceNode(tag, [], start_event.start_mark, None, flow_style=start_event.flow_style, comment=start_event.comment, anchor=anchor) if anchor is not None: self.anchors[anchor] = node index = 0 while not self.check_event(SequenceEndEvent): node.value.append(self.compose_node(node, index)) index += 1 end_event = self.get_event() if node.flow_style is True and end_event.comment is not None: if node.comment is not None: print('Warning: unexpected end_event commment in sequence ' 'node {}'.format(node.flow_style)) node.comment = end_event.comment node.end_mark = end_event.end_mark self.check_end_doc_comment(end_event, node) return node
def flatten_mapping(node): if not isinstance(node, MappingNode): return by_key = {} for i, (k, v) in enumerate(node.value): by_key.setdefault(k.value, []).append((i, v)) to_remove = [] for k, vs in by_key.items(): if len(vs) == 1: continue if k in SEQUENCE_FIELDS: if not isinstance(vs[0], SequenceNode): node.value[vs[0][0]] = ( node.value[vs[0][0]][0], SequenceNode( tag='tag:yaml.org,2002:seq', value=[vs[0][1]], start_mark=node.value[vs[0][0]][1].start_mark, end_mark=node.value[vs[0][0]][1].end_mark)) primary = node.value[vs[0][0]][1] for i, v in vs[1:]: if isinstance(v, SequenceNode): primary.value.extend(v.value) elif isinstance(v, MappingNode): primary.value.append(v) else: primary.value.append(v) to_remove.append((i, k)) else: # Preserve the first value. # TODO(jelmer): Make a more informed choice. for (i, v) in vs[1:]: to_remove.append((i, k)) if not to_remove: return for i, k in sorted(to_remove, reverse=True): editor.force_rewrite() del node.value[i] fixed_lintian_tag('source', 'upstream-metadata-yaml-invalid') report_result('Remove duplicate values for fields %s ' 'in debian/upstream/metadata.' % ', '.join([k for (i, k) in sorted(to_remove)]))