2018-10-31 12:49:54 +00:00
|
|
|
"""ruamel.yaml utility functions."""
|
2021-03-17 20:46:07 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-12-09 15:42:10 +00:00
|
|
|
from collections import OrderedDict
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2018-10-31 12:49:54 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2018-11-23 21:56:58 +00:00
|
|
|
from os import O_CREAT, O_TRUNC, O_WRONLY, stat_result
|
2021-03-17 20:46:07 +00:00
|
|
|
from typing import Dict, List, Union
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
import ruamel.yaml
|
2020-03-12 10:52:20 +00:00
|
|
|
from ruamel.yaml import YAML # type: ignore
|
2019-12-09 15:42:10 +00:00
|
|
|
from ruamel.yaml.compat import StringIO
|
2018-10-31 12:49:54 +00:00
|
|
|
from ruamel.yaml.constructor import SafeConstructor
|
|
|
|
from ruamel.yaml.error import YAMLError
|
|
|
|
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2019-12-09 15:42:10 +00:00
|
|
|
from homeassistant.util.yaml import secret_yaml
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
JSON_TYPE = Union[List, Dict, str] # pylint: disable=invalid-name
|
|
|
|
|
|
|
|
|
|
|
|
class ExtSafeConstructor(SafeConstructor):
|
|
|
|
"""Extended SafeConstructor."""
|
|
|
|
|
2021-03-17 20:46:07 +00:00
|
|
|
name: str | None = None
|
2019-07-20 21:35:22 +00:00
|
|
|
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
class UnsupportedYamlError(HomeAssistantError):
|
|
|
|
"""Unsupported YAML."""
|
|
|
|
|
|
|
|
|
|
|
|
class WriteError(HomeAssistantError):
|
|
|
|
"""Error writing the data."""
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _include_yaml(
|
|
|
|
constructor: ExtSafeConstructor, node: ruamel.yaml.nodes.Node
|
|
|
|
) -> JSON_TYPE:
|
2018-10-31 12:49:54 +00:00
|
|
|
"""Load another YAML file and embeds it using the !include tag.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
device_tracker: !include device_tracker.yaml
|
2019-08-04 15:05:43 +00:00
|
|
|
|
2018-10-31 12:49:54 +00:00
|
|
|
"""
|
2019-07-20 21:35:22 +00:00
|
|
|
if constructor.name is None:
|
|
|
|
raise HomeAssistantError(
|
2019-07-31 19:25:30 +00:00
|
|
|
"YAML include error: filename not set for %s" % node.value
|
|
|
|
)
|
2018-10-31 12:49:54 +00:00
|
|
|
fname = os.path.join(os.path.dirname(constructor.name), node.value)
|
|
|
|
return load_yaml(fname, False)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
def _yaml_unsupported(
|
|
|
|
constructor: ExtSafeConstructor, node: ruamel.yaml.nodes.Node
|
|
|
|
) -> None:
|
2018-10-31 12:49:54 +00:00
|
|
|
raise UnsupportedYamlError(
|
2020-04-05 15:48:55 +00:00
|
|
|
f"Unsupported YAML, you can not use {node.tag} in "
|
|
|
|
f"{os.path.basename(constructor.name or '(None)')}"
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def object_to_yaml(data: JSON_TYPE) -> str:
|
|
|
|
"""Create yaml string from object."""
|
2019-07-31 19:25:30 +00:00
|
|
|
yaml = YAML(typ="rt")
|
2018-10-31 12:49:54 +00:00
|
|
|
yaml.indent(sequence=4, offset=2)
|
|
|
|
stream = StringIO()
|
|
|
|
try:
|
|
|
|
yaml.dump(data, stream)
|
2019-09-04 03:36:04 +00:00
|
|
|
result: str = stream.getvalue()
|
2018-10-31 12:49:54 +00:00
|
|
|
return result
|
|
|
|
except YAMLError as exc:
|
|
|
|
_LOGGER.error("YAML error: %s", exc)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise HomeAssistantError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def yaml_to_object(data: str) -> JSON_TYPE:
|
|
|
|
"""Create object from yaml string."""
|
2019-07-31 19:25:30 +00:00
|
|
|
yaml = YAML(typ="rt")
|
2018-10-31 12:49:54 +00:00
|
|
|
try:
|
2021-03-17 20:46:07 +00:00
|
|
|
result: list | dict | str = yaml.load(data)
|
2018-10-31 12:49:54 +00:00
|
|
|
return result
|
|
|
|
except YAMLError as exc:
|
|
|
|
_LOGGER.error("YAML error: %s", exc)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise HomeAssistantError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def load_yaml(fname: str, round_trip: bool = False) -> JSON_TYPE:
|
|
|
|
"""Load a YAML file."""
|
|
|
|
if round_trip:
|
2019-07-31 19:25:30 +00:00
|
|
|
yaml = YAML(typ="rt")
|
2020-03-12 10:52:20 +00:00
|
|
|
yaml.preserve_quotes = True
|
2018-10-31 12:49:54 +00:00
|
|
|
else:
|
2019-07-20 21:35:22 +00:00
|
|
|
if ExtSafeConstructor.name is None:
|
2018-11-11 16:15:58 +00:00
|
|
|
ExtSafeConstructor.name = fname
|
2019-07-31 19:25:30 +00:00
|
|
|
yaml = YAML(typ="safe")
|
2018-10-31 12:49:54 +00:00
|
|
|
yaml.Constructor = ExtSafeConstructor
|
|
|
|
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
with open(fname, encoding="utf-8") as conf_file:
|
2018-10-31 12:49:54 +00:00
|
|
|
# If configuration file is empty YAML returns None
|
|
|
|
# We convert that to an empty dict
|
|
|
|
return yaml.load(conf_file) or OrderedDict()
|
|
|
|
except YAMLError as exc:
|
|
|
|
_LOGGER.error("YAML error in %s: %s", fname, exc)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise HomeAssistantError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
except UnicodeDecodeError as exc:
|
|
|
|
_LOGGER.error("Unable to read file %s: %s", fname, exc)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise HomeAssistantError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
def save_yaml(fname: str, data: JSON_TYPE) -> None:
|
|
|
|
"""Save a YAML file."""
|
2019-07-31 19:25:30 +00:00
|
|
|
yaml = YAML(typ="rt")
|
2018-10-31 12:49:54 +00:00
|
|
|
yaml.indent(sequence=4, offset=2)
|
2020-01-03 13:47:06 +00:00
|
|
|
tmp_fname = f"{fname}__TEMP__"
|
2018-10-31 12:49:54 +00:00
|
|
|
try:
|
2018-11-23 21:56:58 +00:00
|
|
|
try:
|
|
|
|
file_stat = os.stat(fname)
|
|
|
|
except OSError:
|
2019-07-31 19:25:30 +00:00
|
|
|
file_stat = stat_result((0o644, -1, -1, -1, -1, -1, -1, -1, -1, -1))
|
|
|
|
with open(
|
|
|
|
os.open(tmp_fname, O_WRONLY | O_CREAT | O_TRUNC, file_stat.st_mode),
|
|
|
|
"w",
|
|
|
|
encoding="utf-8",
|
|
|
|
) as temp_file:
|
2018-10-31 12:49:54 +00:00
|
|
|
yaml.dump(data, temp_file)
|
|
|
|
os.replace(tmp_fname, fname)
|
2019-07-31 19:25:30 +00:00
|
|
|
if hasattr(os, "chown") and file_stat.st_ctime > -1:
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(OSError):
|
2018-11-05 20:41:19 +00:00
|
|
|
os.chown(fname, file_stat.st_uid, file_stat.st_gid)
|
2018-10-31 12:49:54 +00:00
|
|
|
except YAMLError as exc:
|
|
|
|
_LOGGER.error(str(exc))
|
2020-08-28 11:50:32 +00:00
|
|
|
raise HomeAssistantError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
except OSError as exc:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.exception("Saving YAML file %s failed: %s", fname, exc)
|
2020-08-28 11:50:32 +00:00
|
|
|
raise WriteError(exc) from exc
|
2018-10-31 12:49:54 +00:00
|
|
|
finally:
|
|
|
|
if os.path.exists(tmp_fname):
|
|
|
|
try:
|
|
|
|
os.remove(tmp_fname)
|
|
|
|
except OSError as exc:
|
|
|
|
# If we are cleaning up then something else went wrong, so
|
|
|
|
# we should suppress likely follow-on errors in the cleanup
|
|
|
|
_LOGGER.error("YAML replacement cleanup failed: %s", exc)
|
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ExtSafeConstructor.add_constructor("!secret", secret_yaml)
|
|
|
|
ExtSafeConstructor.add_constructor("!include", _include_yaml)
|
2018-10-31 12:49:54 +00:00
|
|
|
ExtSafeConstructor.add_constructor(None, _yaml_unsupported)
|