Remove ordered_dict validator (#7375)

* Remove ordered_dict validator

* Lint

* Update test_config_validation.py
This commit is contained in:
Paulus Schoutsen 2017-04-30 10:55:03 -07:00 committed by GitHub
parent 1f4f2d7086
commit 9afbbbf3fe
7 changed files with 8 additions and 90 deletions

View file

@ -64,7 +64,7 @@ GROUP_SCHEMA = vol.Schema({
}) })
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({
DOMAIN: cv.ordered_dict(vol.All(_conf_preprocess, GROUP_SCHEMA)) DOMAIN: vol.Schema({cv.match_all: vol.All(_conf_preprocess, GROUP_SCHEMA)})
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
# List of ON/OFF state tuples for groupable states # List of ON/OFF state tuples for groupable states

View file

@ -22,7 +22,7 @@ CONF_SOURCES = 'sources'
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.string, vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_SOURCES): cv.ordered_dict(cv.string, cv.string), vol.Optional(CONF_SOURCES): vol.Schema({cv.string: cv.string}),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}) })

View file

@ -20,8 +20,8 @@ DEPENDENCIES = ['http']
DOMAIN = 'rss_feed_template' DOMAIN = 'rss_feed_template'
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({
DOMAIN: cv.ordered_dict( DOMAIN: vol.Schema({
vol.Schema({ cv.match_all: vol.Schema({
vol.Optional('requires_api_password', default=True): cv.boolean, vol.Optional('requires_api_password', default=True): cv.boolean,
vol.Optional('title'): cv.template, vol.Optional('title'): cv.template,
vol.Required('items'): vol.All( vol.Required('items'): vol.All(
@ -32,7 +32,7 @@ CONFIG_SCHEMA = vol.Schema({
}] }]
) )
}) })
) })
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)

View file

@ -125,7 +125,7 @@ CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_DEVICE_CONFIG, default={}): vol.Optional(CONF_DEVICE_CONFIG, default={}):
vol.Schema({cv.entity_id: DEVICE_CONFIG_SCHEMA_ENTRY}), vol.Schema({cv.entity_id: DEVICE_CONFIG_SCHEMA_ENTRY}),
vol.Optional(CONF_DEVICE_CONFIG_GLOB, default={}): vol.Optional(CONF_DEVICE_CONFIG_GLOB, default={}):
cv.ordered_dict(DEVICE_CONFIG_SCHEMA_ENTRY, cv.string), vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
vol.Optional(CONF_DEVICE_CONFIG_DOMAIN, default={}): vol.Optional(CONF_DEVICE_CONFIG_DOMAIN, default={}):
vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}), vol.Schema({cv.string: DEVICE_CONFIG_SCHEMA_ENTRY}),
vol.Optional(CONF_DEBUG, default=DEFAULT_DEBUG): cv.boolean, vol.Optional(CONF_DEBUG, default=DEFAULT_DEBUG): cv.boolean,

View file

@ -112,7 +112,7 @@ CUSTOMIZE_CONFIG_SCHEMA = vol.Schema({
vol.Optional(CONF_CUSTOMIZE_DOMAIN, default={}): vol.Optional(CONF_CUSTOMIZE_DOMAIN, default={}):
vol.Schema({cv.string: dict}), vol.Schema({cv.string: dict}),
vol.Optional(CONF_CUSTOMIZE_GLOB, default={}): vol.Optional(CONF_CUSTOMIZE_GLOB, default={}):
cv.ordered_dict(OrderedDict, cv.string), vol.Schema({cv.string: OrderedDict}),
}) })
CORE_CONFIG_SCHEMA = CUSTOMIZE_CONFIG_SCHEMA.extend({ CORE_CONFIG_SCHEMA = CUSTOMIZE_CONFIG_SCHEMA.extend({
@ -454,7 +454,7 @@ def _identify_config_schema(module):
except (AttributeError, KeyError): except (AttributeError, KeyError):
return (None, None) return (None, None)
t_schema = str(schema) t_schema = str(schema)
if t_schema.startswith(('{', '<function ordered_dict')): if t_schema.startswith('{'):
return ('dict', schema) return ('dict', schema)
if t_schema.startswith(('[', 'All(<function ensure_list')): if t_schema.startswith(('[', 'All(<function ensure_list')):
return ('list', schema) return ('list', schema)

View file

@ -1,5 +1,4 @@
"""Helpers for config validation using voluptuous.""" """Helpers for config validation using voluptuous."""
from collections import OrderedDict
from datetime import timedelta, datetime as datetime_sys from datetime import timedelta, datetime as datetime_sys
import os import os
import re import re
@ -373,29 +372,6 @@ def x10_address(value):
return str(value).lower() return str(value).lower()
def ordered_dict(value_validator, key_validator=match_all):
"""Validate an ordered dict validator that maintains ordering.
value_validator will be applied to each value of the dictionary.
key_validator (optional) will be applied to each key of the dictionary.
"""
item_validator = vol.Schema({key_validator: value_validator})
def validator(value):
"""Validate ordered dict."""
config = OrderedDict()
if not isinstance(value, dict):
raise vol.Invalid('Value {} is not a dictionary'.format(value))
for key, val in value.items():
v_res = item_validator({key: val})
config.update(v_res)
return config
return validator
def ensure_list_csv(value: Any) -> Sequence: def ensure_list_csv(value: Any) -> Sequence:
"""Ensure that input is a list or make one from comma-separated string.""" """Ensure that input is a list or make one from comma-separated string."""
if isinstance(value, str): if isinstance(value, str):

View file

@ -1,5 +1,4 @@
"""Test config validators.""" """Test config validators."""
from collections import OrderedDict
from datetime import timedelta, datetime, date from datetime import timedelta, datetime, date
import enum import enum
import os import os
@ -448,63 +447,6 @@ def test_has_at_least_one_key():
schema(value) schema(value)
def test_ordered_dict_only_dict():
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.match_all))
for value in (None, [], 100, 'hello'):
with pytest.raises(vol.MultipleInvalid):
schema(value)
def test_ordered_dict_order():
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(int, cv.string))
val = OrderedDict()
val['first'] = 1
val['second'] = 2
validated = schema(val)
assert isinstance(validated, OrderedDict)
assert ['first', 'second'] == list(validated.keys())
def test_ordered_dict_key_validator():
"""Test ordered_dict key validator."""
schema = vol.Schema(cv.ordered_dict(cv.match_all, cv.string))
with pytest.raises(vol.Invalid):
schema({None: 1})
schema({'hello': 'world'})
schema = vol.Schema(cv.ordered_dict(cv.match_all, int))
with pytest.raises(vol.Invalid):
schema({'hello': 1})
schema({1: 'works'})
def test_ordered_dict_value_validator(): # pylint: disable=invalid-name
"""Test ordered_dict validator."""
schema = vol.Schema(cv.ordered_dict(cv.string))
with pytest.raises(vol.Invalid):
schema({'hello': None})
schema({'hello': 'world'})
schema = vol.Schema(cv.ordered_dict(int))
with pytest.raises(vol.Invalid):
schema({'hello': 'world'})
schema({'hello': 5})
def test_enum(): def test_enum():
"""Test enum validator.""" """Test enum validator."""
class TestEnum(enum.Enum): class TestEnum(enum.Enum):