-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate_json.py
103 lines (90 loc) · 3.24 KB
/
generate_json.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from json import dumps
from sys import argv
from re import compile
from pathlib import Path
if __name__ != "__main__":
exit(0)
if len(argv) != 3:
print("Usage: generate_json.py <TDLib_version> <Last_TDLib_commit_hash>")
exit(1)
data = {
"name": "Auto-generated JSON TDLib API for Pytdbot ~ https://github.com/pytdbot/client",
"version": argv[1],
"commit_hash": argv[2],
"classes": {},
"types": {},
"updates": {},
"functions": {},
}
params = {}
start = False
is_functions = False
description = ""
class_regex = compile(r"//@class\s(?P<name>.*)\s@description\s(?P<description>.*)")
description_regex = compile(r"//@description\s(?P<description>.*)$")
parameter_regex = compile(r"@(.*?)\s+([^@]+)")
end_param_regex = compile(r"(?P<name>\w+):(?P<type>[<\w>]+)")
end_regex = compile(r"^(?P<name>.*?)\s(?P<params>.*)=\s(?P<type>\w+);$")
tl = Path("td_api.tl").read_text().replace("\n//-", " ")
def is_optional(d):
return (
"; may be null" in d
or "; pass null" in d
or "; may be empty" in d
or "If non-empty," in d
)
for line in tl.split("\n"):
if "--functions--" in line:
is_functions = True
continue
if line.startswith("//"):
start = True
if line != "" and start:
if _class := class_regex.match(line):
data["classes"][_class.group("name").strip()] = {
"description": _class.group("description").strip(),
"types": [],
"functions": [],
}
elif _param := parameter_regex.findall(line):
for name, _description in _param:
if name.strip() == "description":
description = _description.strip()
else:
params[name.replace("param_", "").strip()] = _description.strip()
elif _end := end_regex.match(line):
_data = {
"description": description,
"args": {},
"type": _end.group("type").strip(),
}
for keyv in end_param_regex.finditer(_end.group("params")):
k, v = keyv.group("name").strip(), keyv.group("type").strip()
_data["args"][k] = {
"description": params[k],
"is_optional": is_optional(params[k]),
"type": v,
}
json_name = _end.group("name").strip()
if is_functions:
data["functions"][json_name] = _data
if _data["type"] in data["classes"]:
data["classes"][_data["type"]]["functions"].append(json_name)
elif json_name.startswith("update"):
data["updates"][json_name] = _data
else:
data["types"][json_name] = _data
if _data["type"] in data["classes"]:
data["classes"][_data["type"]]["types"].append(json_name)
params = {}
description = ""
with open("td_api.json", "w") as f:
f.write(dumps(data, indent=4))
print(
"Classes: {}\nTypes: {}\nFunctions: {}\nUpdates: {}".format(
len(data["classes"]),
len(data["types"]),
len(data["functions"]),
len(data["updates"]),
)
)