-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathhelper.py
1945 lines (1708 loc) · 64.4 KB
/
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
"""
Copyright (C) 2019-2021 Intel Corporation
SPDX-License-Identifier: MIT
"""
import re
"""
Extracts traits from a spec object
"""
class obj_traits:
@staticmethod
def is_function(obj):
try:
return True if re.match(r"function", obj['type']) else False
except:
return False
@staticmethod
def is_function_with_input_handles(obj):
try:
if re.match(r"function", obj['type']):
for param in obj['params']:
if param_traits.is_input(param) and type_traits.is_handle(param['type']):
return True
return False
except:
return False
@staticmethod
def is_class(obj):
try:
return True if re.match(r"class", obj['type']) else False
except:
return False
@staticmethod
def is_experimental(obj):
try:
return True if re.search("Exp[0-9]*$", obj['name']) else False
except:
return False
@staticmethod
def class_name(obj):
try:
return obj['class']
except:
return None
"""
Extracts traits from a class name
"""
class class_traits:
@staticmethod
def is_global(name, tags):
try:
return True if name in tags else False
except:
return False
@staticmethod
def is_namespace(name, namespace, tags):
try:
return tags[name] == namespace
except:
return False
@staticmethod
def is_singleton(item):
try:
return "singleton" == item['attribute']
except:
return False
@staticmethod
def get_handle(item, meta):
try:
return meta['class'][item['name']]['handle'][0]
except:
return ""
"""
Extracts traits from a type name
"""
class type_traits:
RE_HANDLE = r"(.*)handle_t"
RE_IPC = r"(.*)ipc(.*)handle_t"
RE_POINTER = r"(.*\w+)\*+"
RE_DESC = r"(.*)desc_t.*"
RE_PROPS = r"(.*)properties_t.*"
RE_FLAGS = r"(.*)flags_t"
RE_COUNTERS = r"(.*)counters_t"
RE_PORT_CONFIG = r"(.*)port_config_t"
RE_FAN_CONFIG = r"(.*)fan_config_t"
RE_RAS_CONFIG = r"(.*)ras_config_t"
RE_TEMP_CONFIG = r"(.*)temp_config_t"
RE_DEVICE_STATE = r"(.*)device_state_t"
RE_PROCESS_STATE = r"(.*)process_state_t"
RE_PCI_STATE = r"(.*)pci_state_t"
RE_FABRIC_PORT_STATE = r"(.*)fabric_port_state_t"
RE_FREQ_STATE = r"(.*)freq_state_t"
RE_LED_STATE = r"(.*)led_state_t"
RE_MEM_STATE = r"(.*)mem_state_t"
RE_PSU_STATE = r"(.*)psu_state_t"
RE_RAS_STATE = r"(.*)ras_state_t"
RE_CAPABILITIES = r"(.*)capabilities_t"
RE_PROPS_EXP = r"(.*)properties_exp_t"
@staticmethod
def base(name):
return _remove_const_ptr(name)
@classmethod
def is_handle(cls, name):
try:
return True if re.match(cls.RE_HANDLE, name) else False
except:
return False
@classmethod
def is_ipc_handle(cls, name):
try:
return True if re.match(cls.RE_IPC, name) else False
except:
return False
@staticmethod
def is_class_handle(name, meta):
try:
name = _remove_const_ptr(name)
return len(meta['handle'][name]['class']) > 0
except:
return False
@classmethod
def is_pointer(cls, name):
try:
return True if re.match(cls.RE_POINTER, name) else False
except:
return False
@classmethod
def is_descriptor(cls, name):
try:
return True if re.match(cls.RE_DESC, name) else False
except:
return False
@classmethod
def is_properties(cls, name):
try:
return True if re.match(cls.RE_PROPS, name) else False
except:
return False
@classmethod
def is_counters(cls, name):
try:
return True if re.match(cls.RE_COUNTERS, name) else False
except:
return False
@classmethod
def is_port_config(cls, name):
try:
return True if re.match(cls.RE_PORT_CONFIG, name) else False
except:
return False
@classmethod
def is_fan_config(cls, name):
try:
return True if re.match(cls.RE_FAN_CONFIG, name) else False
except:
return False
@classmethod
def is_ras_config(cls, name):
try:
return True if re.match(cls.RE_RAS_CONFIG, name) else False
except:
return False
@classmethod
def is_temp_config(cls, name):
try:
return True if re.match(cls.RE_TEMP_CONFIG, name) else False
except:
return False
@classmethod
def is_device_state(cls, name):
try:
return True if re.match(cls.RE_DEVICE_STATE, name) else False
except:
return False
@classmethod
def is_process_state(cls, name):
try:
return True if re.match(cls.RE_PROCESS_STATE, name) else False
except:
return False
@classmethod
def is_pci_state(cls, name):
try:
return True if re.match(cls.RE_PCI_STATE, name) else False
except:
return False
@classmethod
def is_fabric_port_state(cls, name):
try:
return True if re.match(cls.RE_FABRIC_PORT_STATE, name) else False
except:
return False
@classmethod
def is_freq_state(cls, name):
try:
return True if re.match(cls.RE_FREQ_STATE, name) else False
except:
return False
@classmethod
def is_led_state(cls, name):
try:
return True if re.match(cls.RE_LED_STATE, name) else False
except:
return False
@classmethod
def is_mem_state(cls, name):
try:
return True if re.match(cls.RE_MEM_STATE, name) else False
except:
return False
@classmethod
def is_psu_state(cls, name):
try:
return True if re.match(cls.RE_PSU_STATE, name) else False
except:
return False
@classmethod
def is_ras_state(cls, name):
try:
return True if re.match(cls.RE_RAS_STATE, name) else False
except:
return False
@classmethod
def is_properties_exp(cls, name):
try:
return True if re.match(cls.RE_PROPS_EXP, name) else False
except:
return False
@classmethod
def is_capabilities(cls, name):
try:
return True if re.match(cls.RE_CAPABILITIES, name) else False
except:
return False
@classmethod
def is_flags(cls, name):
try:
return True if re.match(cls.RE_FLAGS, name) else False
except:
return False
@staticmethod
def is_known(name, meta):
try:
name = _remove_const_ptr(name)
for group in meta:
if name in meta[group]:
return True
return False
except:
return False
@staticmethod
def is_enum(name, meta):
try:
name = _remove_const_ptr(name)
if name in meta['enum']:
return True
return False
except:
return False
@staticmethod
def is_struct(name, meta):
try:
name = _remove_const_ptr(name)
if name in meta['struct']:
return True
return False
except:
return False
@staticmethod
def find_class_name(name, meta):
try:
name = _remove_const_ptr(name)
for group in meta:
if name in meta[group]:
return meta[group][name]['class']
return None
except:
return None
"""
Extracts traits from a value name
"""
class value_traits:
RE_VERSION = r"\$X_MAKE_VERSION\(\s*(\d+)\s*\,\s*(\d+)\s*\)"
RE_BIT = r".*BIT\(\s*(.*)\s*\)"
RE_HEX = r"0x\w+"
RE_MACRO = r"(\$\w+)\(.*\)"
RE_ARRAY = r"(.*)\[(.*)\]"
@classmethod
def is_ver(cls, name):
try:
return True if re.match(cls.RE_VERSION, name) else False
except:
return False
@classmethod
def get_major_ver(cls, name):
try:
return int(re.sub(cls.RE_VERSION, r"\1", name))
except:
return 0
@classmethod
def get_minor_ver(cls, name):
try:
return int(re.sub(cls.RE_VERSION, r"\2", name))
except:
return 0
@classmethod
def is_bit(cls, name):
try:
return True if re.match(cls.RE_BIT, name) else False
except:
return False
@classmethod
def get_bit_count(cls, name):
try:
return int(re.sub(cls.RE_BIT, r"\1", name))
except:
return 0
@classmethod
def is_hex(cls, name):
try:
return True if re.match(cls.RE_HEX, name) else False
except:
return False
@classmethod
def is_macro(cls, name, meta):
try:
name = cls.get_macro_name(name)
name = cls.get_array_length(name)
return True if name in meta['macro'] else False
except:
return False
@classmethod
def get_macro_name(cls, name):
try:
return re.sub(cls.RE_MACRO, r"\1", name) # 'NAME()' -> 'NAME'
except:
return name
@classmethod
def is_array(cls, name):
try:
return True if re.match(cls.RE_ARRAY, name) else False
except:
return False
@classmethod
def get_array_name(cls, name):
try:
return re.sub(cls.RE_ARRAY, r"\1", name) # 'name[len]' -> 'name'
except:
return name
@classmethod
def get_array_length(cls, name):
try:
return re.sub(cls.RE_ARRAY, r"\2", name) # 'name[len]' -> 'len'
except:
return name
@classmethod
def find_enum_name(cls, name, meta):
try:
name = cls.get_array_name(name)
# if the value is an etor, return the name of the enum
for e in meta['enum']:
if name in meta['enum'][e]['etors']:
return e
return None
except:
return None
"""
Extracts traits from a parameter object
"""
class param_traits:
RE_MBZ = r".*\[mbz\].*"
RE_IN = r"^\[in\].*"
RE_OUT = r"^\[out\].*"
RE_INOUT = r"^\[in,out\].*"
RE_OPTIONAL = r".*\[optional\].*"
RE_RANGE = r".*\[range\((.+),\s*(.+)\)\][\S\s]*"
RE_RELEASE = r".*\[release\].*"
@classmethod
def is_mbz(cls, item):
try:
return True if re.match(cls.RE_MBZ, item['desc']) else False
except:
return False
@classmethod
def is_input(cls, item):
try:
return True if re.match(cls.RE_IN, item['desc']) else False
except:
return False
@classmethod
def is_output(cls, item):
try:
return True if re.match(cls.RE_OUT, item['desc']) else False
except:
return False
@classmethod
def is_inoutput(cls, item):
try:
return True if re.match(cls.RE_INOUT, item['desc']) else False
except:
return False
@classmethod
def is_optional(cls, item):
try:
return True if re.match(cls.RE_OPTIONAL, item['desc']) else False
except:
return False
@classmethod
def is_range(cls, item):
try:
return True if re.match(cls.RE_RANGE, item['desc']) else False
except:
return False
@classmethod
def range_start(cls, item):
try:
return re.sub(cls.RE_RANGE, r"\1", item['desc'])
except:
return None
@classmethod
def range_end(cls, item):
try:
return re.sub(cls.RE_RANGE, r"\2", item['desc'])
except:
return None
@classmethod
def is_release(cls, item):
try:
return True if re.match(cls.RE_RELEASE, item['desc']) else False
except:
return False
"""
Extracts traits from a function object
"""
class function_traits:
@staticmethod
def is_static(item):
try:
return True if re.match(r"static", item['decl']) else False
except:
return False
@staticmethod
def is_global(item, tags):
try:
return True if item['class'] in tags else False
except:
return False
"""
Public:
substitues each tag['key'] with tag['value']
if cpp, then remove each tag['key'] if matches namespace
if comment, then insert doxygen '::' notation at beginning (for autogen links)
"""
def subt(namespace, tags, string, comment=False, cpp=False, remove_namespace=False):
for key, value in tags.items():
if comment or not cpp: # generating c names
string = re.sub(r"-%s"%re.escape(key), "-"+value, string) # hack for compile options
repl = "::"+value if comment and "$OneApi" != key else value # replace tag; e.g., "$x" -> "xe"
string = re.sub(re.escape(key), repl, string)
string = re.sub(re.escape(key.upper()), repl.upper(), string)
elif re.match(namespace, value): # generating c++ names and tag matches current namespace
repl = "" # remove tags; e.g., "$x" -> ""
string = re.sub(r"%s_?"%re.escape(key), repl, string)
string = re.sub(r"%s_?"%re.escape(key.upper()), repl.upper(), string)
elif remove_namespace: # generating c++ names and tags do _not_ match current namespace
repl = "" # remove namespace; e.g. "$x" -> ""
string = re.sub(r"%s_?"%re.escape(key), repl, string)
string = re.sub(r"%s_?"%re.escape(key.upper()), repl.upper(), string)
else: # generating c++ names and tags do _not_ match current namespace
repl = value+"::" # add namespace; e.g. "$x" -> "xe::"
string = re.sub(r"%s_?"%re.escape(key), repl, string)
string = re.sub(r"%s_?"%re.escape(key.upper()), repl.upper(), string)
return string
"""
Public:
appends whitespace (in multiples of 4) to the end of the string,
until len(string) > count
"""
def append_ws(string, count):
while len(string) > count:
count = count + 4
string = '{str: <{width}}'.format(str=string, width=count)
return string
"""
Public:
split the line of text into a list of strings,
where each length of each entry is less-than count
"""
def split_line(line, ch_count):
if not line:
return [""]
RE_NEWLINE = r"(.*)\n(.*)"
words = line.split(" ")
lines = []
word_list = []
for word in words:
if re.match(RE_NEWLINE, word):
prologue = re.sub(RE_NEWLINE,r"\1",word)
epilogue = re.sub(RE_NEWLINE,r"\2",word)
word_list.append(prologue)
lines.append(" ".join(word_list))
word_list = []
if len(epilogue):
word_list.append(epilogue)
elif sum(map(len, word_list)) + len(word_list) + len(word) <= ch_count:
word_list.append(word)
else:
lines.append(" ".join(word_list))
word_list = [word]
if len(word_list):
lines.append(" ".join(word_list))
return lines
"""
Private:
converts string from camelCase to snake_case
"""
def _camel_to_snake(name):
str = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
str = re.sub('([a-z0-9])([A-Z])', r'\1_\2', str).lower()
return str
"""
Public:
removes items from the list with the key and whose value do not match filter
"""
def filter_items(lst, key, filter):
flst = []
for item in lst:
if key in item:
if filter == item[key]:
flst.append(item)
return flst
"""
Public:
returns a list of items with key from a list of dict
"""
def extract_items(lst, key):
klst = []
for item in lst:
if key in item:
klst.append(item[key])
return klst
"""
Public:
returns a list of all objects of type in all specs
"""
def extract_objs(specs, value):
objs = []
for s in specs:
for obj in s['objects']:
if re.match(value, obj['type']):
objs.append(obj)
return objs
"""
Private:
removes 'const' from c++ type
"""
def _remove_const(name):
name = name.split(" ")[-1]
return name
"""
Private:
removes '*' from c++ type
"""
def _remove_ptr(name, last=True):
if last:
name = re.sub(r"(.*)\*$", r"\1", name) # removes only last '*'
else:
name = re.sub(r"\*", "", name) # removes all '*'
return name
"""
Private:
removes 'const' and '*' from c++ type
"""
def _remove_const_ptr(name):
name = _remove_ptr(_remove_const(name))
return name
"""
Private:
adds class name to type
e.g., "const type*" -> "const cname::type*"
"""
def _add_class(name, cname):
words = name.split(" ")
words[-1] = "%s::%s"%(cname, words[-1])
return " ".join(words)
"""
Private:
removes class name from type
e.g., "const cls_type*" -> "const type*"
"""
def _remove_class(name, cname, upper_case=False):
if cname:
cname = _camel_to_snake(cname)
if upper_case:
cname = cname.upper()
RE_CLS = r"(.*)(%s_)(\w+)"%cname # remove "cls_" part
if re.match(RE_CLS, name):
name = re.sub(RE_CLS, r"\1\3", name)
return name
"""
Public:
returns c/c++ name of macro
"""
def make_macro_name(namespace, tags, obj, params=True):
if params:
return subt(namespace, tags, obj['name'])
else:
name = re.sub(r"(.*)\(.*", r"\1", obj['name']) # remove '()' part
return subt(namespace, tags, name)
"""
Public:
returns c/c++ name of enums, structs, unions, typedefs...
"""
def make_type_name(namespace, tags, obj, cpp=False):
name = subt(namespace, tags, obj['name'], cpp=cpp)
# if c++, remove class part of name
if cpp and 'class' in obj:
cname = subt(namespace, tags, obj['class'], cpp=cpp)
name = _remove_class(name, cname)
return name
"""
Public:
returns c/c++ name of enums...
"""
def make_enum_name(namespace, tags, obj, cpp=False):
name = make_type_name(namespace, tags, obj, cpp)
if type_traits.is_flags(obj['name']):
name = re.sub(r"flags", r"flag", name)
return name
"""
Public:
returns c/c++ name of etor
"""
def make_etor_name(namespace, tags, enum, etor, cpp=False, py=False, meta=None):
if cpp or py:
# if c++, remove the verbose enum part of the etor
if type_traits.is_flags(enum) and not py:
# e.g., "CLS_ENUM_NAME_ETOR_NAME" -> "ENUM_NAME_ETOR_NAME"
cname = type_traits.find_class_name(enum, meta)
cname = subt(namespace, tags, cname, cpp=cpp)
name = subt(namespace, tags, etor, cpp=cpp)
name = _remove_class(name, cname, upper_case=True)
else:
# e.g., "ENUM_NAME_ETOR_NAME" -> "ETOR_NAME"
if type_traits.is_flags(enum):
prefix = re.sub(r"(\w+)_flags_t", r"\1_flag", subt(namespace, tags, enum, cpp=cpp)).upper()
else:
prefix = re.sub(r"(\w+)_t", r"\1", subt(namespace, tags, enum, cpp=cpp)).upper()
name = re.sub(r"%s_(\w+)"%prefix, r"\1", subt(namespace, tags, etor, cpp=cpp))
name = re.sub(r"^(\d+\w*)", r"_\1", name)
else:
name = subt(namespace, tags, etor, cpp=cpp)
return name
"""
Private:
returns c/c++ name of value
"""
def _get_value_name(namespace, tags, value, cpp, meta, is_array_size=False, cbase=None):
if cpp:
if value_traits.is_macro(value, meta):
value = subt(namespace, tags, value)
else:
name = value_traits.find_enum_name(value, meta)
if name:
# e.g., "ETOR_NAME" -> "ENUM_NAME::ETOR_NAME"
enum = subt(namespace, tags, name, cpp=cpp)
# e.g., "CLS_ENUM_NAME" -> "ENUM_NAME"
cname = type_traits.find_class_name(name, meta)
cname = subt(namespace, tags, cname, cpp=cpp)
enum = _remove_class(enum, cname)
if cname and cbase:
cbase = subt(namespace, tags, cbase, cpp=cpp)
if cbase == cname:
enum = _remove_class(enum, cname)
else:
enum = "%s::%s"%(cname, enum)
if is_array_size:
value = "static_cast<int>(%s::%s)"%(enum, make_etor_name(namespace, tags, name, value, cpp=cpp, meta=meta))
else:
value = "%s::%s"%(enum, make_etor_name(namespace, tags, name, value, cpp=cpp, meta=meta))
else:
value = subt(namespace, tags, value, cpp=cpp)
else:
value = subt(namespace, tags, value, cpp=cpp)
return value
"""
Public:
returns a list of strings for declaring each enumerator in an enumeration
c++ format: "ETOR_NAME = VALUE, ///< DESCRIPTION"
python format: "ETOR_NAME = VALUE, ## DESCRIPTION"
"""
def make_etor_lines(namespace, tags, obj, cpp=False, py=False, meta=None):
lines = []
for item in obj['etors']:
name = make_etor_name(namespace, tags, obj['name'], item['name'], cpp, py, meta)
if 'value' in item:
delim = "," if not py else ""
value = _get_value_name(namespace, tags, item['value'], cpp, meta, cbase=obj_traits.class_name(obj))
prologue = "%s = %s%s"%(name, value, delim)
elif py:
prologue = "%s = auto()"%(name)
else:
prologue = "%s,"%(name)
comment_style = "##" if py else "///<"
for line in split_line(subt(namespace, tags, item['desc'], True, cpp), 70):
lines.append("%s%s %s"%(append_ws(prologue, 48), comment_style, line))
prologue = ""
if cpp and not type_traits.is_flags(obj['name']):
lines.append("FORCE_UINT32 = 0x7fffffff")
elif not py:
lines.append("%sFORCE_UINT32 = 0x7fffffff"%make_enum_name(namespace, tags, obj, cpp)[:-1].upper())
return lines
"""
Public:
determines whether the enumeration represents a bitfield
"""
def is_enum_bitfield(obj):
for item in obj['etors']:
if 'value' in item and value_traits.is_bit(item['value']):
return True
return False
"""
Public:
returns c/c++ name of any type
"""
def get_type_name(namespace, tags, obj, item, cpp=False, meta=None, handle_to_class=True):
name = subt(namespace, tags, item, cpp=cpp)
if cpp:
cname = type_traits.find_class_name(item, meta)
if cname:
is_global = class_traits.is_global(cname, tags)
is_namespace = class_traits.is_namespace(cname, namespace, tags) # cname == namespace? e.g., cname == "$x"
is_handle = type_traits.is_handle(item)
is_inscope = False
if obj_traits.is_class(obj): # if the obj _is_ a class
is_inscope = cname == obj['name'] # then is the item's class this obj?
elif not is_global: # else if the obj belongs to a class
is_inscope = cname == obj_traits.class_name(obj) # then is the item's class the same as the obj?
cname_no_namespace = subt(namespace, tags, cname, cpp=cpp, remove_namespace=True)
cname = subt(namespace, tags, cname, cpp=cpp) # remove tags from class name
if not (is_global or is_namespace or is_handle or is_inscope):
# need to prepend the class name to the type after removing namespace from the type
name = subt(namespace, tags, item, cpp=cpp, remove_namespace=True)
name = _remove_class(name, cname_no_namespace)
name = _add_class(name, cname)
elif handle_to_class and is_handle and not obj_traits.is_class(obj):
# convert handles to class pointers
name = re.sub(r"(const\s*)?(\w*:?:?\w+)(\**)", r"\1%s*\3"%cname, name) # e.g., const name* -> const cname**
if not is_handle:
# remove the verbose class part from the type name
name = _remove_class(name, cname)
return name
"""
Private:
returns c/c++ name of any type
"""
def _get_type_name(namespace, tags, obj, item, cpp=False, meta=None, handle_to_class=True):
return get_type_name(namespace, tags, obj, item['type'], cpp, meta, handle_to_class)
"""
Private:
returns python c_type name of any type
"""
def get_ctype_name(namespace, tags, item):
name = subt(namespace, tags, item['type'])
name = _remove_const(name)
name = re.sub(r"void\*", "c_void_p", name)
name = re.sub(r"char\*", "c_char_p", name)
name = re.sub(r"uint8_t", "c_ubyte", name)
name = re.sub(r"uint16_t", "c_ushort", name)
name = re.sub(r"uint32_t", "c_ulong", name)
name = re.sub(r"uint64_t", "c_ulonglong", name)
name = re.sub(r"size_t", "c_size_t", name)
name = re.sub(r"float", "c_float", name)
name = re.sub(r"double", "c_double", name)
name = re.sub(r"\bchar", "c_char", name)
name = re.sub(r"\bint", "c_int", name)
if type_traits.is_pointer(name):
name = _remove_ptr(name)
name = "POINTER(%s)"%name
elif 'name' in item and value_traits.is_array(item['name']):
length = subt(namespace, tags, value_traits.get_array_length(item['name']))
name = "%s * %s"%(name, length)
return name
"""
Public:
returns c/c++ name of member of struct/class
"""
def make_member_name(namespace, tags, item, prefix="", cpp=False, meta=None, remove_array=False, cbase=None):
if cpp and value_traits.is_macro(item['name'], meta):
name = subt(namespace, tags, item['name'])
elif cpp and value_traits.is_array(item['name']):
name = value_traits.get_array_name(item['name'])
name = subt(namespace, tags, name)
alength = value_traits.get_array_length(item['name'])
alength = _get_value_name(namespace, tags, alength, cpp, meta, is_array_size=True, cbase=cbase)
name = "%s[%s]"%(name, alength)
else:
name = subt(namespace, tags, prefix+item['name'], cpp=cpp)
if remove_array:
name = value_traits.get_array_name(name)
return name
"""
Public:
returns a list of strings for each member of a structure or class
c++ format: "TYPE NAME = INIT, ///< DESCRIPTION"
python format: "("NAME", TYPE)" ## DESCRIPTION"
"""
def make_member_lines(namespace, tags, obj, prefix="", cpp=False, py=False, meta=None):
lines = []
if 'members' not in obj:
return lines
for i, item in enumerate(obj['members']):
name = make_member_name(namespace, tags, item, prefix, cpp, meta, remove_array=py, cbase=obj_traits.class_name(obj))
if py:
tname = get_ctype_name(namespace, tags, item)
else:
tname = _get_type_name(namespace, tags, obj, item, cpp, meta)
if cpp and 'init' in item:
value = _get_value_name(namespace, tags, item['init'], cpp, meta, cbase=obj_traits.class_name(obj))
prologue = "%s %s = %s;"%(tname, name, value)
elif py:
delim = "," if i < (len(obj['members'])-1) else ""
prologue = "(\"%s\", %s)%s"%(name, tname, delim)
else:
prologue = "%s %s;"%(tname, name)
comment_style = "##" if py else "///<"
ws_count = 64 if py else 48
for line in split_line(subt(namespace, tags, item['desc'], True, cpp), 70):
lines.append("%s%s %s"%(append_ws(prologue, ws_count), comment_style, line))
prologue = ""
return lines
"""
Public:
returns a list of c++ strings for each member of a class
format: "auto getNAME( void ) const { return MEMBER; }"
"""
def make_member_function_lines(namespace, tags, obj, prefix=""):
lines = []
if 'members' not in obj:
return lines
for item in obj['members']:
name = subt(namespace, tags, item['name'], cpp=True)
is_pointer = type_traits.is_pointer(item['type'])
if is_pointer and re.match(r"p\w+", name): # if this is a pointer and starts with 'p',
fname = name[1:].title() # then remove the 'p' part of the name
else:
fname = name.title()
lines.append("auto get%s( void ) const { return %s; }"%(fname, prefix+name))
return lines
"""
Private:
returns the list of parameters, filtering based on desc tags
"""
def _filter_param_list(params, filters1=["[in]", "[in,out]", "[out]"], filters2=[""]):
lst = []
for p in params:
for f1 in filters1:
if f1 in p['desc']:
for f2 in filters2:
if f2 in p['desc']:
lst.append(p)
break
break
return lst
"""
Private:
returns c/c++ name of parameter
"""
def _get_param_name(namespace, tags, item, cpp):
name = subt(namespace, tags, item['name'], cpp=cpp)
if cpp and type_traits.is_handle(item['type']):
name = re.sub(r"\bh([A-Z]\w+)", r"p\1", name) # change "hName" to "pName"
name = re.sub(r"\bph([A-Z]\w+)", r"pp\1", name) # change "phName" to "ppName"
if param_traits.is_output(item) and not param_traits.is_optional(item):
name = re.sub(r"p(p[A-Z]\w+)", r"\1", name) #change ppName to pName
return name