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