2021-03-09 02:53:20 +01:00
|
|
|
"""Helpers to retrieve schema from voluptuous validators.
|
|
|
|
|
|
|
|
These are a helper decorators to help get schema from some
|
2022-04-03 09:30:22 +02:00
|
|
|
components which uses voluptuous in a way where validation
|
2021-03-09 02:53:20 +01:00
|
|
|
is hidden in local functions
|
|
|
|
These decorators should not modify at all what the functions
|
|
|
|
originally do.
|
|
|
|
However there is a property to further disable decorator
|
|
|
|
impact."""
|
|
|
|
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
# This is set to true by script/build_language_schema.py
|
2021-03-08 01:05:08 +01:00
|
|
|
# only, so data is collected (again functionality is not modified)
|
2022-06-17 03:46:20 +02:00
|
|
|
EnableSchemaExtraction = False
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
extended_schemas = {}
|
|
|
|
list_schemas = {}
|
|
|
|
registry_schemas = {}
|
|
|
|
hidden_schemas = {}
|
|
|
|
typed_schemas = {}
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
# This key is used to generate schema files of Esphome configuration.
|
|
|
|
SCHEMA_EXTRACT = object()
|
2021-03-08 01:05:08 +01:00
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
|
|
|
|
def schema_extractor(validator_name):
|
|
|
|
if EnableSchemaExtraction:
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
def decorator(func):
|
2022-04-03 09:30:22 +02:00
|
|
|
hidden_schemas[repr(func)] = validator_name
|
2021-03-08 01:05:08 +01:00
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def dummy(f):
|
|
|
|
return f
|
|
|
|
|
|
|
|
return dummy
|
|
|
|
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
def schema_extractor_extended(func):
|
|
|
|
if EnableSchemaExtraction:
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
def decorate(*args, **kwargs):
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
assert len(args) == 2
|
2022-04-03 09:30:22 +02:00
|
|
|
extended_schemas[repr(ret)] = args
|
2021-03-08 01:05:08 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
return decorate
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
def schema_extractor_list(func):
|
|
|
|
if EnableSchemaExtraction:
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
def decorate(*args, **kwargs):
|
|
|
|
ret = func(*args, **kwargs)
|
|
|
|
# args length might be 2, but 2nd is always validator
|
2022-04-03 09:30:22 +02:00
|
|
|
list_schemas[repr(ret)] = args
|
2021-03-08 01:05:08 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
return decorate
|
|
|
|
|
|
|
|
return func
|
|
|
|
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
def schema_extractor_registry(registry):
|
|
|
|
if EnableSchemaExtraction:
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
def decorator(func):
|
2022-04-03 09:30:22 +02:00
|
|
|
registry_schemas[repr(func)] = registry
|
2021-03-08 01:05:08 +01:00
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
def dummy(f):
|
|
|
|
return f
|
|
|
|
|
|
|
|
return dummy
|
|
|
|
|
|
|
|
|
2022-06-17 03:46:20 +02:00
|
|
|
def schema_extractor_typed(func):
|
|
|
|
if EnableSchemaExtraction:
|
2021-03-08 01:05:08 +01:00
|
|
|
|
|
|
|
def decorate(*args, **kwargs):
|
|
|
|
ret = func(*args, **kwargs)
|
2022-04-03 09:30:22 +02:00
|
|
|
typed_schemas[repr(ret)] = (args, kwargs)
|
2021-03-08 01:05:08 +01:00
|
|
|
return ret
|
|
|
|
|
|
|
|
return decorate
|
|
|
|
|
|
|
|
return func
|