I am currently generating YAML files automatically with some configuration values. Sometimes these values have a comment attached and the comment is added to the YAML file.
Simple example
import sys
from ruamel.yaml import CommentedMap, YAML
top = CommentedMap()
top['sub_key1'] = data = CommentedMap()
data['a'] = 1
data['b'] = 'asdf'
data['c'] = 3.3333
data.yaml_add_eol_comment('comment 1', 'a')
data.yaml_add_eol_comment('comment 2', 'b')
data.yaml_add_eol_comment('comment 3', 'c')
top['sub_key2'] = data = CommentedMap()
data['a'] = 'long text'
data['b'] = 'an even longer text'
data.yaml_add_eol_comment('comment 4', 'a')
data.yaml_add_eol_comment('comment 5', 'b')
YAML().dump(top, sys.stdout)
This works and outputs this as expected
sub_key1:
a: 1 # comment 1
b: asdf # comment 2
c: 3.3333 # comment 3
sub_key2:
a: long text # comment 4
b: an even longer text # comment 5
However I'd really like to have the comments aligned like this (or even better with two spaces after the value).
sub_key1:
a: 1 # comment 1
b: asdf # comment 2
c: 3.3333 # comment 3
sub_key2:
a: long text # comment 4
b: an even longer text # comment 5
I can't use the column parameter when adding the eol comment because the column is absolute from the start and I don't know
- on which level I am
- how long the generated key/value pair is
- what the current indentation i
Is there any way to align the comments after creation?