diff --git a/api/src/automations/loader.py b/api/src/automations/loader.py index f144e1d..39665f9 100644 --- a/api/src/automations/loader.py +++ b/api/src/automations/loader.py @@ -4,8 +4,7 @@ from typing import Any, Iterator, Tuple, Union from src.json_serializer import normalize_obj from src.logger import get_logger from src.utils import extract_files -from src.yaml_serializer import dump_yaml, load_yaml -from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir +from src.yaml_serializer import dump_yaml, load_yaml, IncludedYaml, IncludedYamlDir from ..hass_config.loader import HassConfig from .errors import InvalidAutomationFile diff --git a/api/src/hass_config/loader.py b/api/src/hass_config/loader.py index ca32c9a..02d0b37 100644 --- a/api/src/hass_config/loader.py +++ b/api/src/hass_config/loader.py @@ -1,9 +1,8 @@ from pathlib import Path -from typing import List, Optional +from typing import Optional from src.automations.tags import TagManager -from src.yaml_serializer import dump_yaml, load_yaml -from src.yaml_serializer.types import IncludedYaml +from src.yaml_serializer import load_yaml, IncludedYaml class HassConfig: diff --git a/api/src/yaml_serializer/__init__.py b/api/src/yaml_serializer/__init__.py index c3f785b..3619ac3 100644 --- a/api/src/yaml_serializer/__init__.py +++ b/api/src/yaml_serializer/__init__.py @@ -1,41 +1,67 @@ -import io -from typing import Any, TextIO, Union +from yaml.nodes import Node +from yaml.representer import BaseRepresenter -import yaml +from .manager import YamlManager -from src.yaml_serializer.dumper import YamlSafeDumper -from src.yaml_serializer.loader import YamlSafeLoader +dump_yaml = YamlManager.dump_yaml +load_yaml = YamlManager.load_yaml + +from typing import NamedTuple -def dump_yaml(obj: Any) -> str: - """Convert a python object to a hass-compliant yaml string - - Args: - obj (Any) - - Returns: - str +@YamlManager.as_constructor("!include") +class IncludedYaml(NamedTuple): + """Any time something in the yaml is specified as `include !filepath` + this data type will represent it. """ - stream = io.StringIO() - yaml.dump( # nosec - obj, - stream, - default_flow_style=False, - allow_unicode=True, - sort_keys=False, - Dumper=YamlSafeDumper, - ) - stream.seek(0) - return stream.read() + + path_str: str # original node path + + @classmethod + def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"): + return representer.represent_scalar("!include", node.path_str) + + @classmethod + def from_yaml(cls, constructor: YamlManager.Loader, node: Node): + return cls(node.value) -def load_yaml(yaml_stream: TextIO) -> Union[dict, list]: - """Load yaml from string using some hass-compliant yaml strings +# TODO: add support for these +NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED" - Args: - yaml_stream (io.StringIO) - Returns: - Union[dict, list] - """ - return yaml.load(yaml_stream, Loader=YamlSafeLoader) # nosec +@YamlManager.as_constructor("!secret") +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 + + +@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) diff --git a/api/src/yaml_serializer/dumper.py b/api/src/yaml_serializer/dumper.py index f61596d..24f7d9b 100644 --- a/api/src/yaml_serializer/dumper.py +++ b/api/src/yaml_serializer/dumper.py @@ -1,16 +1,19 @@ import yaml -from src.yaml_serializer.types import ( - IncludedYaml, - IncludedYamlDir, - SecretValue, -) +from pathlib import Path +from typing import Optional, TextIO 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 - -YamlSafeDumper.add_representer(IncludedYaml, IncludedYaml.to_yaml) # type: ignore -YamlSafeDumper.add_representer(SecretValue, SecretValue.to_yaml) # type: ignore -YamlSafeDumper.add_representer(IncludedYamlDir, IncludedYamlDir.to_yaml) # type: ignore + super().__init__( + stream, + default_flow_style=False, + allow_unicode=True, + sort_keys=False, + ) diff --git a/api/src/yaml_serializer/loader.py b/api/src/yaml_serializer/loader.py index 986c15e..b88ce93 100644 --- a/api/src/yaml_serializer/loader.py +++ b/api/src/yaml_serializer/loader.py @@ -1,20 +1,15 @@ import yaml -from src.yaml_serializer.types import ( - IncludedYaml, - IncludedYamlDir, - SecretValue, -) +from pathlib import Path +from typing import Optional, Union, TextIO class YamlSafeLoader(yaml.SafeLoader): - pass - - -YamlSafeLoader.add_constructor("!include", IncludedYaml.from_yaml) -YamlSafeLoader.add_constructor("!secret", SecretValue.from_yaml) -YamlSafeLoader.add_constructor("!env_var", IncludedYamlDir.from_yaml) -YamlSafeLoader.add_constructor("!include_dir_list", IncludedYamlDir.from_yaml) -YamlSafeLoader.add_constructor("!include_dir_merge_list", IncludedYamlDir.from_yaml) -YamlSafeLoader.add_constructor("!include_dir_named", IncludedYamlDir.from_yaml) -YamlSafeLoader.add_constructor("!include_dir_merge_named", IncludedYamlDir.from_yaml) + 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: + self.root_path = stream.parent + elif root_path is None: + self.root_path = Path.cwd() + else: + self.root_path = root_path diff --git a/api/src/yaml_serializer/manager.py b/api/src/yaml_serializer/manager.py new file mode 100644 index 0000000..5ae17ce --- /dev/null +++ b/api/src/yaml_serializer/manager.py @@ -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() diff --git a/api/src/yaml_serializer/types.py b/api/src/yaml_serializer/types.py index dd40c64..2bc88d2 100644 --- a/api/src/yaml_serializer/types.py +++ b/api/src/yaml_serializer/types.py @@ -1,61 +1,14 @@ -from typing import NamedTuple +from typing import Protocol, TypeVar from yaml.nodes import Node from yaml.representer import BaseRepresenter -class IncludedYaml(NamedTuple): - """Any time something in the yaml is specified as `include !filepath` - this data type will represent it. - """ - - path_str: str # original node path - +class Constructor(Protocol): @classmethod - def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"): - return representer.represent_scalar("!include", node.path_str) + def to_yaml(cls, representer: BaseRepresenter, node: "Constructor"): + ... @classmethod 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) + ... diff --git a/api/tests/unittests/test_json_serializer.py b/api/tests/unittests/test_json_serializer.py index eae32c5..03eb868 100644 --- a/api/tests/unittests/test_json_serializer.py +++ b/api/tests/unittests/test_json_serializer.py @@ -2,7 +2,7 @@ import json from unittest import TestCase 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): diff --git a/api/tests/unittests/test_yaml_serializer.py b/api/tests/unittests/test_yaml_serializer.py index 5163264..54134b6 100644 --- a/api/tests/unittests/test_yaml_serializer.py +++ b/api/tests/unittests/test_yaml_serializer.py @@ -2,7 +2,7 @@ import io from unittest import TestCase from src.yaml_serializer import dump_yaml, load_yaml -from src.yaml_serializer.types import ( +from src.yaml_serializer import ( IncludedYaml, IncludedYamlDir, SecretValue,