forked from backman-git/python-UML-sequence-diagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjector.py
129 lines (80 loc) · 2.15 KB
/
injector.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
import re
class Injector:
def __init__(self,f):
self.f=f
self.cName=""
print >>self.f, "SD = open(\"./SD\",\"w\")"
def write(self,line):
print >>self.f, line
def injectNameMethod(self,cName,nTab):
self.cName=cName
for i in range(nTab):
print>>self.f, '\t',
print >>self.f, "\tdef name(self,n):"
for i in range(nTab):
print>>self.f, '\t',
print >>self.f, "\t\tself.name=n"
def injectToast(self,m,nTab):
for i in range(nTab):
print>>self.f, '\t',
print>>self.f, "\tprint>>SD, \""+ "->\"+str(self.name)+\""+":"+str(m)+ "\""
def injectCallerToast(self,nTab,flag):
for i in range(nTab):
print>>self.f, '\t',
if flag is False:
print>>self.f, "print>>SD, str(self.name), "
else:
print>>self.f, "print>>SD, \'main\',"
def injectNewNameToast(self,obj,nTab):
for i in range(nTab):
print>>self.f, '\t',
print>>self.f, str(obj)+".name(\""+str(obj)+"\""+")"
source = open("./test.py","r")
output = open("./sourceInject.py","w")
injector=Injector(output)
classAry=[]
runTimeStack=["main"]
def countTab(line):
count=0
for obj in line:
if obj =='\t':
count+=1
return count
#only for class
#search for class
flag =False
for line in source:
classObj=re.match(r'\t*class (.*):',line)
methodObj=re.match(r'\t*def (.*)\((.*)\):',line)
newObj = re.match(r'\t*(.*)\s?=\s?(.*)\(\)',line)
callObj=re.match(r'\t*(.*)\.(.*)\((.*)\)',line)
mainObj= re.match(r"if __name__ ==\'__main__\':",line)
if mainObj:
flag=True
if classObj:
n=countTab(line)
className=classObj.group(1)
classAry.append(className)
# inject Name method
injector.write(line)
injector.injectNameMethod(className,n)
elif methodObj:
n=countTab(line)
methodName = methodObj.group(1)
initMethod=re.match(r"__init__(.*)",methodName)
if not initMethod:
injector.write(line)
injector.injectToast(methodName,n)
elif callObj:
n=countTab(line)
injector.injectCallerToast(n,flag)
injector.write(line)
runTimeStack.append(callObj.group(1))
pass
elif newObj:
n=countTab(line)
objName=newObj.group(1)
injector.write(line)
injector.injectNewNameToast(objName,n)
else:
injector.write(line)