forked from bgempire/fake-blender-api-2.79
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinspect_modules.py
More file actions
159 lines (122 loc) · 4.42 KB
/
inspect_modules.py
File metadata and controls
159 lines (122 loc) · 4.42 KB
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
import inspect
import importlib
MODULES = [
# "aud",
# "bge.app",
# "bge.constraints",
# "bge.events",
"bge.logic",
# "bge.render",
# "bge.texture",
"bge.types",
# "mathutils",
# "mathutils.noise",
]
def inspectModule(moduleName):
_module = None
try:
_module = importlib.import_module(moduleName)
print("\n> Imported module:", moduleName)
except:
print("\nX Could not import", moduleName)
return
finalString = "### MODULE: " + moduleName + " ###\n\n"
functionsString = ""
attributesString = ""
classesString = "\n# Classes\n"
functions = []
attributes = []
# Iterate over module attribute names
for _attr1Name in dir(_module):
if _attr1Name[0] != '_':
# Get string in format 'module.attribute'
attributePath = _module.__name__ + "." + _attr1Name
_attr1 = eval("_module." + _attr1Name)
# Append functions to list
if inspect.isfunction(_attr1):
functions.append([_attr1Name, _attr1])
# Build string for classes
elif inspect.isclass(_attr1):
# Main class variables
members = []
methods = []
# Add class name
classesString += attributePath + "\n"
_instance = None
# Get the class constructor arguments and instance it
if "__init__" in dir(_attr1):
_init_args = inspect.getfullargspec(_attr1.__init__).args
_init_args = ['"' + arg + '"' for arg in _init_args if arg != "self"]
_init_args = ", ".join(_init_args)
_instance = eval("_attr1(" + _init_args + ")")
# Iterate over instance members
for _attr2Name in dir(_instance):
if _attr2Name[0] != '_':
# Get the literal member of the instance
_attr2 = eval("_instance." + _attr2Name)
# Append attribute member to attributes
if _attr2Name[0] != '_' and not inspect.isfunction(_attr2) and not inspect.ismethod(_attr2):
members.append([_attr2Name, _attr2])
# Append method member to methods
else:
methods.append([_attr2Name, _attr2])
# Build attributes string based on the list attributes
members.sort()
classesString += "\n\t# Attributes:\n" if members else ""
for attr in members:
classesString += "\t" + attr[0] + " = " + str(type(attr[1])) + "\n"
# Build methods string based on the list methods
methods.sort()
classesString += "\n\t# Methods:\n" if methods else ""
# Get method arg names and set default values for the incoming call
for meth in methods:
returnValue = None
argNames = ""
argValues = ""
args = inspect.getfullargspec(meth[1]).args
for _arg in args:
if _arg != "self":
argNames += _arg
argValues += "None"
if args.index(_arg) < len(args) - 1:
argNames += ", "
argValues += ", "
# Call method to retrieve the return value
returnValue = eval("meth[1](" + argValues + ")")
# Adds to the class string the built method representation
classesString += "\t" + meth[0] + "(" + argNames + ") -> " + str(type(returnValue)) + "\n"
classesString += "\n"
# Append module attribute to attributes list
elif not inspect.ismodule(_attr1):
attributes.append([_attr1Name, _attr1])
# Build the functions string
functions.sort()
functionsString += "# Functions:\n" if functions else ""
# Get function arg names and set default values for the incoming call
for func in functions:
returnValue = None
argNames = ""
argValues = ""
args = inspect.getfullargspec(func[1]).args
for _arg in args:
if _arg != "self":
argNames += _arg
argValues += "None"
if args.index(_arg) < len(args) - 1:
argNames += ", "
argValues += ", "
# Call function to retrieve the return value
returnValue = eval("func[1](" + argValues + ")")
# Adds to the function string the built function representation
functionsString += func[0] + "(" + argNames + ") -> " + str(type(returnValue)) + "\n"
# Build the module attributes string
attributes.sort()
attributesString += "\n# Attributes:\n" if attributes else ""
for attr in attributes:
attributesString += attr[0] + " = " + str(attr[1]) + "\n"
# Build the final string based on the build strings
finalString += functionsString + attributesString + classesString
with open("result_" + moduleName + ".txt", "w") as openedFile:
openedFile.write(finalString)
print("Introspection results saved to:", openedFile.name)
for _mod in MODULES: inspectModule(_mod)