Preserving relative ordering of merge key and explicit keys with ruamel

时间:2019-01-15 18:30:11

标签: python yaml ruamel.yaml

I'm hoping to use ruamel.yaml to perform some automatic edits against a large human-edited YAML file.

The input file contains merge keys, like so:

foo: &foo
  color: red

bar:
  name: qux
  <<: *foo

If possible, I'd like to preserve the relative ordering of the explicit name key and the << merge key, but it looks like ruamel really wants the merge key to come first. Here's what I get when I round-trip this YAML through ruamel:

foo: &foo
  color: red

bar:
  <<: *foo
  name: qux

Is there any way to tell ruamel to preserve the position of the merge key within this block?

1 个答案:

答案 0 :(得分:1)

通过调整表示器中的两行来映射此 可以固定,合并键的位置信息在那里, 它只是不被使用。不幸的是,这是一个相当大的功能 需要一些进口:

import sys
import ruamel.yaml

if ruamel.yaml.version_info < (0, 15, 86):
    from ruamel.yaml.nodes import MappingNode, ScalarNode
    from ruamel.yaml.comments import comment_attrib, merge_attrib

    def represent_mapping(self, tag, mapping, flow_style=None):
        value = []
        try:
            flow_style = mapping.fa.flow_style(flow_style)
        except AttributeError:
            flow_style = flow_style
        try:
            anchor = mapping.yaml_anchor()
        except AttributeError:
            anchor = None
        node = MappingNode(tag, value, flow_style=flow_style, anchor=anchor)
        if self.alias_key is not None:
            self.represented_objects[self.alias_key] = node
        best_style = True
        # no sorting! !!
        try:
            comment = getattr(mapping, comment_attrib)
            node.comment = comment.comment
            if node.comment and node.comment[1]:
                for ct in node.comment[1]:
                    ct.reset()
            item_comments = comment.items
            for v in item_comments.values():
                if v and v[1]:
                    for ct in v[1]:
                        ct.reset()
            try:
                node.comment.append(comment.end)
            except AttributeError:
                pass
        except AttributeError:
            item_comments = {}
        merge_list = [m[1] for m in getattr(mapping, merge_attrib, [])]
        merge_pos = getattr(mapping, merge_attrib, [[0]])[0][0]          # <<<<<<<< line added
        item_count = 0
        if bool(merge_list):
            items = mapping.non_merged_items()
        else:
            items = mapping.items()
        for item_key, item_value in items:
            item_count += 1
            node_key = self.represent_key(item_key)
            node_value = self.represent_data(item_value)
            item_comment = item_comments.get(item_key)
            if item_comment:
                assert getattr(node_key, 'comment', None) is None
                node_key.comment = item_comment[:2]
                nvc = getattr(node_value, 'comment', None)
                if nvc is not None:  # end comment already there
                    nvc[0] = item_comment[2]
                    nvc[1] = item_comment[3]
                else:
                    node_value.comment = item_comment[2:]
            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 ((item_count != 0) or bool(merge_list)) and self.default_flow_style is not None:
                node.flow_style = self.default_flow_style
            else:
                node.flow_style = best_style
        if bool(merge_list):
            # because of the call to represent_data here, the anchors
            # are marked as being used and thereby created
            if len(merge_list) == 1:
                arg = self.represent_data(merge_list[0])
            else:
                arg = self.represent_data(merge_list)
                arg.flow_style = True
            value.insert(merge_pos, (ScalarNode(u'tag:yaml.org,2002:merge', '<<'), arg))   # <<<<< line changed
        return node

    ruamel.yaml.representer.RoundTripRepresenter.represent_mapping = represent_mapping



yaml_str = """\
foo: &foo
  color: red

bar:
  name: qux
  <<: *foo
"""

yaml = ruamel.yaml.YAML()
data = yaml.load(yaml_str)
yaml.dump(data, sys.stdout)

给出:

foo: &foo
  color: red

bar:
  name: qux
  <<: *foo

以上内容尝试保持绝对位置,而不删除 或将键值对插入到帐户中。

在使用下一版本的时,以上内容不会修补任何内容 ruamel.yaml,其中将包含这些更改。