mirror of
https://github.com/yavook/kiwi-scp.git
synced 2024-11-21 20:33:00 +00:00
Added kiwi_scp.misc.YAML
This commit is contained in:
parent
b8027777d9
commit
c331e77060
4 changed files with 14 additions and 14 deletions
|
@ -5,10 +5,9 @@ from pathlib import Path
|
||||||
from typing import Optional, Dict, List, Any, TextIO
|
from typing import Optional, Dict, List, Any, TextIO
|
||||||
|
|
||||||
from pydantic import BaseModel, constr, root_validator, validator
|
from pydantic import BaseModel, constr, root_validator, validator
|
||||||
from ruamel.yaml import YAML
|
|
||||||
|
|
||||||
from ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME
|
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):
|
class StorageConfig(BaseModel):
|
||||||
|
@ -191,9 +190,7 @@ class KiwiConfig(BaseModel):
|
||||||
def dump_kiwi_yml(self, stream: TextIO) -> None:
|
def dump_kiwi_yml(self, stream: TextIO) -> None:
|
||||||
"""dump a kiwi.yml file"""
|
"""dump a kiwi.yml file"""
|
||||||
|
|
||||||
yml = YAML()
|
YAML().dump(self.kiwi_dict, stream=stream, transform=_format_kiwi_yml)
|
||||||
yml.indent(offset=2)
|
|
||||||
yml.dump(self.kiwi_dict, stream=stream, transform=_format_kiwi_yml)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def kiwi_yml(self) -> str:
|
def kiwi_yml(self) -> str:
|
||||||
|
|
|
@ -5,10 +5,10 @@ from typing import List, Dict, Any, Generator
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import click
|
import click
|
||||||
from ruamel.yaml import YAML
|
|
||||||
|
|
||||||
from ._constants import COMPOSE_FILE_NAME
|
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)
|
_RE_CONFDIR = re.compile(r"^\s*\$(?:CONFDIR|{CONFDIR})/+(.*)$", flags=re.UNICODE)
|
||||||
|
|
||||||
|
@ -52,8 +52,7 @@ class Instance:
|
||||||
@functools.lru_cache(maxsize=10)
|
@functools.lru_cache(maxsize=10)
|
||||||
def _parse_compose_file(cls, directory: Path):
|
def _parse_compose_file(cls, directory: Path):
|
||||||
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
|
with open(directory.joinpath(COMPOSE_FILE_NAME), "r") as cf:
|
||||||
yml = YAML()
|
return YAML().load(cf)
|
||||||
return yml.load(cf)
|
|
||||||
|
|
||||||
def get_services(self, project_name: str) -> Generator[Service, None, None]:
|
def get_services(self, project_name: str) -> Generator[Service, None, None]:
|
||||||
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
|
yml = Instance._parse_compose_file(self.directory.joinpath(project_name))
|
||||||
|
|
|
@ -3,6 +3,7 @@ from typing import Any, Type, List, Callable
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import click
|
import click
|
||||||
|
import ruamel.yaml
|
||||||
from click.decorators import FC
|
from click.decorators import FC
|
||||||
|
|
||||||
from ._constants import HEADER_KIWI_CONF_NAME
|
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}")
|
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):
|
def _format_kiwi_yml(yml_string: str):
|
||||||
# insert newline before every main key
|
# insert newline before every main key
|
||||||
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
yml_string = re.sub(r'^(\S)', r'\n\1', yml_string, flags=re.MULTILINE)
|
||||||
|
|
|
@ -3,10 +3,10 @@ from ipaddress import IPv4Network
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
import ruamel.yaml
|
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from kiwi_scp.config import KiwiConfig
|
from kiwi_scp.config import KiwiConfig
|
||||||
|
from kiwi_scp.misc import YAML
|
||||||
|
|
||||||
|
|
||||||
class UnCoercible:
|
class UnCoercible:
|
||||||
|
@ -45,12 +45,9 @@ class TestDefault:
|
||||||
}
|
}
|
||||||
assert c.kiwi_dict == kiwi_dict
|
assert c.kiwi_dict == kiwi_dict
|
||||||
|
|
||||||
yml = ruamel.yaml.YAML()
|
|
||||||
yml.indent(offset=2)
|
|
||||||
|
|
||||||
sio = io.StringIO()
|
sio = io.StringIO()
|
||||||
from kiwi_scp.misc import _format_kiwi_yml
|
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()
|
yml_string = sio.getvalue()
|
||||||
sio.close()
|
sio.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue