Skip to content

Commit

Permalink
Switched from ruamel.yaml to PyYAML
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Feb 26, 2024
1 parent 774a733 commit 52ce6a6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Expand Up @@ -30,3 +30,4 @@ repos:
additional_dependencies:
- anyio
- pytest
- types-PyYAML
1 change: 1 addition & 0 deletions docs/versionhistory.rst
Expand Up @@ -45,6 +45,7 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
- Added the ``start_background_task()`` Context method (with a free-function variant
for convenience) that handles startup and shutdown of context-bound background tasks
for various purposes
- Switched from ruamel.yaml to PyYAML as the backing YAML library
- Dropped support for Python 3.7

**4.12.0**
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Expand Up @@ -34,7 +34,7 @@ dependencies = [
"importlib_metadata >= 4.4; python_version < '3.10'",
"typing_extensions; python_version < '3.10'",
"exceptiongroup >= 1.2.0",
"ruamel.yaml >= 0.15",
"pyyaml ~= 6.0",
"click >= 6.6"
]
dynamic = ["version"]
Expand Down
31 changes: 16 additions & 15 deletions src/asphalt/core/_cli.py
Expand Up @@ -8,26 +8,32 @@

import anyio
import click
from ruamel.yaml import YAML, ScalarNode
from ruamel.yaml.loader import Loader
import yaml
from yaml import Loader, ScalarNode

from ._runner import run_application
from ._utils import merge_config, qualified_name


def env_constructor(loader: Loader, node: ScalarNode) -> str | None:
value = loader.construct_scalar(node)
return os.getenv(value)
return os.getenv(node.value)


def text_file_constructor(loader: Loader, node: ScalarNode) -> str:
value = loader.construct_scalar(node)
return Path(value).read_text()
return Path(node.value).read_text()


def binary_file_constructor(loader: Loader, node: ScalarNode) -> bytes:
value = loader.construct_scalar(node)
return Path(value).read_bytes()
return Path(node.value).read_bytes()


class AsphaltLoader(Loader):
pass


AsphaltLoader.add_constructor("!Env", env_constructor)
AsphaltLoader.add_constructor("!TextFile", text_file_constructor)
AsphaltLoader.add_constructor("!BinaryFile", binary_file_constructor)


@click.group()
Expand Down Expand Up @@ -56,15 +62,10 @@ def main() -> None:
help="set configuration",
)
def run(configfile, service: str | None, set_: list[str]) -> None:
yaml = YAML()
yaml.constructor.add_constructor("!Env", env_constructor)
yaml.constructor.add_constructor("!TextFile", text_file_constructor)
yaml.constructor.add_constructor("!BinaryFile", binary_file_constructor)

# Read the configuration from the supplied YAML files
config: dict[str, Any] = {}
for path in configfile:
config_data = yaml.load(path)
config_data = yaml.load(path, AsphaltLoader)
assert isinstance(
config_data, dict
), "the document root element must be a dictionary"
Expand All @@ -78,7 +79,7 @@ def run(configfile, service: str | None, set_: list[str]) -> None:
)

key, value = override.split("=", 1)
parsed_value = yaml.load(value)
parsed_value = yaml.load(value, AsphaltLoader)
keys = [k.replace(r"\.", ".") for k in re.split(r"(?<!\\)\.", key)]
section = config
for i, part_key in enumerate(keys[:-1]):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cli.py
Expand Up @@ -4,8 +4,8 @@
from unittest.mock import patch

import pytest
from _pytest.monkeypatch import MonkeyPatch
from click.testing import CliRunner
from pytest import MonkeyPatch

from asphalt.core import Component, _cli

Expand Down

0 comments on commit 52ce6a6

Please sign in to comment.