New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch from pyyaml to ruamel.yaml #3170
Comments
Hello! For what it's worth, If you're curious, here's where the magic happens: Lines 616 to 664 in f6ad98a
Thanks for pointing out |
Thanks for the heads-up. This is mentioned in the documentation (although I had to look for quite a while earlier today until I found it) and I have been using this feature ever since I started using beets. I've also implemented preservation of dict order by myself in some pyyaml-based parser before. By the way, another problem with pyyaml is that it encourages adding parsers or representers for certain tags or Python objects to the global default loaders. |
Cool! Thanks for the extra details. That (avoiding global modifications to the loaders) does sound very attractive—it would be worth investigating a switch. I'm changing the title of this issue to make it about that. |
Actually, it looks like pyyaml is finally getting some traction: yaml/pyyaml#257 (also related yaml/pyyaml#193, CHANGES). Of course, if you were using Initially the issue wasn't about prompting a switch, although I find it weird that pyyaml by default doesn't load In [1]: import yaml
In [3]: yaml.safe_load("""
...: !!omap
...: - this: is a
...: - map: but
...: - with: all keys
...: - in: order
...: """)
Out[3]: [('this', 'is a'), ('map', 'but'), ('with', 'all keys'), ('in', 'order')]
In [4]: from ruamel.yaml import YAML
In [5]: yaml2 = YAML()
In [7]: yaml2.load("""
...: !!omap
...: - this: is a
...: - map: but
...: - with: all keys
...: - in: order
...: """)
Out[7]: CommentedOrderedMap([('this', 'is a'), ('map', 'but'), ('with', 'all keys'), ('in', 'order')]) Also note that roundtrip loading (which preserves formatting and comments) can be disabled if you don't intend on writing the data again. |
Cool; good to know. The lack of global state was what convinced me this was worth looking into, but I'm also intrigued to learn about the round-trip capability. We currently apply a few hacks to fake round-trippability; it would be nice to rely on a real implementation in the library... |
With the current title this looks like a duplicate of #1702 |
Thanks! It does indeed. |
Problem
The config parser does not accept the
!!omap
tag for an ordered mapping.YAML mappings are, by default unordered, so in order to ensure that a mapping is to be parsd and represented in order, one need to use the ordered map format, indicated by
!!omap
and a list of one-elemental mappings. However, this is not supported.Config file:
Via
setup.py
, beets uses pyyaml. I'd like to suggest using ruamel.yaml instead, which is the only YAML 1.2 parser in Python that I know of and has been maintained for a long time. pyyaml has recently switched maintainers, but there are still many inherent problems with the distribution, most notablyyaml.load
being unsafe by default, and the fact it still only parses YAML1.1.This is only a minor inconvenience as the current setup works, although it technically shouldn't be relied on by the YAML spec.
Setup
The text was updated successfully, but these errors were encountered: