2016-03-07 22:20:48 +00:00
|
|
|
"""YAML utility functions."""
|
2016-01-24 06:37:15 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2016-04-09 03:53:27 +00:00
|
|
|
from collections import OrderedDict
|
2016-01-24 06:37:15 +00:00
|
|
|
|
2016-05-14 04:16:04 +00:00
|
|
|
import glob
|
2016-01-24 06:37:15 +00:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-04-09 03:53:27 +00:00
|
|
|
# pylint: disable=too-many-ancestors
|
|
|
|
class SafeLineLoader(yaml.SafeLoader):
|
|
|
|
"""Loader class that keeps track of line numbers."""
|
|
|
|
|
|
|
|
def compose_node(self, parent, index):
|
|
|
|
"""Annotate a node with the first line it was seen."""
|
|
|
|
last_line = self.line
|
|
|
|
node = super(SafeLineLoader, self).compose_node(parent, index)
|
|
|
|
node.__line__ = last_line + 1
|
|
|
|
return node
|
|
|
|
|
|
|
|
|
2016-01-24 06:37:15 +00:00
|
|
|
def load_yaml(fname):
|
|
|
|
"""Load a YAML file."""
|
|
|
|
try:
|
|
|
|
with open(fname, encoding='utf-8') as conf_file:
|
|
|
|
# If configuration file is empty YAML returns None
|
|
|
|
# We convert that to an empty dict
|
2016-04-09 03:53:27 +00:00
|
|
|
return yaml.load(conf_file, Loader=SafeLineLoader) or {}
|
2016-04-09 22:25:01 +00:00
|
|
|
except yaml.YAMLError as exc:
|
|
|
|
_LOGGER.error(exc)
|
|
|
|
raise HomeAssistantError(exc)
|
2016-01-24 06:37:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _include_yaml(loader, node):
|
2016-03-07 22:20:48 +00:00
|
|
|
"""Load another YAML file and embeds it using the !include tag.
|
2016-01-24 06:37:15 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
device_tracker: !include device_tracker.yaml
|
|
|
|
"""
|
|
|
|
fname = os.path.join(os.path.dirname(loader.name), node.value)
|
|
|
|
return load_yaml(fname)
|
|
|
|
|
|
|
|
|
2016-05-14 04:16:04 +00:00
|
|
|
def _include_dir_named_yaml(loader, node):
|
|
|
|
"""Load multiple files from dir."""
|
|
|
|
mapping = OrderedDict()
|
|
|
|
files = os.path.join(os.path.dirname(loader.name), node.value, '*.yaml')
|
|
|
|
for fname in glob.glob(files):
|
|
|
|
filename = os.path.splitext(os.path.basename(fname))[0]
|
|
|
|
mapping[filename] = load_yaml(fname)
|
|
|
|
return mapping
|
|
|
|
|
|
|
|
|
|
|
|
def _include_dir_list_yaml(loader, node):
|
|
|
|
"""Load multiple files from dir."""
|
|
|
|
files = os.path.join(os.path.dirname(loader.name), node.value, '*.yaml')
|
|
|
|
return [load_yaml(f) for f in glob.glob(files)]
|
|
|
|
|
|
|
|
|
2016-01-24 06:37:15 +00:00
|
|
|
def _ordered_dict(loader, node):
|
2016-03-07 22:20:48 +00:00
|
|
|
"""Load YAML mappings into an ordered dict to preserve key order."""
|
2016-01-24 06:37:15 +00:00
|
|
|
loader.flatten_mapping(node)
|
2016-04-06 04:21:16 +00:00
|
|
|
nodes = loader.construct_pairs(node)
|
2016-01-24 06:37:15 +00:00
|
|
|
|
2016-04-09 03:53:27 +00:00
|
|
|
seen = {}
|
2016-05-08 05:24:04 +00:00
|
|
|
min_line = None
|
2016-04-09 03:53:27 +00:00
|
|
|
for (key, _), (node, _) in zip(nodes, node.value):
|
|
|
|
line = getattr(node, '__line__', 'unknown')
|
2016-05-08 05:24:04 +00:00
|
|
|
if line != 'unknown' and (min_line is None or line < min_line):
|
|
|
|
min_line = line
|
2016-04-09 03:53:27 +00:00
|
|
|
if key in seen:
|
|
|
|
fname = getattr(loader.stream, 'name', '')
|
2016-04-09 22:25:01 +00:00
|
|
|
first_mark = yaml.Mark(fname, 0, seen[key], -1, None, None)
|
|
|
|
second_mark = yaml.Mark(fname, 0, line, -1, None, None)
|
|
|
|
raise yaml.MarkedYAMLError(
|
|
|
|
context="duplicate key: \"{}\"".format(key),
|
|
|
|
context_mark=first_mark, problem_mark=second_mark,
|
|
|
|
)
|
2016-04-09 03:53:27 +00:00
|
|
|
seen[key] = line
|
|
|
|
|
2016-05-08 05:24:04 +00:00
|
|
|
processed = OrderedDict(nodes)
|
|
|
|
processed.__config_file__ = loader.name
|
|
|
|
processed.__line__ = min_line
|
|
|
|
return processed
|
2016-01-24 06:37:15 +00:00
|
|
|
|
2016-05-04 01:41:14 +00:00
|
|
|
|
|
|
|
def _env_var_yaml(loader, node):
|
|
|
|
"""Load environment variables and embed it into the configuration YAML."""
|
|
|
|
if node.value in os.environ:
|
|
|
|
return os.environ[node.value]
|
|
|
|
else:
|
|
|
|
_LOGGER.error("Environment variable %s not defined.", node.value)
|
|
|
|
raise HomeAssistantError(node.value)
|
|
|
|
|
|
|
|
|
2016-01-30 23:46:08 +00:00
|
|
|
yaml.SafeLoader.add_constructor('!include', _include_yaml)
|
|
|
|
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
|
|
|
_ordered_dict)
|
2016-05-04 01:41:14 +00:00
|
|
|
yaml.SafeLoader.add_constructor('!env_var', _env_var_yaml)
|
2016-05-14 04:16:04 +00:00
|
|
|
yaml.SafeLoader.add_constructor('!include_dir_list', _include_dir_list_yaml)
|
|
|
|
yaml.SafeLoader.add_constructor('!include_dir_named', _include_dir_named_yaml)
|