refactor of yaml serializor
This commit is contained in:
1 parent
616c8c3b4b
commit
924eea0c0a
9 files changed
+151
-116
No files matched your search
@@ -4,8 +4,7 @@ from typing import Any, Iterator, Tuple, Union
|
|||||||
from src.json_serializer import normalize_obj
|
from src.json_serializer import normalize_obj
|
||||||
from src.logger import get_logger
|
from src.logger import get_logger
|
||||||
from src.utils import extract_files
|
from src.utils import extract_files
|
||||||
from src.yaml_serializer import dump_yaml, load_yaml
|
from src.yaml_serializer import dump_yaml, load_yaml, IncludedYaml, IncludedYamlDir
|
||||||
from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir
|
|
||||||
|
|
||||||
from ..hass_config.loader import HassConfig
|
from ..hass_config.loader import HassConfig
|
||||||
from .errors import InvalidAutomationFile
|
from .errors import InvalidAutomationFile
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import List, Optional
|
from typing import Optional
|
||||||
|
|
||||||
from src.automations.tags import TagManager
|
from src.automations.tags import TagManager
|
||||||
from src.yaml_serializer import dump_yaml, load_yaml
|
from src.yaml_serializer import load_yaml, IncludedYaml
|
||||||
from src.yaml_serializer.types import IncludedYaml
|
|
||||||
|
|
||||||
|
|
||||||
class HassConfig:
|
class HassConfig:
|
||||||
|
|||||||
@@ -1,41 +1,67 @@
|
|||||||
import io
|
from yaml.nodes import Node
|
||||||
from typing import Any, TextIO, Union
|
from yaml.representer import BaseRepresenter
|
||||||
|
|
||||||
import yaml
|
from .manager import YamlManager
|
||||||
|
|
||||||
from src.yaml_serializer.dumper import YamlSafeDumper
|
dump_yaml = YamlManager.dump_yaml
|
||||||
from src.yaml_serializer.loader import YamlSafeLoader
|
load_yaml = YamlManager.load_yaml
|
||||||
|
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
|
|
||||||
def dump_yaml(obj: Any) -> str:
|
@YamlManager.as_constructor("!include")
|
||||||
"""Convert a python object to a hass-compliant yaml string
|
class IncludedYaml(NamedTuple):
|
||||||
|
"""Any time something in the yaml is specified as `include !filepath`
|
||||||
Args:
|
this data type will represent it.
|
||||||
obj (Any)
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
str
|
|
||||||
"""
|
"""
|
||||||
stream = io.StringIO()
|
|
||||||
yaml.dump( # nosec
|
path_str: str # original node path
|
||||||
obj,
|
|
||||||
stream,
|
@classmethod
|
||||||
default_flow_style=False,
|
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
||||||
allow_unicode=True,
|
return representer.represent_scalar("!include", node.path_str)
|
||||||
sort_keys=False,
|
|
||||||
Dumper=YamlSafeDumper,
|
@classmethod
|
||||||
)
|
def from_yaml(cls, constructor: YamlManager.Loader, node: Node):
|
||||||
stream.seek(0)
|
return cls(node.value)
|
||||||
return stream.read()
|
|
||||||
|
|
||||||
|
|
||||||
def load_yaml(yaml_stream: TextIO) -> Union[dict, list]:
|
# TODO: add support for these
|
||||||
"""Load yaml from string using some hass-compliant yaml strings
|
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
||||||
|
|
||||||
Args:
|
|
||||||
yaml_stream (io.StringIO)
|
|
||||||
|
|
||||||
Returns:
|
@YamlManager.as_constructor("!secret")
|
||||||
Union[dict, list]
|
class SecretValue(NamedTuple):
|
||||||
"""
|
name: str
|
||||||
return yaml.load(yaml_stream, Loader=YamlSafeLoader) # nosec
|
value: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_yaml(cls, representer: BaseRepresenter, node: "SecretValue"):
|
||||||
|
return representer.represent_scalar("!secret", node.name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_yaml(cls, _constructor, node: Node):
|
||||||
|
return cls(node.value, "n/a")
|
||||||
|
|
||||||
|
def to_normalized_json(self):
|
||||||
|
return NOT_IMPLEMENTED_SV_MSG
|
||||||
|
|
||||||
|
|
||||||
|
@YamlManager.as_constructor(
|
||||||
|
"!include_dir_list",
|
||||||
|
"!include_dir_merge_list",
|
||||||
|
"!include_dir_named",
|
||||||
|
"!include_dir_merge_named",
|
||||||
|
"!env_var",
|
||||||
|
)
|
||||||
|
class IncludedYamlDir(NamedTuple):
|
||||||
|
original_tag: str # original node name
|
||||||
|
name: str # original node name
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYamlDir"):
|
||||||
|
return representer.represent_scalar(f"!{node.original_tag}", node.name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_yaml(cls, _constructor, node: Node):
|
||||||
|
return cls(node.tag[1:], node.value)
|
||||||
@@ -1,16 +1,19 @@
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from src.yaml_serializer.types import (
|
from pathlib import Path
|
||||||
IncludedYaml,
|
from typing import Optional, TextIO
|
||||||
IncludedYamlDir,
|
|
||||||
SecretValue,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class YamlSafeDumper(yaml.SafeDumper):
|
class YamlSafeDumper(yaml.SafeDumper):
|
||||||
pass
|
def __init__(self, stream: TextIO, root_path: Optional[Path] = None) -> None:
|
||||||
|
if root_path is None:
|
||||||
|
self.root_path = Path.cwd()
|
||||||
|
else:
|
||||||
|
self.root_path = root_path
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
YamlSafeDumper.add_representer(IncludedYaml, IncludedYaml.to_yaml) # type: ignore
|
stream,
|
||||||
YamlSafeDumper.add_representer(SecretValue, SecretValue.to_yaml) # type: ignore
|
default_flow_style=False,
|
||||||
YamlSafeDumper.add_representer(IncludedYamlDir, IncludedYamlDir.to_yaml) # type: ignore
|
allow_unicode=True,
|
||||||
|
sort_keys=False,
|
||||||
|
)
|
||||||
@@ -1,20 +1,15 @@
|
|||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from src.yaml_serializer.types import (
|
from pathlib import Path
|
||||||
IncludedYaml,
|
from typing import Optional, Union, TextIO
|
||||||
IncludedYamlDir,
|
|
||||||
SecretValue,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class YamlSafeLoader(yaml.SafeLoader):
|
class YamlSafeLoader(yaml.SafeLoader):
|
||||||
pass
|
def __init__(self, stream: Union[TextIO, Path], root_path: Optional[Path] = None) -> None:
|
||||||
|
super().__init__(stream)
|
||||||
|
if isinstance(stream, Path) and root_path is None:
|
||||||
YamlSafeLoader.add_constructor("!include", IncludedYaml.from_yaml)
|
self.root_path = stream.parent
|
||||||
YamlSafeLoader.add_constructor("!secret", SecretValue.from_yaml)
|
elif root_path is None:
|
||||||
YamlSafeLoader.add_constructor("!env_var", IncludedYamlDir.from_yaml)
|
self.root_path = Path.cwd()
|
||||||
YamlSafeLoader.add_constructor("!include_dir_list", IncludedYamlDir.from_yaml)
|
else:
|
||||||
YamlSafeLoader.add_constructor("!include_dir_merge_list", IncludedYamlDir.from_yaml)
|
self.root_path = root_path
|
||||||
YamlSafeLoader.add_constructor("!include_dir_named", IncludedYamlDir.from_yaml)
|
|
||||||
YamlSafeLoader.add_constructor("!include_dir_merge_named", IncludedYamlDir.from_yaml)
|
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import io
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Any, Optional, TextIO, TypeVar, Union
|
||||||
|
from .dumper import YamlSafeDumper
|
||||||
|
from .loader import YamlSafeLoader
|
||||||
|
from .types import Constructor
|
||||||
|
|
||||||
|
ConstructorT = TypeVar("ConstructorT", bound=Constructor)
|
||||||
|
|
||||||
|
|
||||||
|
class YamlManager:
|
||||||
|
Loader = YamlSafeLoader
|
||||||
|
Dumper = YamlSafeDumper
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def as_constructor(cls, *tags: str):
|
||||||
|
def wrapper(constructor: ConstructorT) -> ConstructorT:
|
||||||
|
for tag in tags:
|
||||||
|
cls.Loader.add_constructor(tag, constructor.from_yaml)
|
||||||
|
cls.Dumper.add_representer(constructor, constructor.to_yaml)
|
||||||
|
return constructor
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def load_yaml(cls, yaml_stream: TextIO, root_path: Optional[Path] = None) -> Union[dict, list]:
|
||||||
|
"""Load yaml from string using some hass-compliant yaml strings
|
||||||
|
|
||||||
|
Args:
|
||||||
|
yaml_stream (io.StringIO)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Union[dict, list]
|
||||||
|
"""
|
||||||
|
loader = cls.Loader(yaml_stream, root_path=root_path)
|
||||||
|
try:
|
||||||
|
return loader.get_single_data()
|
||||||
|
finally:
|
||||||
|
loader.dispose()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def dump_yaml(cls, obj: Any, root_path: Optional[Path] = None) -> str:
|
||||||
|
"""Convert a python object to a hass-compliant yaml string
|
||||||
|
|
||||||
|
Args:
|
||||||
|
obj (Any)
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str
|
||||||
|
"""
|
||||||
|
stream = io.StringIO()
|
||||||
|
dumper = cls.Dumper(stream, root_path=root_path)
|
||||||
|
try:
|
||||||
|
dumper.open()
|
||||||
|
dumper.represent(obj)
|
||||||
|
dumper.close()
|
||||||
|
finally:
|
||||||
|
dumper.dispose()
|
||||||
|
stream.seek(0)
|
||||||
|
return stream.read()
|
||||||
@@ -1,61 +1,14 @@
|
|||||||
from typing import NamedTuple
|
from typing import Protocol, TypeVar
|
||||||
|
|
||||||
from yaml.nodes import Node
|
from yaml.nodes import Node
|
||||||
from yaml.representer import BaseRepresenter
|
from yaml.representer import BaseRepresenter
|
||||||
|
|
||||||
|
|
||||||
class IncludedYaml(NamedTuple):
|
class Constructor(Protocol):
|
||||||
"""Any time something in the yaml is specified as `include !filepath`
|
|
||||||
this data type will represent it.
|
|
||||||
"""
|
|
||||||
|
|
||||||
path_str: str # original node path
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
def to_yaml(cls, representer: BaseRepresenter, node: "Constructor"):
|
||||||
return representer.represent_scalar("!include", node.path_str)
|
...
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
def from_yaml(cls, _constructor, node: Node):
|
||||||
return cls(node.value)
|
...
|
||||||
|
|
||||||
|
|
||||||
# TODO: add support for these
|
|
||||||
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
|
||||||
|
|
||||||
|
|
||||||
class SecretValue(NamedTuple):
|
|
||||||
name: str
|
|
||||||
value: str
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "SecretValue"):
|
|
||||||
return representer.represent_scalar("!secret", node.name)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
|
||||||
return cls(node.value, "n/a")
|
|
||||||
|
|
||||||
def to_normalized_json(self):
|
|
||||||
return NOT_IMPLEMENTED_SV_MSG
|
|
||||||
|
|
||||||
|
|
||||||
class IncludedYamlDir(NamedTuple):
|
|
||||||
"""
|
|
||||||
This is a stub for when we see, still need to add support!
|
|
||||||
- !include_dir_list
|
|
||||||
- !include_dir_merge_list
|
|
||||||
- !include_dir_named
|
|
||||||
- !include_dir_merge_named
|
|
||||||
"""
|
|
||||||
|
|
||||||
original_tag: str # original node name
|
|
||||||
name: str # original node name
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYamlDir"):
|
|
||||||
return representer.represent_scalar(f"!{node.original_tag}", node.name)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_yaml(cls, _constructor, node: Node):
|
|
||||||
return cls(node.tag[1:], node.value)
|
|
||||||
@@ -2,7 +2,7 @@ import json
|
|||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from src.json_serializer import json_dumps
|
from src.json_serializer import json_dumps
|
||||||
from src.yaml_serializer.types import NOT_IMPLEMENTED_SV_MSG, SecretValue
|
from src.yaml_serializer import NOT_IMPLEMENTED_SV_MSG, SecretValue
|
||||||
|
|
||||||
|
|
||||||
class json_serializer_tests(TestCase):
|
class json_serializer_tests(TestCase):
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import io
|
|||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from src.yaml_serializer import dump_yaml, load_yaml
|
from src.yaml_serializer import dump_yaml, load_yaml
|
||||||
from src.yaml_serializer.types import (
|
from src.yaml_serializer import (
|
||||||
IncludedYaml,
|
IncludedYaml,
|
||||||
IncludedYamlDir,
|
IncludedYamlDir,
|
||||||
SecretValue,
|
SecretValue,
|
||||||
|
|||||||
Reference in new issue
Block a user