This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathdocs.py
108 lines (73 loc) · 2.46 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
'''
Generates documentation for all available actions in the shortcuts.actions module
'''
import argparse
from shortcuts.actions import actions_registry
from shortcuts.actions.base import VariablesField
DOC_TEMPLATE = '''
# Supported Actions
This is a list of all actions supported by **python-shortcuts**.
Legend:
* *keyword*: This keyword you can use in `toml` files to describe action
* *shortcuts identifier*: (*itype*) this identifier will be used to generate an action in a shortcut
System variables:
* `{{{{ask_when_run}}}}` - ask the user for an input when the shortcut is running.
----
{actions}
'''
def _build_docs():
actions_docs = []
actions_docs = [_build_action_doc(a) for a in actions_registry.actions]
return DOC_TEMPLATE.format(actions='\n\n'.join(actions_docs))
ACTION_TEMPLATE = '''
### {name}
{doc}
**keyword**: `{keyword}`
**shortcuts identifier**: `{identifier}`
{params}
'''
def _build_action_doc(action):
params = '\n'.join([_build_params_doc(f) for f in action().fields]).strip()
params = f'params:\n\n{params}' if params else ''
doc = ''
if action.__doc__:
# remove spaces from the beginning of _each_ line
doc = '\n'.join([l.strip() for l in action.__doc__.splitlines()])
return ACTION_TEMPLATE.format(
name=action.__name__,
doc=doc,
keyword=action.keyword,
identifier=action.itype,
params=params,
).strip()
PARAM_TEMPLATE = '* {name} {opts}'
def _build_params_doc(field):
properties = ', '.join(_get_field_properties(field))
opts = f'({properties})'
choices = getattr(field, 'choices', None)
if choices:
opts += f' {field.help} | _choices_:\n'
opts += '\n'.join([f'\n * `{choice}`' for choice in choices])
return PARAM_TEMPLATE.format(
name=field._attr,
opts=opts,
help=field.help,
).strip()
def _get_field_properties(field):
properties = []
if field.required:
properties.append('*required*')
if field.default:
properties.append(f'default={field.default}')
if isinstance(field, VariablesField):
properties.append('*variables support*')
return properties
def main():
parser = argparse.ArgumentParser(description='Actions documentation generator')
parser.add_argument('output', help='output file')
args = parser.parse_args()
doc = _build_docs()
with open(args.output, 'w') as f:
f.write(doc)
if __name__ == '__main__':
main()