mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
[WIP] Config validation error line numbers (#3976)
Config validation error line numbers
This commit is contained in:
parent
d308ea69ce
commit
0dfcf40d37
@ -431,9 +431,10 @@ def log_exception(ex, domain, config, hass=None):
|
|||||||
else:
|
else:
|
||||||
message += '{}.'.format(humanize_error(config, ex))
|
message += '{}.'.format(humanize_error(config, ex))
|
||||||
|
|
||||||
if hasattr(config, '__line__'):
|
domain_config = config.get(domain, config)
|
||||||
message += " (See {}:{})".format(
|
message += " (See {}:{})".format(
|
||||||
config.__config_file__, config.__line__ or '?')
|
getattr(domain_config, '__config_file__', '?'),
|
||||||
|
getattr(domain_config, '__line__', '?'))
|
||||||
|
|
||||||
if domain != 'homeassistant':
|
if domain != 'homeassistant':
|
||||||
message += (' Please check the docs at '
|
message += (' Please check the docs at '
|
||||||
|
@ -134,11 +134,8 @@ def _ordered_dict(loader: SafeLineLoader,
|
|||||||
nodes = loader.construct_pairs(node)
|
nodes = loader.construct_pairs(node)
|
||||||
|
|
||||||
seen = {} # type: Dict
|
seen = {} # type: Dict
|
||||||
min_line = None
|
for (key, _), (child_node, _) in zip(nodes, node.value):
|
||||||
for (key, _), (node, _) in zip(nodes, node.value):
|
line = child_node.start_mark.line
|
||||||
line = getattr(node, '__line__', 'unknown')
|
|
||||||
if line != 'unknown' and (min_line is None or line < min_line):
|
|
||||||
min_line = line
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
hash(key)
|
hash(key)
|
||||||
@ -146,7 +143,7 @@ def _ordered_dict(loader: SafeLineLoader,
|
|||||||
fname = getattr(loader.stream, 'name', '')
|
fname = getattr(loader.stream, 'name', '')
|
||||||
raise yaml.MarkedYAMLError(
|
raise yaml.MarkedYAMLError(
|
||||||
context="invalid key: \"{}\"".format(key),
|
context="invalid key: \"{}\"".format(key),
|
||||||
context_mark=yaml.Mark(fname, 0, min_line, -1, None, None)
|
context_mark=yaml.Mark(fname, 0, line, -1, None, None)
|
||||||
)
|
)
|
||||||
|
|
||||||
if key in seen:
|
if key in seen:
|
||||||
@ -161,7 +158,22 @@ def _ordered_dict(loader: SafeLineLoader,
|
|||||||
|
|
||||||
processed = OrderedDict(nodes)
|
processed = OrderedDict(nodes)
|
||||||
setattr(processed, '__config_file__', loader.name)
|
setattr(processed, '__config_file__', loader.name)
|
||||||
setattr(processed, '__line__', min_line)
|
setattr(processed, '__line__', node.start_mark.line)
|
||||||
|
return processed
|
||||||
|
|
||||||
|
|
||||||
|
def _construct_seq(loader: SafeLineLoader, node: yaml.nodes.Node):
|
||||||
|
"""Add line number and file name to Load YAML sequence."""
|
||||||
|
obj, = loader.construct_yaml_seq(node)
|
||||||
|
|
||||||
|
class NodeClass(list):
|
||||||
|
"""Wrapper class to be able to add attributes on a list."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
processed = NodeClass(obj)
|
||||||
|
setattr(processed, '__config_file__', loader.name)
|
||||||
|
setattr(processed, '__line__', node.start_mark.line)
|
||||||
return processed
|
return processed
|
||||||
|
|
||||||
|
|
||||||
@ -231,6 +243,8 @@ def _secret_yaml(loader: SafeLineLoader,
|
|||||||
yaml.SafeLoader.add_constructor('!include', _include_yaml)
|
yaml.SafeLoader.add_constructor('!include', _include_yaml)
|
||||||
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
yaml.SafeLoader.add_constructor(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
|
||||||
_ordered_dict)
|
_ordered_dict)
|
||||||
|
yaml.SafeLoader.add_constructor(
|
||||||
|
yaml.resolver.BaseResolver.DEFAULT_SEQUENCE_TAG, _construct_seq)
|
||||||
yaml.SafeLoader.add_constructor('!env_var', _env_var_yaml)
|
yaml.SafeLoader.add_constructor('!env_var', _env_var_yaml)
|
||||||
yaml.SafeLoader.add_constructor('!secret', _secret_yaml)
|
yaml.SafeLoader.add_constructor('!secret', _secret_yaml)
|
||||||
yaml.SafeLoader.add_constructor('!include_dir_list', _include_dir_list_yaml)
|
yaml.SafeLoader.add_constructor('!include_dir_list', _include_dir_list_yaml)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user