forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_snippets_to_file.py
368 lines (339 loc) · 14.7 KB
/
add_snippets_to_file.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Copyright 2016 Google Inc. All rights reserved.
#
# 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
#
# http://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 argparse
import logging
import os
import plyj.model as ast
import plyj.parser as plyj
import re
NEWLINE_PATTERN = re.compile(r'\n')
LOGGER = logging.getLogger(__name__)
class Parameter(object):
def __init__(self, name, signature_type, complete_type):
self.name = name
self.signature_type = signature_type
self.complete_type = complete_type
class Method(object):
def __init__(self, name, parameters, signature, regex):
self.name = name
self.parameters = parameters
self.signature = signature
self.regex = regex
@classmethod
def parse_type(csl, ast_type, is_vararg):
parameter_type = ''
parameter_complete_type = ''
if isinstance(ast_type, ast.Wildcard):
parameter_type = '?'
parameter_complete_type = '?'
elif isinstance(ast_type, ast.Type):
if isinstance(ast_type.name, ast.Name):
parameter_type = ast_type.name.value
parameter_complete_type = ast_type.name.value
else:
parameter_type = ast_type.name
parameter_complete_type = ast_type.name
if hasattr(ast_type, 'type_arguments') and ast_type.type_arguments:
parameter_complete_type += '<{}>'.format(
', '.join(Method.parse_type(astSubType, False)[1]
for astSubType in ast_type.type_arguments))
dimensions = ''.join('[]' * ast_type.dimensions)
parameter_type += dimensions
parameter_complete_type += dimensions
else:
parameter_type = ast_type
parameter_complete_type = ast_type
if is_vararg:
parameter_type += '...'
parameter_complete_type += '...'
return (parameter_type, parameter_complete_type)
@classmethod
def parse_arguments(csl, ast_method_declaration):
parameters = []
for ast_parameter in ast_method_declaration.parameters:
(parameter_signature_type, parameter_complete_type) = Method.parse_type(
ast_parameter.type,
hasattr(ast_parameter, 'vararg') and ast_parameter.vararg)
parameter = Parameter(
ast_parameter.variable.name,
parameter_signature_type,
parameter_complete_type)
parameters.append(parameter)
return parameters
@classmethod
def parse_methods(cls, data, class_name):
parser = plyj.Parser()
tree = parser.parse_string(data)
LOGGER.info('Parsing methods for class %s.', class_name)
methods = []
for ast_type_declaration in tree.type_declarations:
if ast_type_declaration.name == class_name:
for ast_declaration in ast_type_declaration.body:
if isinstance(ast_declaration, ast.MethodDeclaration):
method_name = ast_declaration.name
parameters = Method.parse_arguments(ast_declaration)
signature = '{}({})'.format(
method_name, ', '.join(
[parameter.signature_type for parameter in parameters]))
regex = '{}\(\s*{}\)'.format(re.escape(method_name),
',\s+'.join(['(final\s+)?{}\s+{}'.format(
re.escape(parameter.complete_type), re.escape(parameter.name))
for parameter in parameters]))
method = cls(method_name, parameters, signature, regex)
methods.append(method)
return methods
class Snippet(object):
def __init__(self, name, target, caption, variables, code, signature, parameters):
self.name = name
self.target = target
self.caption = caption
self.variables = variables
self.code = code
self.signature = signature
self.parameters = parameters
def to_javadoc(snippet):
"""
Returns a list of string representing the snippet in javadoc format.
"""
javadoc = []
caption = snippet.caption
head, tail = caption[0], caption[1:]
parameters = snippet.parameters
variables = snippet.variables
if len(parameters) != len(variables):
raise ValueError('Parameters do not match variables for method {}'.format(
snippet.signature))
javadoc.append(' *\n')
javadoc.append(' * <p>' + head + '\n')
for line in tail:
javadoc.append(' * ' + line + '\n')
javadoc.append(' * <pre> {@code\n')
for parameter, value in zip(parameters, variables):
javadoc.append(' * {} {} = {};\n'.format(
parameter.complete_type,
parameter.name,
value))
first_code_line = snippet.code[0]
indent = len(first_code_line) - len(first_code_line.lstrip())
for codeLine in snippet.code:
javadoc.append(' * ' + codeLine[indent:] + '\n')
javadoc.append(' * }</pre>\n')
return javadoc
@classmethod
def parse_caption(cls, index, lines):
caption = []
while index < len(lines):
stripped_line = lines[index].strip()
if stripped_line.startswith('*/'):
return (index, caption)
caption.append(stripped_line.strip('*').lstrip())
index += 1
raise ValueError('Javadoc comment is never closed')
@classmethod
def parse_code(cls, index, lines, name):
code = []
while index < len(lines):
line = lines[index].rstrip()
if line.lstrip().startswith('// [END ' + name + ']'):
return (index, code)
code.append(line)
index += 1
raise ValueError('Snippet {} is missing an END tag'.format(name))
@classmethod
def parse_snippet_javadoc(csl, index, lines):
javadoc = []
while index >= 0:
line = lines[index]
index -= 1
javadoc.append(line)
stripped_line = line.lstrip()
if not stripped_line.startswith('*'):
raise ValueError('Could not parse javadoc snippet:\n{}'.format(javadoc))
if stripped_line.startswith('* <p>'):
if index >= 0:
next_line = lines[index].strip()
if next_line == '*':
javadoc.append(next_line)
return javadoc
raise ValueError('Could not parse javadoc snippet:\n{}'.format(javadoc))
@classmethod
def remove_snippets(csl, signature, lines, line_numbers):
"""Removes javadoc snippets for a method with the provided signature.
This method removes the lines that correspond to javadoc snippets, returns the updated
lines and updates the line_numbers data structure in the process.
A snippet's javadoc should have the following format (as generated by to_javadoc()):
* <p>...
* ...
* ...
* ...}</pre>
"""
index = line_numbers.get(signature)
LOGGER.info('Removing snippets for method %s', signature)
while index >= 0:
line = lines[index].lstrip()
if line.startswith('/**'):
return lines
if line.rstrip().endswith('}</pre>'):
javadoc = Snippet.parse_snippet_javadoc(index, lines);
new_index = index - len(javadoc)
line_numbers.update(index, - len(javadoc))
lines = lines[:new_index + 1] + lines[index + 1:]
index = new_index
else:
index -= 1
raise ValueError('Could not parse javadoc snippets for method {}'.format(signautre))
@classmethod
def parse_snippets(cls, snippets_filename):
"""Parses a file looking for code snippets. Returns a list of Snippet objects.
Snippets should have the following format:
/**
* Snippet caption.
*/
// [TARGET method(Type1, Type2)]
// [VARIABLE "stringValue"]
// [VARIABLE 42]
public void snippetMethod(String arg1, int arg2) {
// [START snippetMethod]
here goes snippet code
// [END snippetMethod]
}
"""
snippets_name = os.path.splitext(os.path.basename(snippets_filename))[0]
LOGGER.info('Parsing snippets from class %s.', snippets_name)
snippets = []
with open(snippets_filename, 'r') as snippets_file:
snippets_string = snippets_file.read()
methods = Method.parse_methods(snippets_string, snippets_name)
target = None
caption = []
variables = []
code = []
snippets_lines = snippets_string.splitlines(True)
index = 0
method_index = 0
while index < len(snippets_lines):
stripped_line = snippets_lines[index].strip()
index += 1
if stripped_line.startswith('public class {} {{'.format(snippets_name)):
break
while index < len(snippets_lines):
stripped_line = snippets_lines[index].strip()
index += 1
if stripped_line.startswith('/**'):
target = None
caption = ''
variables = []
code = ''
(index, caption) = Snippet.parse_caption(index, snippets_lines)
continue
if stripped_line.startswith('// [TARGET'):
target = stripped_line.replace('// [TARGET ', '').rstrip(']')
continue
if stripped_line.startswith('// [START '):
snippet_name = stripped_line.replace('// [START ', '').rstrip(']')
(index, code) = Snippet.parse_code(index, snippets_lines, snippet_name)
snippet = cls(
snippet_name,
target,
caption,
variables,
code,
methods[method_index].signature,
methods[method_index].parameters)
snippets.append(snippet)
method_index += 1
continue
if stripped_line.startswith('// [VARIABLE'):
variables.append(stripped_line.replace('// [VARIABLE ', '').rstrip(']'))
continue
return snippets
@classmethod
def write_snippets(cls, snippets, filename):
"""
Writes the collection of the Snippets to the provided file.
"""
with open (filename, 'r+') as java_file:
string = java_file.read()
lines = string.splitlines(True)
class_name = os.path.splitext(os.path.basename(filename))[0]
methods = Method.parse_methods(string, class_name)
line_numbers = LineNumbers(string, methods)
for method in methods:
lines = Snippet.remove_snippets(method.signature, lines, line_numbers)
for snippet in snippets:
target = snippet.target
LOGGER.info('Building snippet for method %s#%s.', class_name, target)
target_line = line_numbers.get(target)
index = target_line - 2
javadoc = snippet.to_javadoc()
while index >= 0:
stripped_line = lines[index].strip()
if (stripped_line.startswith('/**')):
break
index -= 1
previous_line = ''
while index <= len(lines):
stripped_line = lines[index].strip()
if stripped_line == '*/' or stripped_line.startswith('* @'):
if previous_line.strip() != '*':
indent = len(previous_line) - len(previous_line.lstrip())
javadoc = javadoc + [previous_line[:indent] + '*\n']
else:
index -= 1
break
previous_line = lines[index]
index += 1
lines[index:index] = javadoc
line_numbers.update(target_line, len(javadoc))
java_file.seek(0)
java_file.writelines(lines)
java_file.truncate()
class LineNumbers(object):
def __init__(self, data, methods):
self.line_numbers = {}
for method in methods:
pattern = re.compile('\s+' + method.regex)
for match in pattern.finditer(data):
line_number = len(NEWLINE_PATTERN.findall(data[:match.start()])) + 1
signature = method.signature
LOGGER.info('Method %s found at line %d.', signature, line_number)
self.line_numbers[signature] = line_number
def get(self, signature):
"""
Returns the line number for the provided method.
"""
return self.line_numbers[signature]
def update(self, at, count):
"""
Inserts (if count is positive) or removes (if count is negative) count lines at the
provided position. Line numbers are updated accordingly.
"""
updated_line_numbers = {}
for target in self.line_numbers:
target_line = self.line_numbers[target]
if (target_line >= at):
target_line += count
updated_line_numbers[target] = target_line
self.line_numbers = updated_line_numbers
def main():
parser = argparse.ArgumentParser(description='Add snippets to Javadoc.')
parser.add_argument('snippets_path', help='Path to the Java source file for the snippets.')
parser.add_argument('class_path', help='Path to the Java source file where to add snippets.')
args = parser.parse_args()
snippets_file = args.snippets_path
class_file = args.class_path
snippets = Snippet.parse_snippets(snippets_file)
Snippet.write_snippets(snippets, class_file)
if __name__ == '__main__':
main()