-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpydef.py
362 lines (312 loc) · 11.6 KB
/
pydef.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
import inspect
class PyDefUtils:
@staticmethod
def is_class(thing):
if inspect.isclass(thing):
return True
else:
return False
@staticmethod
def is_module(thing):
if inspect.ismodule(thing):
return True
else:
return False
@staticmethod
def is_valid_name(name):
valids = ["__new__", "__init__"]
if name.startswith("__"):
if name in valids:
return True
else:
return False
else:
return True
@staticmethod
def is_method(thing):
if (inspect.ismethod(thing) or inspect.isfunction(thing) or inspect.ismethoddescriptor(thing)) and PyDefUtils.is_valid_method(thing) and not PyDefUtils.is_field(thing):
return True
else:
return False
@staticmethod
def is_valid_method(thing):
invalids = ["dictproxy", "NoneType", "PyCapsule", "tuple", "frozenset", "set", "object"]
if not type(thing).__name__ in invalids:
return True
else:
return False
@staticmethod
def is_function(thing):
if inspect.isfunction(thing) or inspect.ismethoddescriptor(thing) or type(thing).__name__ == "function" or type(thing).__name__ == "builtin_function_or_method" or type(thing).__name__ == "instancemethod":
return True
else:
return False
@staticmethod
def is_data(thing):
datas = ['int', 'float', 'str', 'long', 'complex', 'bool', 'Nonetype']
if type(thing).__name__ in datas or inspect.isdatadescriptor(thing):
return True
else:
return False
@staticmethod
def is_field(field):
if inspect.isdatadescriptor(field) or PyDefUtils.is_data(field) and not inspect.isgetsetdescriptor(field):
return True
else:
return False
class PyFieldInfo:
def __init__(self, name, obj):
self.name = name
self.val = obj
self.type = type(obj)
class PyFunctionInfo:
def __init__(self, method):
self.name = method.__name__
self.args = []
self.defaults = []
self.extract(method)
def extract(self, method):
try:
args = inspect.getargspec(method)
self.args = args[0]
self.defaults = args.defaults
except:
pass
class PyClassInfo:
def __init__(self, cls):
self.name = cls.__name__
self.members = dir(cls)
self.fields = []
self.methods = []
self.bases = []
self.extract(cls)
def extract(self, cls):
# base class(es)
for base in cls.__bases__:
self.bases.append(PyClassInfo(base))
# fields
for field in self.members:
try:
field_obj = getattr(cls, field)
except:
return
if PyDefUtils.is_field(field_obj):
self.fields.append(PyFieldInfo(field, field_obj))
# methods
for method in self.members:
method_obj = getattr(cls, method)
if PyDefUtils.is_method(method_obj):
self.methods.append(PyFunctionInfo(method_obj))
class PyModuleInfo:
def __init__(self, module):
self.name = module.__name__
self.members = []
self.extract(module)
def extract(self, module):
if not inspect.ismodule(module):
raise ValueError("Given object is not a module")
for member in dir(module):
member_obj = getattr(module, member)
if PyDefUtils.is_data(member_obj):
self.members.append(PyFieldInfo(member, member_obj))
if PyDefUtils.is_class(member_obj):
self.members.append(PyClassInfo(member_obj))
if PyDefUtils.is_function(member_obj):
self.members.append(PyFunctionInfo(member_obj))
#if PyDefUtils.is_module(member_obj):
#self.members.append(PyModuleInfo(member_obj))
class PyModuleDumper:
def __init__(self, module):
self.module = PyModuleInfo(module)
def dump_field(self, field, indent):
field_def = '\t' * indent
if PyDefUtils.is_data(field.val):
field_def += field.name + " = " + str(field.val) + " # of type '" + field.type.__name__ + "'"
else:
field_def += field.name + " = " + field.type.__name__
field_def += '\n'
return field_def
def dump_function(self, function, indent):
function_def = ('\t' * indent) + "def " + function.name + '(' + ", ".join(function.args) + "):\n" + ('\t' * (indent + 1)) + "pass"
function_def += "\n\n"
return function_def
def dump_class(self, cls, indent):
class_def = ('\t' * indent) + "class " + cls.name
if cls.bases:
class_def += "(" + ", ".join(map(lambda base: base.name, cls.bases)) + "):"
else:
class_def += ":"
class_def += '\n'
for field in cls.fields:
class_def += self.dump_field(field, indent + 1)
for method in cls.methods:
if PyDefUtils.is_valid_name(method.name):
class_def += self.dump_function(method, indent + 1)
return class_def
def dump_to_file(self, fd):
fields = []
functions = []
classes = []
seen = []
for member in self.module.members:
if isinstance(member, PyFieldInfo):
fields.append(member)
if isinstance(member, PyFunctionInfo):
functions.append(member)
if isinstance(member, PyClassInfo):
classes.append(member)
for field in fields:
if not "field" + field.name in seen:
fd.write(self.dump_field(field, 0))
seen.append("field" + field.name)
for function in functions:
if not "func" + function.name in seen:
fd.write(self.dump_function(function, 0))
seen.append("func" + function.name)
for cls in classes:
if not "class" + cls.name in seen:
fd.write(self.dump_class(cls, 0))
seen.append("class" + cls.name)
fd.flush()
def dump_print(self):
fields = []
functions = []
classes = []
for member in self.module.members:
if isinstance(member, PyFieldInfo):
fields.append(member)
if isinstance(member, PyFunctionInfo):
functions.append(member)
if isinstance(member, PyClassInfo):
classes.append(member)
for field in fields:
print(self.dump_field(field, 0))
for function in functions:
print(self.dump_function(function, 0))
for cls in classes:
print(self.dump_class(cls, 0))
class PyDefDumper:
mods_to_dump = None
classes_to_dump = None
def __init__(self):
self.mods_to_dump = []
self.classes_to_dump = []
def is_class(self, thing):
if inspect.isclass(thing):
return True
else:
return False
def is_module(self, thing):
if inspect.ismodule(thing):
return True
else:
return False
def is_valid_name(self, name):
valids = ["__new__", "__init__"]
if name.startswith("__"):
if name in valids:
return True
else:
return False
else:
return True
def is_method(self, thing):
if inspect.ismethod(thing) or inspect.isfunction(thing) or inspect.ismethoddescriptor(thing):
return True
else:
return False
def is_function(self, thing):
if inspect.isfunction(thing) or type(thing).__name__ == "function" or type(thing).__name__ == "builtin_function_or_method":
return True
else:
return False
def is_data(self, thing):
datas = ['int', 'float', 'str', 'long', 'complex', 'bool', 'Nonetype']
if type(thing).__name__ in datas:
return True
else:
return False
def is_field(self, field):
if inspect.isdatadescriptor(field) or self.is_data(field):
return True
else:
return False
def dump_class_inheritance(self, bases):
inheritance = "("
multiple = True if len(bases) > 1 else False
if multiple:
for idx in xrange(0,len(bases)):
if idx == len(bases)-1: # is last?
inheritance += bases[idx].__name__ + "):\n"
else:
inheritance += bases[idx].__name__ + ", "
else:
inheritance += bases[0].__name__ + "):\n"
return inheritance
def dump_method_args(self, method):
args = []
try:
args = inspect.getargspec(method)
print(args)
return args[0]
except:
pass
return args
def dump_class_methods(self, cls, members):
memberdef = ""
for member in members:
if self.is_method(getattr(cls, member)) and self.is_valid_name(member) and not self.is_field(getattr(cls, member)):
memberdef += "\tdef " + member + '('
args = self.dump_method_args(getattr(cls, member))
if not args:
memberdef += "):\n\t\tpass # ...\n\n"
else:
for idx in xrange(0,len(args)):
if idx == len(args)-1:
memberdef += args[idx] + "):\n\t\tpass # ...\n\n"
else:
memberdef += args[idx] + ", "
return memberdef
def dump_class_fields(self, cls, members):
fielddef = ""
for field in members:
fld = getattr(cls, field)
if self.is_field(fld) and self.is_valid_name(field):
if self.is_data(fld):
fielddef += "\t" + field + " = " + str(fld) + "\n"
else:
fielddef += "\t" + field + " = " + type(fld).__name__ + "\n"
if len(fielddef) > 2:
fielddef += '\n'
return fielddef
def dump_class(self, cls):
entries = dir(cls)
classdef = "class " + cls.__name__
if cls.__bases__:
classdef += self.dump_class_inheritance(cls.__bases__)
else:
classdef = classdef + ":\n"
classdef += self.dump_class_fields(cls, entries)
classdef += self.dump_class_methods(cls, entries)
return classdef
def dump_lib(self, lib, name=""):
name = lib.__name__ if not name else name
libfile = open(name + ".def.py" , "w")
entries = dir(lib)
for entry in entries:
obj = getattr(lib, entry)
if self.is_data(obj) and self.is_valid_name(entry):
libfile.write(entry + " = " + str(obj) + '\n')
if self.is_class(obj):
self.classes_to_dump.append(obj)
elif self.is_module(obj):
self.mods_to_dump.append(obj)
print('\n')
if self.classes_to_dump:
for cls in self.classes_to_dump:
libfile.write(self.dump_class(cls))
if self.mods_to_dump:
for mod in self.mods_to_dump:
libfile.write("# Submodule not dumped: " + mod.__name__ + "\n")
libfile.flush()
libfile.close()