forked from pyinfra-dev/pyinfra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_connectors_docs.py
110 lines (90 loc) · 3.17 KB
/
generate_connectors_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
#!/usr/bin/env python
import sys
from inspect import cleandoc, getdoc, getfullargspec
from os import makedirs, path
from typing import get_type_hints
from pyinfra.api.connectors import get_all_connectors
sys.path.append(".")
from docs.utils import title_line # noqa: E402
def build_connectors_docs():
this_dir = path.dirname(path.realpath(__file__))
docs_dir = path.abspath(path.join(this_dir, "..", "docs"))
makedirs(path.join(docs_dir, "connectors"), exist_ok=True)
for connector_name, connector in get_all_connectors().items():
lines = []
full_title = "``@{0}`` Connector".format(connector_name)
lines.append(full_title)
lines.append(title_line("-", full_title))
lines.append("")
doc = getdoc(connector)
if doc:
lines.append(doc)
lines.append("")
examples_doc = getattr(connector, "__examples_doc__", None)
if examples_doc:
lines.append("Examples")
lines.append(title_line("~", "Examples"))
lines.append("")
lines.append(cleandoc(examples_doc))
lines.append("")
else:
data_title = "Usage"
lines.append(data_title)
lines.append(title_line("~", data_title))
lines.append("")
names_argument_key = getfullargspec(connector.make_names_data).args[0]
if names_argument_key == "_":
names_argument_key = ""
else:
names_argument_key = f"/{names_argument_key}"
lines.append(
f"""
.. code:: shell
pyinfra @{connector_name}{names_argument_key} ...
""".strip(),
)
lines.append("")
data_key_lines = []
for key, type_ in get_type_hints(connector.data_cls).items():
if key.startswith("_"):
continue
meta = connector.data_meta[key]
default = "" if meta.default is None else f"``{meta.default}``"
data_key_lines.append(
f""" * - ``{key}``
- {meta.description}
- ``{type_.__name__}``
- {default}
"""
)
if data_key_lines:
data_title = "Available Data"
lines.append(data_title)
lines.append(title_line("~", data_title))
lines.append("")
lines.append(
"The following keys can be set as host or group data to control "
"how this connector interacts with the target."
)
lines.append("")
data_key_lines = "\n".join(data_key_lines)
lines.append(
f"""
.. list-table::
:header-rows: 1
:widths: 25 45 15 15
* - Key
- Description
- Type
- Default
{data_key_lines}
""".strip(),
)
lines.append("")
module_filename = path.join(docs_dir, "connectors", f"{connector_name}.rst")
print("--> Writing {0}".format(module_filename))
with open(module_filename, "w", encoding="utf-8") as outfile:
outfile.write("\n".join(lines))
if __name__ == "__main__":
print("### Building connectors docs")
build_connectors_docs()