-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathadd_MonoPInvokeCallback_attribute.py
49 lines (44 loc) · 2.07 KB
/
add_MonoPInvokeCallback_attribute.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
import os
import sys
DECORATORS = ["// ATTRIBUTE AUTOMATICALLY ADDED.",
"[MonoPInvokeCallback]"]
METHODS_TO_DECORATE = ["SetPendingApplicationException(",
"SetPendingArithmeticException(",
"SetPendingDivideByZeroException(",
"SetPendingIndexOutOfRangeException(",
"SetPendingInvalidCastException(",
"SetPendingInvalidOperationException(",
"SetPendingIOException(",
"SetPendingNullReferenceException(",
"SetPendingOutOfMemoryException(",
"SetPendingOverflowException(",
"SetPendingSystemException(",
"SetPendingArgumentException(",
"SetPendingArgumentNullException(",
"SetPendingArgumentOutOfRangeException(",
"EntryPoint=\"SWIGRegisterExceptionCallbacks_UsdCs",
"EntryPoint=\"SWIGRegisterExceptionArgumentCallbacks_UsdCs",
"CreateString("]
def decorate(line):
leading_spaces = len(line) - len(line.lstrip(' '))
decorated = ["{0}{1}".format(" "*leading_spaces, d) for d in DECORATORS]
decorated.append(line)
return "\n".join(decorated)
#TODO: add proper argument handling
def main():
print("Adding MonoPInvokeCallback attribute to the appropriate methods ...\n")
build_path = sys.argv[1]
cwd = os.path.abspath(os.path.dirname(__file__))
filein_path = os.path.join(cwd, build_path, "UsdCsPINVOKE.cs")
fileout_path = os.path.join(cwd, build_path, "UsdCsPINVOKE.cs.out")
prev_line = ""
with open(filein_path, "r") as filein, open(fileout_path, "w") as fileout:
for line in filein:
if DECORATORS[1] not in prev_line and any(methods in line for methods in METHODS_TO_DECORATE):
fileout.write(decorate(line))
else:
fileout.write(line)
prev_line = line
os.replace(fileout_path, filein_path)
if __name__ == "__main__":
main()