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