misc.YAML.dump* methods
This commit is contained in:
parent
34988e5894
commit
2a8764577f
3 changed files with 27 additions and 25 deletions
|
@ -7,7 +7,7 @@ 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 ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME
|
from ._constants import RE_SEMVER, RE_VARNAME, KIWI_CONF_NAME
|
||||||
from .misc import YAML, _format_kiwi_yml
|
from .misc import YAML
|
||||||
|
|
||||||
|
|
||||||
class StorageConfig(BaseModel):
|
class StorageConfig(BaseModel):
|
||||||
|
@ -187,21 +187,16 @@ class KiwiConfig(BaseModel):
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def dump_kiwi_yml(self, stream: TextIO) -> None:
|
def dump_kiwi_yml(self, stream: TextIO = None) -> Optional[str]:
|
||||||
"""dump a kiwi.yml file"""
|
"""dump a kiwi.yml file"""
|
||||||
|
|
||||||
YAML().dump(self.kiwi_dict, stream=stream, transform=_format_kiwi_yml)
|
return YAML().dump_kiwi_yml(self.kiwi_dict, stream=stream)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def kiwi_yml(self) -> str:
|
def kiwi_yml(self) -> str:
|
||||||
"""get a kiwi.yml dump as a string"""
|
"""get a kiwi.yml dump as a string"""
|
||||||
|
|
||||||
sio = io.StringIO()
|
return self.dump_kiwi_yml()
|
||||||
self.dump_kiwi_yml(sio)
|
|
||||||
result: str = sio.getvalue()
|
|
||||||
sio.close()
|
|
||||||
|
|
||||||
return result
|
|
||||||
|
|
||||||
@validator("shells", pre=True)
|
@validator("shells", pre=True)
|
||||||
@classmethod
|
@classmethod
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import re
|
import re
|
||||||
from typing import Any, Type, List, Callable
|
from typing import Any, Type, List, Callable, Optional
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import click
|
import click
|
||||||
import ruamel.yaml
|
import ruamel.yaml
|
||||||
|
import ruamel.yaml.compat
|
||||||
from click.decorators import FC
|
from click.decorators import FC
|
||||||
|
|
||||||
from ._constants import HEADER_KIWI_CONF_NAME
|
from ._constants import HEADER_KIWI_CONF_NAME
|
||||||
|
@ -60,8 +61,18 @@ class YAML(ruamel.yaml.YAML):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
self.indent(offset=2)
|
self.indent(offset=2)
|
||||||
|
|
||||||
|
def dump(self, data, stream=None, **kwargs) -> Optional[str]:
|
||||||
|
into_str: bool = False
|
||||||
|
if stream is None:
|
||||||
|
into_str = True
|
||||||
|
stream = ruamel.yaml.compat.StringIO()
|
||||||
|
|
||||||
def _format_kiwi_yml(yml_string: str):
|
super().dump(data, stream=stream, **kwargs)
|
||||||
|
if into_str:
|
||||||
|
return stream.getvalue()
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
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)
|
||||||
|
|
||||||
|
@ -71,6 +82,9 @@ def _format_kiwi_yml(yml_string: str):
|
||||||
|
|
||||||
return yml_string
|
return yml_string
|
||||||
|
|
||||||
|
def dump_kiwi_yml(self, data, **kwargs) -> Optional[str]:
|
||||||
|
return self.dump(data, transform=YAML._format_kiwi_yml, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _surround(string, bang):
|
def _surround(string, bang):
|
||||||
midlane = f"{bang * 3} {string} {bang * 3}"
|
midlane = f"{bang * 3} {string} {bang * 3}"
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import io
|
|
||||||
from ipaddress import IPv4Network
|
from ipaddress import IPv4Network
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -45,13 +44,7 @@ class TestDefault:
|
||||||
}
|
}
|
||||||
assert c.kiwi_dict == kiwi_dict
|
assert c.kiwi_dict == kiwi_dict
|
||||||
|
|
||||||
sio = io.StringIO()
|
assert c.kiwi_yml == YAML().dump_kiwi_yml(kiwi_dict)
|
||||||
from kiwi_scp.misc import _format_kiwi_yml
|
|
||||||
YAML(typ="safe").dump(kiwi_dict, stream=sio, transform=_format_kiwi_yml)
|
|
||||||
yml_string = sio.getvalue()
|
|
||||||
sio.close()
|
|
||||||
|
|
||||||
assert c.kiwi_yml == yml_string
|
|
||||||
|
|
||||||
|
|
||||||
class TestVersion:
|
class TestVersion:
|
||||||
|
|
Loading…
Reference in a new issue