-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathvitess_api_reference.py
executable file
·778 lines (706 loc) · 32.2 KB
/
vitess_api_reference.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
#!/usr/bin/python
# Copyright 2018 The Vitess Authors.
#
# 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 json
import os
import optparse
import pprint
import re
#def print_api_summary(doc, service_summary):
# doc.write(service_summary + '\n\n')
def print_method_summary(doc, proto_contents, methods):
for method in sorted(methods, key=lambda k: k['name']):
method_group_info = method['comment'].split(' API group: ')
if len(method_group_info) > 1:
method['group'] = method_group_info[1]
method['comment'] = method_group_info[0]
else:
method['group'] = 'Uncategorized'
doc.write('This document describes Vitess API methods that enable your ' +
'client application to more easily talk to your storage system ' +
'to query data. API methods are grouped into the following ' +
'categories:\n\n')
last_group = ''
for group in proto_contents['group-ordering']:
for method in sorted(methods, key=lambda k: (k['group'], k['name'])):
if (group.lower() == method['group'].lower() and
not method['group'] == last_group):
if not method['group'] == last_group:
doc.write('* [' + method['group'] + ']' +
'(#' + method['group'].replace(' ', '-').lower() + ')\n')
last_group = method['group']
doc.write('\n\n')
doc.write('The following table lists the methods in each group and links ' +
'to more detail about each method:\n\n')
doc.write('<table id="api-method-summary">\n')
last_group = ''
for group in proto_contents['group-ordering']:
for method in sorted(methods, key=lambda k: (k['group'], k['name'])):
if (group.lower() == method['group'].lower() and
not method['group'] == last_group):
print_method_summary_group_row(doc, method['group'])
last_group = method['group']
print_method_summary_row(doc, method)
elif group.lower() == method['group'].lower():
print_method_summary_row(doc, method)
doc.write('</table>\n')
def print_method_summary_group_row(doc, group_name):
doc.write('<tr><td class="api-method-summary-group" colspan="2">' +
group_name + '</td></tr>\n')
def print_method_summary_row(doc, method):
doc.write('<tr>\n')
doc.write('<td><code><a href="#' + method['name'].lower() + '">' +
method['name'] + '</a></code></td>\n<td>')
if 'comment' in method and method['comment']:
doc.write(method['comment'])
doc.write('</td>\n')
doc.write('</tr>\n')
def recursively_add_objects(new_objects, method_file, obj,
properties, proto_contents):
if obj in new_objects:
return
[op_enum_file, op_enum] = get_op_item(proto_contents, obj, 'enums')
[op_file, op_method] = get_op_item(proto_contents, obj, 'messages')
if properties:
if method_file not in new_objects:
new_objects[method_file] = {'messages': {}}
elif 'messages' not in new_objects[method_file]:
new_objects[method_file]['messages'] = {}
new_objects[method_file]['messages'][obj] = 1
for prop in properties:
type_list = prop['type'].split('.')
if len(type_list) == 1:
if (method_file in proto_contents and
'messages' in proto_contents[method_file]):
lil_pc = proto_contents[method_file]['messages']
if type_list[0] in lil_pc and 'properties' in lil_pc[type_list[0]]:
new_objects = recursively_add_objects(new_objects, method_file,
prop['type'], lil_pc[type_list[0]]['properties'],
proto_contents)
elif (obj in lil_pc and
'messages' in lil_pc[obj] and
type_list[0] in lil_pc[obj]['messages'] and
'properties' in lil_pc[obj]['messages'][type_list[0]]):
new_objects = recursively_add_objects(new_objects, method_file,
prop['type'],
lil_pc[obj]['messages'][type_list[0]]['properties'],
proto_contents)
else:
[op_file, op_method] = get_op_item(proto_contents, obj, 'messages')
[op_enum_file, op_enum] = get_op_item(proto_contents, obj, 'enums')
if (op_file and
op_file in proto_contents and
'messages' in proto_contents[op_file] and
type_list[1] in proto_contents[op_file]['messages'] and
'properties' in proto_contents[op_file]['messages'][type_list[1]]):
new_objects = recursively_add_objects(new_objects, op_file, type_list[1],
proto_contents[op_file]['messages'][type_list[1]]['properties'],
proto_contents)
elif (op_enum_file and
op_enum_file in proto_contents and
'enums' in proto_contents[op_enum_file] and
type_list[1] in proto_contents[op_enum_file]['enums']):
if not op_enum_file in new_objects['enums']:
new_objects['enums'][op_enum_file] = {}
new_objects['enums'][op_enum_file][type_list[1]] = (
proto_contents[op_enum_file]['enums'][type_list[1]])
return new_objects
def print_method_details(doc, proto_contents, proto, methods, objects):
last_group = ''
for group in proto_contents['group-ordering']:
for method in sorted(methods, key=lambda k: (k['group'], k['name'])):
if (group.lower() == method['group'].lower() and
not method['group'] == last_group):
doc.write('##' + method['group'] + '\n')
last_group = method['group']
print_method_detail_header(doc, method)
print_method_detail_request(doc, proto_contents, proto, method)
print_method_detail_response(doc, proto_contents, proto, method)
elif group.lower() == method['group'].lower():
print_method_detail_header(doc, method)
print_method_detail_request(doc, proto_contents, proto, method)
print_method_detail_response(doc, proto_contents, proto, method)
new_objects = {}
for obj in sorted(objects):
type_list = obj.split('.')
if len(type_list) == 1:
method_file = objects[obj]['methods'][0]['method_file']
if (method_file in proto_contents and
'messages' in proto_contents[method_file] and
obj in proto_contents[method_file]['messages'] and
'properties' in proto_contents[method_file]['messages'][obj]):
new_objects = recursively_add_objects(new_objects, method_file, obj,
proto_contents[method_file]['messages'][obj]['properties'],
proto_contents)
else:
[op_file, op_method] = get_op_item(proto_contents, obj, 'messages')
[op_enum_file, op_enum] = get_op_item(proto_contents, obj, 'enums')
if (op_file and
op_file in proto_contents and
'messages' in proto_contents[op_file] and
type_list[1] in proto_contents[op_file]['messages'] and
'properties' in proto_contents[op_file]['messages'][type_list[1]]):
new_objects = recursively_add_objects(new_objects, op_file, type_list[1],
proto_contents[op_file]['messages'][type_list[1]]['properties'],
proto_contents)
elif (op_enum_file and
op_enum_file in proto_contents and
'enums' in proto_contents[op_enum_file] and
type_list[1] in proto_contents[op_enum_file]['enums']):
if not op_enum_file in new_objects:
new_objects[op_enum_file] = {'enums':{}}
elif not 'enums' in new_objects[op_enum_file]:
new_objects[op_enum_file]['enums'] = {}
new_objects[op_enum_file]['enums'][type_list[1]] = (
proto_contents[op_enum_file]['enums'][type_list[1]])
#print json.dumps(new_objects, sort_keys=True, indent=2)
print_nested_objects(doc, new_objects, proto_contents)
def print_nested_objects(doc, objects, proto_contents):
doc.write('## Enums\n\n')
for obj in sorted(objects):
if obj == 'vtgate.proto':
print_proto_enums(doc, proto_contents, obj, objects,
{'strip-proto-name': 1})
for obj in sorted(objects):
if not obj == 'vtgate.proto':
print_proto_enums(doc, proto_contents, obj, objects, {})
doc.write('## Messages\n\n')
for obj in sorted(objects):
if obj == 'vtgate.proto':
print_proto_messages(doc, proto_contents, obj, objects,
{'strip-proto-name': 1})
for obj in sorted(objects):
if not obj == 'vtgate.proto':
print_proto_messages(doc, proto_contents, obj, objects, {})
def print_message_detail_header(doc, proto, message_details, message_name,
options):
header_size = '###'
if 'header-size' in options:
header_size = options['header-size']
message = (proto.replace('.proto', '').replace('.pb.go', '') +
'.' + message_name)
if (options and
'strip-proto-name' in options and
options['strip-proto-name']):
message = message_name
elif options and 'add-method-name' in options and 'method-name' in options:
message = options['method-name'] + '.' + message_name
doc.write(header_size + ' ' + message + '\n\n')
if 'comment' in message_details and message_details['comment']:
doc.write(message_details['comment'].strip() + '\n\n')
def print_method_detail_header(doc, method):
doc.write('### ' + method['name'] + '\n\n')
if 'comment' in method and method['comment']:
doc.write(method['comment'] + '\n\n')
def print_properties_header(doc, header, table_headers):
if header:
doc.write('##### ' + header + '\n\n')
if table_headers:
doc.write('| ')
for field in table_headers:
doc.write(field + ' |')
doc.write('\n')
for field in table_headers:
doc.write('| :-------- ')
doc.write('\n')
def print_property_row(doc, proto_contents, proto, method_file, method, prop):
# Print property/parameter name
if 'name' in prop:
doc.write('| <code>' + prop['name'] + '</code> ')
method_in_messages = False
enum_in_messages = False
for key in proto_contents[proto]['messages']:
if key == method:
method_in_messages = True
for key in proto_contents[proto]['enums']:
if key == prop['type']:
enum_in_messages = True
# Print property/parameter type
if 'type' in prop and prop['type']:
doc.write('<br>')
prop_text = ''
if prop['type'][0:5] == 'map <':
map_value = prop['type'].split(',')[1].split('>')[0].strip()
if (map_value and
map_value in proto_contents[proto]['messages']):
prop_text = (prop['type'].split(',')[0] + ', [' + map_value + ']' +
'(#' + proto.lower().replace('.proto', '') + '.' +
map_value.lower() + ')' + '>')
prop_text = prop_text.replace('<', '<').replace('>', '>')
else:
type_list = prop['type'].split('.')
if len(type_list) == 2:
prop_text = ('[' + prop['type'] + '](#' + type_list[0] + '.' +
type_list[1].lower() + ')')
elif (method_file and
prop['type'] in proto_contents[method_file]['messages']):
if method_file == 'vtgate.proto':
prop_text = '[' + prop['type'] + '](#' + prop['type'].lower() + ')'
else:
prop_text = ('[' + prop['type'] +
'](#' + proto.lower().replace('.proto', '') + '.' +
prop['type'].lower() + ')')
elif enum_in_messages:
prop_text = ('[' + prop['type'] + ']' +
'(#' + proto.lower().replace('.proto', '') + '.' +
prop['type'].lower() + ')')
elif (method_file and
prop['type'] in proto_contents[method_file]['enums']):
prop_text = '[' + prop['type'] + '](#' + prop['type'].lower() + ')'
elif (method_in_messages and
'messages' in proto_contents[proto]['messages'][method]):
if prop['type'] in proto_contents[proto]['messages'][method]['messages']:
prop_text = '[' + prop['type'] + '](#' + method.lower() + '.' + prop['type'].lower() + ')'
elif prop['type'] in proto_contents[proto]['messages'][method]['enums']:
prop_text = '[' + prop['type'] + '](#' + method.lower() + '.' + prop['type'].lower() + ')'
else:
prop_text = prop['type']
else:
prop_text = prop['type']
bad_text = True
for p in proto_contents:
if 'messages' in proto_contents[p] and proto_contents[p]['messages']:
for m in proto_contents[p]['messages']:
if ('messages' in proto_contents[p]['messages'][m] and
proto_contents[p]['messages'][m]['messages'] and
prop['type'] in proto_contents[p]['messages'][m]['messages']):
prop_text = ('[' + prop['type'] + ']' +
'(#' + m.lower() + '.' + prop['type'].lower() + ')')
bad_text = False
if bad_text and isinstance(method, basestring):
for p in proto_contents:
if ('messages' in proto_contents[p] and
proto_contents[p]['messages']):
for m in proto_contents[p]['messages']:
if ('messages' in proto_contents[p]['messages'][m] and
proto_contents[p]['messages'][m]['messages'] and
method in proto_contents[p]['messages'][m]['messages'] and
'enums' in proto_contents[p]['messages'][m]['messages'][method] and
prop['type'] in proto_contents[p]['messages'][m]['messages'][method]['enums']):
prop_text = ('[' + prop['type'] + ']' +
'(#' + m.lower() + '.' + method.lower() + '.' +
prop['type'].lower() + ')')
if 'status' in prop and prop['status'] == 'repeated':
prop_text = 'list <' + prop_text + '>'
doc.write(prop_text)
# Print property/parameter definition.
if 'type' in prop and prop['type']:
# If type contains period -- e.g. vtrpc.CallerId -- then it refers to
# a message in another proto. We want to print the comment identifying
# that message. In that case, the link field should also link to a doc
# for that proto.
type_list = prop['type'].split('.')
[op_file, op_method] = get_op_item(proto_contents, prop['type'],
'messages')
[op_enum_file, op_enum] = get_op_item(proto_contents, prop['type'], 'enums')
if op_method:
doc.write('| ' + op_method['comment'].strip())
elif op_enum:
doc.write('| ' + op_enum['comment'].strip())
elif (method_file and
prop['type'] in proto_contents[method_file]['messages']):
if ('comment' in proto_contents[method_file]['messages'][prop['type']] and
proto_contents[method_file]['messages'][prop['type']]['comment']):
doc.write('| ' +
proto_contents[method_file]['messages'][prop['type']]['comment'].strip())
else:
doc.write('|')
elif 'comment' in prop and prop['comment']:
doc.write('| ' + prop['comment'].strip())
else:
doc.write('|')
elif 'comment' in prop and prop['comment']:
doc.write('| ' + prop['comment'].strip())
else:
doc.write('|')
doc.write(' |\n')
def get_op_item(proto_contents, item, item_type):
item_list = item.split('.')
if len(item_list) == 2:
item_file = item_list[0] + '.proto'
item_enum = item_list[1]
if item_file in proto_contents:
if item_type in proto_contents[item_file]:
if item_enum in proto_contents[item_file][item_type]:
return item_file, proto_contents[item_file][item_type][item_enum]
else:
return [None, None]
else:
return [None, None]
else:
return [None, None]
else:
return [None, None]
def print_method_detail_request(doc, proto_contents, proto, method):
if method['request']:
[op_file, op_method] = get_op_item(proto_contents, method['request'],
'messages')
if (op_method and
'comment' in op_method and
op_method['comment']):
doc.write('#### Request\n\n')
doc.write(op_method['comment'] + '\n\n')
print_properties_header(doc, 'Parameters', ['Name', 'Description'])
for prop in op_method['properties']:
print_property_row(doc, proto_contents, proto, op_file, op_method, prop)
doc.write('\n')
if 'messages' in op_method and op_method['messages']:
doc.write('#### Messages\n\n')
for message in sorted(op_method['messages']):
print_proto_message(doc, proto, proto_contents,
op_method['messages'][message], message,
{'header-size': '#####',
'add-method-name': 1,
'method-name': method['request'].split('.')[1]})
def print_method_detail_response(doc, proto_contents, proto, method):
if method['response']:
[op_file, op_method] = get_op_item(proto_contents,
method['response'].replace('stream ', ''), 'messages')
if (op_method and
'comment' in op_method and
op_method['comment']):
doc.write('#### Response\n\n')
doc.write(op_method['comment'] + '\n\n')
print_properties_header(doc, 'Properties', ['Name', 'Description'])
for prop in op_method['properties']:
print_property_row(doc, proto_contents, proto, op_file, op_method, prop)
doc.write('\n')
if 'messages' in op_method and op_method['messages']:
doc.write('#### Messages\n\n')
for message in sorted(op_method['messages']):
print_proto_message(doc, proto, proto_contents,
op_method['messages'][message], message,
{'header-size': '#####',
'add-method-name': 1,
'method-name': method['response'].split('.')[1]})
def print_proto_file_definition(doc, proto_contents, proto):
if ('file_definition' in proto_contents[proto] and
proto_contents[proto]['file_definition']):
doc.write(proto_contents[proto]['file_definition'].strip() + '\n\n')
def print_proto_enum(doc, enum_details, enum_name, proto, options):
header_size = '###'
if 'header-size' in options:
header_size = options['header-size']
# Print name of enum as header
enum_header = proto.replace('.proto', '') + '.' + enum_name
if options and 'add-method-name' in options and 'method-name' in options:
enum_header = options['method-name'] + '.' + enum_name
elif (options and
'strip-proto-name' in options and
options['strip-proto-name']):
enum_header = enum_name
doc.write(header_size + ' ' + enum_header + '\n\n')
if 'comment' in enum_details and enum_details['comment']:
doc.write(enum_details['comment'] + '\n\n')
print_properties_header(doc, None, ['Name', 'Value', 'Description'])
for value in enum_details['values']:
# Print enum text
if 'text' in value:
doc.write('| <code>' + value['text'] + '</code> ')
else:
doc.write('| ')
# Print enum value
if 'value' in value and value['value']:
doc.write('| <code>' + value['value'] + '</code> ')
else:
doc.write('| ')
# Print enum value description
if 'comment' in value and value['comment']:
doc.write('| ' + value['comment'].strip() + ' ')
else:
doc.write('| ')
doc.write(' |\n')
doc.write('\n')
def print_proto_message(doc, proto, proto_contents, message_details, message,
options):
print_message_detail_header(doc, proto, message_details, message, options)
if 'header-size' in options:
doc.write('<em>Properties</em>\n\n')
else:
doc.write('#### Properties\n\n')
print_properties_header(doc, None, ['Name', 'Description'])
for prop in message_details['properties']:
print_property_row(doc, proto_contents, proto, proto, message, prop)
doc.write('\n')
option_method_name = message
if 'method-name' in options:
option_method_name = options['method-name'] + '.' + message
if 'enums' in message_details and message_details['enums']:
doc.write('#### Enums\n\n')
for enum in sorted(message_details['enums']):
print_proto_enum(doc, message_details['enums'][enum], enum, proto,
{'header-size': '#####',
'add-method-name': 1,
'method-name': option_method_name})
if 'messages' in message_details and message_details['messages']:
doc.write('#### Messages\n\n')
for child_message in sorted(message_details['messages']):
print_proto_message(doc, proto, proto_contents,
message_details['messages'][child_message], child_message,
{'header-size': '#####',
'add-method-name': 1,
'method-name': option_method_name})
def print_proto_messages(doc, proto_contents, proto, objects_to_print, options):
if 'messages' in proto_contents[proto] and proto_contents[proto]['messages']:
for message in sorted(proto_contents[proto]['messages']):
if ('messages' in objects_to_print[proto] and
message in objects_to_print[proto]['messages']):
print_proto_message(doc, proto, proto_contents,
proto_contents[proto]['messages'][message],
message, options)
def print_proto_enums(doc, proto_contents, proto, objects_to_print, options):
if 'enums' in proto_contents[proto] and proto_contents[proto]['enums']:
for enum in sorted(proto_contents[proto]['enums']):
if ('enums' in objects_to_print[proto] and
enum in objects_to_print[proto]['enums']):
print_proto_enum(doc, proto_contents[proto]['enums'][enum], enum,
proto, options)
def create_reference_doc(proto_directory, doc_directory, proto_contents,
addl_types):
for proto in proto_contents:
if 'service' in proto_contents[proto]:
if (proto_contents[proto]['service']['name'] and
proto_contents[proto]['service']['name'] == 'Vitess'):
doc = open(doc_directory + 'VitessApi.md', 'w')
#if proto_contents[proto]['file_definition']:
# print_api_summary(doc, proto_contents[proto]['file_definition'])
if proto_contents[proto]['service']['methods']:
print_method_summary(doc, proto_contents,
proto_contents[proto]['service']['methods'])
if proto_contents[proto]['service']['methods']:
print_method_details(doc, proto_contents, proto,
proto_contents[proto]['service']['methods'],
addl_types)
doc.close()
return
def parse_method_details(line):
details = re.findall(r'rpc ([^\(]+)\(([^\)]+)\) returns \(([^\)]+)', line)
if details:
return {'name': details[0][0],
'request': details[0][1],
'response': details[0][2]}
return {}
def get_enum_struct(comment):
return {'comment': comment,
'values': []}
def get_message_struct(comment):
return {'comment': comment,
'enums': {},
'messages': {},
'properties': []}
def add_property(message, prop_data, prop_type, comment):
message['properties'].append({'type': prop_type,
'name': prop_data[0][2],
'position': prop_data[0][3],
'status': prop_data[0][0],
'comment': comment})
return message
def build_property_type_list(types, proto_contents, method):
[op_file, op_method] = get_op_item(proto_contents, method, 'messages')
if op_method and 'properties' in op_method:
for prop in op_method['properties']:
if 'map' in prop['type']:
map_fields = re.findall(r'map\s*<([^\,]+)\,\s*([^\>]+)', prop['type'])
if map_fields:
for x in range(0,2):
if '.' in map_fields[0][x]:
types.append(map_fields[0][x])
elif map_fields[0][x][0].isupper():
types.append(op_file.replace('.proto', '') + '.' +
map_fields[0][x])
elif '.' in prop['type']:
types.append(prop['type'])
elif prop['type'][0].isupper():
if prop['type'] in proto_contents[op_file]['messages']:
types.append(op_file.replace('.proto', '') + '.' + prop['type'])
elif prop['type'] in proto_contents[op_file]['enums']:
types.append(op_file.replace('.proto', '') + '.' + prop['type'])
else:
for message in proto_contents[op_file]['messages']:
if 'messages' in proto_contents[op_file]['messages'][message]:
child_messages = (
proto_contents[op_file]['messages'][message]['messages'])
if (prop['type'] in child_messages and
'properties' in child_messages[prop['type']]):
for child_prop in child_messages[prop['type']]['properties']:
if '.' in child_prop['type']:
types.append(child_prop['type'])
return types
def main(proto_directory, doc_directory):
arg_definitions = {}
commands = {}
command_groups = {}
error_counts = {}
functions = {}
# Read the .go files in the /vitess/go/vt/vtctl/ directory
api_file_path = proto_directory
proto_dirs = next(os.walk(api_file_path))[2]
proto_lines = {}
proto_contents = {}
for path in proto_dirs:
if not path.endswith('.proto'):
continue
api_proto_file = open(proto_directory + path, 'rU')
proto_lines[path.replace('pb.go', 'proto')] = api_proto_file.readlines()
api_proto_file.close()
# parse .proto files
for path in proto_lines:
comment = ''
enum_values = []
inside_service = ''
current_message = {}
current_top_level_message = {}
current_hierarchy = []
current_struct = ''
syntax_specified = False
proto_contents[path] = {'file_definition': '',
'imports': [],
'enums': {},
'messages': {},
'methods': {},
'service': {'name': '',
'methods': []}
}
for original_line in proto_lines[path]:
line = original_line.strip()
if line[0:8] == 'syntax =':
syntax_specified = True
continue
if line[0:2] == '//' and not syntax_specified:
proto_contents[path]['file_definition'] += (' ' + line[2:].strip())
continue
elif line[0:2] == '//':
if 'TODO' not in line:
comment += ' ' + line[2:].strip()
elif line[0:6] == 'import':
import_file = line[6:].strip().rstrip(';').strip('"').split('/').pop()
proto_contents[path]['imports'].append(import_file)
elif line[0:8] == 'service ':
service = line[8:].strip().rstrip('{').strip()
proto_contents[path]['service']['name'] = service
inside_service = service
comment = ''
elif inside_service:
if line[0:4] == 'rpc ':
method_details = parse_method_details(line)
if method_details:
if comment:
method_details['comment'] = comment.strip()
proto_contents[path]['service']['methods'].append(method_details)
comment = ''
elif line == '}':
item_to_add = current_hierarchy.pop().split('-')
if item_to_add[0] == 'enum':
current_enum['values'] = enum_values
enum_values = []
if len(current_hierarchy) > 0:
go_back_to_struct = current_hierarchy[-1].split('-')[0]
if go_back_to_struct == 'topLevelMessage':
current_top_level_message['enums'][item_to_add[1]] = current_enum
elif go_back_to_struct == 'message':
current_message['enums'][item_to_add[1]] = current_enum
current_struct = go_back_to_struct
else:
if current_struct == 'enum':
proto_contents[path]['enums'][item_to_add[1]] = current_enum
current_struct = ''
elif item_to_add[0] == 'message':
current_top_level_message['messages'][item_to_add[1]] = (
current_message)
current_struct = current_hierarchy[-1].split('-')[0]
elif item_to_add[0] == 'topLevelMessage':
proto_contents[path]['messages'][item_to_add[1]] = (
current_top_level_message)
current_struct = ''
elif original_line[0:8] == 'message ':
message = line[8:].strip().rstrip('{').strip()
current_top_level_message = get_message_struct(comment)
comment = ''
current_hierarchy.append('topLevelMessage-' + message)
current_struct = 'topLevelMessage'
elif line[0:8] == 'message ':
message = line[8:].strip().rstrip('{').strip()
current_message = get_message_struct(comment)
current_hierarchy.append('message-' + message)
current_struct = 'message'
elif line[0:5] == 'enum ':
enum = line[5:].strip().rstrip('{').strip()
current_enum = get_enum_struct(comment)
current_hierarchy.append('enum-' + enum)
current_struct = 'enum'
comment = ''
elif current_struct == 'enum':
enum_value_data = re.findall(r'([a-zA-Z0-9_]+)\s*=\s*(\d+)', line)
if enum_value_data:
enum_values.append({'comment': comment,
'text': enum_value_data[0][0],
'value': enum_value_data[0][1]})
comment = ''
else:
prop_data = re.findall(r'(optional|repeated|required)?\s*([\w\.\_]+)\s+([\w\.\_]+)\s*=\s*(\d+)', line)
if prop_data:
if current_struct == 'topLevelMessage':
current_top_level_message = add_property(current_top_level_message,
prop_data, prop_data[0][1],
comment)
elif current_struct == 'message':
current_message = add_property(current_message, prop_data,
prop_data[0][1], comment)
comment = ''
else:
prop_data = re.findall(r'(optional|repeated|required)?\s*map\s*\<([^\>]+)\>\s+([\w\.\_]+)\s*=\s*(\d+)', line)
if prop_data:
prop_type = 'map <' + prop_data[0][1] + '>'
if current_struct == 'topLevelMessage':
current_top_level_message = add_property(
current_top_level_message, prop_data, prop_type, comment)
elif current_struct == 'message':
current_message = add_property(current_message, prop_data,
prop_type, comment)
comment = ''
#print json.dumps(proto_contents, sort_keys=True, indent=2)
methods = []
types = []
for method in proto_contents['vtgateservice.proto']['service']['methods']:
methods.append(method['request'])
methods.append(method['response'].replace('stream ', ''))
for method in methods:
types = build_property_type_list(types, proto_contents, method)
types = list(set(types))
type_length = len(types)
new_type_length = 100
for x in range(0, 10):
#while type_length < new_type_length:
for prop_type in types:
types = build_property_type_list(types, proto_contents, prop_type)
types = list(set(types))
new_type_length = len(types)
proto_contents['group-ordering'] = ['Range-based Sharding',
'Transactions',
'Custom Sharding',
'Map Reduce',
'Topology',
'v3 API (alpha)']
create_reference_doc(proto_directory, doc_directory, proto_contents, types)
return
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-p', '--proto-directory', default='../proto/',
help='The root directory for the Vitess GitHub tree')
parser.add_option('-d', '--doc-directory', default='',
help='The directory where the documentation resides.')
(options,args) = parser.parse_args()
main(options.proto_directory, options.doc_directory)