noop yaml loader refactor
This commit is contained in:
1 parent
0cf3f2706a
commit
97521097dc
17 files changed
+147
-93
No files matched your search
@@ -1,8 +1,8 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from src.automations.loader import extract_automation_paths
|
||||
from src.env import BUILD_VERSION, HASSIO_TOKEN, HASSIO_WS
|
||||
from src.hass_config import HassConfig
|
||||
from src.automations.loader import extract_automation_paths
|
||||
|
||||
|
||||
def make_details_router(hass_config: HassConfig) -> APIRouter:
|
||||
|
||||
@@ -7,7 +7,12 @@ from typing import Iterable, Iterator, List, Optional
|
||||
|
||||
from src.logger import get_logger
|
||||
|
||||
from .errors import DBDataError, DBError, DBNoAutomationFound, RepeatedAutomationId
|
||||
from .errors import (
|
||||
DBDataError,
|
||||
DBError,
|
||||
DBNoAutomationFound,
|
||||
RepeatedAutomationId,
|
||||
)
|
||||
from .types import ExtenededAutomation
|
||||
|
||||
logger = get_logger(__file__)
|
||||
|
||||
@@ -4,7 +4,12 @@ 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, IncludedYaml, IncludedYamlDir
|
||||
from src.yaml_serializer import (
|
||||
IncludedYaml,
|
||||
IncludedYamlDir,
|
||||
dump_yaml,
|
||||
load_yaml,
|
||||
)
|
||||
|
||||
from ..hass_config.loader import HassConfig
|
||||
from .errors import InvalidAutomationFile
|
||||
@@ -23,10 +28,8 @@ def extract_automation_paths(
|
||||
for key, value in extract_automation_keys(hass_config):
|
||||
logger.info(f"Loading automations from '{key}'")
|
||||
found = True
|
||||
if isinstance(value, IncludedYaml):
|
||||
yield key, value.path_str
|
||||
elif isinstance(value, IncludedYamlDir):
|
||||
yield key, value.name
|
||||
if isinstance(value, (IncludedYaml, IncludedYamlDir)):
|
||||
yield key, str(value.path.relative_to(hass_config.root_path))
|
||||
else:
|
||||
logger.warning(f"found an invalid type in {key}!")
|
||||
if not found:
|
||||
@@ -38,17 +41,18 @@ def get_base_automation_key(hass_config: HassConfig):
|
||||
for key, value in extract_automation_keys(hass_config):
|
||||
found_any = True
|
||||
if isinstance(value, IncludedYaml):
|
||||
return key, value.path_str
|
||||
return key, str(value.path.relative_to(hass_config.root_path))
|
||||
configurations = hass_config.configurations
|
||||
if found_any:
|
||||
out_path = hass_config.get_backup_automation_file_path()
|
||||
else:
|
||||
out_path = hass_config.get_default_automation_path()
|
||||
if not (out_path := hass_config.get_default_automation_path()): # type: ignore
|
||||
raise AssertionError("Failed to find automation files...")
|
||||
out_file = str(out_path.relative_to(hass_config.root_path))
|
||||
configurations[AUTOMATION_CONFIGURATION_KEY] = IncludedYaml(out_file)
|
||||
configurations[AUTOMATION_CONFIGURATION_KEY] = IncludedYaml(out_path)
|
||||
out_path.touch(exist_ok=True)
|
||||
with hass_config.get_configuration_path().open("w") as fp:
|
||||
fp.write(dump_yaml(configurations))
|
||||
fp.write(dump_yaml(configurations, root_path=hass_config.root_path))
|
||||
return AUTOMATION_CONFIGURATION_KEY, out_file
|
||||
|
||||
|
||||
@@ -92,7 +96,7 @@ def load_automation_path(
|
||||
else:
|
||||
try:
|
||||
with automation_path.open("r") as fp:
|
||||
automations = load_yaml(fp)
|
||||
automations = load_yaml(fp, root_path=root_path)
|
||||
except Exception as err:
|
||||
raise InvalidAutomationFile(
|
||||
when="loading yaml",
|
||||
|
||||
@@ -14,7 +14,11 @@ from .errors import (
|
||||
DBError,
|
||||
InvalidAutomationFile,
|
||||
)
|
||||
from .loader import extract_automation_paths, get_base_automation_key, load_automation_path
|
||||
from .loader import (
|
||||
extract_automation_paths,
|
||||
get_base_automation_key,
|
||||
load_automation_path,
|
||||
)
|
||||
from .tags import TagManager
|
||||
from .types import Automation, ExtenededAutomation
|
||||
|
||||
@@ -92,7 +96,7 @@ class AutomationManager:
|
||||
objs: Optional[Union[list, dict]] = None
|
||||
if automation_path.exists():
|
||||
with automation_path.open("r") as fp:
|
||||
objs = load_yaml(fp)
|
||||
objs = load_yaml(fp, self.hass_config.root_path)
|
||||
|
||||
if automation.source_file_type == "obj":
|
||||
if isinstance(objs, list):
|
||||
@@ -100,7 +104,7 @@ class AutomationManager:
|
||||
f"{automation_path} is a list, but expected a obj for {automation}"
|
||||
)
|
||||
raw_auto = automation.to_primitive(include_tags=False)
|
||||
automation_path.write_text(dump_yaml(raw_auto))
|
||||
automation_path.write_text(dump_yaml(raw_auto, self.hass_config.root_path))
|
||||
else:
|
||||
if isinstance(objs, dict):
|
||||
raise AttemptingToOverwriteAnIncompatibleFileError(
|
||||
@@ -125,7 +129,7 @@ class AutomationManager:
|
||||
raise InvalidAutomationFile(
|
||||
"Could not update automations, not found in source file."
|
||||
)
|
||||
automation_path.write_text(dump_yaml(objs))
|
||||
automation_path.write_text(dump_yaml(objs, self.hass_config.root_path))
|
||||
self.update_tags(automation.id, automation.tags)
|
||||
|
||||
def update_tags(self, automation_id: str, tags: Dict[str, str]):
|
||||
@@ -145,7 +149,7 @@ class AutomationManager:
|
||||
automation_path.unlink(missing_ok=True)
|
||||
else:
|
||||
with automation_path.open("r") as fp:
|
||||
objs = load_yaml(fp)
|
||||
objs = load_yaml(fp, self.hass_config.root_path)
|
||||
if not isinstance(objs, list):
|
||||
raise AssertionError(
|
||||
f"Attemption to save automation to {automation_path} but encountering an invalid list yaml file. Did you modify this file prior to saving?\n{automation}"
|
||||
@@ -154,5 +158,5 @@ class AutomationManager:
|
||||
for i, obj in enumerate(objs):
|
||||
if obj["id"] != automation.id:
|
||||
kept.append(obj)
|
||||
automation_path.write_text(dump_yaml(kept))
|
||||
automation_path.write_text(dump_yaml(kept, self.hass_config.root_path))
|
||||
self.delete_tags(automation.id)
|
||||
@@ -8,9 +8,9 @@ class TagManager(Dict[str, Dict[str, str]]):
|
||||
def save(self, path: Path):
|
||||
if not path.parent.exists():
|
||||
path.parent.mkdir(parents=True)
|
||||
path.write_text(dump_yaml(dict(self)))
|
||||
path.write_text(dump_yaml(dict(self), root_path=path.parent))
|
||||
|
||||
@classmethod
|
||||
def load(cls, path: Path):
|
||||
with path.open("r") as fp:
|
||||
return cls(load_yaml(fp))
|
||||
return cls(load_yaml(fp, root_path=path.parent))
|
||||
@@ -2,7 +2,7 @@ from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from src.automations.tags import TagManager
|
||||
from src.yaml_serializer import load_yaml, IncludedYaml
|
||||
from src.yaml_serializer import IncludedYaml, load_yaml
|
||||
|
||||
|
||||
class HassConfig:
|
||||
@@ -12,7 +12,7 @@ class HassConfig:
|
||||
@property
|
||||
def configurations(self) -> dict:
|
||||
with self.get_configuration_path().open("r") as f:
|
||||
return dict(load_yaml(f)) # type: ignore
|
||||
return dict(load_yaml(f, root_path=self.root_path)) # type: ignore
|
||||
|
||||
@property
|
||||
def automation_tags(self) -> TagManager:
|
||||
@@ -35,7 +35,7 @@ class HassConfig:
|
||||
if "automation" in config:
|
||||
automation_ref = config["automation"]
|
||||
if isinstance(automation_ref, IncludedYaml):
|
||||
return self.root_path / automation_ref.path_str
|
||||
return automation_ref.path
|
||||
else:
|
||||
return None
|
||||
return self.root_path / "automations.yaml"
|
||||
@@ -51,6 +51,3 @@ class HassConfig:
|
||||
|
||||
def get_configuration_path(self) -> Path:
|
||||
return self.root_path / "configuration.yaml"
|
||||
|
||||
def save_tags(self, tags: TagManager):
|
||||
tags.save(self.get_automation_tags_path())
|
||||
@@ -1,5 +1,6 @@
|
||||
from pathlib import Path
|
||||
|
||||
from yaml.nodes import Node
|
||||
from yaml.representer import BaseRepresenter
|
||||
|
||||
from .manager import YamlManager
|
||||
|
||||
@@ -15,15 +16,21 @@ class IncludedYaml(NamedTuple):
|
||||
this data type will represent it.
|
||||
"""
|
||||
|
||||
path_str: str # original node path
|
||||
path: Path # original node path
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYaml"):
|
||||
return representer.represent_scalar("!include", node.path_str)
|
||||
def to_yaml(cls, representer, node: "IncludedYaml"):
|
||||
return representer.represent_scalar(
|
||||
"!include", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor: YamlManager.Loader, node: Node):
|
||||
return cls(node.value)
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(constructor.root_path / node.value)
|
||||
|
||||
def to_normalized_json(self):
|
||||
with self.path.open("r") as fp:
|
||||
return load_yaml(fp, root_path=self.path.parent)
|
||||
|
||||
|
||||
# TODO: add support for these
|
||||
@@ -36,11 +43,11 @@ class SecretValue(NamedTuple):
|
||||
value: str
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "SecretValue"):
|
||||
def to_yaml(cls, representer, node: "SecretValue"):
|
||||
return representer.represent_scalar("!secret", node.name)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, _constructor, node: Node):
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(node.value, "n/a")
|
||||
|
||||
def to_normalized_json(self):
|
||||
@@ -52,16 +59,32 @@ class SecretValue(NamedTuple):
|
||||
"!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
|
||||
path: Path # original node name
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "IncludedYamlDir"):
|
||||
return representer.represent_scalar(f"!{node.original_tag}", node.name)
|
||||
def to_yaml(cls, representer, node: "IncludedYamlDir"):
|
||||
return representer.represent_scalar(
|
||||
f"!{node.original_tag}", str(node.path.relative_to(representer.root_path))
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, _constructor, node: Node):
|
||||
return cls(node.tag[1:], node.value)
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(node.tag[1:], constructor.root_path / node.value)
|
||||
|
||||
|
||||
@YamlManager.as_constructor(
|
||||
"!env_var",
|
||||
)
|
||||
class EnvVar(NamedTuple):
|
||||
value: str # original node name
|
||||
|
||||
@classmethod
|
||||
def to_yaml(cls, representer, node: "EnvVar"):
|
||||
return representer.represent_scalar("!env_var", node.value)
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, constructor, node: Node):
|
||||
return cls(node.value)
|
||||
@@ -1,8 +1,8 @@
|
||||
import yaml
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional, TextIO
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class YamlSafeDumper(yaml.SafeDumper):
|
||||
def __init__(self, stream: TextIO, root_path: Optional[Path] = None) -> None:
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import yaml
|
||||
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union, TextIO
|
||||
from typing import Optional, TextIO
|
||||
|
||||
import yaml
|
||||
|
||||
|
||||
class YamlSafeLoader(yaml.SafeLoader):
|
||||
def __init__(self, stream: Union[TextIO, Path], root_path: Optional[Path] = None) -> None:
|
||||
def __init__(self, stream: TextIO, 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:
|
||||
if root_path is None:
|
||||
self.root_path = Path.cwd()
|
||||
else:
|
||||
self.root_path = root_path
|
||||
@@ -1,11 +1,12 @@
|
||||
import io
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional, TextIO, TypeVar, Union
|
||||
from typing import Any, TextIO, Type, TypeVar, Union
|
||||
|
||||
from .dumper import YamlSafeDumper
|
||||
from .loader import YamlSafeLoader
|
||||
from .types import Constructor
|
||||
|
||||
ConstructorT = TypeVar("ConstructorT", bound=Constructor)
|
||||
ConstructorT = TypeVar("ConstructorT", bound=Type[Constructor])
|
||||
|
||||
|
||||
class YamlManager:
|
||||
@@ -23,7 +24,7 @@ class YamlManager:
|
||||
return wrapper
|
||||
|
||||
@classmethod
|
||||
def load_yaml(cls, yaml_stream: TextIO, root_path: Optional[Path] = None) -> Union[dict, list]:
|
||||
def load_yaml(cls, yaml_stream: TextIO, root_path: Path) -> Union[dict, list]:
|
||||
"""Load yaml from string using some hass-compliant yaml strings
|
||||
|
||||
Args:
|
||||
@@ -39,7 +40,7 @@ class YamlManager:
|
||||
loader.dispose()
|
||||
|
||||
@classmethod
|
||||
def dump_yaml(cls, obj: Any, root_path: Optional[Path] = None) -> str:
|
||||
def dump_yaml(cls, obj: Any, root_path: Path) -> str:
|
||||
"""Convert a python object to a hass-compliant yaml string
|
||||
|
||||
Args:
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
from typing import Protocol, TypeVar
|
||||
from typing import Protocol
|
||||
|
||||
from yaml.nodes import Node
|
||||
from yaml.representer import BaseRepresenter
|
||||
|
||||
from .dumper import YamlSafeDumper
|
||||
from .loader import YamlSafeLoader
|
||||
|
||||
|
||||
class Constructor(Protocol):
|
||||
@classmethod
|
||||
def to_yaml(cls, representer: BaseRepresenter, node: "Constructor"):
|
||||
def to_yaml(cls, representer: YamlSafeDumper, node: "Constructor"):
|
||||
...
|
||||
|
||||
@classmethod
|
||||
def from_yaml(cls, _constructor, node: Node):
|
||||
def from_yaml(cls, constructor: YamlSafeLoader, node: Node):
|
||||
...
|
||||
Reference in new issue
Block a user