This repository was archived by the owner on Aug 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathdocs.py
264 lines (225 loc) · 7.35 KB
/
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
# SPDX-License-Identifier: MIT
# Copyright (c) 2019 Intel Corporation
import pwd
import pathlib
import inspect
import argparse
import importlib
import configparser
import pkg_resources
import unittest.mock
from typing import Type
def traverse_get_config(target, *args):
current = target
last = target
for level in args:
last = current[level]
current = last["config"]
return current
MODULE_TEMPLATE = """{tag}
{name}
{underline}
.. code-block:: console
pip install {install}
"""
TEMPLATE = """{tag}
{name}
{underline}
*{maintenance}*
{help}"""
def data_type_string(data_type, nargs=None):
if nargs is not None:
return "List of %ss" % (data_type_string(data_type).lower(),)
elif hasattr(data_type, "SINGLETON"):
return "List of %ss" % (data_type_string(data_type.SINGLETON).lower(),)
if hasattr(data_type, "__func__"):
return data_type_string(data_type.__func__)
elif data_type is str:
return "String"
elif data_type is int:
return "Integer"
elif data_type is bool:
return "Boolean"
elif data_type is Type:
return "Type"
elif hasattr(data_type, "__qualname__"):
name = data_type.__qualname__
if name[::-1].startswith(".load"[::-1]):
return name[: -len(".load")]
return name
else:
return str(data_type)
def sanitize_default(default):
if not isinstance(default, str):
return sanitize_default(str(default))
return default
def build_args(config):
args = []
for key, value in config.items():
plugin = value["plugin"]
if plugin is None:
continue
build = ""
build += "- %s: %s\n" % (
key,
data_type_string(
plugin.get("type", str), plugin.get("nargs", None)
),
)
if "default" in plugin or "help" in plugin:
build += "\n"
if "default" in plugin:
build += " - default: %s\n" % (
sanitize_default(plugin["default"]),
)
if "help" in plugin:
build += " - %s\n" % (plugin["help"],)
args.append(build.rstrip())
if args:
return "**Args**\n\n" + "\n\n".join(args)
return False
def type_name(value):
if inspect.isclass(value):
return value.__qualname__
return value
def format_op_definitions(definitions):
for key, definition in definitions.items():
item = "- %s: %s(type: %s)" % (
key,
definition.name,
definition.primitive,
)
if definition.spec is not None:
item += "\n\n"
item += "\n".join(
[
" - %s: %s%s"
% (
name,
type_name(param.annotation),
"(default: %s)" % (param.default,)
if param.default is not inspect.Parameter.empty
else "",
)
for name, param in inspect.signature(
definition.spec
).parameters.items()
]
)
yield item
def format_op(op):
build = []
build.append("**Stage: %s**\n\n" % (op.stage.value))
if op.inputs:
build.append(
"**Inputs**\n\n" + "\n".join(format_op_definitions(op.inputs))
)
if op.outputs:
build.append(
"**Outputs**\n\n" + "\n".join(format_op_definitions(op.outputs))
)
if op.conditions:
build.append(
"**Conditions**\n\n"
+ "\n".join(
[
"- %s: %s" % (definition.name, definition.primitive)
for definition in op.conditions
]
)
)
return "\n\n".join(build)
def gen_docs(entrypoint: str, maintenance: str = "Official"):
per_module = {}
packagesconfig = configparser.ConfigParser()
packagesconfig.read("scripts/packagesconfig.ini")
# For some reason duplicates are showing up
done = set()
for i in pkg_resources.iter_entry_points(entrypoint):
# Skip duplicates
if i.name in done:
continue
cls = i.load()
plugin_type = "_".join(cls.ENTRY_POINT_NAME)
if plugin_type == "opimp":
plugin_type = "operation"
module_name = i.module_name.split(".")[0]
per_module.setdefault(module_name, [None, "", []])
per_module[module_name][0] = importlib.import_module(module_name)
per_module[module_name][1] = plugin_type
doc = cls.__doc__
if doc is None:
doc = "No description"
else:
doc = inspect.cleandoc(doc)
formatting = {
"tag": f".. _plugin_{plugin_type}_{module_name}_{i.name.replace('.', '_')}:",
"name": i.name,
"underline": "~" * len(i.name),
"maintenance": maintenance,
"help": doc,
}
formatted = TEMPLATE.format(**formatting)
if getattr(cls, "imp", False):
cls = cls.imp
if getattr(cls, "op", False):
formatted += "\n\n" + format_op(cls.op)
if getattr(cls, "args", False):
defaults = cls.args({})
if defaults:
config = traverse_get_config(defaults, *cls.add_orig_label())
formatted += "\n\n" + build_args(config)
per_module[module_name][2].append(formatted)
done.add(i.name)
return "\n\n".join(
[
MODULE_TEMPLATE.format(
**{
"tag": f".. _plugin_{plugin_type}_{name}:",
"name": name,
"install": name.replace("_", "-"),
"underline": "+" * len(name),
}
)
+ (
(
inspect.getdoc(module) + "\n\n"
if inspect.getdoc(module)
else ""
)
+ (
"\n\n".join(docs)
if name not in packagesconfig["NO ARGS"]
else ""
)
)
for name, (module, plugin_type, docs) in per_module.items()
if docs
]
)
def fake_getpwuid(uid):
return pwd.struct_passwd(
("user", "x", uid, uid, "", "/home/user", "/bin/bash")
)
def main():
parser = argparse.ArgumentParser(description="Generate plugin docs")
parser.add_argument("--entrypoint", help="Entrypoint to document")
parser.add_argument("--modules", help="Modules to care about", nargs="+")
parser.add_argument(
"--maintenance",
default="Official",
help="Maintained as a part of DFFML or community managed",
)
args = parser.parse_args()
with unittest.mock.patch("pwd.getpwuid", new=fake_getpwuid):
if getattr(args, "entrypoint", False):
print(gen_docs(args.entrypoint, args.maintenance))
return
templates_dir = pathlib.Path("scripts", "docs", "templates")
for template_path in templates_dir.glob("dffml_*.rst"):
entrypoint = template_path.stem.replace("_", ".")
pathlib.Path("docs", "plugins", template_path.name).write_text(
template_path.read_text() + gen_docs(entrypoint)
)
if __name__ == "__main__":
main()