简体   繁体   English

如何在使用 ruamel.yaml 加载和转储时使用 yamllint 强制注释位置有效?

[英]How to force the comment position to be valid with yamllint when load and dump with ruamel.yaml?

I would like to be able to "reposition" existing comments in a yaml document so that they are valid when running yamllint on the output produced, the default configuration being min-spaces-from-content=2 (ref: https://yamllint.readthedocs.io/en/stable/rules.html#module-yamllint.rules.comments ).我希望能够在 yaml 文档中“重新定位”现有注释,以便在对生成的输出运行yamllint时它们是有效的,默认配置为min-spaces-from-content=2 (参考: https://yamllint .readthedocs.io/en/stable/rules.html#module-yamllint.rules.comments )。

According to How can I add a comment with ruamel.yaml , using something like yaml_add_eol_comment('the comment', 'the key', column=None) should do the trick, but that's not the result I've got so far.根据How can I add a comment with ruamel.yaml ,使用类似yaml_add_eol_comment('the comment', 'the key', column=None)应该可以解决问题,但这不是我到目前为止得到的结果。

Here is a piece of code (using ruamel.yaml in version 0.16.7 ) I wrote to demonstrate the current behaviour I'm having :这里是(使用一段代码ruamel.yaml版本0.16.7 )我写证明我在当前的行为:

"""Play with comments."""
from __future__ import print_function
import sys
import ruamel.yaml

yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
inp = """\
---
list-of-maps:
  - part_no:   A4786 # comment 1
    part_henon: mouhaha    # you're not funny
  - part_yes: A21 # also a comment here
    part_iculier: partenaire # I don't always understand how it works
    part_third: key # komment
list-only:
  - first # comment 2
  - third # I have a comment too
  - second # what?
simple-map:
  what-are-you-waiting-for: christmas? # duke nukem rulez
  jingle: bels # not christmas yet
map-of-maps:
  key:
    another-sub-key: w00t # casimir
    sub-key: sub-value # comment 3
    my-sub-key-name-is-longuer-than-yours: 1 # sentinel vs superman
"""

data = yaml.load(inp)


def process_comments(data, column=None):
    if isinstance(data, dict):
        if data.ca:
            if data.ca.items:
                for key in data.ca.items.keys():
                    if data.ca.items[key][2]:
                        comment = data.ca.items[key][2].value.replace("\n", "")
                        data.yaml_add_eol_comment(comment, key, column=column)
        for k, v in data.items():
            process_comments(k, column=column)
            process_comments(v, column=column)
    elif isinstance(data, list):
        if data.ca:
            if data.ca.items:
                for key in data.ca.items.keys():
                    if data.ca.items[key][0]:
                        comment = data.ca.items[key][0].value.replace("\n", "")
                        data.yaml_add_eol_comment(comment, key, column=column)
        for elem in data:
            process_comments(elem, column=column)


process_comments(data, column=None)
yaml.dump(data, sys.stdout)

The expected output is :预期的输出是:

list-of-maps:
  - part_no: A4786  # comment 1
    part_henon: mouhaha  # you're not funny
  - part_yes: A21  # also a comment here
    part_iculier: partenaire  # I don't always understand how it works
    part_third: key  # komment
list-only:
  - first  # comment 2
  - third  # I have a comment too
  - second  # what?
simple-map:
  what-are-you-waiting-for: christmas?  # duke nukem rulez
  jingle: bels  # not christmas yet
map-of-maps:
  key:
    another-sub-key: w00t  # casimir
    sub-key: sub-value  # comment 3
    my-sub-key-name-is-longuer-than-yours: 1  # sentinel vs superman

The actual output is :实际输出是:

list-of-maps:
  - part_no: A4786  # comment 1
    part_henon: mouhaha # you're not funny
  - part_yes: A21  # also a comment here
    part_iculier: partenaire # I don't always understand how it works
    part_third: key # komment
list-only:
  - first # comment 2
  - third # I have a comment too
  - second # what?
simple-map:
  what-are-you-waiting-for: christmas?  # duke nukem rulez
  jingle: bels # not christmas yet
map-of-maps:
  key:
    another-sub-key: w00t  # casimir
    sub-key: sub-value # comment 3
    my-sub-key-name-is-longuer-than-yours: 1 # sentinel vs superman

So it seems that :所以似乎:

  • for CommentedMaps, the comment for the first key gets two spaces before the #, but not the other comments associated to the other keys of the CommentedMap对于 CommentedMaps,第一个键的注释在 # 前有两个空格,但与 CommentedMap 的其他键关联的其他注释没有
  • for CommentedSeqs, there is always a single space before the #对于 CommentedSeqs,# 前总是有一个空格

Am I missing something?我错过了什么吗?

Additionnal informations :附加信息:

By the way, a simpler example gives the same output/behaviour :顺便说一下,一个更简单的例子给出了相同的输出/行为:

"""Test comments on a very simple CommentedMap."""
import sys
import ruamel.yaml


comment_column = None
insert = ruamel.yaml.comments.CommentedMap()
insert["test"] = "asdf"
insert.yaml_add_eol_comment("Test Comment!", "test", column=comment_column)
insert["second-key"] = "yop"
insert.yaml_add_eol_comment("Another comment", "second-key", column=comment_column)

yaml = ruamel.yaml.YAML()
yaml.dump(insert, sys.stdout)

outputs :输出:

test: asdf  # Test Comment!
second-key: yop # Another comment

Disclaimers :免责声明:

  • thank you very much to the author of ruamel.yaml , it's an awesome lib ... and thank you for watching after SO questions about the lib非常感谢ruamel.yaml的作者,它是一个很棒的库……感谢您观看有关库的 SO 问题
  • I am not pretending to be a good python developer at all so please forgive me if my code is not of a good quality我根本没有假装是一个优秀的 Python 开发人员,所以如果我的代码质量不好,请原谅我

That you get two spaces on the first comment of a mapping is a bug.在映射的第一个注释上有两个空格是一个错误。 If you don't specify a column the column is sort of guessed based on the preceding key.如果您不指定列,则该列会根据前面的键进行猜测。 For the first key-value pair in a mapping that is not available, and that results in a slightly different code path.对于不可用的映射中的第一个键值对,这会导致代码路径略有不同。

Fixing that bug will not help you, you'll have to provide your own yaml_add_eol_comment that always adds an extra space:修复该错误对您没有帮助,您必须提供自己的yaml_add_eol_comment ,它总是会增加一个额外的空间:

import sys
import ruamel.yaml


yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
inp = """
list-of-maps:
  - part_no: A4786 # comment 1
    part_henon: mouhaha    # you're not funny
  - part_yes: A21 # also a comment here
    part_iculier: partenaire # I don't always understand how it works
    part_third: key # komment
list-only:
  - first # comment 2
  - third # I have a comment too
  - second # what?
simple-map:
  what-are-you-waiting-for: christmas? # duke nukem rulez
  jingle: bels # not christmas yet
map-of-maps:
  key:
    another-sub-key: w00t # casimir
    sub-key: sub-value # comment 3
    my-sub-key-name-is-longuer-than-yours: 1 # sentinel vs superman
"""

data = yaml.load(inp)


def my_add_eol_comment(self, comment, key=ruamel.yaml.comments.NoComment, column=None):
    org_col = column
    if column is None:
        try:
            column = self._yaml_get_column(key)
        except AttributeError:
            column = 0
    if comment[0] != '#':
        comment = '# ' + comment
    if org_col != 0:  # only do this if the specified colunn is not the beginning of the line
        if comment[0] == '#':
            comment = ' ' + comment
            column = 0
    start_mark = ruamel.yaml.error.CommentMark(column)
    ct = [ruamel.yaml.tokens.CommentToken(comment, start_mark, None), None]
    self._yaml_add_eol_comment(ct, key=key)

ruamel.yaml.comments.CommentedBase.yaml_add_eol_comment = my_add_eol_comment


def process_comments(data, column=None):
    if isinstance(data, dict):
        if data.ca:
            if data.ca.items:
                for key in data.ca.items.keys():
                    if data.ca.items[key][2]:
                        comment = data.ca.items[key][2].value.replace("\n", "")
                        data.yaml_add_eol_comment(comment, key, column=column)
        for k, v in data.items():
            process_comments(k, column=column)
            process_comments(v, column=column)
    elif isinstance(data, list):
        if data.ca:
            if data.ca.items:
                for key in data.ca.items.keys():
                    if data.ca.items[key][0]:
                        comment = data.ca.items[key][0].value.replace("\n", "")
                        data.yaml_add_eol_comment(comment, key, column=column)
        for elem in data:
            process_comments(elem, column=column)

process_comments(data, column=None)
yaml.dump(data, sys.stdout)

which gives:这使:

list-of-maps:
  - part_no: A4786  # comment 1
    part_henon: mouhaha  # you're not funny
  - part_yes: A21  # also a comment here
    part_iculier: partenaire  # I don't always understand how it works
    part_third: key  # komment
list-only:
  - first  # comment 2
  - third  # I have a comment too
  - second  # what?
simple-map:
  what-are-you-waiting-for: christmas?  # duke nukem rulez
  jingle: bels  # not christmas yet
map-of-maps:
  key:
    another-sub-key: w00t  # casimir
    sub-key: sub-value  # comment 3
    my-sub-key-name-is-longuer-than-yours: 1  # sentinel vs superman

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM