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"""
|
||||
|
||||
Reference in new issue
Block a user