-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_docs.py
196 lines (158 loc) · 5.45 KB
/
generate_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
import re
import sys
import warnings
from pydoc import ModuleScanner
def scan_modules():
"""taken from the source code of help('modules')
https://github.com/python/cpython/blob/63298930fb531ba2bb4f23bc3b915dbf1e17e9e1/Lib/pydoc.py#L2178"""
modules = {}
def callback(path, modname, desc, modules=modules):
if modname and modname[-9:] == ".__init__":
modname = modname[:-9] + " (package)"
if modname.find(".") < 0:
modules[modname] = 1
def onerror(modname):
callback(None, modname, None)
with warnings.catch_warnings():
# ignore warnings from importing deprecated modules
warnings.simplefilter("ignore")
ModuleScanner().run(callback, onerror=onerror)
return list(modules.keys())
def import_module(module_name):
import io
from contextlib import redirect_stdout
# Importing modules causes ('Constant String', 2, None, 4) and
# "Hello world!" to be printed to stdout.
f = io.StringIO()
with warnings.catch_warnings(), redirect_stdout(f):
# ignore warnings caused by importing deprecated modules
warnings.filterwarnings("ignore", category=DeprecationWarning)
try:
module = __import__(module_name)
except Exception as e:
return e
return module
def is_child(module, item):
import inspect
item_mod = inspect.getmodule(item)
return item_mod is module
def traverse(module, names, item):
import inspect
has_doc = inspect.ismodule(item) or inspect.isclass(item) or inspect.isbuiltin(item)
if has_doc and isinstance(item.__doc__, str):
yield names, item.__doc__
attr_names = dir(item)
for name in attr_names:
if name in [
"__class__",
"__dict__",
"__doc__",
"__objclass__",
"__name__",
"__qualname__",
"__annotations__",
]:
continue
try:
attr = getattr(item, name)
except AttributeError:
assert name == "__abstractmethods__", name
continue
if module is item and not is_child(module, attr):
continue
is_type_or_module = (type(attr) is type) or (type(attr) is type(__builtins__))
new_names = names.copy()
new_names.append(name)
if item == attr:
pass
elif not inspect.ismodule(item) and inspect.ismodule(attr):
pass
elif is_type_or_module:
yield from traverse(module, new_names, attr)
elif (
callable(attr)
or not issubclass(type(attr), type)
or type(attr).__name__ in ("getset_descriptor", "member_descriptor")
):
if inspect.isbuiltin(attr):
yield new_names, attr.__doc__
else:
assert False, (module, new_names, attr, type(attr).__name__)
def traverse_all(root):
from glob import glob
import os.path
files = (
glob(f"{root}/Lib/*")
+ glob(f"{root}/vm/src/stdlib/*")
+ glob(f"{root}/stdlib/src/*")
)
allowlist = set(
[os.path.basename(file).lstrip("_").rsplit(".", 1)[0] for file in files]
)
for denied in ("this", "antigravity"):
allowlist.remove(denied)
for module_name in scan_modules():
if module_name.lstrip("_") not in allowlist:
print("skipping:", module_name, file=sys.stderr)
continue
module = import_module(module_name)
if hasattr(module, "__cached__"): # python module
continue
yield from traverse(module, [module_name], module)
def f():
pass
builtin_types = [
type(bytearray().__iter__()),
type(bytes().__iter__()),
type(dict().__iter__()),
type(dict().values().__iter__()),
type(dict().items().__iter__()),
type(dict().values()),
type(dict().items()),
type(set().__iter__()),
type(list().__iter__()),
type(range(0).__iter__()),
type(str().__iter__()),
type(tuple().__iter__()),
type(None),
type(f),
]
for typ in builtin_types:
names = ["builtins", typ.__name__]
if not isinstance(typ.__doc__, str):
yield names, typ.__doc__
yield from traverse(__builtins__, names, typ)
def docs(rustpython_path):
return ((".".join(names), escape(doc)) for names, doc in traverse_all(rustpython_path))
UNICODE_ESCAPE = re.compile(r"\\u([0-9]+)")
def escape(doc):
if doc is None:
return None
return re.sub(UNICODE_ESCAPE, r"\\u{\1}", doc)
def test_escape():
input = r"It provides access to APT\u2019s idea of the"
expected = r"It provides access to APT\u{2019}s idea of the"
output = escape(input)
assert output == expected
if __name__ == "__main__":
import sys
import json
try:
rustpython_path = sys.argv[1]
except IndexError:
print("1st argument is rustpython source code path")
raise SystemExit
try:
out_path = sys.argv[2]
except IndexError:
out_path = "-"
def dump(docs):
yield "[\n"
for name, doc in docs:
if doc is None:
yield f" ({json.dumps(name)}, None),\n"
else:
yield f" ({json.dumps(name)}, Some({json.dumps(doc)})),\n"
yield "]\n"
out_file = open(out_path, "w") if out_path != "-" else sys.stdout
out_file.writelines(dump(docs(rustpython_path)))