保留合并键和显式键的相对顺序



我希望使用 ruamel.yaml 对人工编辑的大型 YAML 文件执行一些自动编辑。

输入文件包含合并键,如下所示:

foo: &foo
  color: red
bar:
  name: qux
  <<: *foo

如果可能的话,我想保留显式name键和<<合并键的相对顺序,但看起来 ruamel 真的希望合并键排在第一位。这是我通过ruamel往返这个YAML时得到的:

foo: &foo
  color: red
bar:
  <<: *foo
  name: qux

有没有办法告诉 ruamel 保留合并键在这个块中的位置?

通过调整表示器中的两行来映射此可以修复,合并键的位置信息在那里,它只是没有被使用。不幸的是,这是一个相当大的函数,需要一些导入:

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 ,其中将包括这些更改。

最新更新