-
Notifications
You must be signed in to change notification settings - Fork 45
/
generate_schema_docs.py
67 lines (57 loc) · 2.14 KB
/
generate_schema_docs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import Dict
import re
import components.opentherm.schema as schema
LINESEP = "\n"
MD_LINEBREAK = " \n"
README = "README.md"
BEGIN_PATTERN = re.compile(r"<!-- BEGIN schema_docs:(\w+) -->")
END_PATTERN = re.compile(r"<!-- END schema_docs:(\w+) -->")
def begins_section(line: str) -> str | None:
match = BEGIN_PATTERN.match(line)
if match:
return match.group(1)
return None
def ends_section(line: str) -> bool:
match = END_PATTERN.match(line)
return match != None
def replace_docs(sections: Dict[str, str]) -> None:
with open(README, "r") as f:
lines = f.readlines()
with open(README, "w", encoding = "utf-8") as f:
in_section = False
for line in lines:
section = begins_section(line)
if section:
in_section = True
f.write(line)
f.write(sections[section])
continue
if ends_section(line):
in_section = False
if not in_section:
f.write(line)
sections = {
"input": LINESEP.join([
f"- `{key}`: {sch['description']} ({sch['unit_of_measurement']})"
+ MD_LINEBREAK + f" Default `min_value`: {sch['range'][0]}"
+ MD_LINEBREAK + f" Default `max_value`: {sch['range'][1]}"
+ (MD_LINEBREAK + f" Supports `auto_min_value`" if "auto_min_value" in sch else "")
+ (MD_LINEBREAK + f" Supports `auto_max_value`" if "auto_max_value" in sch else "")
for key, sch in schema.INPUTS.items()
]) + LINESEP,
"switch": LINESEP.join([
f"- `{key}`: {sch['description']}"
+ MD_LINEBREAK + f" Defaults to *{sch['default_mode'].endswith('on')}*"
for key, sch in schema.SWITCHES.items()
]) + LINESEP,
"binary_sensor": LINESEP.join([
f"- `{key}`: {sch['description']}"
for key, sch in schema.BINARY_SENSORS.items()
]) + LINESEP,
"sensor": LINESEP.join([
f"- `{key}`: {sch['description']}"
+ (f" ({sch['unit_of_measurement']})" if "unit_of_measurement" in sch else "")
for key, sch in schema.SENSORS.items()
]) + LINESEP,
}
replace_docs(sections)