noop yaml loader refactor

This commit is contained in:
ari committed 2022-08-24 10:57:41 -04:00
1 parent 0cf3f2706a
commit 97521097dc
17 files changed
+147 -93

No files matched your search

+9 -8
View File
@@ -1,8 +1,9 @@
get_index # unused function (src/api/app.py:43)
list_autos # unused function (src/api/routes/automations.py:12)
upsert_auto # unused function (src/api/routes/automations.py:24)
delete_auto # unused function (src/api/routes/automations.py:32)
read_details # unused function (src/api/routes/details.py:10)
list_autos # unused function (src/api/routes/automations.py:13)
insert_auto # unused function (src/api/routes/automations.py:28)
update_auto # unused function (src/api/routes/automations.py:32)
delete_auto # unused function (src/api/routes/automations.py:40)
read_details # unused function (src/api/routes/details.py:11)
read_ping # unused function (src/api/routes/ping.py:10)
websocket_endpoint # unused function (src/api/routes/socket.py:13)
params # unused variable (src/api/types.py:16)
@@ -12,8 +13,8 @@ trigger_variables # unused variable (src/automations/types.py:13)
trigger # unused variable (src/automations/types.py:15)
condition # unused variable (src/automations/types.py:16)
action # unused variable (src/automations/types.py:17)
_.to_json # unused method (src/automations/types.py:38)
_.automation_tags # unused property (src/hass_config/loader.py:18)
_.save_automations # unused method (src/hass_config/loader.py:48)
_.save_tags # unused method (src/hass_config/loader.py:56)
_.to_json # unused method (src/automations/types.py:45)
_.automation_tags # unused property (src/hass_config/loader.py:17)
json_dumps # unused function (src/json_serializer.py:5)
SecretValue # unused class (src/yaml_serializer/__init__.py:40)
EnvVar # unused class (src/yaml_serializer/__init__.py:78)
+1 -1
View File
@@ -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:
+6 -1
View File
@@ -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__)
+14 -10
View 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",
+10 -6
View File
@@ -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)
+2 -2
View File
@@ -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))
+3 -6
View File
@@ -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())
+37 -14
View File
@@ -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)
+2 -2
View File
@@ -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:
+5 -7
View File
@@ -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
+5 -4
View File
@@ -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:
+6 -4
View File
@@ -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):
...
+5 -1
View File
@@ -6,7 +6,11 @@ from fastapi.testclient import TestClient
from src.api.app import make_app
from src.automations.types import Automation, ExtenededAutomation
from tests.utils import HA_CONFIG2_EXAMPLE, HA_CONFIG6_EXAMPLE, get_example_automation_loader
from tests.utils import (
HA_CONFIG2_EXAMPLE,
HA_CONFIG6_EXAMPLE,
get_example_automation_loader,
)
class BaseTestCase(TestCase):
+2 -1
View File
@@ -1,7 +1,8 @@
from pathlib import Path
from src.automations.types import ExtenededAutomation
from src.automations.errors import DBDataError
from src.automations.types import ExtenededAutomation
from .utils import TestWithDB
@@ -229,7 +229,8 @@ class manager_tests(TestWithDB):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r")
(config_path / "automations/include_dir_merge_list/sub/list2.yaml").open("r"),
root_path=config_path,
)
self.assertIsInstance(data, list)
@@ -254,7 +255,8 @@ class manager_tests(TestWithDB):
automation_manager.get("newlymade")
data = yaml_serializer.load_yaml(
(config_path / "automations/include_dir_list/home/down.yaml").open("r")
(config_path / "automations/include_dir_list/home/down.yaml").open("r"),
config_path,
)
self.assertIsInstance(data, dict)
+29 -19
View File
@@ -1,11 +1,13 @@
import io
from pathlib import Path
from unittest import TestCase
from src.yaml_serializer import dump_yaml, load_yaml
from src.yaml_serializer import (
IncludedYaml,
IncludedYamlDir,
SecretValue,
dump_yaml,
load_yaml,
)
@@ -14,23 +16,29 @@ class dumping_yamls_tests(TestCase):
yaml = dump_yaml(
{
"automations": IncludedYaml(
"automations.yaml",
Path.cwd() / "automations.yaml",
)
}
},
Path.cwd(),
)
self.assertEqual(yaml, "automations: !include 'automations.yaml'\n")
def test_dumping_secrets(self):
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")})
yaml = dump_yaml({"google_password": SecretValue("gpass", "supersec")}, Path.cwd())
self.assertEqual(yaml, "google_password: !secret 'gpass'\n")
def test_dumping_include_dir_list(self):
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml")})
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors/*yaml'\n")
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors")}, Path.cwd()
)
self.assertEqual(yaml, "sensor: !include_dir_list 'sensors'\n")
def test_dumping_include_dir_merge_list(self):
yaml = dump_yaml({"sensor": IncludedYamlDir("include_dir_merge_list", "sensors/*yaml")})
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors/*yaml'\n")
yaml = dump_yaml(
{"sensor": IncludedYamlDir("include_dir_merge_list", Path.cwd() / "sensors")},
Path.cwd(),
)
self.assertEqual(yaml, "sensor: !include_dir_merge_list 'sensors'\n")
def test_load_complex_automation_setup(self):
yaml_obj = load_yaml(
@@ -41,19 +49,20 @@ class dumping_yamls_tests(TestCase):
automation: !include automations/base.yaml
automation cools: !include_dir_merge_list automations/include_dir_merge_list
"""
)
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
{
"automation manual": IncludedYamlDir(
"include_dir_list", "./automations/include_dir_list"
"include_dir_list", Path.cwd() / "automations/include_dir_list"
),
"automation ui": IncludedYaml("automations/ui.yaml"),
"automation ui": IncludedYaml(Path.cwd() / "automations/ui.yaml"),
"automation cools": IncludedYamlDir(
"include_dir_merge_list", "automations/include_dir_merge_list"
"include_dir_merge_list", Path.cwd() / "automations/include_dir_merge_list"
),
"automation": IncludedYaml("automations/base.yaml"),
"automation": IncludedYaml(Path.cwd() / "automations/base.yaml"),
},
)
@@ -71,10 +80,11 @@ class dumping_yamls_tests(TestCase):
bobIsNull:
bobIsNull2: null
google_password: !secret gpass
sensor: !include_dir_list sensors/*yaml
sensor: !include_dir_list sensors
automations: !include automations.yaml
"""
)
),
Path.cwd(),
)
self.assertDictEqual(
dict(yaml_obj),
@@ -85,12 +95,12 @@ class dumping_yamls_tests(TestCase):
"bobIsNull": None,
"bobIsNull2": None,
"google_password": SecretValue("gpass", "n/a"),
"sensor": IncludedYamlDir("include_dir_list", "sensors/*yaml"),
"automations": IncludedYaml("automations.yaml"),
"sensor": IncludedYamlDir("include_dir_list", Path.cwd() / "sensors"),
"automations": IncludedYaml(Path.cwd() / "automations.yaml"),
},
)
self.assertEqual(
dump_yaml(yaml_obj),
dump_yaml(yaml_obj, Path.cwd()),
"""name: cool
date: '10:00'
bools:
@@ -101,7 +111,7 @@ bools:
bobIsNull: null
bobIsNull2: null
google_password: !secret 'gpass'
sensor: !include_dir_list 'sensors/*yaml'
sensor: !include_dir_list 'sensors'
automations: !include 'automations.yaml'
""",
)
+7 -5
View File
@@ -6,7 +6,7 @@ from typing import List, Literal, Optional, Tuple
from src.automations.manager import AutomationManager
from src.automations.types import BaseAutomation
from src.hass_config.loader import HassConfig
from src.yaml_serializer import dump_yaml, IncludedYaml
from src.yaml_serializer import IncludedYaml, dump_yaml
THIS_FOLDER = Path(__file__).parent
SAMPLES_FOLDER = THIS_FOLDER / "samples"
@@ -96,11 +96,13 @@ def create_dummy_config_folder(
configuration_yaml["automation"] = auto_prims
elif automation_in_conifguration_mode == "include":
configuration_yaml["automation"] = IncludedYaml(
"automations.yaml",
root_folder / "automations.yaml",
)
if automation_in_conifguration_mode != "inline":
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims))
(root_folder / "automations.yaml").write_text(dump_yaml(auto_prims, root_path=root_folder))
if secrets is not None:
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets))
(root_folder / "configuration.yaml").write_text(dump_yaml(configuration_yaml))
(root_folder / "secrets.yaml").write_text(dump_yaml(secrets, root_path=root_folder))
(root_folder / "configuration.yaml").write_text(
dump_yaml(configuration_yaml, root_path=root_folder)
)
return root_folder