-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocgen
executable file
·273 lines (216 loc) · 8.27 KB
/
docgen
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
#!/usr/bin/env python3
"""Generate markdown from terraform module."""
import re
import argparse
import os
from pprint import pprint
class TFDoc:
"""stateful class to parse terraform docstrings."""
def __init__(self):
"""Set private vars to default."""
self._variables = {}
self._outputs = {}
self._sections = {}
self._outline = {'title': "", 'text': ""}
self._footers = {}
self._block = None
self._unclassified_text = ""
self._indent = ""
self._multiline_comment = False
self._in_var = False
self._in_type = False
self.debug = False
def set_indent(self, indent):
"""
Set current default indent for text to 'indent'
"""
self._indent = indent
def variable(self, name):
"""
Add a variable or Argument pass the variable name.
This starts a variable block so calls to description, type, default, or add text will be
associated with this variable.
"""
self._variables[name] = {'description': "", 'type': "", 'default': None, 'text': "", 'object': {} }
self._block = self._variables[name]
self._indent = 2
def description(self, description):
"""Add description text to the active variable."""
self._block['description'] = description
def type(self, type):
"""Set the variable type."""
if '{' in type:
self._block['type'] = type[:-2] + ')'
self._in_type = True
else:
self._block['type'] = type
def object(self, key, _type, description=''):
"""Add object definition for complex types"""
self._block['object'][key] = {'type': _type, 'description': description}
def default(self, default):
"""Define the default value for the current variable."""
self._block['default'] = default
def output(self, name):
"""
Add output or Attribute.
this starts an output block so calls to add_txt are associated with this Attribute.
"""
self._outputs[name] = {'text': ""}
self._block = self._outputs[name]
def section(self, name):
"""
Start an additional section with headding name.
These sections appear between the Argument and attribute sections.
"""
self._sections[name] = {'text': ""}
self._block = self._sections[name]
def footer(self, name):
"""
Start a footer block.
Footer blocks appear after the Attribute block.
"""
self._footers[name] = {'text': ""}
self._block = self._footers[name]
def outline(self, title):
"""
Start the outline block.
The outline block appears first in the docs.
The title provided will be the title of the whole document.
"""
self._outline['title'] = title
self._block = self._outline
def add_text(self, text):
"""Add text tothe current block or the unclissified block if no block is active."""
if not self._block:
self._unclassified_text += text + '\n'
else:
self._block['text'] += " " * self._indent + text + '\n'
def end_block(self):
"""
End the current block.
prevents stray text being added to this block and resets indent.
"""
self._block = None
self._indent = 0
def __str__(self):
"""Output the masterpiece."""
text = f'''# {self._outline['title']}\n\n{self._outline['text']}'''
text += '## Argument Reference\n\n'
for var in sorted(self._variables.keys()):
attribute = self._variables[var]
default = f'''(optional, default: {attribute['default']})''' if attribute['default'] else '(Required)'
_object = attribute['object']
text += f'''- ```{var}``` - {default} [{attribute['type']}] {attribute['description']} '''
if _object:
text += '\n##### object'
text += '''
| key | type | description |
|-----|------|-------------|
'''
for key, definition in _object.items():
text += f'''|`{key}` | {definition['type']} | {definition['description']} |\n'''
text += f'''{attribute['text']}\n\n'''
for (section, attribute) in self._sections.items():
text += f'''## {section}\n\n{attribute['text']}\n'''
if len(self._outputs) > 0:
text += '## Attributes Reference\n\n'
for var in sorted(self._outputs.keys()):
attribute=self._outputs[var]
text += f'''- ```{var}``` {attribute['text']}\n'''
for (section, attribute) in self._footers.items():
text += f'''## {section}\n\n{attribute['text']}\n'''
text += self._unclassified_text + '\n'
return text
def __add__(self, other):
if type(other) == str:
self.process_line(other)
return self
elif type(other) == list:
for line in other:
self.process_line(line)
return self
else:
return NotImplemented
def process_line(self, raw_line):
"""
Process a single line from a terraform file
"""
line = raw_line.strip()
not self.debug or print(line)
if self._in_type:
if '}' in line:
self._in_type = False
return
key, rest = line.split('=', 1)
(_type, description) = rest.split('#=') if '#=' in rest else (rest,'')
self.object(key.strip(), _type.strip(), description.strip())
return
if words := line.split(None, 1):
if words[0] == '#=':
self.add_text(line[3:] if len(words) > 1 else "\n")
return
if words[0] == '#=OUTLINE=':
self.outline(words[1])
return
if words[0] == '#=SECTION=':
self.section(words[1])
return
if words[0] == '#=FOOTER=':
self.footer(words[1])
return
if words[0] == "#=INDENT=":
self.set_indent(int(words[1]))
if words[0] == '#==' or words[0] == "}":
self.end_block()
self._in_var = False
return
if words[0] == '/*=' and '*/' not in line:
self._multiline_comment = True
return
if self._multiline_comment and '*/' in line:
self._multiline_comment = False
return
if self._multiline_comment:
self.add_text(raw_line)
return
# Language components
# The following words might incidentally exist in multiline comments
# So must go last
if words[0] == 'variable':
name = re.search('"(.*)"', words[1])
self.variable(name.group(1))
self._in_var = True
if words[0] == 'description' and self._in_var:
description = re.search('"(.*)"', words[1])
self.description(description.group(1))
# TODO Handle multiline types
if words[0] == 'type' and self._in_var:
self.type(words[1].split('=')[1].strip())
if words[0] == 'default' and self._in_var:
self.default(words[1])
if words[0] == 'output':
name = re.search('"(.*)"', words[1])
self.output(name.group(1))
in_var = True
elif self._multiline_comment:
self.add_text(raw_line)
parser = argparse.ArgumentParser(description='Generate terraform docs', prog='PROG', usage='%(prog)s [options]')
parser.add_argument('source', nargs='+')
parser.add_argument('--out', nargs=1)
parser.add_argument('--verbose', '-v', action='store_true')
parser.add_argument('--debug', '-d', action='store_true')
args = parser.parse_args()
docs = TFDoc()
docs.debug = args.debug
for file in args.source:
if os.path.isdir(file):
continue
if args.verbose: print(f'>>> {file}')
with open(file) as f:
docs += f.readlines()
if args.out:
file = open(args.out[0], 'w+')
print(docs, file=file)
file.close()
else:
print(docs)