You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Now that the default YAML library is ruamel.yaml, and its notable feature is round trip comment preservation, my expectation is that this should work:
In [1]: fromboximportBoxIn [2]: yaml_doc="""--- ...: # This is my YAML doc ...: zlast: 1 # This is a line comment on zlast ...: afirst: 2 # This should be shown second, not first!"""In [3]: mybox=Box.from_yaml(yaml_doc)
In [4]: print(mybox.to_yaml())
afirst: 2zlast: 1
Unordered, and no comments! However ruamel.yaml has RoundTripLoader and RoundTripDumper available!
In [8]: importruamel.yamlasyamlIn [9]: data=yaml.load(yaml_doc, Loader=yaml.RoundTripLoader)
In [10]: print(yaml.dump(data, Dumper=yaml.RoundTripDumper))
# This is my YAML doczlast: 1# This is a line comment on zlastafirst: 2# This should be shown second, not first!
But using them with Box only helps resolve the creation order issue -- the comments are gone:
In [11]: mybox=Box.from_yaml(yaml_doc, Loader=yaml.RoundTripLoader)
In [11]: print(mybox.to_yaml(Dumper=yaml.RoundTripDumper))
zlast: 1afirst: 2
Naturally -- as the incoming data is unrolled into a Box in the constructor:
I'm not sure if there's reasonable way for Box to coexist with the ruamel.yaml.CommentedMap, but it would be VERY convenient to work with my YAML files in box dot notation, while leaving the human readable comments intact.
The text was updated successfully, but these errors were encountered:
This would be great! In the mean time, is there a work-around I could do? Is it somehow possible to use Box's ingenious dotted nested access to update something in a YAML object without losing the comments?
I am not opposed to this feature, and would be really cool, but is also something I sadly do not have time to work on myself.
A fun hurdle about it would be that PyYAML still has to be supported as well (not looking to have the same comment saving feature, just that it can't break PyYAML users).
Looking at the ruamel.yaml code quickly some it seems that the comments are stored under .ca of the object, but looks like just doing a copy of that into a new class isn't enough for the loader. Someone would have to go through and see what parts of the CommentedMap need to be replicated for the round trip loader to work as expected. https://sourceforge.net/p/ruamel-yaml/code/ci/default/tree/comments.py
Now that the default YAML library is
ruamel.yaml
, and its notable feature is round trip comment preservation, my expectation is that this should work:Unordered, and no comments! However
ruamel.yaml
hasRoundTripLoader
andRoundTripDumper
available!But using them with Box only helps resolve the creation order issue -- the comments are gone:
Naturally -- as the incoming
data
is unrolled into a Box in the constructor:Box/box/box.py
Line 223 in 4a50512
I'm not sure if there's reasonable way for Box to coexist with the
ruamel.yaml.CommentedMap
, but it would be VERY convenient to work with my YAML files in box dot notation, while leaving the human readable comments intact.The text was updated successfully, but these errors were encountered: