added simpler loader
This commit is contained in:
1 parent
d6adbb897d
commit
c493c0a3d0
6 files changed
+118
-12
No files matched your search
@@ -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
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
@@ -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"""
|
||||
|
||||
@@ -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",
|
||||
)
|
||||
Reference in new issue
Block a user