-
Notifications
You must be signed in to change notification settings - Fork 4
/
usdstubgen.py
executable file
·1015 lines (900 loc) · 31.9 KB
/
usdstubgen.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
#!/usr/bin/env python
"""
Generates __init__.pyi files which will live alongside their .py equivalent.
pyi files will be properly detected in pycharm 2017+ and other editors that
fully support PEP484.
"""
import os
import glob
import re
import json
import sys
import shutil
import subprocess
from multiprocessing import Pool
from collections import defaultdict
from distutils.dir_util import copy_tree
import xml.etree.ElementTree as ET
DEFAULT_NUM_PROCS = 10
# FIXME/TODO:
# - Doxygen is not being run on functions (i.e. non-methods). this also affects
# our ability to document python static methods which are derived from these
# functions.
# - every object has a __reduce__ method which appears to be there just to raise
# an error if the object is pickled. the return type should be typing.NoReturn
# but we don't have a new enough version of typing yet.
pymelpath = os.path.expandvars('$REPO_PATH/python/pymel/')
sys.path.insert(0, pymelpath)
# a python identifier
IDENTIFIER = r'([a-zA-Z_][a-zA-Z0-9_]*)'
# mapping from c++ operators to python special methods
OPERATORS = {
"operator!=": '__neq__',
"operator==": '__eq__',
"operator<": '__lt__',
"operator<=": '__le__',
"operator>": '__gt__',
"operator>=": '__ge__',
"operator bool": '__nonzero__',
"operator[]": '__getitem__',
"operator()": '__call__',
}
# c++ to python type subsitutions
LIST_PROXY_TYPE_MAP = [
(r'\bSdfSubLayerProxy\b', 'pxr.Sdf.ListProxy_SdfSubLayerTypePolicy'),
# nameTokenKey
(r'\bSdfNameOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
(r'\bSdfNameChildrenOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
(r'\bSdfPropertyOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
]
TYPE_MAP = [
(r'\bVtArray<\s*SdfAssetPath\s*>', 'SdfAssetPathArray'),
(r'\bstd::string\b', 'str'),
(r'\bstring\b', 'str'),
(r'\bsize_t\b', 'int'),
(r'\bchar\b', 'str'),
(r'\bstd::function<(.+)\((.*)\)>', r'Callable[[\2], \1]'),
(r'\bstd::vector\b', 'List'),
(r'\bstd::pair\b', 'Tuple'),
(r'\bstd::set\b', 'List'),
(r'\bdouble\b', 'float'),
(r'\bboost::python::', ''),
(r'\bvoid\b', 'None'),
(r'\b' + IDENTIFIER + r'Vector\b', r'List[\1]'),
(r'\bTfToken\b', 'str'),
(r'\bVtDictionary\b', 'dict'),
(r'\bUsdMetadataValueMap\b', 'dict'),
# strip suffixes
(r'RefPtr\b', ''),
(r'Ptr\b', ''),
(r'ConstHandle\b', ''),
(r'Const\b', ''),
(r'Handle\b', ''),
# simple renames:
(r'\bSdfBatchNamespaceEdit\b', 'pxr.Sdf.NamespaceEdit'),
# Sdf mapping types:
# FIXME: the following substitutions use python identifiers (should they be moved to a separate find/replace list?)
(r'\bSdfLayerHandleSet\b', 'List[pxr.Sdf.Layer]'),
(r'\bSdfDictionaryProxy\b', 'pxr.Sdf.MapEditProxy_VtDictionary'),
(r'\bSdfReferencesProxy\b', 'pxr.Sdf.ReferenceTypePolicy'),
(r'\bSdfSubLayerProxy\b', 'pxr.Sdf.ListProxy_SdfSubLayerTypePolicy'),
# pathKey
(r'\bSdfInheritsProxy\b', 'pxr.Sdf.ListEditorProxy_SdfPathKeyPolicy'),
(r'\bSdfSpecializesProxy\b', 'pxr.Sdf.ListEditorProxy_SdfPathKeyPolicy'),
# nameTokenKey
(r'\bSdfNameOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
(r'\bSdfNameChildrenOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
(r'\bSdfPropertyOrderProxy\b', 'pxr.Sdf.ListProxy_SdfNameTokenKeyPolicy'),
# nameKey
(r'\bSdfVariantSetNamesProxy\b', 'pxr.Sdf.ListEditorProxy_SdfNameKeyPolicy'),
]
# c++ to python default value subsitutions
DEFAULT_VAL_MAP = [
(r'\bstd::string\(\)', '""'),
(r'\bNULL\b', 'None'),
(r'\bnullptr\b', 'None'),
(r'\btrue\b', 'True'),
(r'\bfalse\b', 'False'),
]
# methods which don't follow the rules go here
METHOD_RENAMES = {
'pxr.Sdf.Path': {
'GetString': 'pathString',
}
}
ARRAY_TYPES = {
'Sdf.AssetPathArray': 'Sdf.AssetPath',
'Vt.IntervalArray': 'Gf.Interval',
'Vt.Matrix2dArray': 'Gf.Matrix2d',
'Vt.Matrix3dArray': 'Gf.Matrix3d',
'Vt.Matrix2fArray': 'Gf.Matrix2f',
'Vt.Matrix3fArray': 'Gf.Matrix3f',
'Vt.Matrix4dArray': 'Gf.Matrix4d',
'Vt.Matrix4fArray': 'Gf.Matrix4f',
'Vt.QuaternionArray': 'Gf.Quaternion',
'Vt.QuatdArray': 'Gf.Quatd',
'Vt.QuatfArray': 'Gf.Quatf',
'Vt.QuathArray': 'Gf.Quath',
'Vt.Range1dArray': 'Gf.Range1d',
'Vt.Range1fArray': 'Gf.Range1f',
'Vt.Range2dArray': 'Gf.Range2d',
'Vt.Range2fArray': 'Gf.Range2f',
'Vt.Range3dArray': 'Gf.Range3d',
'Vt.Range3fArray': 'Gf.Range3f',
'Vt.Rect2iArray': 'Gf.Rect2i',
'Vt.StringArray': 'str',
'Vt.TokenArray': 'Tf.Token',
'Vt.Vec2dArray': 'Gf.Vec2d',
'Vt.Vec2fArray': 'Gf.Vec2f',
'Vt.Vec2hArray': 'Gf.Vec2h',
'Vt.Vec2iArray': 'Gf.Vec2i',
'Vt.Vec3dArray': 'Gf.Vec3d',
'Vt.Vec3fArray': 'Gf.Vec3f',
'Vt.Vec3hArray': 'Gf.Vec3h',
'Vt.Vec3iArray': 'Gf.Vec3i',
'Vt.Vec4dArray': 'Gf.Vec4d',
'Vt.Vec4fArray': 'Gf.Vec4f',
'Vt.Vec4hArray': 'Gf.Vec4h',
'Vt.Vec4iArray': 'Gf.Vec4i',
'Vt.BoolArray': 'bool',
'Vt.FloatArray': 'float',
'Vt.Int64Array': 'long',
'Vt.IntArray': 'int',
# 'Vt.HalfArray': 'pxr_half::half',
# 'Vt.UIntArray': 'unsigned int',
# 'Vt.UInt64Array': 'unsigned int64',
# 'Vt.UShortArray': 'unsigned short',
# 'Vt.UCharArray': 'unsigned char',
# 'Vt.CharArray': 'char',
# 'Vt.ShortArray': 'short',
# 'Vt.DoubleArray': 'double',
}
STRIP = {'*', 'const', '&', 'friend', 'constexpr'}
# FIXME: once we get more of these, move them to a jinja-templated json file?
def create_list_proxy(source_info, classname, proxied_type):
doc = {
'members': {
'append': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': proxied_type
},
],
'result': 'None',
}
]
},
'clear': {
'type': 'method',
'signatures': [
{
'args': [],
'result': 'None',
}
]
},
'copy': {
'type': 'method',
'signatures': [
{
'args': [],
'result': classname,
}
]
},
'count': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': proxied_type
},
],
'result': 'int',
}
]
},
'index': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': proxied_type
},
],
'result': 'int',
}
]
},
'insert': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'index',
'type': 'int'
},
{
'name': 'value',
'type': proxied_type
},
],
'result': 'None',
}
]
},
'remove': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': proxied_type
},
],
'result': 'None',
}
]
},
'replace': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'old',
'type': proxied_type
},
{
'name': 'new',
'type': proxied_type
},
],
'result': 'None',
}
]
},
'ApplyList': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': 'List[%s]' % proxied_type
},
],
'result': 'None',
}
]
},
}
}
return doc
def create_array(source_info, classname, proxied_type):
proxied_type_input = source_info.add_implicit_unions(proxied_type)
doc = {
'members': {
'__init__': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'value',
'type': 'Iterable[%s]' % proxied_type_input
},
],
'result': 'None',
}
]
},
'__iter__': {
'type': 'method',
'signatures': [
{
'args': [],
'result': 'Iterator[%s]' % proxied_type
}
]
},
'__getitem__': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'index',
'type': 'int'
},
],
'result': proxied_type,
}
]
},
'__setitem__': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'index',
'type': 'int'
},
{
'name': 'value',
'type': proxied_type_input
},
],
'result': 'None',
}
]
},
}
}
return doc
def add_list_proxies(source_info, modules):
"""
Add signature information for ListProxy objects, which only exist in python
and are not inspectable.
"""
sdf_doc = modules['pxr']['members']['Sdf']['members']
for _, subst in TYPE_MAP:
if subst.startswith('pxr.Sdf.ListProxy'):
classname = subst.split('.')[-1]
sdf_doc[classname] = create_list_proxy(source_info, classname, 'str')
def add_arrays(source_info, modules):
"""
Add signature information for Array objects, which only exist in python
and are not inspectable.
"""
for array_type, child_type in ARRAY_TYPES.items():
if '.' in child_type:
# we use fully-qualified names which become localized by the stub
# generator
child_type = 'pxr.' + child_type
module, classname = array_type.split('.')
module_doc = modules['pxr']['members'][module]['members']
module_doc[classname] = create_array(source_info, classname, child_type)
def expandpath(p):
"""
Returns
-------
str
"""
return os.path.expanduser(os.path.expandvars(os.path.abspath(p)))
def basename(p):
return os.path.split(p)[1]
def capitalize(s):
"""
Returns
-------
str
"""
return s[0].upper() + s[1:]
def strip_pxr_namespace(s):
"""
Returns
-------
str
"""
if s.startswith('pxr::'):
return s[5:]
return s
def text(node):
"""
Get all the text of an Etree node
Returns
-------
str
"""
return ''.join(node.itertext())
def maybe_result(parts):
"""
return if the argument looks like a c++ result
Returns
-------
bool
"""
return 'const' not in parts and ('*' in parts or '&' in parts)
def should_strip_part(x):
"""
whether the part looks like a c++ keyword
Returns
-------
bool
"""
return x in STRIP or x.endswith('_API')
class SourceInfo(object):
"""
Helper for converting c++ data to python data, using info parsed from the
source
"""
def __init__(self, srcdir, verbose=False):
self.srcdir = expandpath(srcdir)
self._valid_modules = None
self._implicitly_convertible_types = None
self.verbose = verbose
def get_valid_modules(self):
"""
get a cached list of modules from the source
"""
if self._valid_modules is None:
macro_dir = os.path.join(self.srcdir, 'cmake/macros')
if not os.path.exists(macro_dir):
raise RuntimeError("Cannot find cmake macro directory: %s" % macro_dir)
sys.path.append(macro_dir)
import generateDocs
# FIXME: get this list from the actual python modules, so that we can skip
# processing xml files that don't relate to python.
module_dirs = generateDocs._getModules(os.path.join(self.srcdir, 'pxr'))
self._valid_modules = sorted(
[capitalize(os.path.basename(x)) for x in module_dirs],
reverse=True)
return self._valid_modules
def get_implicitly_convertible_types(self):
"""
inspect the boost-python code to parse the rules for implicitly
convertible types
"""
# FIXME: add module prefixes to all types (Output, Input, Parameter, etc are not prefixed)
# FIXME: parse other conversions defined using TfPyContainerConversions
if self._implicitly_convertible_types is None:
output = subprocess.check_output(
['grep', 'implicitly_convertible', '-r',
os.path.join(self.srcdir, 'pxr'),
'--include=wrap*.cpp'])
code_reg = re.compile(r'\s+implicitly_convertible<\s*(?P<from>(%s|:)+),\s*(?P<to>(%s|:)+)\s*>\(\)' %
(IDENTIFIER, IDENTIFIER))
result = defaultdict(set)
for line in output.split('\n'):
line = line.strip()
if line:
path, code = line.split(':', 1)
if '.template.' in path:
# skip jinja templates
continue
# each line looks like:
# 'src/pxr/base/lib/gf/wrapQuatd.cpp: implicitly_convertible<GfQuatf, GfQuatd>();'
m = code_reg.search(code)
if m:
match = m.groupdict()
from_type = match['from']
to_type = match['to']
if to_type == 'This':
parts = path.split(os.path.sep)
name = os.path.splitext(parts[-1])[0]
assert name.startswith('wrap')
to_type = self.to_python_id(capitalize(parts[-2]) + name[4:])
to_type = self.convert_typestr(to_type, is_arg=None)[0]
from_type = self.convert_typestr(from_type, is_arg=None)[0]
result[to_type].add(from_type)
elif self.verbose:
print "no match", line
self._implicitly_convertible_types = dict(result)
print self._implicitly_convertible_types.keys()
return self._implicitly_convertible_types
def split_module(self, typestr):
"""
split the c++ type into module name and object name
Returns
-------
List[str]
"""
for mod in self.get_valid_modules():
if typestr.startswith(mod):
s = typestr[len(mod):]
if s and (s[0].isupper() or s[0] == '_'):
return mod, s
# if typestr.startswith('_'):
# result = split_module(typestr[1:])
# if len(result) == 2:
# return result
return [typestr]
def to_python_id(self, typestr):
"""
Returns
-------
str
"""
typestr = strip_pxr_namespace(typestr)
parts = self.split_module(typestr)
if len(parts) == 1:
return parts[0]
else:
mod = parts[0]
name = parts[1]
return 'pxr.' + mod + '.' + name
def add_implicit_unions(self, typestr):
"""
wrap the type string in a Union[] if it is in the list of types with known
implicit conversions.
Parameters
----------
typestr : str
fully qualified python type identifier
Returns
-------
str
"""
others = self.get_implicitly_convertible_types().get(typestr)
if others is not None:
return 'Union[%s]' % ', '.join([typestr] + sorted(others))
else:
return typestr
def convert_typestr(self, node, is_arg=True):
"""
Convert a c++ type string to a python type string
Returns
-------
Tuple[str, bool]
the new typestring and whether the type appears to be a return value
"""
if isinstance(node, basestring):
typestr = node
else:
if node is None:
return None
typestr = text(node)
parts = typestr.split()
is_result = maybe_result(parts)
parts = [x for x in parts if not should_strip_part(x)]
typestr = ''.join(parts)
for pattern, replace in TYPE_MAP:
typestr = re.sub(pattern, replace, typestr)
# swap container syntax
typestr = typestr.replace('<', '[')
typestr = typestr.replace('>', ']')
# convert to python identifers
parts = [self.to_python_id(x) for x in re.split(IDENTIFIER, typestr)]
# note: None is a valid value for is_arg
if is_arg is True and not is_result:
parts = [self.add_implicit_unions(x) for x in parts]
typestr = ''.join(parts)
typestr = typestr.replace(',', ', ')
typestr = typestr.replace('&', '') # FIXME: leftover shit from std::function
typestr = typestr.replace('::', '.')
return typestr, is_result
def convert_default(self, node):
if isinstance(node, basestring):
valuestr = node
else:
if node is None:
return None
valuestr = text(node)
for pattern, replace in DEFAULT_VAL_MAP:
valuestr = re.sub(pattern, replace, valuestr)
return self.convert_typestr(valuestr, is_arg=False)[0]
def convert_xml(path, srcdir, verbose=False, modules=None):
"""
Add data from a Doxygen xml file into the modules document.
Parameters
----------
path : str
srcdir : str
verbose : bool
modules : Optional[Dict]
Returns
-------
Dict[str, Any]
hierarchy of member data
e.g.
{
'mypackage': {
'members': {
'mymodule': {
'members': {
'MyClass': {
'members': {
'my_func': {
'type': 'method',
'signatures': [
{
'args': [
{
'name': 'foo',
'type': 'str'
},
{
'name': 'foo',
'type': 'int'
},
'result: 'bool'
}
]}}}}}}}}
"""
source_info = SourceInfo(srcdir, verbose=verbose is not False)
base_path = basename(path)
tree = ET.parse(path)
root = tree.getroot()
if modules is None:
modules = {}
name_cache = {}
def is_verbose(cpp_name):
if isinstance(verbose, basestring):
return cpp_name == verbose
else:
return verbose
def get_name_parts(cpp_name):
try:
parts = name_cache[cpp_name]
except KeyError:
dotted_name = source_info.to_python_id(cpp_name)
# if dotted_name.startswith('_'):
# continue
if '.' not in dotted_name:
# we deal in fully-qualified names here, and
# maintenance.stubs.packagestubs() converts them to the local
# scope
if is_verbose(cpp_name):
print("%s: Failed to convert cpp to valid python location %r -> %r" %
(base_path, cpp_name, dotted_name))
return
parts = dotted_name.split('.')
name_cache[cpp_name] = parts
return parts
def get_member_doc(parts, doc, member=None):
"""
Parameters
----------
parts : List[str]
python identifiers. e.g. ['pxr', 'Sdf', 'Path']
doc : Dict
root module document
member : Optional[str]
if this is a class, the method name
Returns
-------
Dict
the member document
"""
if member:
parts = parts + [member]
for parent in parts[:-1]:
parent_doc = doc.setdefault(parent, {})
doc = parent_doc.setdefault('members', {})
return doc.setdefault(parts[-1], {})
for obj in root.iter('compounddef'):
cpp_name = obj.find('compoundname').text
if obj.attrib['kind'] == 'class':
is_class = True
elif obj.attrib['kind'] == 'file':
is_class = False
else:
continue
is_obj_verbose = is_verbose(cpp_name)
for member in obj.iter('memberdef'):
kind = member.attrib['kind']
if kind != 'function':
continue
func_name = member.find('name').text
if is_obj_verbose:
print cpp_name, "member:", repr(func_name)
name_parts = get_name_parts(cpp_name)
if name_parts is None:
continue
args = []
results = []
for i, param in enumerate(member.findall('param')):
# print arg_type
arg_type, is_result = source_info.convert_typestr(
param.find('type'), is_arg=True)
default = source_info.convert_default(param.find('defval'))
declname = param.find('declname')
if declname is None:
arg_name = 'arg%d' % i
if default:
print ("%s: Could not find name for keyword arg: %s.%s "
"(using %r)" %
(base_path, cpp_name, func_name, arg_name))
else:
arg_name = declname.text
if is_result:
results.append(arg_type)
else:
arg = {
'name': arg_name,
'type': arg_type
}
if default is not None:
arg['default'] = default
# print ' ', `arg_name`, `type.text`, `arg_type`
args.append(arg)
result_type = source_info.convert_typestr(member.find('type'),
is_arg=False)[0]
if results and result_type in ['None', 'bool']:
# FIXME: check for the case where results > 1
result_type = results[0]
if is_class and func_name == cpp_name:
func_name = '__init__'
result_type = 'None'
elif func_name.startswith('operator'):
# TODO: skip if not a supported python operator?
func_name = OPERATORS.get(func_name, func_name)
# this must happen after potential renaming of func_name
if is_class:
# a method
doc = get_member_doc(name_parts, modules, func_name)
else:
doc = get_member_doc(name_parts, modules)
try:
signatures = doc['signatures']
except KeyError:
# first visit. fill in the doc:
signatures = []
doc.update({
'type': 'method',
'signatures': signatures
})
sig = {
'args': args,
'result': result_type
}
if sig not in signatures:
# it is possible that after converting types that two
# signatures become the same
signatures.append(sig)
return modules
def iter_xml_files(xmldir):
"""
iterate over all xml files in `xmldir`
"""
for name in sorted(glob.glob(os.path.join(xmldir, '*.xml'))):
basename = os.path.split(name)[1]
if basename.startswith('class') or basename.endswith('8h.xml'):
yield name
def _convert_xml(args):
# here for multiprocessing
return convert_xml(*args)
def merge_dict(d1, d2):
"""
recursively update d2 with values from d1.
"""
for key, from_val in d1.iteritems():
# print key, from_val
if key in d2:
to_val = d2[key]
if hasattr(from_val, 'iteritems') and hasattr(to_val, 'iteritems'):
merge_dict(from_val, to_val)
else:
d2[key] = from_val
else:
d2[key] = from_val
def process_xml(xmldir, srcdir, processes=DEFAULT_NUM_PROCS, verbose=False):
"""
walk through xml files and covert those pertaining to classes
Returns
-------
Dict[str, Any]
hierarchy of member data
"""
if isinstance(verbose, basestring):
# for now convert_xml does not support controlling verbosity at the
# member level.
parts = verbose.split('.')
if parts[0] == 'pxr':
parts.pop(0)
verbose = parts[0]
print "getting files"
args = [(xmlfile, srcdir, verbose) for xmlfile in iter_xml_files(xmldir)]
print "converting in %d processes" % processes
master = {}
if processes > 1:
pool = Pool(processes)
for d in pool.imap_unordered(_convert_xml, args):
merge_dict(d, master)
else:
for argTuple in args:
merge_dict(_convert_xml(argTuple), master)
source_info = SourceInfo(srcdir, verbose=verbose is not False)
add_list_proxies(source_info, master)
add_arrays(source_info, master)
return master
def query_cache(type_data, obj):
import pprint
parts = obj.split('.')
if not parts[0] == 'pxr':
parts.insert(0, 'pxr')
for part in parts:
type_data = type_data[part]
if 'members' in type_data:
type_data = type_data['members']
pprint.pprint(type_data)
def makestubs(outputdir, xmldir, srcdir, force_update=False,
processes=DEFAULT_NUM_PROCS, verbose=False):
# delete the build directory
builddir = os.path.join(outputdir, 'pyi')
if os.path.exists(builddir):
shutil.rmtree(builddir)
# write the json cache
jsonfile = os.path.join(outputdir, 'usd_python.json')
if force_update or not os.path.exists(jsonfile):
if not os.path.exists(outputdir):
os.makedirs(outputdir)
print("processing xml data (force update %s)" %
('ON' if force_update else 'OFF'))
type_data = process_xml(xmldir, srcdir, processes=processes,
verbose=verbose)
print("caching xml data to %s" % jsonfile)
with open(jsonfile, 'w') as f:
json.dump(type_data, f, indent=4, sort_keys=True)
else:
print("loading cached package data from %s" % jsonfile)
with open(jsonfile, 'r') as f:
type_data = json.load(f)
# create the stubs
try:
import maintenance.stubs as stubs
except ImportError:
import stubs
# Notes:
# - UsdHdydra causes stubgen to crash
# - Usdviewq is pure python, except for _usdviewq.so which contains only a few
# utils and is somehow different from the others (doesn't use `Tf.PrepareModule(_usdviewq, locals())`)
# - the last bit of the regex hides any modules starting with an underscore.
# the contents of these modules is imported into the __init__.py files, so we
# don't need separate stubs for them.
stubs.packagestubs(
'pxr', outputdir=outputdir, extensions=('pyi',),
type_data=type_data,
skip_module_regex="pxr[.](UsdMaya|UsdHydra|Usdviewq|Tf.testenv|(.*[.]_.*))")
if isinstance(verbose, basestring):
query_cache(type_data, verbose)
def touch(path):
with open(path, 'w'):
pass
def get_parser():
import argparse
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('xmldir', help='Directory in which to find Doxygen '
'xml files')
parser.add_argument('srcdir', help='Directory in which to find USD '
'source code')
parser.add_argument('pkgdir',
help='Directory in which to find the pxr python package')
parser.add_argument('--force-update', '-f', action='store_true',
help='Force update json package data')
parser.add_argument('--builddir', default='.',
help='Directory in which to generate pyi files')
parser.add_argument('--release', action='store_true',
help='Copy pyi files from builddir to pkgdir')
parser.add_argument('--processes', '-p', type=int, default=DEFAULT_NUM_PROCS,
help='Number of simulataneous processes to run')
parser.add_argument('--verbose', '-v', action='store_true',
help='Enabled verbose output')
parser.add_argument('--query', '-q', type=str,
help='Query the cache. Overrides other behavior')
return parser
def main(argv=None):
if argv is None:
argv = sys.argv[1:]
parser = get_parser()
args = parser.parse_args(argv)
# note: srcdir is expanded later, by SourceInfo (makes it easier to test)
builddir = expandpath(args.builddir)
pkgdir = expandpath(args.pkgdir)
sys.path.insert(0, pkgdir)
if args.query:
verbose = args.query