From c493c0a3d037cff616487d7664e178534444a79c Mon Sep 17 00:00:00 2001 From: Ari Sosnovsky Date: Thu, 11 Nov 2021 12:33:33 -0500 Subject: [PATCH] added simpler loader --- api/src/config/AutomationLoader.py | 4 +++ api/src/yaml_serializer/HassYaml.py | 12 +++----- api/src/yaml_serializer/__init__.py | 34 +++++++++++++++++++++-- api/src/yaml_serializer/base_yaml.py | 10 ++++++- api/src/yaml_serializer/types.py | 29 ++++++++++++++++++++ api/tests/unittests/test_yaml.py | 41 +++++++++++++++++++++++++++- 6 files changed, 118 insertions(+), 12 deletions(-) diff --git a/api/src/config/AutomationLoader.py b/api/src/config/AutomationLoader.py index 5ed315d..05f4b99 100644 --- a/api/src/config/AutomationLoader.py +++ b/api/src/config/AutomationLoader.py @@ -186,3 +186,7 @@ class AutomationLoader: def __len__(self): return len(self.automations_data) + + def save(self, index: int, auto: AutomationData): + if isinstance(self.automation_ref, IncludedYaml): + self.automation_ref.original_path diff --git a/api/src/yaml_serializer/HassYaml.py b/api/src/yaml_serializer/HassYaml.py index a0a85c1..77f6dc8 100644 --- a/api/src/yaml_serializer/HassYaml.py +++ b/api/src/yaml_serializer/HassYaml.py @@ -4,7 +4,7 @@ from ruamel.yaml import YAML from ruamel.yaml.constructor import SafeConstructor from ruamel.yaml.nodes import Node from src.logger import logger -from src.yaml_serializer.types import SecretValue, IncludedYaml +from src.yaml_serializer.types import IncludedYamlDir, SecretValue, IncludedYaml class HassYaml: @@ -35,9 +35,7 @@ class HassYaml: device_tracker: !include device_tracker.yaml """ root_path = ( - self.current_file_path.parent - if self.current_file_path is not None - else Path("/") + self.current_file_path.parent if self.current_file_path is not None else Path("/") ) file_path = _upward_recursive_search(root_path, node.value) if file_path is None: @@ -46,9 +44,7 @@ class HassYaml: else: logger.info(f"Found `!include {node.value}` in {file_path.absolute()}!") - return IncludedYaml( - node.value, file_path, self.__internal_constructor_load(file_path) - ) + return IncludedYaml(node.value, file_path, self.__internal_constructor_load(file_path)) def _secret_yaml(loader, node: Node): """Load secrets and embed it into the configuration YAML.""" @@ -177,4 +173,4 @@ def _stub_tag(constructor, node: Node): logger.warning(f"YAML tag {node.tag} is not supported") seen.add(node.tag) - return {} + return IncludedYamlDir(node.tag, node.value) diff --git a/api/src/yaml_serializer/__init__.py b/api/src/yaml_serializer/__init__.py index 41bf772..c421ca5 100644 --- a/api/src/yaml_serializer/__init__.py +++ b/api/src/yaml_serializer/__init__.py @@ -1,19 +1,49 @@ import io from pathlib import Path -from typing import Any, List +from typing import Any, Union from src.yaml_serializer.base_yaml import yaml from src.yaml_serializer.HassYaml import HassYaml -from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml +from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml, IncludedYamlDir +## TODO: consider deprecating this and using 'simple_load_yaml' for consistency def load_hass_config(file_path: Path) -> HassConfig: + """Load all Hass configrations from the root config folder + **highly opinionated** + + Args: + file_path (Path) + + Returns: + HassConfig + """ HAY = HassYaml() data = HAY.load(file_path / "configuration.yaml") return HassConfig(HAY.secrets, data) def dump_yaml(obj: Any) -> str: + """Convert a python object to a hass-compliant yaml string + + Args: + obj (Any) + + Returns: + str + """ stream = io.StringIO() yaml.dump(obj, stream) stream.seek(0) return stream.read() + + +def simple_load_yaml(yaml_stream: io.StringIO) -> Union[dict, list]: + """Load yaml from string using some hass-compliant yaml strings + + Args: + yaml_stream (io.StringIO) + + Returns: + List[dict, list] + """ + return yaml.load(yaml_stream) diff --git a/api/src/yaml_serializer/base_yaml.py b/api/src/yaml_serializer/base_yaml.py index 9e729f5..1ecfe1f 100644 --- a/api/src/yaml_serializer/base_yaml.py +++ b/api/src/yaml_serializer/base_yaml.py @@ -1,6 +1,14 @@ from ruamel.yaml import YAML -from src.yaml_serializer.types import IncludedYaml, SecretValue +from src.yaml_serializer.types import IncludedYaml, IncludedYamlDir, SecretValue yaml = YAML() yaml.register_class(IncludedYaml) yaml.register_class(SecretValue) +yaml.register_class(IncludedYamlDir) +yaml.constructor.add_constructor("!include", IncludedYaml.from_yaml) +yaml.constructor.add_constructor("!secret", SecretValue.from_yaml) +yaml.constructor.add_constructor("!env_var", IncludedYamlDir.from_yaml) +yaml.constructor.add_constructor("!include_dir_list", IncludedYamlDir.from_yaml) +yaml.constructor.add_constructor("!include_dir_merge_list", IncludedYamlDir.from_yaml) +yaml.constructor.add_constructor("!include_dir_named", IncludedYamlDir.from_yaml) +yaml.constructor.add_constructor("!include_dir_merge_named", IncludedYamlDir.from_yaml) diff --git a/api/src/yaml_serializer/types.py b/api/src/yaml_serializer/types.py index 064e68a..4aa1d76 100644 --- a/api/src/yaml_serializer/types.py +++ b/api/src/yaml_serializer/types.py @@ -17,6 +17,10 @@ class IncludedYaml(NamedTuple): def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"): return representer.represent_scalar("!include", node.name) + @classmethod + def from_yaml(cls, _constructor, node: Node): + return cls(node.value, Path("/"), "n/a") + class SecretValue(NamedTuple): name: str @@ -26,6 +30,31 @@ class SecretValue(NamedTuple): def to_yaml(cls, representer: BaseRepresenter, node: Node): return representer.represent_scalar("!secret", node.name) + @classmethod + def from_yaml(cls, _constructor, node: Node): + return cls(node.value, "n/a") + + +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: "IncludedYaml"): + 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) + class HassConfig(NamedTuple): """The final output for load_hass_config""" diff --git a/api/tests/unittests/test_yaml.py b/api/tests/unittests/test_yaml.py index b7760f1..9140e63 100644 --- a/api/tests/unittests/test_yaml.py +++ b/api/tests/unittests/test_yaml.py @@ -1,6 +1,13 @@ +import io from pathlib import Path from unittest import TestCase -from src.yaml_serializer import dump_yaml, IncludedYaml, SecretValue +from src.yaml_serializer import ( + dump_yaml, + IncludedYaml, + SecretValue, + IncludedYamlDir, + simple_load_yaml, +) class dumping_yamls_tests(TestCase): @@ -21,3 +28,35 @@ class dumping_yamls_tests(TestCase): def test_dumping_secrets(self): yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}) self.assertEqual(yaml, "google_password: !secret gpass\n") + + def test_dumping_stub(self): + yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml")}) + self.assertEqual(yaml, "sensor: !include_dir_list sensors/*yaml\n") + + def test_load_stub(self): + yaml_obj = simple_load_yaml( + io.StringIO( + """ + name: cool + google_password: !secret gpass + sensor: !include_dir_list sensors/*yaml + automations: !include automations.yaml + """ + ) + ) + self.assertDictEqual( + dict(yaml_obj), + { + "name": "cool", + "google_password": SecretValue("gpass", "n/a"), + "sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"), + "automations": IncludedYaml("automations.yaml", Path("/"), "n/a"), + }, + ) + self.assertEqual( + dump_yaml(yaml_obj), + "name: cool\n" + "google_password: !secret gpass\n" + "sensor: !include_dir_list sensors/*yaml\n" + "automations: !include automations.yaml\n", + )