1
|
|
|
""" |
2
|
|
|
Documents Mandos commands. |
3
|
|
|
""" |
4
|
|
|
|
5
|
|
|
from __future__ import annotations |
6
|
|
|
|
7
|
|
|
import inspect |
8
|
|
|
import os |
9
|
|
|
from pathlib import Path |
10
|
|
|
from typing import Sequence, Mapping |
11
|
|
|
from dataclasses import dataclass |
12
|
|
|
from textwrap import wrap |
13
|
|
|
|
14
|
|
|
import pandas as pd |
|
|
|
|
15
|
|
|
import typer |
|
|
|
|
16
|
|
|
from typeddfs import TypedDfs |
|
|
|
|
17
|
|
|
from typeddfs.utils import Utils as TypedDfsUtils |
|
|
|
|
18
|
|
|
from typeddfs.file_formats import FileFormat |
|
|
|
|
19
|
|
|
from typer.models import CommandInfo |
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
DocFrame = ( |
23
|
|
|
TypedDfs.typed("DocFrame") |
24
|
|
|
.require("command", dtype=str) |
25
|
|
|
.reserve("description", "parameters", dtype=str) |
26
|
|
|
).build() |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
@dataclass(frozen=True, repr=True) |
|
|
|
|
30
|
|
|
class Documenter: |
31
|
|
|
level: int |
32
|
|
|
main: bool |
33
|
|
|
search: bool |
34
|
|
|
hidden: bool |
35
|
|
|
common: bool |
36
|
|
|
width: int |
37
|
|
|
flatten: bool |
38
|
|
|
|
39
|
|
|
def __post_init__(self): |
40
|
|
|
if self.main and self.search: |
41
|
|
|
raise ValueError("Cannot provide both --only-main and --only-search") |
42
|
|
|
|
43
|
|
|
def document(self, commands: Sequence[CommandInfo], to: Path, style: str) -> None: |
|
|
|
|
44
|
|
|
cmds = [c for c in commands if (self.hidden or not c.hidden)] |
45
|
|
|
if self.main: |
46
|
|
|
cmds = [c for c in cmds if c.name.startswith(":")] |
47
|
|
|
elif self.search: |
48
|
|
|
cmds = [c for c in cmds if not c.name.startswith(":")] |
49
|
|
|
table = DocFrame([self._doc_row(c) for c in cmds]) |
50
|
|
|
self._write(to, table, style) |
51
|
|
|
|
52
|
|
|
def _doc_row(self, c: CommandInfo) -> pd.Series: |
|
|
|
|
53
|
|
|
doc = c.callback.__doc__ |
54
|
|
|
args = self._typer_param_docs(c) |
55
|
|
|
dct = dict(command=c.name) |
56
|
|
|
# descriptions |
57
|
|
|
if self.level >= 3: |
58
|
|
|
dct["description"] = doc |
59
|
|
|
elif self.level >= 1: |
60
|
|
|
dct["description"] = doc.splitlines()[0] |
61
|
|
|
# parameters |
62
|
|
|
if self.level >= 4: |
63
|
|
|
for i, (k, v) in enumerate(args.items()): |
|
|
|
|
64
|
|
|
dct[f"parameter_{i}"] = f"{k}:: \n\n{v}" |
65
|
|
|
elif self.level >= 3: |
66
|
|
|
for i, (k, v) in enumerate(args.items()): |
|
|
|
|
67
|
|
|
dct[f"parameter_{i}"] = f"{k}:: {v.splitlines()[0]}" |
68
|
|
|
elif self.level == 2: |
69
|
|
|
dct["parameters"] = " ".join(args.keys()) |
70
|
|
|
return pd.Series(dct) |
71
|
|
|
|
72
|
|
|
def _typer_param_docs(self, c: CommandInfo) -> Mapping[str, str]: |
|
|
|
|
73
|
|
|
_args = inspect.signature(c.callback).parameters |
74
|
|
|
args = {} |
75
|
|
|
for k, p in _args.items(): |
|
|
|
|
76
|
|
|
dtype = str(p.annotation) # TODO: bad |
|
|
|
|
77
|
|
|
v = p.default |
|
|
|
|
78
|
|
|
if not isinstance(v, (typer.models.ParameterInfo, typer.models.OptionInfo)): |
79
|
|
|
raise AssertionError(f"{p} can't be {v} on {c.name}!") |
80
|
|
|
if isinstance(v, typer.models.OptionInfo): |
81
|
|
|
k = "--" + k |
82
|
|
|
k = k.replace("_", "-") |
83
|
|
|
k = k.replace("kind", "type") # TODO: bad |
|
|
|
|
84
|
|
|
doc = f"[type: {dtype}] " + v.help |
85
|
|
|
if self.hidden or not v.hidden: |
86
|
|
|
if self.common or k not in ["--verbose", "--quiet", "--log"]: |
87
|
|
|
if ( |
88
|
|
|
self.common |
|
|
|
|
89
|
|
|
or c.name.startswith(":") |
|
|
|
|
90
|
|
|
or k not in ["path", "--key", "--to", "--as-of", "--check", "--no-setup"] |
|
|
|
|
91
|
|
|
): |
92
|
|
|
if v.show_default: |
93
|
|
|
args[k] = doc + f"\n[default: {v.default}]" |
94
|
|
|
else: |
95
|
|
|
args[k] = doc |
96
|
|
|
return args |
97
|
|
|
|
98
|
|
|
def _write(self, to: Path, table: pd.DataFrame, style: str) -> None: |
|
|
|
|
99
|
|
|
if FileFormat.from_path(to) is FileFormat.lines and style != "none": |
|
|
|
|
100
|
|
|
if self.flatten: |
101
|
|
|
rows = [] |
102
|
|
|
for r in range(len(table)): |
|
|
|
|
103
|
|
|
row = table.iat[r, 0] |
104
|
|
|
for c in range(1, len(table.columns)): |
|
|
|
|
105
|
|
|
iat = str(table.iat[r, c]) |
106
|
|
|
if iat != "nan": |
107
|
|
|
if c > 1: |
108
|
|
|
row += "\n\n" + "." * int(self.width * 0.75) + "\n\n" |
109
|
|
|
row += iat |
110
|
|
|
rows.append(row) |
111
|
|
|
table = DocFrame(rows, columns=["command"]) |
112
|
|
|
table = table.applymap(lambda s: self._format(str(s))) |
113
|
|
|
content = table.pretty_print(style) |
114
|
|
|
TypedDfsUtils.write(to, content) |
115
|
|
|
else: |
116
|
|
|
table.write_file(to) |
117
|
|
|
|
118
|
|
|
def _format(self, s: str) -> str: |
|
|
|
|
119
|
|
|
s = s.strip() |
120
|
|
|
if s == "nan": |
121
|
|
|
return "" |
122
|
|
|
if self.width == 0: |
123
|
|
|
return s |
124
|
|
|
lines = [] |
125
|
|
|
for line in s.split("\n\n"): |
126
|
|
|
lines.extend(wrap(line, width=self.width)) |
127
|
|
|
lines.append(os.linesep) |
128
|
|
|
lines = [line.strip(" ").strip("\t") for line in lines] |
129
|
|
|
return os.linesep.join(lines).replace(os.linesep * 2, os.linesep) |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
__all__ = ["Documenter"] |
133
|
|
|
|