-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathdocumentation_helper.py
1564 lines (1333 loc) · 59.7 KB
/
documentation_helper.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
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from .codegen_helper import filter_parameters
from .codegen_helper import get_params_snippet
from .documentation_snippets import attr_note_text
from .documentation_snippets import enum_note_text
from .documentation_snippets import func_note_text
from .documentation_snippets import rep_cap_attr_desc_docstring_r
from .documentation_snippets import rep_cap_attr_desc_docstring_rw
from .documentation_snippets import rep_cap_attr_desc_docstring_w
from .documentation_snippets import rep_cap_attr_desc_rst_r
from .documentation_snippets import rep_cap_attr_desc_rst_rw
from .documentation_snippets import rep_cap_attr_desc_rst_w
from .documentation_snippets import rep_cap_method_desc_docstring
from .documentation_snippets import rep_cap_method_desc_rst
from .helper import get_array_type_for_api_type
from .helper import get_numpy_type_for_api_type
from .parameter_usage_options import ParameterUsageOptions
import pprint
import re
import string
import sys
pp = pprint.PrettyPrinter(indent=4, width=80)
# Python 2/3 compatibility
def _normalize_string_type(d):
'''Normalize string type between python2 & python3'''
if sys.version_info.major < 3:
if type(d) is dict:
for k in d:
d[k] = _normalize_string_type(d[k])
elif type(d) is str:
d = d.decode('utf-8')
return d
def get_indented_docstring_snippet(d, indent=4):
'''Returns a docstring with the correct amount of indentation.
First line is not indented.
Can't use similar construct as get_dictionary_snippet
('\n' + (' ' * indent)).join(d_lines) because empty lines would get
the spaces, which violates pep8 and causes the flake8 step to fail
Args:
docstring (str): multiline string to format
indent (int): How much to indent lines 2+
Returns:
str: formatted string
'''
d_lines = d.strip().splitlines()
ret_val = ''
for l in d_lines:
if len(ret_val) > 0:
ret_val += '\n'
if len(l.rstrip()) > 0:
ret_val += (' ' * indent)
ret_val += _normalize_string_type(l.rstrip())
return ret_val
def get_rst_header_snippet(t, header_level='='):
'''Get rst formatted heading'''
ret_val = t + '\n'
ret_val += header_level * len(t)
return ret_val
def get_rst_picture_reference(tag, url, title, link, indent=0):
'''Get rst formatted snippet that represents a picture'''
ret_val = (' ' * indent) + '.. |{0}| image:: {1}\n'.format(tag, url)
ret_val += (' ' * (indent + 4)) + ':alt: {0}\n'.format(title)
ret_val += (' ' * (indent + 4)) + ':target: {0}\n'.format(link)
return ret_val
def _get_rst_table_snippet(node, d, config, indent=0, make_link=True):
'''Returns an rst table snippet if table_header and/or table_body are in the dictionary'''
if 'table_body' in d:
table_body = d['table_body']
else:
return ''
if len(table_body) == 0:
return ''
header = False
# If there is no body, then we ignore the header
if 'table_header' in d:
table_header = d['table_header']
header = True
table_contents = []
if header:
header_contents = []
for i in table_header:
contents = _fix_references(node, i, config, make_link)
header_contents.append(contents)
table_contents.append(header_contents)
for t in table_body:
line_contents = []
for i in t:
contents = _fix_references(node, i, config, make_link)
line_contents.append(contents)
table_contents.append(line_contents)
table = as_rest_table(table_contents, header=header)
return get_indented_docstring_snippet(table, indent)
def get_rst_admonition_snippet(node, admonition, d, config, indent=0):
'''Returns a rst formatted admonition if the given admonition ('note', 'caution') exists in the dictionary'''
if admonition in d:
admonition_content = d[admonition]
if not isinstance(admonition_content, list):
admonition_content = [admonition_content]
a = ''
for admonition_text in admonition_content:
a += '\n\n' + (' ' * indent) + '.. {0}:: '.format(admonition)
a += get_indented_docstring_snippet(_fix_references(node, admonition_text, config, make_link=True), indent + 4)
return a
else:
return ''
def add_attribute_rep_cap_tip_rst(attr, config):
'''Add the appropriate (r/w/rw/none) rst formatted tip for an attribute'''
if 'repeated_capability_type' in attr:
if attr['access'] == 'read only':
tip = rep_cap_attr_desc_rst_r
elif attr['access'] == 'write only':
tip = rep_cap_attr_desc_rst_w
else:
assert attr['access'] == 'read-write'
tip = rep_cap_attr_desc_rst_rw
if 'documentation' not in attr:
attr['documentation'] = {}
attr['documentation']['tip'] = tip.format(config['module_name'], attr['repeated_capability_type'], attr["name"].lower())
def get_documentation_for_node_rst(node, config, indent=0):
'''Returns any documentaion information formatted for rst
Documentation will be in the following order (if existing)
- 'caution' admonition
- 'description'
- table made of 'table_header' and 'table_body'
- 'note' admonition
Args:
node (dict) - Node possibly containing documentation
config (dict) - build configuration
indent (int) - how much each line should be indented
Returns:
str - formatted documentation, empty string if none
'''
doc = ''
if 'documentation' not in node:
return doc
nd = node['documentation']
doc += get_rst_admonition_snippet(node, 'caution', nd, config, indent)
if 'description' in nd:
doc += '\n\n' + (' ' * indent) + get_indented_docstring_snippet(_fix_references(node, nd['description'], config, make_link=True), indent)
doc += '\n\n' + (' ' * indent) + _get_rst_table_snippet(node, nd, config, indent)
doc += get_rst_admonition_snippet(node, 'note', nd, config, indent)
doc += '\n'
doc += get_rst_admonition_snippet(node, 'tip', nd, config, indent)
doc += '\n'
return doc
def get_docstring_admonition_snippet(node, admonition, d, config, indent=0, extra_newline='\n'):
'''Returns a docstring formatted admonition if the given admonition ('note', 'caution') exists in the dictionary
Args:
admonition (str) - admonition to check and format. I.e. 'note', 'tip', etc.
d (dict) - documentation note dictionary
config (dict) - build configuration
indent (int) - how much each line should be indented
extra_newline (str) - empty string or newline - needed to keep docstring formatting correct
Returns:
str - empty string if no admonition, else formatted string with one or more admonitions
'''
if admonition in d:
admonition_content = d[admonition]
if not isinstance(admonition_content, list):
admonition_content = [admonition_content]
a = ''
for admonition_text in admonition_content:
admonition_text = '{0}: {1}'.format(admonition.title(), admonition_text)
admonition_text = _fix_references(node, admonition_text, config, make_link=False)
a += '\n' + extra_newline + (' ' * indent) + get_indented_docstring_snippet(admonition_text, indent)
extra_newline = '\n'
return a
else:
return ''
def add_attribute_rep_cap_tip_docstring(attr, config):
'''Add the appropriate (r/w/rw/none) docstring formatted tip for an attribute'''
if 'repeated_capability_type' in attr:
if attr['access'] == 'read only':
tip = rep_cap_attr_desc_docstring_r
elif attr['access'] == 'write only':
tip = rep_cap_attr_desc_docstring_w
else:
assert attr['access'] == 'read-write'
tip = rep_cap_attr_desc_docstring_rw
if 'documentation' not in attr:
attr['documentation'] = {}
attr['documentation']['tip'] = tip.format(config['module_name'], attr['repeated_capability_type'], attr["name"].lower())
def get_documentation_for_node_docstring(node, config, indent=0):
'''Returns any documentaion information formatted for docstring
Documentation will be in the following order (if existing)
- 'caution' admonition
- 'description'
- table made of 'table_header' and 'table_body'
- 'note' admonition
- 'tip' admonition
Args:
node (dict) - Node possibly containing documentation
config (dict) - build configuration
indent (int) - how much each line should be indented
Returns:
str - formatted documentation, empty string if none
'''
doc = ''
if 'documentation' not in node:
return doc
nd = node['documentation']
extra_newline = ''
if 'caution' in nd:
doc += get_docstring_admonition_snippet(node, 'caution', nd, config, indent, extra_newline)
extra_newline = '\n'
if 'description' in nd:
doc += '\n' + extra_newline + (' ' * indent) + get_indented_docstring_snippet(_fix_references(node, nd['description'], config, make_link=False), indent)
extra_newline = '\n'
tbl = _get_rst_table_snippet(node, nd, config, indent, make_link=False)
if len(tbl) > 0:
doc += '\n' + extra_newline + (' ' * indent) + tbl
extra_newline = '\n'
doc += get_docstring_admonition_snippet(node, 'note', nd, config, indent, extra_newline)
doc += get_docstring_admonition_snippet(node, 'tip', nd, config, indent, extra_newline)
return doc.strip()
# We need this in the global namespace so we can reference it from the sub() callback
config = None
def find_enum_by_value(enums, value, start_enum=None):
'''Returns the enum that contains the given value if there is one
There should only be one so return that individual parameter and not a list
'''
enum = []
values = []
if start_enum:
e = enums[start_enum]
for v in e['values']:
if v['name'] == value:
enum.append(e)
values.append(v)
if len(enum) == 1:
return enum[0], values[0]
for e_name in enums:
e = enums[e_name]
for v in e['values']:
if v['name'] == value:
enum.append(e)
values.append(v)
if len(enum) == 0 or len(enum) > 1:
return None, None
return enum[0], values[0]
def _replace_enum_python_name(e_match):
'''callback function for enum value regex sub command
Args:
m (match object): Match object from the attribute substitution command
Returns:
str: python name of the enum value, possibly set to sphinx python domain data item link
'''
ename = e_match.group(1)
start_enum = config['start_enum']
if e_match:
ename = '{0}_VAL_{1}'.format(config['module_name'].upper(), ename.replace('\\', ''))
enum, value = find_enum_by_value(config['enums'], ename, start_enum)
if enum and enum['codegen_method'] != 'no':
ename = enum['python_name'] + '.' + value['python_name']
if config['make_link']:
return ':py:data:`~{0}.{1}`'.format(config['module_name'], ename)
else:
return '{0}'.format(ename)
def find_attribute_by_name(attributes, name):
'''Returns the attribute with the given name if there is one
There should only be one so return that individual parameter and not a list
'''
attr = [attributes[x] for x in attributes if attributes[x]['name'] == name]
assert len(attr) <= 1, '{0} attributes with name {1}. No more than one is allowed'.format(len(attr), name)
if len(attr) == 0:
return None
return attr[0]
def _replace_attribute_python_name(a_match):
'''callback function for attribute regex sub command
Args:
m (match object): Match object from the attribute substitution command
Returns:
str: python name of the attribute, possibly set to sphinx python domain data item link
'''
aname = "Unknown"
if a_match:
aname = a_match.group(1).replace('\\', '')
attr = find_attribute_by_name(config['attributes'], aname)
if attr:
aname = attr['name'].lower()
if config['make_link']:
if config['module_name'] == 'nitclk':
return ':py:attr:`{0}.SessionReference.{1}`'.format(config['module_name'], aname)
else:
return ':py:data:`{0}.Session.{1}`'.format(config['module_name'], aname)
else:
return '{0}'.format(aname)
def _replace_func_python_name(f_match):
'''callback function for function regex sub command
Args:
f_match (match object): Match object from the function substitution command
Returns:
str: rst link to function using python name, possibly set to sphinx python domain meth item link
'''
fname = "Unknown"
if f_match:
fname = f_match.group(1).replace('.', '').replace(',', '').replace('\\', '')
try:
if 'method_name_for_documentation' in config['functions'][fname]:
fname = config['functions'][fname]['method_name_for_documentation']
else:
fname = config['functions'][fname]['python_name']
except KeyError:
print('Warning: "{0}" not found in function metadata. Typo? Generated code will be funky!'.format(fname))
else:
print('Unknown function name: {0}'.format(f_match.group(1)))
print(config['functions'])
if config['make_link']:
if config['module_name'] == 'nitclk':
return ':py:func:`{0}.{1}`'.format(config['module_name'], fname)
else:
return ':py:meth:`{0}.Session.{1}`'.format(config['module_name'], fname)
else:
return '{0}'.format(fname)
def _replace_urls(u_match):
'''callback function for regex when url link needed
Args:
u_match (match object): Match object from the function substitution command
Returns:
str: replacement url
'''
if config['make_link']:
pages = u_match.group(1)
pages_list = pages.split(',')
url_template = config['driver_urls'][config['url_key']]
url = url_template.format(*pages_list)
return url
else:
return u_match.group(1)
def _fix_references(node, doc, cfg, make_link=False):
'''Replace ATTR and function mentions in documentation
Args:
doc (str): documentation string to be updated
config (dict): config dictionary from metadata
make_link (bool): Default False
True - references are replaced with a rst style link
False - references are replaced with just the python name
Returns:
str: documentation with references replaces based on make_link
'''
# We have to put config into the global namespace because we need the information in the search
# callbacks but cannot pass them in via the search command itself
global config
config = cfg
config['make_link'] = make_link
config['start_enum'] = None
if 'enum' in node:
config['start_enum'] = node['enum']
attr_search_string = '{0}_ATTR_([A-Z0-9_]+)'.format(config['module_name'].upper())
func_search_string = '{0}_([A-Za-z0-9_]+)'.format(config['c_function_prefix'].replace('_', ''))
func_search_string_lower = '{0}_([A-Za-z0-9_]+)'.format(config['c_function_prefix'].lower().replace('_', ''))
enum_search_string = '{0}_VAL_([A-Z0-9_]+)'.format(config['module_name'].upper())
attr_re = re.compile(attr_search_string)
func_re = re.compile(func_search_string)
func_lower_re = re.compile(func_search_string_lower)
enum_re = re.compile(enum_search_string)
doc = attr_re.sub(_replace_attribute_python_name, doc)
doc = func_re.sub(_replace_func_python_name, doc)
doc = func_lower_re.sub(_replace_func_python_name, doc)
doc = enum_re.sub(_replace_enum_python_name, doc)
if 'driver_urls' in cfg:
for url_key in cfg['driver_urls']:
url_re = re.compile(r'{0}\((.+?)\)'.format(url_key))
config['url_key'] = url_key
doc = url_re.sub(_replace_urls, doc)
# Clean up config
del config['make_link']
del config['start_enum']
# Several other standard replacements
doc = re.sub(r'\bVI_FALSE\b', 'False', doc)
doc = re.sub(r'\bVI_TRUE\b', 'True', doc)
doc = re.sub(r'\ban attribute\b', 'a property', doc)
doc = re.sub(r'\bAn attribute\b', 'A property', doc)
doc = re.sub(r'\bAn Attribute\b', 'A Property', doc)
doc = re.sub(r'\battribute\b', 'property', doc)
doc = re.sub(r'\battributes\b', 'properties', doc)
doc = re.sub(r'\bAttribute\b', 'Property', doc)
doc = re.sub(r'\bAttributes\b', 'Properties', doc)
doc = re.sub(r'\bfunction\b', 'method', doc)
doc = re.sub(r'\bfunctions\b', 'methods', doc)
doc = re.sub(r'\bFunction\b', 'Method', doc)
doc = re.sub(r'\bFunctions\b', 'Methods', doc)
return doc
def format_type_for_rst_documentation(param, numpy, config):
if numpy and param['numpy']:
p_type = param['numpy_type']
elif param['enum'] is not None:
p_type = ':py:data:`{0}.{1}`'.format(config['module_name'], param['enum'])
else:
p_type = param['type_in_documentation']
if param['is_string'] is True:
p_type = 'str'
elif param['is_buffer'] is True and numpy is True:
p_type = 'numpy.array(dtype=numpy.{0})'.format(get_numpy_type_for_api_type(param['type'], config))
elif param['use_list'] is True:
p_type = 'list of ' + p_type
elif param['use_array'] is True:
p_type = 'array.array("{0}")'.format(get_array_type_for_api_type(param['type']))
return p_type
def get_function_rst(function, method_template, numpy, config, indent=0, method_or_function='method'):
'''Gets formatted documentation for given function or method that can be used in rst documentation
Args:
function (dict): function dictionary
config (dict): configuration dictoionary (from metadata)
method_template (dict): entry from function['methos_temlates'] that corresponds to specific entry we are processon
numpy (boolean): Is the entry we are processing a numpy based method
indent (int): default 0 - initial indentation
Returns:
str: rst formatted documentation
'''
suffix = method_template['method_python_name_suffix']
session_method = ParameterUsageOptions.DOCUMENTATION_SESSION_METHOD
session_declaration = ParameterUsageOptions.SESSION_METHOD_DECLARATION
output_parameters = ParameterUsageOptions.OUTPUT_PARAMETERS_FOR_DOCS
if numpy:
session_declaration = ParameterUsageOptions.SESSION_NUMPY_INTO_METHOD_DECLARATION
if function['has_repeated_capability'] is True:
function['documentation']['tip'] = rep_cap_method_desc_rst.format(config['module_name'], function['repeated_capability_type'], function['python_name'], get_params_snippet(function, session_method))
rst = '.. py:{0}:: {1}{2}('.format(method_or_function, function['python_name'], suffix)
rst += get_params_snippet(function, session_method) + ')'
indent += 4
rst += get_documentation_for_node_rst(function, config, indent)
input_params = filter_parameters(function, session_declaration)
if len(input_params) > 0:
rst += '\n'
for p in input_params:
rst += '\n' + (' ' * indent) + ':param {0}:'.format(p['python_name']) + '\n'
rst += get_documentation_for_node_rst(p, config, indent + 4)
p_type = format_type_for_rst_documentation(p, numpy, config)
rst += '\n' + (' ' * indent) + ':type {0}: '.format(p['python_name']) + p_type
output_params = filter_parameters(function, output_parameters)
if len(output_params) > 1:
rst += '\n\n' + (' ' * indent) + ':rtype: tuple (' + ', '.join([p['python_name'] for p in output_params]) + ')\n\n'
rst += (' ' * (indent + 4)) + 'WHERE\n'
for p in output_params:
p_type = format_type_for_rst_documentation(p, numpy, config)
rst += '\n' + (' ' * (indent + 4)) + '{0} ({1}): '.format(p['python_name'], p_type) + '\n'
rst += get_documentation_for_node_rst(p, config, indent + 8)
elif len(output_params) == 1:
p = output_params[0]
p_type = format_type_for_rst_documentation(p, numpy, config)
rst += '\n\n' + (' ' * indent) + ':rtype: ' + p_type + '\n'
rst += (' ' * indent) + ':return:\n' + get_documentation_for_node_rst(p, config, indent + 8)
return rst
def _format_type_for_docstring(param, numpy, config):
if numpy and param['numpy']:
p_type = param['numpy_type']
else:
p_type = param['type_in_documentation']
# We assume everything that is a buffer of ViChar is really a string (otherwise
# it would end up as 'list of int'
if param['is_string'] is True:
p_type = 'str'
elif param['is_buffer'] is True and numpy is True:
p_type = 'numpy.array(dtype=numpy.{0})'.format(get_numpy_type_for_api_type(param['type'], config))
elif param['use_list'] is True:
p_type = 'list of ' + p_type
elif param['use_array'] is True:
p_type = 'array.array("{0}")'.format(get_array_type_for_api_type(param['type']))
return p_type
def get_function_docstring(function, numpy, config, indent=0):
'''Gets formatted documentation for given function that can be used as a docstring
Args:
function (dict): function dictionary
config (dict): configuration dictoionary (from metadata)
numpy (boolean): Is the entry we are processing a numpy based method
indent (int): default 0 - initial indentation
Returns:
str: docstring formatted documentation
'''
session_method = ParameterUsageOptions.DOCUMENTATION_SESSION_METHOD
session_declaration = ParameterUsageOptions.SESSION_METHOD_DECLARATION
output_parameters = ParameterUsageOptions.OUTPUT_PARAMETERS_FOR_DOCS
if numpy:
session_declaration = ParameterUsageOptions.SESSION_NUMPY_INTO_METHOD_DECLARATION
docstring = ''
if function['has_repeated_capability'] is True:
function['documentation']['tip'] = rep_cap_method_desc_docstring.format(config['module_name'], function['repeated_capability_type'], function['python_name'], get_params_snippet(function, session_method))
docstring += get_documentation_for_node_docstring(function, config, indent)
input_params = filter_parameters(function, session_declaration)
if len(input_params) > 0:
docstring += '\n\n' + (' ' * indent) + 'Args:'
for p in input_params:
docstring += '\n' + (' ' * (indent + 4)) + '{0} ({1}):'.format(p['python_name'], _format_type_for_docstring(p, numpy, config))
ds = get_documentation_for_node_docstring(p, config, indent + 8)
if len(ds) > 0:
docstring += ' ' + ds
docstring += '\n'
output_params = filter_parameters(function, output_parameters)
if len(output_params) > 0:
docstring += '\n\n' + (' ' * indent) + 'Returns:'
for p in output_params:
docstring += '\n' + (' ' * (indent + 4)) + '{0} ({1}):'.format(p['python_name'], _format_type_for_docstring(p, numpy, config))
ds = get_documentation_for_node_docstring(p, config, indent + 8)
if len(ds) > 0:
docstring += ' ' + ds
docstring += '\n'
return docstring
# From http://code.activestate.com/recipes/579054-generate-sphinx-table/
def as_rest_table(data, header=True):
"""Create rst formatted table
>>> data = [('what', 'how', 'who'),
... ('lorem', 'that is a long value', 3.1415),
... ('ipsum', 89798, 0.2)]
>>> print(as_rest_table(data))
+-------+----------------------+--------+
| what | how | who |
+=======+======================+========+
| lorem | that is a long value | 3.1415 |
+-------+----------------------+--------+
| ipsum | 89798 | 0.2 |
+-------+----------------------+--------+
>>> print(as_rest_table(data, header=False))
+-------+----------------------+--------+
| what | how | who |
+-------+----------------------+--------+
| lorem | that is a long value | 3.1415 |
+-------+----------------------+--------+
| ipsum | 89798 | 0.2 |
+-------+----------------------+--------+
"""
data = data if data else [['No Data']]
table = []
# max size of each column
sizes = map(max, zip(*[[len(str(elt)) for elt in member] for member in data]))
if sys.version_info.major >= 3:
sizes = list(sizes)
num_elts = len(sizes)
start_of_line = '| '
vertical_separator = ' | '
end_of_line = ' |'
line_marker = '-'
meta_template = vertical_separator.join(['{{{{{0}:{{{0}}}}}}}'.format(i) for i in range(num_elts)])
template = '{0}{1}{2}'.format(start_of_line, meta_template.format(*sizes), end_of_line)
# determine top/bottom borders
to_separator = {ord('|'): '+', ord(' '): '-'}
if sys.version_info.major < 3:
to_separator = string.maketrans('| ', '+-')
start_of_line = start_of_line.translate(to_separator)
vertical_separator = vertical_separator.translate(to_separator)
end_of_line = end_of_line.translate(to_separator)
separator = '{0}{1}{2}'.format(start_of_line, vertical_separator.join([x * line_marker for x in sizes]), end_of_line)
# determine header separator
th_separator_tr = {ord('-'): '='}
if sys.version_info.major < 3:
th_separator_tr = string.maketrans('-', '=')
start_of_line = start_of_line.translate(th_separator_tr)
line_marker = line_marker.translate(th_separator_tr)
vertical_separator = vertical_separator.translate(th_separator_tr)
end_of_line = end_of_line.translate(th_separator_tr)
th_separator = '{0}{1}{2}'.format(start_of_line, vertical_separator.join([x * line_marker for x in sizes]), end_of_line)
# prepare result
table.append(separator)
# set table header
titles = data[0]
table.append(template.format(*titles))
if header:
table.append(th_separator)
else:
table.append(separator)
for d in data[1:-1]:
table.append(template.format(*d))
table.append(separator)
table.append(template.format(*data[-1]))
table.append(separator)
return '\n'.join(table)
def _square_up_table(nd):
'''_square_up_table
The function we use to generate rst tables requires the table be a rectangle. I.e. all
rows must have the same number of cells. This will check 'table_header' and 'table_body'
to get the longest row and then make sure they are all that length
'''
if 'table_header' not in nd and 'table_body' not in nd:
return # We don't need to do anything
# First we get max length
max_len = 0
table_header = nd['table_header'] if 'table_header' in nd else None
table_body = nd['table_body']
if table_header:
max_len = len(table_header)
for line in table_body:
if len(line) > max_len:
max_len = len(line)
# Now make sure all lines have the same number of cells
if table_header:
while len(table_header) != max_len:
table_header.append('')
for line in table_body:
while len(line) != max_len:
line.append('')
def square_up_tables(config):
'''Go through all documentation and make sure tables rows have consistent lengths'''
# First we go through the function and parameter documentation
for f_name in config['functions']:
f = config['functions'][f_name]
for p in f['parameters']:
if 'documentation' not in p:
continue
_square_up_table(p['documentation'])
if 'documentation' not in f:
continue
_square_up_table(f['documentation'])
# Check attribute documentation
for a_id in config['attributes']:
a = config['attributes'][a_id]
if 'documentation' not in a:
continue
_square_up_table(a['documentation'])
# Check enum documentation
for e_name in config['enums']:
e = config['enums'][e_name]
if 'documentation' not in e:
continue
_square_up_table(e['documentation'])
for v in e['values']:
if 'documentation' not in v:
continue
_square_up_table(v['documentation'])
def _need_func_note(nd, config):
'''Determine if we need the extra note about function names not matching anything in Python'''
func_re = re.compile('{0}_([A-Za-z0-9_]+)'.format(config['c_function_prefix'].replace('_', '')))
for m in func_re.finditer(nd):
fname = m.group(1).replace('.', '').replace(',', '').replace('\\', '')
try:
config['functions'][fname]['python_name']
except KeyError:
return True
return False
def _need_attr_note(nd, config):
'''Determine if we need the extra note about attribute names not matching anything in Python'''
attr_re = re.compile('{0}_ATTR_([A-Z0-9_]+)'.format(config['module_name'].upper()))
for m in attr_re.finditer(nd):
aname = m.group(1).replace('\\', '')
attr = find_attribute_by_name(config['attributes'], aname)
if not attr:
return True
return False
def _need_enum_note(nd, config, start_enum=None):
'''Determine if we need the extra note about enum names not matching anything in Python'''
enum_re = re.compile('{0}_VAL_([A-Z0-9_]+)'.format(config['module_name'].upper()))
for m in enum_re.finditer(nd):
ename = '{0}_VAL_{1}'.format(config['module_name'].upper(), m.group(1).replace('\\', ''))
enum, _ = find_enum_by_value(config['enums'], ename, start_enum=start_enum)
if not enum or enum['codegen_method'] == 'no':
return True
return False
def _check_documentation(nd, config, start_enum=None):
'''_check_documentation
Look through all the different documentation pieces for this node documentation object for
any references to functions, attributes or enums that will not exist in the Python API for
whatever reason. If we find something, we will add a note admonition stating that.
Args:
nd (dict) - documentation dictionary - expected to follow standard layout we have been using
config (dict) - configuration information'
start_enum (book) - possible context - used for finding enums based on the value
'''
keys_to_check = ['description', 'tip', 'caution', 'note'] # table_body needs special handling
need_func_note = False
need_attr_note = False
need_enum_note = False
for k in keys_to_check:
if k in nd:
try:
need_func_note = need_func_note or _need_func_note(nd[k], config)
need_attr_note = need_attr_note or _need_attr_note(nd[k], config)
need_enum_note = need_enum_note or _need_enum_note(nd[k], config)
except TypeError:
# If we get a type error then we will assume it is an iterable (list)
for n in nd[k]:
need_func_note = need_func_note or _need_func_note(n, config)
need_attr_note = need_attr_note or _need_attr_note(n, config)
need_enum_note = need_enum_note or _need_enum_note(n, config)
if 'table_body' in nd:
tb = nd['table_body']
for line in tb:
for cell in line:
need_func_note = need_func_note or _need_func_note(cell, config)
need_attr_note = need_attr_note or _need_attr_note(cell, config)
need_enum_note = need_enum_note or _need_enum_note(cell, config)
if need_func_note or need_attr_note or need_enum_note:
if 'note' not in nd:
nd['note'] = []
elif not isinstance(nd['note'], list):
nd['note'] = [nd['note']]
if need_func_note:
nd['note'].append(func_note_text)
if need_attr_note:
nd['note'].append(attr_note_text)
if need_enum_note:
nd['note'].append(enum_note_text)
def add_notes_re_links(config):
'''_add_notes_re_links
Go through all documentation looking for names that won't exist in the Python API and
adding a note about it.
'''
# First we go through the function and parameter documentation
for f_name in config['functions']:
f = config['functions'][f_name]
for p in f['parameters']:
start_enum = None
if 'documentation' not in p:
continue
if 'enum' in p:
start_enum = p['enum']
_check_documentation(p['documentation'], config, start_enum)
if 'documentation' not in f:
continue
start_enum = None
if 'enum' in f:
start_enum = f['enum']
_check_documentation(f['documentation'], config, start_enum)
# Check attribute documentation
for a_id in config['attributes']:
a = config['attributes'][a_id]
if 'documentation' not in a:
continue
start_enum = None
if 'enum' in a:
start_enum = a['enum']
_check_documentation(a['documentation'], config, start_enum)
# Check enum documentation
for e_name in config['enums']:
e = config['enums'][e_name]
if 'documentation' not in e:
continue
_check_documentation(e['documentation'], config)
for v in e['values']:
if 'documentation' not in v:
continue
_check_documentation(v['documentation'], config)
# Unit Tests
def _remove_trailing_whitespace(s):
'''Removes trailing whitespace and empty lines in multi-line strings.'''
initial_lines = s.strip().splitlines()
fixed_lines = []
blank_lines = 0
for l in initial_lines:
stripped_line = l.strip()
if len(stripped_line) == 0 and blank_lines == 0:
fixed_lines.append(stripped_line)
blank_lines = 1
if len(stripped_line) > 0:
fixed_lines.append(stripped_line)
blank_lines = 0
return fixed_lines
def assert_rst_strings_are_equal(expected, actual):
'''Asserts rst formatted strings (multiline) are equal. Ignores trailing whitespace and empty lines.'''
expected = _remove_trailing_whitespace(expected)
actual = _remove_trailing_whitespace(actual)
for expected_line, actual_line in zip(expected, actual):
assert expected_line == actual_line, 'Difference found:\n{0}\n{1}'.format(expected_line, actual_line)
config = {
'functions': {
'GetTurtleID': {
'codegen_method': 'public',
'returns': 'ViStatus',
'method_templates': [{'filename': '/default_method', 'method_python_name_suffix': '', }, ],
'parameters': [
{
'direction': 'in',
'enum': None,
'name': 'vi',
'type': 'ViSession',
'documentation': {
'description': 'Identifies a particular instrument session.'
},
'python_name': 'vi',
'python_type': 'int',
'type_in_documentation': 'int',
'ctypes_variable_name': 'vi_ctype',
'ctypes_type': 'ViSession',
'ctypes_type_library_call': 'ViSession',
'size': {
'mechanism': 'fixed',
'value': 1
},
'is_buffer': False,
'is_string': False,
'use_list': False,
'use_array': False,
'python_name_with_default': 'vi',
'python_name_with_doc_default': 'vi',
'is_repeated_capability': False,
'is_session_handle': True,
'library_method_call_snippet': 'self._vi',
'use_in_python_api': True,
},
{
'direction': 'in',
'enum': 'Turtle',
'name': 'turtleType',
'type': 'ViInt32',
'documentation': {
'description': '''Specifies the type of Turtle type
wanted to choose.''',
'note': 'You wont be able to import NIFAKE_VAL_RAPHAEL',
'table_body': [
['NIFAKE_VAL_LEONARDO (default)', '0', 'LEONARDO'],
['NIFAKE_VAL_DONATELLO', '1', 'DONATELLO'],
['NIFAKE_VAL_RAPHAEL', '2', 'RAPHAEL'],
['NIFAKE_VAL_MICHELANGELO', '3', 'MICHELANGELO']
]
},
'python_name': 'turtle_type',
'python_type': 'Turtle',
'type_in_documentation': 'Turtle',
'ctypes_variable_name': 'turtle_type_ctype',
'ctypes_type': 'ViInt32',
'ctypes_type_library_call': 'ViInt32',
'size': {
'mechanism': 'fixed',
'value': 1
},
'is_buffer': False,
'is_string': False,
'use_list': False,
'use_array': False,
'python_name_with_default': 'turtle_type',
'python_name_with_doc_default': 'turtle_type',
'is_repeated_capability': False,
'is_session_handle': False,
'library_method_call_snippet': 'turtle_type',
'use_in_python_api': True,
},
{
'direction': 'out',
'enum': None,
'name': 'turtleId',
'type': 'ViReal64',
'documentation': {