1
mirror of https://github.com/home-assistant/core synced 2024-07-30 21:18:57 +02:00

Sorted yaml output for check_config (#3354)

* Consistent display of check_config dicts

* OrderedDict

* remove sorted
This commit is contained in:
Johann Kellerman 2016-09-23 09:10:19 +02:00 committed by Paulus Schoutsen
parent de4c63b437
commit de51cfbc07

View File

@ -7,7 +7,7 @@ from platform import system
from unittest.mock import patch
from typing import Dict, List, Sequence
from collections import OrderedDict
import homeassistant.bootstrap as bootstrap
import homeassistant.config as config_util
import homeassistant.loader as loader
@ -110,7 +110,7 @@ def run(script_args: List) -> int:
domain_info.append(domain)
print(' ', color('bold_red', domain + ':'),
color('red', '', reset='red'))
dump_dict(config, reset='red', indent_count=3)
dump_dict(config, reset='red')
print(color('reset'))
if domain_info:
@ -118,14 +118,14 @@ def run(script_args: List) -> int:
print(color('bold_white', 'Successful config (all)'))
for domain, config in res['components'].items():
print(' ', color(C_HEAD, domain + ':'))
dump_dict(config, indent_count=3)
dump_dict(config)
else:
print(color('bold_white', 'Successful config (partial)'))
for domain in domain_info:
if domain == ERROR_STR:
continue
print(' ', color(C_HEAD, domain + ':'))
dump_dict(res['components'].get(domain, None), indent_count=3)
dump_dict(res['components'].get(domain, None))
if args.secrets:
flatsecret = {}
@ -152,11 +152,11 @@ def run(script_args: List) -> int:
def check(config_path):
"""Perform a check by mocking hass load functions."""
res = {
'yaml_files': {}, # yaml_files loaded
'secrets': {}, # secret cache and secrets loaded
'except': {}, # exceptions raised (with config)
'components': {}, # successful components
'secret_cache': {},
'yaml_files': OrderedDict(), # yaml_files loaded
'secrets': OrderedDict(), # secret cache and secrets loaded
'except': OrderedDict(), # exceptions raised (with config)
'components': OrderedDict(), # successful components
'secret_cache': OrderedDict(),
}
def mock_load(filename): # pylint: disable=unused-variable
@ -236,7 +236,7 @@ def check(config_path):
return res
def dump_dict(layer, indent_count=1, listi=False, **kwargs):
def dump_dict(layer, indent_count=3, listi=False, **kwargs):
"""Display a dict.
A friendly version of print yaml.yaml.dump(config).
@ -249,11 +249,18 @@ def dump_dict(layer, indent_count=1, listi=False, **kwargs):
**kwargs)
return ''
def sort_dict_key(val):
"""Return the dict key for sorting."""
skey = str.lower(val[0])
if str(skey) == 'platform':
skey = '0'
return skey
indent_str = indent_count * ' '
if listi or isinstance(layer, list):
indent_str = indent_str[:-1] + '-'
if isinstance(layer, Dict):
for key, value in layer.items():
for key, value in sorted(layer.items(), key=sort_dict_key):
if isinstance(value, dict) or isinstance(value, list):
print(indent_str, key + ':', line_src(value))
dump_dict(value, indent_count + 2)
@ -263,6 +270,6 @@ def dump_dict(layer, indent_count=1, listi=False, **kwargs):
if isinstance(layer, Sequence):
for i in layer:
if isinstance(i, dict):
dump_dict(i, indent_count, True)
dump_dict(i, indent_count+2, True)
else:
print(indent_str, i)
print(' ', indent_str, i)