Replace PyYAML with ruamel.yaml (#1416)

* Remove Python 3.7 and 3.8 from github actions workflows

* Update lockfile after running poetry update a couple times

* Update poetry lock

* Remove Python 3.7 and 3.8 from pyproject.toml and run poetry lock

* Switch from pyyaml to ruamel.yaml

* Remove duplicate editor key in config

* Use ruamel.yaml instead of pyyaml in legacy template.py

* Prevent ruamel from collapsing config YAML

* Run make format
This commit is contained in:
Micah Jerome Ellison 2022-03-12 13:24:31 -08:00 committed by GitHub
parent e46b5a171f
commit d3de5b778b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 154 additions and 129 deletions

View file

@ -3,7 +3,7 @@ import os
import sys
import colorama
import yaml
from ruamel.yaml import YAML
import xdg.BaseDirectory
from . import __version__
@ -46,7 +46,8 @@ def make_yaml_valid_dict(input: list) -> dict:
# yaml compatible strings are of the form Key:Value
yamlstr = YAML_SEPARATOR.join(input)
runtime_modifications = yaml.load(yamlstr, Loader=yaml.SafeLoader)
runtime_modifications = YAML(typ="safe").load(yamlstr)
return runtime_modifications
@ -54,18 +55,16 @@ def make_yaml_valid_dict(input: list) -> dict:
def save_config(config, alt_config_path=None):
"""Supply alt_config_path if using an alternate config through --config-file."""
config["version"] = __version__
yaml = YAML(typ="safe")
yaml.default_flow_style = False # prevents collapsing of tree structure
with open(
alt_config_path if alt_config_path else get_config_path(),
"w",
encoding=YAML_FILE_ENCODING,
) as f:
yaml.safe_dump(
config,
f,
encoding=YAML_FILE_ENCODING,
allow_unicode=True,
default_flow_style=False,
)
yaml.dump(config, f)
def get_config_path():
@ -160,7 +159,9 @@ def verify_config_colors(config):
def load_config(config_path):
"""Tries to load a config file from YAML."""
with open(config_path, encoding=YAML_FILE_ENCODING) as f:
return yaml.load(f, Loader=yaml.SafeLoader)
yaml = YAML(typ="safe")
yaml.allow_duplicate_keys = True
return yaml.load(f)
def is_config_json(config_path):

View file

@ -3,7 +3,7 @@
import re
import yaml
from ruamel.yaml import YAML
VAR_RE = r"[_a-zA-Z][a-zA-Z0-9_]*"
EXPRESSION_RE = r"[\[\]():.a-zA-Z0-9_]*"
@ -26,7 +26,7 @@ class Template:
def from_file(cls, filename):
with open(filename) as f:
front_matter, body = f.read().strip("-\n").split("---", 2)
front_matter = yaml.load(front_matter, Loader=yaml.SafeLoader)
front_matter = YAML(typ="safe").load(front_matter)
template = cls(body)
template.__dict__.update(front_matter)
return template