I'm trying to add new elements to a YAML file, but I would like that the new elements to keep the visual order, to make the file easier to read for humans. I was bale to keep the order with orderdict, but with this we get the !!omap directive "polluting" the YAML file. I'd like new elements to have the same order has the "yaml_doc" example in the code below.
Is there a way to add new elements to YAML, and keep the order when dumping the file and not showing the omap directive?
Below is the code I have and the output I get:
import sys
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedSeq as cs
from ruamel.yaml.comments import TaggedScalar as ts
from ruamel.yaml.scalarstring import SingleQuotedScalarString as sq
from ruamel.ordereddict import ordereddict
yaml_doc = """\
version: 0
projects:
- name: A1
dir: B1
aplan:
when: ["X", "Y", "Z"]
wflow: l
"""
yaml = YAML()
yaml.preserve_quotes = True
yaml.width = 4096
data = yaml.load(yaml_doc)
ref = data['projects']
record_to_add = dict(name='A2', dir='B2',
aplan=dict(when=["X", "Y", "Z"]), wflow='l')
ref.append(record_to_add)
record_to_add_2 = ordereddict([('name', 'A3'), ('dir', 'B3'), ('aplan', ordereddict(
[('when', ['X', 'Y', 'Z'])])), ('wflow', 'l')])
ref.append(record_to_add_2)
yaml.dump(data, sys.stdout)
Output of the dump
version: 0
projects:
- name: A1
dir: B1
aplan:
when: ["X", "Y", "Z"]
wflow: l
- aplan:
when:
- X
- Y
- Z
wflow: l
name: A2
dir: B2
- !!omap
- name: A3
- dir: B3
- aplan: !!omap
- when:
- X
- Y
- Z
- wflow: l