noop yaml loader refactor
This commit is contained in:
1 parent
0cf3f2706a
commit
97521097dc
17 files changed
+147
-93
No files matched your search
@@ -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))
|
||||
Reference in new issue
Block a user