-
Notifications
You must be signed in to change notification settings - Fork 13
/
gen_pyi.py
133 lines (119 loc) · 3.61 KB
/
gen_pyi.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
# type: ignore
import pdoc
import textwrap
import re
import mirai
# from objprint import op
def indent(text, n):
return textwrap.indent(text, ' ' * n * 4)
module = pdoc.Module(mirai.models.api)
s = ""
for api in sorted(
set(mirai.models.api.ApiModel.__indexes__.values()),
key=lambda x: x.__name__
):
c = module.find_class(api)
print('正在处理:', api.__name__)
anno = api.__annotations__
# op(c)
params = ', '.join(
'{}: {}'.format(
k, c.doc[k].type_annotation().replace("\xa0", "") + (
"" if api.__fields__[k].
required else f" = {api.__fields__[k].default!r}"
)
) for k, v in anno.items() if k[0] != '_' and k != 'Info'
)
params_doc = '\n'.join(
'{} (`{}`): {}'.format(
k, c.doc[k].type_annotation().replace("\xa0", ""),
c.doc[k].docstring.rstrip('。') + (
"。" if api.__fields__[k].
required else f",默认值 {api.__fields__[k].default!r}。"
)
) for k, v in anno.items() if k[0] != '_' and k != 'Info'
)
s += f'''
# {api.__name__}
'''
try:
response_type = api.Info.response_type
response_type_name = response_type.__qualname__
except AttributeError:
response_type_name = 'None'
try:
response_post_type = api.Info.response_type_post
response_post_type_name = response_post_type.__name__
except AttributeError:
response_post_type_name = 'None'
print(response_type_name)
if issubclass(api, mirai.models.api.ApiGet):
s += f'''
@type_check_only
class __{api.__name__}Proxy():
async def get(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
async def __call__(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
'''
elif issubclass(api, mirai.models.api.ApiPost):
s += f'''
@type_check_only
class __{api.__name__}Proxy():
async def set(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
async def __call__(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
'''
elif issubclass(api, mirai.models.api.ApiRest):
s += f'''
@type_check_only
class __{api.__name__}Proxy():
async def get(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
async def set(self, {params}) -> {response_post_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
async def __call__(self, {params}) -> {response_type_name}:
"""{c.docstring}
Args:
{indent(params_doc, 4)}
"""
'''
s += f'''
@property
def {api.Info.alias}(self) -> __{api.__name__}Proxy:
"""{c.docstring}
Args:
{indent(params_doc, 3)}
"""
'''
s = re.sub(r'Args:(\n\s*)*\s*"""', '"""', s)
s = re.sub(r'"""(.+)(\n\s*)+\s*"""', r'"""\1"""', s)
s = s.replace('NoneType', 'None').replace(', )', ')')
s = s.replace('pathlib.Path', 'Path')
s = re.sub(r'mirai\.(\w+\.)*(\w)', lambda m: m.group(2), s)
with open('./mirai/bot.pyi', 'r', encoding='utf-8') as f:
pyi = f.read()
p = '### 以下为自动生成 ###'
s = pyi[:pyi.find(p) + len(p)] + s
with open('./mirai/bot.pyi', 'w', encoding='utf-8') as f:
f.write(s)