forked from kbengine/kbengine_docs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_python_tips.py
230 lines (182 loc) · 5.02 KB
/
generate_python_tips.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# -*- coding: utf-8 -*-
import os
import os.path
import sys
import time
import shutil
rootdir = "api"
pythonstr = ""
isModuleAPI = False
ignoreKeys = ["属性文档", "回调函数文档", "成员函数文档"]
def findDocsFiles():
files = []
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
f = parent + "\\" + filename
if ".htm" in filename and ("Classes" in parent or "Modules" in parent):
files.append(f)
return files
def modifyDatas(prefix, data):
data = data.replace(" ", " ").replace("\r", "")
data = data.replace("<", "〈").replace(">", "〉")
#data = data.rstrip()
data.replace(r"""<br>""", "\n")
while True:
i = data.find("<")
if i < 0:
break
ii = data.find(">", i)
if ii > 0:
data = data.replace(data[i : ii + 1], "")
if len(data) == 0:
return ""
return prefix + data
def parseFunctions(data, substate):
global pythonstr
global isModuleAPI
if substate == 0:
if r"""<div class="function_description">""" in data:
substate = 1
data = modifyDatas(isModuleAPI + "\t", data)
pythonstr += data
if len(data) > 0 and not isModuleAPI:
data += "\t"
elif substate == 1:
if r"""definition">""" in data:
substate = 4
else:
if r"""</div>""" in data:
substate = 2
else:
pythonstr += modifyDatas(isModuleAPI + "\t", data)
elif substate == 2:
if r"""definition">""" in data:
substate = 4
else:
if r"""</table>""" in data:
substate = 3
else:
hasArgTitle = "function_parameter_name" in data
if hasArgTitle:
hasArgTitle = "@"
else:
hasArgTitle = ""
pythonstr += modifyDatas(isModuleAPI + "\t" + hasArgTitle, data)
else:
if r"""definition">""" in data:
substate = 4
else:
pythonstr += modifyDatas(isModuleAPI + "\t", data)
return substate
def parseAttributes(data, substate):
global pythonstr
global isModuleAPI
if substate == 0:
if r"""<div class="attribute_description">""" in data:
substate = 1
pythonstr += modifyDatas(isModuleAPI + "\t", data)
elif substate == 1:
if r"""definition">""" in data:
substate = 2
else:
if r"""</div>""" in data:
substate = 2
else:
pythonstr += modifyDatas(isModuleAPI + "\t", data)
return substate
def writeTipsPy(processName, moduleName, datas):
dirstr = "tips\\" + processName + "\\"
try:
os.makedirs(dirstr)
except:
pass
f = open(dirstr + moduleName + ".py", "a+", encoding="UTF-8")
f.write(datas)
f.close()
print("save to: %s\\%s.py" % (os.getcwd(), dirstr + moduleName))
def parseDocs(f):
print("====>parseDocs(): " + f + "...")
fd = open(f, "r")
lines = fd.readlines()
fd.close()
# 主状态1:分析方法
# 子状态1:分析主描述
# 子状态2:分析参数描述
# 子状态3:分析返回值描述
# 子状态4:分析结束
# 主状态2:分析属性
# 子状态1:分析主描述
# 子状态2:分析结束
# 主状态3:分析子类
# 子状态1:分析主描述
# 子状态2:分析结束
state = 0
substate = 0
global pythonstr
global isModuleAPI
moduleDocs = ""
pythonstr = ""
isModuleAPI = "Modules" in f
processName = f.split("\\")[1]
moduleName = os.path.basename(f).split(".")[0]
if not isModuleAPI:
isModuleAPI = "\t"
moduleDocs += "class " + moduleName + ":\n"
else:
isModuleAPI = ""
for data in lines:
for key in ignoreKeys:
if key in data:
state = 0
substate = 0
continue
#print("%i---%i--%s" % (state, substate, pythonstr))
if state == 0:
if len(pythonstr) > 0:
pythonstr += "\n\t" + isModuleAPI + "\"\"\"\n\t" + isModuleAPI + "pass" +"\n\n"
moduleDocs += pythonstr
pythonstr = ""
if r"""<span class="function_definition">""" in data:
pythonstr = modifyDatas(isModuleAPI, data)
if len(pythonstr) > 0 and pythonstr[-1] == "\n":
pythonstr = pythonstr[0 : len(pythonstr) - 1]
state = 1
pythonstr += "\n\t" + isModuleAPI + "\"\"\""
elif r"""<span class="attribute_definition">""" in data:
pythonstr = isModuleAPI + "@property\n"
pythonstr += modifyDatas(isModuleAPI + "def ", data)
if len(pythonstr) > 0 and pythonstr[-1] == "\n":
pythonstr = pythonstr[0 : len(pythonstr) - 1]
if len(isModuleAPI) > 0:
pythonstr += "( self ):\n"
else:
pythonstr += "():\n"
pythonstr += "\n\t" + isModuleAPI + "\"\"\""
state = 2
elif state == 1:
substate = parseFunctions(data, substate)
if substate == 4:
substate = 0
state = 0
else:
substate = parseAttributes(data, substate)
if substate == 2:
substate = 0
state = 0
if len(pythonstr) > 0:
pythonstr += "\n\t" + isModuleAPI + "\"\"\"\n\t" + isModuleAPI + "pass" +"\n\n"
pythonstr = pythonstr.replace("版权归KBEngine所有。", "")
moduleDocs += pythonstr
writeTipsPy(processName, "KBEngine", moduleDocs)
def generatePythonTips():
print("generatePythonTips: start...")
try:
shutil.rmtree(r'tips')
except:
pass
files = findDocsFiles()
for f in files:
parseDocs(f)
print("generatePythonTips: over!")
if __name__ == "__main__":
generatePythonTips()