forked from GoogleCloudPlatform/rad-lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tfdoc.py
292 lines (247 loc) · 8.4 KB
/
tfdoc.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#! /usr/bin/env python3
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import collections
import enum
import glob
import os
import re
import string
import click
MARK_BEGIN = '<!-- BEGIN TFDOC -->'
MARK_END = '<!-- END TFDOC -->'
RE_OUTPUTS = re.compile(r'''(?smx)
(?:^\s*output\s*"([^"]+)"\s*\{$) |
(?:^\s*description\s*=\s*"((?:[^"\\]|\\")+)"\s*$) |
(?:^\s*sensitive\s*=\s*(\S+)\s*$)
''')
RE_TYPE = re.compile(r'([\(\{\}\)])')
RE_VARIABLES = re.compile(r'''(?smx)
# empty lines
(^\s*$) |
# block comment
(^\s*/\*.*?\*/$) |
# line comment
(^\s*\#.*?$) |
# variable declaration start
(?:^\s*variable\s*"([^"]+)"\s*\{$) |
# variable description start
(?:^\s*description\s*=\s*"((?:[^"\\]|\\")+)\.) |
# variable type start
(?:^\s*type\s*=\s*(.*?)$) |
# variable default start
(?:^\s*default\s*=\s*"?(.*?)"?\s*$) |
# variable body
(?:^\s*(\S.*?)$)
''')
REPL_VALID = string.digits + string.ascii_letters + ' .,;:_-'
OutputData = collections.namedtuple('Output', 'name description sensitive')
OutputToken = enum.Enum('OutputToken', 'NAME DESCRIPTION SENSITIVE')
VariableData = collections.namedtuple(
'Variable', 'name description type default required')
VariableToken = enum.Enum(
'VariableToken',
'EMPTY BLOCK_COMMENT LINE_COMMENT NAME DESCRIPTION TYPE DEFAULT REST')
class ItemParsed(Exception):
pass
class Output(object):
"Output parsing helper class."
def __init__(self):
self.in_progress = False
self.name = self.description = self.sensitive = None
def parse_token(self, token_type, token_data):
if token_type == 'NAME':
if self.in_progress:
raise ItemParsed(self.close())
self.in_progress = True
self.name = token_data
else:
setattr(self, token_type.lower(), token_data)
def close(self):
return OutputData(self.name, self.description, self.sensitive)
class Variable(object):
"Variable parsing helper class."
def __init__(self):
self.in_progress = False
self.name = self.description = self.type = self.default = None
self._data = []
self._data_context = None
def parse_token(self, token_type, token_data):
if token_type == 'NAME':
if self.in_progress:
raise ItemParsed(self.close())
self.in_progress = True
self.name = token_data
elif token_type == 'DESCRIPTION':
setattr(self, token_type.lower(), token_data)
elif token_type in ('DEFAULT', 'TYPE'):
self._start(token_type.lower(), token_data)
elif token_type == 'REST':
self._data.append(token_data)
def _close(self, strip=False):
if self._data_context:
data = self._data
if strip and '}' in data[-1]:
data = data[:-1]
setattr(self, self._data_context, ('\n'.join(data)).strip())
def _start(self, context, data):
if context == self._data_context or getattr(self, context):
self._data.append("%s = %s" % (context, data))
return
self._close()
self._data = [data]
self._data_context = context
def close(self):
self._close(strip=True)
return VariableData(self.name, self.description, self.type, self.default,
self.default is None)
def _escape(s):
"Basic, minimal HTML escaping"
return ''.join(c if c in REPL_VALID else ('&#%s;' % ord(c)) for c in s)
def format_outputs(outputs):
"Format outputs."
if not outputs:
return
outputs.sort(key=lambda v: v.name)
yield '| name | description | sensitive |'
yield '|---|---|:---:|'
for o in outputs:
yield '| {name} | {description} | {sensitive} |'.format(
name=o.name, description=o.description,
sensitive='✓' if o.sensitive else '')
def format_type(type_spec):
"Format variable type."
if not type_spec:
return ''
buffer = []
stack = []
for t in RE_TYPE.split(type_spec.split("\n")[0]):
if not t:
continue
if t in '({':
stack.append(t)
elif t in '})':
stack.pop()
buffer.append(t)
for t in reversed(stack):
buffer.append(')' if t == '(' else '}')
return ''.join(buffer).replace('object({})', 'object({...})')
def format_variables(variables, required_first=True):
"Format variables."
if not variables:
return
variables.sort(key=lambda v: v.name)
variables.sort(key=lambda v: v.required, reverse=True)
yield '| name | description | type | required | default |'
yield '|---|---|:---: |:---:|:---:|'
row = (
'| {name} | {description} | <code title="{type_spec}">{type}</code> '
'| {required} | {default} |'
)
for v in variables:
default = type_spec = ''
if not v.required:
default = '<code title="{title}">{default}</code>'
if '\n' in v.default:
default = default.format(title=_escape(v.default), default='...')
else:
default = default.format(title='', default=v.default or '')
if v.type and '(' in v.type:
type_spec = _escape(v.type)
yield row.format(
name=v.name if v.required else '*%s*' % v.name,
description=v.description, required='✓' if v.required else '',
type=format_type(v.type), type_spec=type_spec,
default=default
)
def get_doc(variables, outputs):
"Return formatted documentation."
buffer = ['## Variables\n']
for line in format_variables(variables):
buffer.append(line)
buffer.append('\n## Outputs\n')
for line in format_outputs(outputs):
buffer.append(line)
return '\n'.join(buffer)
def parse_items(content, item_re, item_enum, item_class, item_data_class):
"Parse variable or output items in data."
item = item_class()
for m in item_re.finditer(content):
try:
item.parse_token(item_enum(m.lastindex).name, m.group(m.lastindex))
except ItemParsed as e:
item = item_class()
item.parse_token(item_enum(m.lastindex).name, m.group(m.lastindex))
yield e.args[0]
if item.in_progress:
yield item.close()
def replace_doc(module, doc):
"Replace document in module's README.md file."
try:
readme = open(os.path.join(module, 'README.md')).read()
m = re.search('(?sm)%s.*%s' % (MARK_BEGIN, MARK_END), readme)
if not m:
raise SystemExit('Pattern not found in README file.')
replacement = "{pre}{begin}\n{doc}\n{end}{post}".format(
pre=readme[:m.start()], begin=MARK_BEGIN, doc=doc,
end=MARK_END, post=readme[m.end():])
open(os.path.join(module, 'README.md'), 'w').write(replacement)
except (IOError, OSError) as e:
raise SystemExit('Error replacing in README: %s' % e)
def get_variables(path):
"Get variables for the module in a path"
variables = []
for path in glob.glob(os.path.join(path, 'variables*tf')):
with open(path) as file:
variables += [v for v in parse_items(
file.read(), RE_VARIABLES, VariableToken, Variable, VariableData)]
return variables
def get_outputs(path):
"Get outputs for the module in a path"
outputs = []
for path in glob.glob(os.path.join(path, 'outputs*tf')):
with open(path) as file:
outputs += [o for o in parse_items(
file.read(), RE_OUTPUTS, OutputToken, Output, OutputData)]
return outputs
def check_state(path):
"""Determine if a module's README has all its variables and outputs
documentation up-to-date."""
try:
variables = get_variables(path)
outputs = get_outputs(path)
readme = open(os.path.join(path, 'README.md')).read()
except (IOError, OSError):
return
m = re.search('(?sm)%s.*%s' % (MARK_BEGIN, MARK_END), readme)
if not m:
return
return get_doc(variables, outputs) in readme
@click.command()
@click.argument('module', type=click.Path(exists=True))
@click.option('--replace/--no-replace', default=True)
def main(module=None, replace=True):
"Program entry point."
try:
variables = get_variables(module)
outputs = get_outputs(module)
except (IOError, OSError) as e:
raise SystemExit(e)
doc = get_doc(variables, outputs)
if replace:
replace_doc(module, doc)
else:
print(doc)
if __name__ == '__main__':
main()