Split up yaml loaders into multiple files (#23774)

* Start moving parts of yaml utils to own module

Move parts of yaml loader out of the single large file and start
to create the structure of the yaml loaders in Ansible [0].

[0]: https://github.com/ansible/ansible/tree/devel/lib/ansible/parsing/yaml

* Finish yaml migration, update tests and mocks

  * Move code around to finish the migration
  * Update the mocks so that `open` is patched in
    `homeassistant.util.yaml.loader` instead of
    `homeassistant.util.yaml`.
  * Updated mypy ignores
  * Updated external API of `homeasistant.util.yaml`, see below:

Checked what part of the api of `homeassistant.util.yaml` was actually
called from outside the tests and added an `__ALL__` that contains only
these elements.

Updated the tests so that references to internal parts of the API (e.g.
the yaml module imported into `homeassistant.util.yaml.loader`) are
referenced directly from `homeassistant.util.yaml.loader`.

In `tests/test_yaml.py` the import `yaml` refers to
`homeassistant.util.yaml` and `yaml_loader` refers to `~.loader`.

Future work that remains for the next iteration is to create a custom
SafeConstructor and refers to that instead of monkey patching `yaml` with
custom loaders.

* Update mocks in yaml dumper, check_config
This commit is contained in:
Ties de Kock
2019-05-09 09:07:56 -07:00
committed by Paulus Schoutsen
parent 118d3bc11c
commit 4004867eda
11 changed files with 179 additions and 139 deletions

View File

@@ -17,7 +17,8 @@ from homeassistant.config import (
CONF_PACKAGES, merge_packages_config, _format_config_error,
find_config_file, load_yaml_config_file,
extract_domain_configs, config_per_platform)
from homeassistant.util import yaml
import homeassistant.util.yaml.loader as yaml_loader
from homeassistant.exceptions import HomeAssistantError
REQUIREMENTS = ('colorlog==4.0.2',)
@@ -25,12 +26,14 @@ REQUIREMENTS = ('colorlog==4.0.2',)
_LOGGER = logging.getLogger(__name__)
# pylint: disable=protected-access
MOCKS = {
'load': ("homeassistant.util.yaml.load_yaml", yaml.load_yaml),
'load*': ("homeassistant.config.load_yaml", yaml.load_yaml),
'secrets': ("homeassistant.util.yaml.secret_yaml", yaml.secret_yaml),
'load': ("homeassistant.util.yaml.loader.load_yaml",
yaml_loader.load_yaml),
'load*': ("homeassistant.config.load_yaml", yaml_loader.load_yaml),
'secrets': ("homeassistant.util.yaml.loader.secret_yaml",
yaml_loader.secret_yaml),
}
SILENCE = (
'homeassistant.scripts.check_config.yaml.clear_secret_cache',
'homeassistant.scripts.check_config.yaml_loader.clear_secret_cache',
)
PATCHES = {}
@@ -195,7 +198,8 @@ def check(config_dir, secrets=False):
if secrets:
# Ensure !secrets point to the patched function
yaml.yaml.SafeLoader.add_constructor('!secret', yaml.secret_yaml)
yaml_loader.yaml.SafeLoader.add_constructor('!secret',
yaml_loader.secret_yaml)
try:
hass = core.HomeAssistant()
@@ -203,7 +207,7 @@ def check(config_dir, secrets=False):
res['components'] = hass.loop.run_until_complete(
check_ha_config_file(hass))
res['secret_cache'] = OrderedDict(yaml.__SECRET_CACHE)
res['secret_cache'] = OrderedDict(yaml_loader.__SECRET_CACHE)
for err in res['components'].errors:
domain = err.domain or ERROR_STR
@@ -221,7 +225,8 @@ def check(config_dir, secrets=False):
pat.stop()
if secrets:
# Ensure !secrets point to the original function
yaml.yaml.SafeLoader.add_constructor('!secret', yaml.secret_yaml)
yaml_loader.yaml.SafeLoader.add_constructor(
'!secret', yaml_loader.secret_yaml)
bootstrap.clear_secret_cache()
return res
@@ -239,7 +244,7 @@ def line_info(obj, **kwargs):
def dump_dict(layer, indent_count=3, listi=False, **kwargs):
"""Display a dict.
A friendly version of print yaml.yaml.dump(config).
A friendly version of print yaml_loader.yaml.dump(config).
"""
def sort_dict_key(val):
"""Return the dict key for sorting."""
@@ -311,7 +316,7 @@ async def check_ha_config_file(hass):
return result.add_error(
"Error loading {}: {}".format(config_path, err))
finally:
yaml.clear_secret_cache()
yaml_loader.clear_secret_cache()
# Extract and validate core [homeassistant] config
try: