1
0
Fork 0
mirror of https://github.com/yavook/kiwi-scp.git synced 2024-11-24 21:52:59 +00:00

Added kiwi_scp.misc.YAML

This commit is contained in:
Jörn-Michael Miehe 2021-10-27 13:33:26 +02:00
parent b8027777d9
commit c331e77060
4 changed files with 14 additions and 14 deletions

View file

@ -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:

View file

@ -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))

View file

@ -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)

View file

@ -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()