Open timmeinhardt opened 6 years ago
That would be pretty cool, I agree. Unfortunately, it seems like PyYAML discards all comments. It seems like ruamel might be able to handle comments, but I currently don't have time to check. If you figure out a way to load/save YAML with comments, then I'd be willing to extend config file handling to include them.
I just checked out the ruamel package and it indeed supports loading/dumping YAML comments. Here is a small example of how to load a YAML configuration and access its comments:
from ruamel.yaml import YAML
# could also be loaded from a file with `open("config.yaml")`
yaml_string = """\
# example
name:
# details
family: Smith # very common
given: Alice # one of the siblings
"""
yaml = YAML()
code = yaml.load(yaml_string)
code['name']['given'] = 'Bob'
print(code.ca)
print(code.ca.items['name'][3][0].value)
print(code["name"].ca)
Output:
Comment(comment=[None, [CommentToken('# example\n', line: 0)]],
items={'name': [None, None, None, [CommentToken('# details\n', line: 2)]]})
# details
Comment(comment=[None, [CommentToken('# details\n', line: 2)]],
items={'family': [None, None, CommentToken('# very common\n', line: 3), None], 'given': [None, None, CommentToken('# one of the siblings\n', line: 4), None]})
The regular YAML entries are accessed as nested directories and the corresponding comments have to be retrieved with the ca
attribute. Each CommentToken
can be in either of 4 different places depending on the position of the comment in the YAML file.
More details on how to use the comments can be found here. Unfortunately the general documentation is not very thorough.
Is there a way to print the comments in a YAML file when executing the
print_config
command? Currently this YAML file:will produce an output like this:
Which unfortunately omits the comments.