added yaml deserializer and move all yamll stuff to one folder
This commit is contained in:
1 parent
a05c1b9dca
commit
2768975f9d
12 files changed
+114
-37
No files matched your search
@@ -1,7 +1,7 @@
|
||||
from src.env import ROOT_FOLDER
|
||||
from src.db.connection import HassDatabase
|
||||
from src.config.AutomationLoader import AutomationLoader
|
||||
from src.config.HassSafeConstructor import load_hass_config
|
||||
from src.yaml_serializer import load_hass_config
|
||||
|
||||
hass_config = load_hass_config(ROOT_FOLDER / "tests/samples")
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
from typing import Dict, Iterable, List, Literal, Optional, Union
|
||||
from pydantic.fields import Field
|
||||
from pydantic.error_wrappers import ValidationError
|
||||
from src.json_serializer import json_dumps, normalize_obj
|
||||
from src.config.HassSafeConstructor import HassConfig, IncludedYaml, SecretValue
|
||||
from src.yaml_serializer import HassConfig, IncludedYaml, SecretValue
|
||||
from pydantic import BaseModel
|
||||
|
||||
# errors
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import os
|
||||
from decouple import config
|
||||
from pathlib import Path
|
||||
from .HassSafeConstructor import load_hass_config
|
||||
from src.yaml_serializer import load_hass_config
|
||||
|
||||
|
||||
def find_hass_config() -> Path:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import orjson
|
||||
from typing import Any, Union
|
||||
from src.config.HassSafeConstructor import SecretValue
|
||||
from src.yaml_serializer import SecretValue
|
||||
|
||||
# TODO: add support for these
|
||||
NOT_IMPLEMENTED_SV_MSG = "SECRET VALUE NOT IMPLEMENTED"
|
||||
|
||||
+5
-29
@@ -1,36 +1,10 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, Literal, NamedTuple, Optional, Union
|
||||
from typing import Any, NamedTuple, Optional
|
||||
from ruamel.yaml import YAML
|
||||
from ruamel.yaml.constructor import SafeConstructor
|
||||
from ruamel.yaml.nodes import Node
|
||||
from src.logger import logger
|
||||
|
||||
|
||||
class HassConfig(NamedTuple):
|
||||
"""The final output for load_hass_config"""
|
||||
|
||||
secrets: dict # the secrets file content
|
||||
config: dict # the complete configration.yaml tree
|
||||
|
||||
|
||||
class IncludedYaml(NamedTuple):
|
||||
"""Any time something in the yaml is specified as `include !filepath`
|
||||
this data type will represent it.
|
||||
"""
|
||||
|
||||
original_path: Path # path to location of original file
|
||||
data: Any # the parsed data
|
||||
|
||||
|
||||
class SecretValue(NamedTuple):
|
||||
name: str
|
||||
value: str
|
||||
|
||||
|
||||
def load_hass_config(file_path: Path) -> HassConfig:
|
||||
HAY = HassYaml()
|
||||
data = HAY.load(file_path / "configuration.yaml")
|
||||
return HassConfig(HAY.secrets, data)
|
||||
from src.yaml_serializer.types import SecretValue, IncludedYaml
|
||||
|
||||
|
||||
class HassYaml:
|
||||
@@ -72,7 +46,9 @@ class HassYaml:
|
||||
else:
|
||||
logger.info(f"Found `!include {node.value}` in {file_path.absolute()}!")
|
||||
|
||||
return IncludedYaml(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."""
|
||||
@@ -0,0 +1,19 @@
|
||||
import io
|
||||
from pathlib import Path
|
||||
from typing import Any, List
|
||||
from src.yaml_serializer.base_yaml import yaml
|
||||
from src.yaml_serializer.HassSafeConstructor import HassYaml
|
||||
from src.yaml_serializer.types import HassConfig, SecretValue, IncludedYaml
|
||||
|
||||
|
||||
def load_hass_config(file_path: Path) -> HassConfig:
|
||||
HAY = HassYaml()
|
||||
data = HAY.load(file_path / "configuration.yaml")
|
||||
return HassConfig(HAY.secrets, data)
|
||||
|
||||
|
||||
def dump_yaml(obj: Any) -> str:
|
||||
stream = io.StringIO()
|
||||
yaml.dump(obj, stream)
|
||||
stream.seek(0)
|
||||
return stream.read()
|
||||
@@ -0,0 +1,6 @@
|
||||
from ruamel.yaml import YAML
|
||||
from src.yaml_serializer.types import IncludedYaml, SecretValue
|
||||
|
||||
yaml = YAML()
|
||||
yaml.register_class(IncludedYaml)
|
||||
yaml.register_class(SecretValue)
|
||||
@@ -0,0 +1,34 @@
|
||||
from pathlib import Path
|
||||
from typing import Any, NamedTuple
|
||||
from ruamel.yaml.nodes import Node
|
||||
from ruamel.yaml.representer import BaseRepresenter
|
||||
|
||||
|
||||
class IncludedYaml(NamedTuple):
|
||||
"""Any time something in the yaml is specified as `include !filepath`
|
||||
this data type will represent it.
|
||||
"""
|
||||
|
||||
name: str # original node name
|
||||
original_path: Path # path to location of original file
|
||||
data: Any # the parsed data
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
||||
return representer.represent_scalar("!include", node.name)
|
||||
|
||||
|
||||
class SecretValue(NamedTuple):
|
||||
name: str
|
||||
value: str
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: Node):
|
||||
return representer.represent_scalar("!secret", node.name)
|
||||
|
||||
|
||||
class HassConfig(NamedTuple):
|
||||
"""The final output for load_hass_config"""
|
||||
|
||||
secrets: dict # the secrets file content
|
||||
config: dict # the complete configration.yaml tree
|
||||
Reference in new issue
Block a user