Skip to content

Commit

Permalink
add generated loggers list
Browse files Browse the repository at this point in the history
  • Loading branch information
glmnet committed Mar 3, 2021
1 parent a9f0726 commit 01758b5
Showing 1 changed file with 63 additions and 16 deletions.
79 changes: 63 additions & 16 deletions script/build_jsonschema.py
Expand Up @@ -141,6 +141,21 @@ def get_dirs():
return dir_names


def get_logger_tags():
from esphome.config import CORE_COMPONENTS_PATH
import glob
pattern = re.compile(r'^static const char(\*\s|\s\*)TAG = "(\w.*)";', re.MULTILINE)
tags = ['app', 'component', 'esphal', 'helpers', 'preferences', 'scheduler', 'api.service']
for x in os.walk(CORE_COMPONENTS_PATH):
for y in glob.glob(os.path.join(x[0], '*.cpp')):
with open(y, 'r') as file:
data = file.read()
match = pattern.search(data)
if match:
tags.append(match.group(2))
return tags


def load_components():
import esphome.config_validation as cv
from esphome.config import get_component
Expand Down Expand Up @@ -307,7 +322,19 @@ def get_entry(parent_key, vschema):
return None
else:
# everything else just accept string and let ESPHome validate
entry = default_schema()
try:
from esphome.core import ID
v = vschema(None)
if isinstance(v, ID):
entry = {'type': 'string', 'id_type': v.type.base}
elif isinstance(v, str):
entry = {'type': 'string'}
elif isinstance(v, list):
entry = {'type': 'array'}
else:
entry = default_schema()
except:
entry = default_schema()

return entry

Expand Down Expand Up @@ -498,23 +525,41 @@ def convert_schema(path, vschema, un_extend=True):
if DUMP_COMMENTS:
output[JSC_COMMENT] = 'converted: ' + path + '/' + str(vschema)

for k in vschema:
v = vschema[k]
prop = {}
if path == 'logger-logs':
tags = get_logger_tags()
for k in tags:
props[k] = {"enum":
['NONE', 'ERROR', 'WARN', 'INFO', 'DEBUG', 'VERBOSE', 'VERY_VERBOSE']}

if isinstance(v, vol.Schema):
prop = get_jschema(path + '-' + str(k), v.schema)
elif hasattr(v, 'validators'):
prop = convert_schema(path + '-' + str(k), v, False)
else:
prop = get_entry(path + '-' + str(k), v)
else:
for k in vschema:
if str(k).startswith('<function'):
# generate all logger tags

# TODO handle key functions

if prop: # Deprecated (cv.Invalid) properties not added
props[str(k)] = prop
# TODO: see required, sometimes completions doesn't show up because of this...
# if isinstance(k, cv.Required):
# required.append(str(k))
continue

v = vschema[k]
prop = {}

if isinstance(v, vol.Schema):
prop = get_jschema(path + '-' + str(k), v.schema)
elif hasattr(v, 'validators'):
prop = convert_schema(path + '-' + str(k), v, False)
else:
prop = get_entry(path + '-' + str(k), v)

if prop: # Deprecated (cv.Invalid) properties not added
props[str(k)] = prop
# TODO: see required, sometimes completions doesn't show up because of this...
# if isinstance(k, cv.Required):
# required.append(str(k))
try:
if str(k.default) != '...':
prop['default'] = k.default()
except:
pass
return output


Expand All @@ -538,7 +583,8 @@ def dump_schema():

schema_registry[cv.boolean] = {"type": "boolean"}

for v in [cv.int_, cv.int_range, cv.float_, cv.positive_float, cv.positive_float,
for v in [cv.int_, cv.int_range, cv.positive_int,
cv.float_, cv.positive_float, cv.positive_float,
cv.positive_not_null_int, cv.negative_one_to_one_float, cv.port]:
schema_registry[v] = {"type": "number"}

Expand All @@ -547,6 +593,7 @@ def dump_schema():
pins.output_pin, pins.input_pin, pins.input_pullup_pin,
cv.subscribe_topic, cv.publish_topic, cv.mqtt_payload,
cv.ssid,
cv.percentage_int, cv.percentage, cv.possibly_negative_percentage,
cv.positive_time_period, cv.positive_time_period_microseconds,
cv.positive_time_period_milliseconds, cv.positive_time_period_minutes,
cv.positive_time_period_seconds]:
Expand Down

0 comments on commit 01758b5

Please sign in to comment.