Repository navigation
Expand file tree
/
Copy pathgenerate_tool_docs.py
More file actions
441 lines (402 loc) · 16.3 KB
/
Copy pathgenerate_tool_docs.py
File metadata and controls
441 lines (402 loc) · 16.3 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#!/usr/bin/env python3
"""Generate Markdown and optional Sphinx documentation without running tools."""
import argparse
import ast
import os
from pathlib import Path
import subprocess
import sys
import tokenize
class ParserExtractionError(ValueError):
"""A parser cannot be reconstructed safely from its source."""
PARSER_METHODS = {
"add_argument",
"add_argument_group",
"add_mutually_exclusive_group",
"add_subparsers",
"add_parser",
"set_defaults",
}
ARGPARSE_VALUES = {
name: getattr(argparse, name)
for name in (
"SUPPRESS",
"REMAINDER",
"RawDescriptionHelpFormatter",
"RawTextHelpFormatter",
"ArgumentDefaultsHelpFormatter",
"MetavarTypeHelpFormatter",
)
}
def load_parser(filename):
"""Reconstruct literal argparse declarations; never execute source code."""
filename = Path(filename)
with tokenize.open(str(filename)) as source:
tree = ast.parse(source.read(), filename=str(filename))
values = {"int": int, "str": str, "float": float, "version": sys.version_info[0]}
parser_objects = set()
roots = []
def fail(node, message):
raise ParserExtractionError(
"{}:{}: {}".format(filename, node.lineno, message)
)
def is_constructor(node):
return (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Name)
and node.func.value.id == "argparse"
and node.func.attr == "ArgumentParser"
)
def is_parser_call(node):
return (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Name)
and node.func.value.id in values
and id(values[node.func.value.id]) in parser_objects
and node.func.attr in PARSER_METHODS
)
def contains_declarations(node):
return any(
is_constructor(child) or is_parser_call(child)
for child in ast.walk(node)
)
def value(node):
if isinstance(node, ast.Name) and node.id in values:
return values[node.id]
if isinstance(node, (ast.List, ast.Tuple)):
items = [value(item) for item in node.elts]
return tuple(items) if isinstance(node, ast.Tuple) else items
if (
isinstance(node, ast.Attribute)
and isinstance(node.value, ast.Name)
and node.value.id == "argparse"
and node.attr in ARGPARSE_VALUES
):
return ARGPARSE_VALUES[node.attr]
if (
isinstance(node, ast.Call)
and isinstance(node.func, ast.Attribute)
and isinstance(node.func.value, ast.Name)
):
owner, method = node.func.value.id, node.func.attr
if (owner, method) in (("Path", "cwd"), ("os", "getcwd")):
if not node.args and not node.keywords:
return Path.cwd() if owner == "Path" else str(Path.cwd())
if (owner, method) == ("argparse", "FileType"):
args, kwargs = arguments(node)
return argparse.FileType(*args, **kwargs)
try:
return ast.literal_eval(node)
except (ValueError, TypeError):
fail(node, "unsupported dynamic argparse value")
def arguments(node):
if any(isinstance(item, ast.Starred) for item in node.args) or any(
item.arg is None for item in node.keywords
):
fail(node, "expanded argparse arguments are not supported")
return (
[value(item) for item in node.args],
{item.arg: value(item.value) for item in node.keywords},
)
def declaration(node):
args, kwargs = arguments(node)
if is_constructor(node):
kwargs.setdefault("prog", filename.name)
result = argparse.ArgumentParser(*args, **kwargs)
roots.append(result)
else:
result = getattr(values[node.func.value.id], node.func.attr)(*args, **kwargs)
parser_objects.add(id(result))
return result
def visit(statements):
for statement in statements:
if isinstance(statement, (ast.FunctionDef, ast.AsyncFunctionDef)):
if contains_declarations(statement):
visit(statement.body)
elif isinstance(statement, ast.If) and contains_declarations(statement):
test = statement.test
if (
isinstance(test, ast.Compare)
and isinstance(test.left, ast.Name)
and test.left.id == "version"
and len(test.ops) == 1
and isinstance(test.ops[0], ast.Eq)
and len(test.comparators) == 1
):
is_current_version = (
value(test.comparators[0]) == sys.version_info[0]
)
branch = statement.body if is_current_version else statement.orelse
visit(branch)
else:
fail(statement, "unsupported conditional parser declaration")
elif isinstance(statement, ast.Try) and contains_declarations(statement):
branches = [
block for block in [statement.body, *(h.body for h in statement.handlers)]
if any(contains_declarations(item) for item in block)
]
if len(branches) != 1 or statement.orelse or statement.finalbody:
fail(statement, "ambiguous parser declarations in try statement")
visit(branches[0])
elif isinstance(statement, ast.Assign) and len(statement.targets) == 1:
target = statement.targets[0]
if not isinstance(target, ast.Name):
continue
expression = statement.value
if is_constructor(expression) or is_parser_call(expression):
values[target.id] = declaration(expression)
else:
try:
constant = ast.literal_eval(expression)
except (ValueError, TypeError):
if target.id == "cwd":
values[target.id] = value(expression)
else:
values[target.id] = constant
elif isinstance(statement, ast.Expr) and is_parser_call(statement.value):
declaration(statement.value)
elif contains_declarations(statement):
fail(statement, "unsupported parser declaration")
visit(tree.body)
if not roots:
return None
root = values.get("parser")
if root not in roots:
if len(roots) != 1:
raise ParserExtractionError("{}: ambiguous root parser".format(filename))
root = roots[0]
return root
def discover_tools(source_dir, excludes):
"""Find tool parsers without importing or executing the source files."""
source_dir = Path(source_dir).resolve()
if not source_dir.is_dir():
raise ValueError("Source directory does not exist: {}".format(source_dir))
candidates = sorted(source_dir.glob("*.py"))
unknown = set(excludes) - {path.name for path in candidates}
if unknown:
raise ValueError("Excluded files not found: {}".format(", ".join(sorted(unknown))))
tools = []
for filename in candidates:
if filename.name in excludes:
continue
tool_parser = load_parser(filename)
if tool_parser is None:
print("Skipping {}: no argparse parser found".format(filename.name))
else:
tools.append((filename, tool_parser))
if not tools:
raise ValueError("No argparse tools found after exclusions")
return tools
def markdown_text(value):
"""Escape a value for inclusion in a Markdown table cell."""
return str(value).replace("|", r"\|").replace("\n", "<br>")
def format_metavar(action):
if action.metavar is not None:
if isinstance(action.metavar, tuple):
return " ".join(str(item) for item in action.metavar)
return str(action.metavar)
if action.option_strings:
return action.dest.upper()
return action.dest
def format_default(value):
if isinstance(value, Path):
return "current working directory"
return repr(value)
def render_argument(action):
names = ", ".join(action.option_strings) or format_metavar(action)
if action.option_strings and action.nargs != 0:
names += " " + format_metavar(action)
details = []
if action.required:
details.append("Required")
if action.choices is not None:
details.append("Choices: `{}`".format(
markdown_text(", ".join(str(choice) for choice in action.choices))
))
if action.default is not argparse.SUPPRESS:
details.append(
"Default: `{}`".format(markdown_text(format_default(action.default)))
)
if action.nargs not in (None, 0):
details.append("Arguments: `{}`".format(markdown_text(action.nargs)))
if action.type is not None:
type_name = getattr(action.type, "__name__", str(action.type))
details.append("Type: `{}`".format(markdown_text(type_name)))
return (
"| `{}` | {} | {} |".format(
markdown_text(names),
markdown_text(action.help or ""),
"; ".join(details),
)
)
def render_parser(parser, depth=3):
"""Render one parser, including nested subcommands, as Markdown."""
sections = []
if parser.description:
sections.extend([markdown_text(parser.description), ""])
sections.extend([
"```text",
parser.format_usage().strip(),
"```",
"",
])
subparser_actions = []
for group in parser._action_groups:
actions = [
action for action in group._group_actions
if action.dest != "help" and action.help != argparse.SUPPRESS
]
if not actions:
continue
visible_actions = [
action for action in actions
if not isinstance(action, argparse._SubParsersAction)
]
if visible_actions:
sections.extend([
"{} {}".format("#" * depth, group.title),
"",
"| Option | Description | Details |",
"| --- | --- | --- |",
])
sections.extend(render_argument(action) for action in visible_actions)
sections.append("")
subparser_actions.extend(
action for action in actions
if isinstance(action, argparse._SubParsersAction)
)
for subparsers in subparser_actions:
sections.extend([
"{} Subcommands".format("#" * depth),
"",
])
for name, child_parser in subparsers.choices.items():
description = subparsers._choices_actions
help_text = next(
(choice.help for choice in description if choice.dest == name),
"",
)
sections.extend([
"{} `{}`".format("#" * (depth + 1), name),
])
if help_text:
sections.extend(["", markdown_text(help_text)])
sections.extend(["", *render_parser(child_parser, depth + 2)])
return sections
def write_markdown(tools, destination):
"""Write the generated tool reference to a Markdown file."""
destination = Path(destination).resolve()
destination.parent.mkdir(parents=True, exist_ok=True)
lines = [
"# pyviyatools Tool Reference",
"",
"This reference is generated from the tools' `argparse` definitions. "
"Regenerate it after changing a tool's arguments.",
"",
]
for filename, parser in tools:
source_link = Path(os.path.relpath(filename, destination.parent)).as_posix()
lines.extend([
"## {}".format(filename.name),
"",
"[Source]({})".format(source_link),
"",
])
lines.extend(render_parser(parser))
destination.write_text("\n".join(lines).rstrip() + "\n", encoding="utf-8")
return destination
def write_sphinx_source(tools, output_dir):
"""Write the Sphinx source tree used for the optional HTML reference."""
output_dir = Path(output_dir).resolve()
sphinx_source = output_dir / "source"
sphinx_source.mkdir(parents=True, exist_ok=True)
(sphinx_source / "conf.py").write_text(
"import sys\n"
"from pathlib import Path\n"
"sys.path.insert(0, str(Path(__file__).parent))\n"
"sys.path.insert(0, {!r})\n"
"project = 'pyviyatools Tool Reference'\n"
"extensions = ['sphinxarg.ext']\n"
"master_doc = 'index'\n"
"html_theme = 'alabaster'\n".format(str(Path(__file__).resolve().parent)),
encoding="utf-8",
)
adapters = ["from generate_tool_docs import load_parser\n"]
title = "pyviyatools Tool Reference"
pages = [title, "=" * len(title), "", "Generated from argparse definitions.", ""]
for index, (filename, _) in enumerate(tools):
function = "parser_{}".format(index)
adapters.append(
"\ndef {}():\n return load_parser({!r})\n".format(function, str(filename))
)
pages.extend([
filename.name, "-" * len(filename.name), "",
".. argparse::",
" :module: _tool_parsers",
" :func: " + function,
" :prog: " + filename.name,
"",
])
(sphinx_source / "_tool_parsers.py").write_text("".join(adapters), encoding="utf-8")
(sphinx_source / "index.rst").write_text("\n".join(pages), encoding="utf-8")
return sphinx_source, len(tools)
def generate_docs(source_dir, markdown_file, excludes, html_output_dir=None):
"""Generate the committed Markdown reference and optional Sphinx sources."""
tools = discover_tools(source_dir, excludes)
markdown_path = write_markdown(tools, markdown_file)
sphinx_source = None
if html_output_dir is not None:
sphinx_source, _ = write_sphinx_source(tools, html_output_dir)
return markdown_path, sphinx_source, len(tools)
def main(argv=None):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--source-dir", type=Path, default=Path(__file__).resolve().parents[1],
help="Directory containing tools (non-recursive; default: repository root)",
)
parser.add_argument(
"--markdown-file", type=Path, default=Path("docs/TOOLS.md"),
help="Generated Markdown tool reference (default: docs/TOOLS.md)",
)
parser.add_argument(
"--html", action="store_true",
help="Also generate the optional Sphinx HTML reference",
)
parser.add_argument(
"--output-dir", type=Path, default=Path("docs/tools"),
help="Generated Sphinx source and HTML directory when --html is used "
"(default: docs/tools)",
)
parser.add_argument(
"--exclude", nargs="+", action="append", default=[], metavar="FILE.py",
help="Exact filenames to exclude; accepts multiple names and can be repeated",
)
args = parser.parse_args(argv)
try:
markdown_path, source, count = generate_docs(
args.source_dir, args.markdown_file,
{name for group in args.exclude for name in group},
args.output_dir if args.html else None,
)
except (OSError, SyntaxError, ValueError, TypeError, argparse.ArgumentError) as error:
parser.exit(1, "Documentation generation failed: {}\n".format(error))
if args.html:
result = subprocess.run([
sys.executable, "-m", "sphinx", "-W", "--keep-going", "-E", "-a",
"-b", "html", str(source), str(Path(args.output_dir).resolve() / "html"),
])
if result.returncode:
parser.exit(
result.returncode,
"Sphinx build failed. Install optional HTML documentation "
"dependencies with 'python3 -m pip install -r requirements-docs.txt'.\n",
)
print("Generated HTML tool reference: {}".format(
Path(args.output_dir).resolve() / "html" / "index.html"
))
print("Documented {} tools in {}".format(count, markdown_path))
return 0
if __name__ == "__main__":
sys.exit(main())