cdgriffith / cdgriffith/Box

REQ: Support ruamel.yaml's RoundTripLoader/Dumper for retaining comments

Open
#184 2 comments 3 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
Python
Stars
2.8k
Forks
135
PR merge metrics
No merged PRs in 30d

Description

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:

```python
In [1]: from box import Box
In [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: 2
zlast: 1
```
Unordered, and no comments! However `ruamel.yaml` has `RoundTripLoader` and `RoundTripDumper` available!
```python
In [8]: import ruamel.yaml as yaml
In [9]: data = yaml.load(yaml_doc, Loader=yaml.RoundTripLoader)
In [10]: print(yaml.dump(data, Dumper=yaml.RoundTripDumper))
# This is my YAML doc
zlast: 1 # This is a line comment on zlast
afirst: 2 # This should be shown second, not first!
```
But using them with Box only helps resolve the creation order issue -- the comments are gone:
```python
In [11]: mybox = Box.from_yaml(yaml_doc, Loader=yaml.RoundTripLoader)
In [11]: print(mybox.to_yaml(Dumper=yaml.RoundTripDumper))
zlast: 1
afirst: 2
```

Naturally -- as the incoming `data` is unrolled into a Box in the constructor:
https://github.com/cdgriffith/Box/blob/4a505124686c1f0b79ea84bb292aec331ee70f51/box/box.py#L223

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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.