diff --git a/kiwi_scp/config.py b/kiwi_scp/config.py index 3d79f0b..e4bf1b9 100644 --- a/kiwi_scp/config.py +++ b/kiwi_scp/config.py @@ -5,10 +5,9 @@ from pathlib import Path from typing import Optional, Dict, List, Any, TextIO from pydantic import BaseModel, constr, root_validator, validator -from ruamel.yaml import YAML from ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME -from .misc import _format_kiwi_yml +from .misc import YAML, _format_kiwi_yml class StorageConfig(BaseModel): @@ -191,9 +190,7 @@ class KiwiConfig(BaseModel): def dump_kiwi_yml(self, stream: TextIO) -> None: """dump a kiwi.yml file""" - yml = YAML() - yml.indent(offset=2) - yml.dump(self.kiwi_dict, stream=stream, transform=_format_kiwi_yml) + YAML().dump(self.kiwi_dict, stream=stream, transform=_format_kiwi_yml) @property def kiwi_yml(self) -> str: diff --git a/kiwi_scp/instance.py b/kiwi_scp/instance.py index c822133..bcc2c6f 100644 --- a/kiwi_scp/instance.py +++ b/kiwi_scp/instance.py @@ -5,10 +5,10 @@ from typing import List, Dict, Any, Generator import attr import click -from ruamel.yaml import YAML from ._constants import COMPOSE_FILE_NAME -from .config import KiwiConfig, ProjectConfig +from .config import KiwiConfig +from .misc import YAML _RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE) @@ -52,8 +52,7 @@ class Instance: @functools.lru_cache(maxsize=10) def _parse_compose_file(cls, directory: Path): with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf: - yml = YAML() - return yml.load(cf) + return YAML().load(cf) def get_services(self, project_name: str) -> Generator[Service, None, None]: yml = Instance._parse_compose_file(self.directory.joinpath(project_name)) diff --git a/kiwi_scp/misc.py b/kiwi_scp/misc.py index 845acf1..72d5d8c 100644 --- a/kiwi_scp/misc.py +++ b/kiwi_scp/misc.py @@ -3,6 +3,7 @@ from typing import Any, Type, List, Callable import attr import click +import ruamel.yaml from click.decorators import FC from ._constants import HEADER_KIWI_CONF_NAME @@ -54,6 +55,12 @@ def user_query(description: str, default: Any, cast_to: Type[Any] = str): click.echo(f"Invalid input: {e}") +class YAML(ruamel.yaml.YAML): + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.indent(offset=2) + + def _format_kiwi_yml(yml_string: str): # insert newline before every main key yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE) diff --git a/tests/test_config.py b/tests/test_config.py index 26fdd1d..1d09e05 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -3,10 +3,10 @@ from ipaddress import IPv4Network from pathlib import Path import pytest -import ruamel.yaml from pydantic import ValidationError from kiwi_scp.config import KiwiConfig +from kiwi_scp.misc import YAML class UnCoercible: @@ -45,12 +45,9 @@ class TestDefault: } assert c.kiwi_dict == kiwi_dict - yml = ruamel.yaml.YAML() - yml.indent(offset=2) - sio = io.StringIO() from kiwi_scp.misc import _format_kiwi_yml - yml.dump(kiwi_dict, stream=sio, transform=_format_kiwi_yml) + YAML(typ="safe").dump(kiwi_dict, stream=sio, transform=_format_kiwi_yml) yml_string = sio.getvalue() sio.close()