Switch from ruamel.yaml to pyyaml (#4555)

* Switch from ruamel.yaml to pyyaml

* Use CLoader and CDumper when available
This commit is contained in:
Mike Degatano 2023-09-13 02:57:01 -04:00 committed by GitHub
parent 0225f574be
commit 2c8e6ca0cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 8 deletions

View File

@ -112,7 +112,7 @@ jobs:
tag: musllinux_1_2
arch: ${{ matrix.arch }}
wheels-key: ${{ secrets.WHEELS_KEY }}
apk: "libffi-dev;openssl-dev"
apk: "libffi-dev;openssl-dev;yaml-dev"
skip-binary: aiohttp
env-file: true
requirements: "requirements.txt"

View File

@ -22,6 +22,7 @@ RUN \
libpulse \
musl \
openssl \
yaml \
\
&& curl -Lso /usr/bin/cosign "https://github.com/home-assistant/cosign/releases/download/${COSIGN_VERSION}/cosign_${BUILD_ARCH}" \
&& chmod a+x /usr/bin/cosign

View File

@ -18,7 +18,7 @@ gitpython==3.1.36
jinja2==3.1.2
pulsectl==23.5.2
pyudev==0.24.1
ruamel.yaml==0.17.21
PyYAML==6.0.1
securetar==2023.3.0
sentry-sdk==1.30.0
voluptuous==0.13.1

View File

@ -3,20 +3,23 @@ import logging
from pathlib import Path
from atomicwrites import atomic_write
from ruamel.yaml import YAML, YAMLError
from yaml import YAMLError, dump, load
try:
from yaml import CDumper as Dumper, CSafeLoader as SafeLoader
except ImportError:
from yaml import SafeLoader, Dumper
from ..exceptions import YamlFileError
_YAML = YAML(typ="safe")
_YAML.allow_duplicate_keys = True
_LOGGER: logging.Logger = logging.getLogger(__name__)
def read_yaml_file(path: Path) -> dict:
"""Read YAML file from path."""
try:
return _YAML.load(path) or {}
with open(path, encoding="utf-8") as yaml_file:
return load(yaml_file, Loader=SafeLoader) or {}
except (YAMLError, AttributeError, OSError) as err:
raise YamlFileError(
@ -28,7 +31,7 @@ def write_yaml_file(path: Path, data: dict) -> None:
"""Write a YAML file."""
try:
with atomic_write(path, overwrite=True) as fp:
_YAML.dump(data, fp)
dump(data, fp, Dumper=Dumper)
path.chmod(0o600)
except (YAMLError, OSError, ValueError, TypeError) as err:
raise YamlFileError(f"Can't write {path!s}: {err!s}", _LOGGER.error) from err