forked from PaddlePaddle/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_api_from_docs.py
411 lines (356 loc) · 12.6 KB
/
extract_api_from_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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Extract used apis from markdown and reStructured documents.
"""
import argparse
import inspect
import logging
import os
import re
from contextlib import contextmanager
import docutils
import docutils.core
import docutils.nodes
import docutils.parsers.rst
import markdown
logger = logging.getLogger()
if logger.handlers:
# we assume the first handler is the one we want to configure
console = logger.handlers[0]
else:
console = logging.StreamHandler()
logger.addHandler(console)
console.setFormatter(
logging.Formatter(
"%(asctime)s - %(funcName)s:%(lineno)d - %(levelname)s - %(message)s"
)
)
def extract_code_blocks_from_rst(docstr):
"""
extract code-blocks from the given docstring.
DON'T include the multiline-string definition in code-blocks.
Args:
docstr - docstring
Return:
A list of code-blocks, indent removed.
"""
code_blocks = []
ds_list = docstr.expandtabs(tabsize=4).split("\n")
lastlineindex = len(ds_list) - 1
cb_started = False
cb_start_pat = re.compile(r"((code)|(code-block))::\s*i?python[23]?")
cb_cur = []
cb_cur_indent = -1
for lineno, linecont in enumerate(ds_list):
if re.search(cb_start_pat, linecont):
if not cb_started:
cb_started = True
continue
else:
# cur block end
if len(cb_cur):
code_blocks.append(inspect.cleandoc("\n".join(cb_cur)))
cb_started = True # another block started
cb_cur_indent = -1
cb_cur = []
else:
# check indent for cur block ends.
if cb_started:
if lineno == lastlineindex:
mo = re.search(r"\S", linecont)
if mo is not None and cb_cur_indent <= mo.start():
cb_cur.append(linecont)
if len(cb_cur):
code_blocks.append(inspect.cleandoc("\n".join(cb_cur)))
break
if cb_cur_indent < 0:
mo = re.search(r"\S", linecont)
if mo is None:
continue
cb_cur_indent = mo.start()
cb_cur.append(linecont)
else:
mo = re.search(r"\S", linecont)
if mo is None:
continue
if cb_cur_indent <= mo.start():
cb_cur.append(linecont)
else:
if linecont[mo.start()] == "#":
continue
else:
# block end
if len(cb_cur):
code_blocks.append(
inspect.cleandoc("\n".join(cb_cur))
)
cb_started = False
cb_cur_indent = -1
cb_cur = []
logger.info("extracted %d code blocks.", len(code_blocks))
return code_blocks
def extract_code_blocks_from_md(docstr):
"""
Extract code blocks from markdown content.
"""
code_blocks = []
pat = re.compile(r"```i?python[23]?(.*?)```", re.MULTILINE + re.DOTALL)
for cbit in pat.finditer(docstr):
code_blocks.append(inspect.cleandoc(cbit.group()))
# logger.info('extracted %d code blocks.', len(code_blocks))
return code_blocks
def extract_code_blocks_from_file(filename):
r = os.path.splitext(filename)
ext = r[1].lower()
if ext == ".md":
return extract_code_blocks_from_md(open(filename, "r").read())
elif ext == ".rst":
return extract_code_blocks_from_rst(open(filename, "r").read())
else:
return []
def get_all_files(p):
"""
Get a filename list from the dir.
"""
filelist = []
for path, dirs, files in os.walk(p):
for filename in files:
r = os.path.splitext(filename)
logger.info("%s found", filename)
if len(r) == 2 and r[1].lower() in [".md", ".rst"]:
filelist.append(os.path.join(path, filename))
logger.info("find %d files from %s.", len(filelist), p)
return filelist
def find_all_paddle_api_from_code_block(cbstr):
"""
Find All Paddle Api
"""
# case 1.1: import paddle.abs # ignore
# case 1.2: from paddle.vision.transforms import ToTensor
# case 1.3: import paddle.vision.transforms.ToTensor as ToTensor # ignore
# case 2: train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=ToTensor())
# case 3: in comments
# case 4: in docstring
api_set = set()
ds_list = cbstr.split("\n")
import_pat = re.compile(r"from\s+([\.\w]+)\s+import\s+(\w+)")
normal_pat = re.compile(r"(paddle\.[\.\w]+)")
docstr_pat = re.compile(r"((\'{3})|(\"{3}))")
in_docstr = False
for line in ds_list:
line = line.strip()
for mo in docstr_pat.finditer(line):
in_docstr = not in_docstr
if in_docstr:
continue
sharp_ind = line.find("#")
mo_i = import_pat.search(line)
if mo_i:
if (sharp_ind < 0 or mo_i.start() < sharp_ind) and mo_i.group(
1
).startswith("paddle"):
api_set.add(f"{mo_i.group(1)}.{mo_i.group(2)}")
else:
mo_n = normal_pat.finditer(line)
for mo in mo_n:
if sharp_ind < 0 or mo.start() < sharp_ind:
api_set.add(mo.group(1))
return api_set
def extract_api_from_file(filename):
api_set = set()
codeblocks = extract_code_blocks_from_file(filename)
logger.info("find %d code-blocks from %s.", len(codeblocks), filename)
for cb in codeblocks:
api_set.update(find_all_paddle_api_from_code_block(cb))
logger.info("find %d apis from %s.", len(api_set), filename)
return api_set
def extract_doc_title_from_file(filename):
r = os.path.splitext(filename)
if len(r) != 2:
return None
if r[1].lower() == ".md":
return extract_md_title(filename)
elif r[1].lower() == ".rst":
return extract_rst_title(filename)
return None
@contextmanager
def find_node_by_class(doctree, node_class, remove):
"""Find the first node of the specified class."""
index = doctree.first_child_matching_class(node_class)
if index is not None:
yield doctree[index]
if remove:
del doctree[index]
else:
yield
def extract_rst_title(filename):
overrides = {
# Disable the promotion of a lone top-level section title to document
# title (and subsequent section title to document subtitle promotion).
"docinfo_xform": 0,
"initial_header_level": 2,
}
with open(filename, "r") as fileobj:
doctree = docutils.core.publish_doctree(
fileobj.read(), settings_overrides=overrides
)
with find_node_by_class(
doctree, docutils.nodes.title, remove=True
) as node:
if node is not None:
return node.astext()
return None
def extract_params_desc_from_rst_file(filename, section_title="参数"):
overrides = {
# Disable the promotion of a lone top-level section title to document
# title (and subsequent section title to document subtitle promotion).
"docinfo_xform": 0,
"initial_header_level": 2,
}
with open(filename, "r") as fileobj:
doctree = docutils.core.publish_doctree(
fileobj.read(), settings_overrides=overrides
)
found = False
for child in doctree.children:
if isinstance(child, docutils.nodes.section) and isinstance(
child.children[0], docutils.nodes.title
):
sectitle = child.children[0].astext()
if isinstance(section_title, (list, tuple)):
for st in section_title:
if sectitle.startswith(st):
found = True
break
else:
if sectitle.startswith(section_title):
found = True
if found:
return child
return None
def extract_md_title(filename):
with open(filename, "r") as fileobj:
html = markdown.markdown(fileobj.read())
mo = re.search(r"<h1>(.*?)</h1>", html)
if mo:
mos = re.search(r"<strong>(.*?)</strong>", mo.group(1))
return mos.group(1) if mos else mo.group(1)
return None
def format_filename(filename):
"""
Format the filename
filename may be "/FluidDoc/docs/guides/xxx" or "../guides/xxx", format it as "guides/xxx".
function get_all_files does not format it.
"""
rp = os.path.realpath(filename)
pat_str = "docs/" # if the structure changed, update this pattern
ind = rp.rindex(pat_str)
if ind >= 0:
return rp[ind + len(pat_str) :]
return filename
def extract_all_infos(docdirs):
apis_dict = {}
file_titles = {}
for p in docdirs:
filelist = get_all_files(p)
for fn in filelist:
ffn = format_filename(fn)
file_titles[ffn] = extract_doc_title_from_file(fn)
apis = extract_api_from_file(fn)
if len(apis):
apis_dict[ffn] = list(apis)
return apis_dict, file_titles
def ref_role(role, rawtext, text, lineno, inliner, options=None, content=None):
"""dummy ref role"""
ref_target = text
node = docutils.nodes.reference(rawtext, text)
return [node], []
docutils.parsers.rst.roles.register_canonical_role("ref", ref_role)
docutils.parsers.rst.roles.register_local_role("ref", ref_role)
class PyFunctionDirective(docutils.parsers.rst.Directive):
"""dummy py:function directive
see https://docutils-zh-cn.readthedocs.io/zh_CN/latest/howto/rst-roles.html
"""
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = True
option_spec = {}
has_content = True
def run(self):
text = "\n".join(self.content)
thenode = docutils.nodes.title(text, text)
return [thenode]
docutils.parsers.rst.directives.register_directive(
"py:function", PyFunctionDirective
) # as abs_cn.rst
docutils.parsers.rst.directives.register_directive(
"py:class", PyFunctionDirective
) # as Tensor_cn.rst
docutils.parsers.rst.directives.register_directive(
"py:method", PyFunctionDirective
) # as grad_cn.rst
class ToctreeDirective(docutils.parsers.rst.Directive):
"""dummy toctree directive"""
required_arguments = 1
optional_arguments = 5
has_content = True
def run(self):
text = self.arguments[0]
thenode = None
return []
docutils.parsers.rst.directives.register_directive("toctree", ToctreeDirective)
arguments = [
# flags, dest, type, default, help
[
"--output",
"output",
str,
"called_apis_from_docs.json",
"output filename. default: called_apis_from_docs.json",
],
]
def parse_args():
"""
Parse input arguments
"""
global arguments
parser = argparse.ArgumentParser(
description="extract all the called apis from md or reST files."
)
parser.add_argument(
"dir",
type=str,
help="travel all the files include this directory",
default=".",
nargs="+",
)
for item in arguments:
parser.add_argument(
item[0], dest=item[1], help=item[4], type=item[2], default=item[3]
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
print(args)
logger.setLevel(logging.DEBUG)
apis_dict, file_titles = extract_all_infos(args.dir)
import json
with open(args.output, "w") as f:
json.dump(apis_dict, f, indent=4)
r = os.path.splitext(args.output)
with open(f"{r[0]}-titles{r[1]}", "w") as f:
json.dump(file_titles, f, indent=4)
print("Done")