-
Notifications
You must be signed in to change notification settings - Fork 1
/
curate.py
288 lines (251 loc) · 10.4 KB
/
curate.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
_author_ = 'ctippur'
import re
import os.path
import os
import sys
import glob
## this is where all generated controller files reside
folder='temp/swagger_server/controllers'
## This is the existing source folder
src='src/python/flask/'
## Temporaru staging area
folderstg='stage'
## Create a stage folder
try:
os.mkdir(folderstg)
except:
pass
default_folders=glob.glob(folder + '/*controller.py')
print (default_folders)
#sys.exit(0)
regexstr=['post','get' ]
## Import string
importStr="""\
import connexion
"""
## Post string
postStrFooter="""\
body = Payload.from_dict(connexion.request.get_json())
return 'do some magic!'
"""
## Footer string
getStrFooter="""\
"""
## Get string
resLogicGet="""\
if res['result'] == 'failure':
res['error_code']=400
return res, 400
else:
return(res)
"""
## Post string
resLogicPost="""\
if res['result'] == 'failure':
res['error_code']=400
return res, 400
else:
return(res)
"""
if not os.path.exists(src + "/swagger_server/models"):
os.makedirs(src + "/swagger_server/models")
modelList=os.listdir(src + "/swagger_server/models")
for model in modelList:
if model=='__init__.py' or model=='base_model_.py':
pass
else:
modelname=re.sub('\.py$', '', model)
fn=modelname.title()
importStr+="\nfrom swagger_server.models." + modelname + " import " + fn
## controllerfile - self explanatory
## operation - _get / _post
## line - function (def)
def createStub(controllerfile,operation,line):
#print (controllerfile, operation, line)
if (re.match(operation, '_post')):
print ("Writing line " + line + "to file " + controllerfile)
if os.path.exists(controllerfile):
with open(controllerfile, "a") as w:
w.write('\n')
w.write(line)
w.write(postStrFooter)
else:
with open(controllerfile, "w") as w:
w.write(importStr)
w.write('\n')
w.write(line)
w.write(postStrFooter)
elif (re.match(operation, '_get')):
if os.path.exists(controllerfile):
with open(controllerfile, "a") as w:
w.write('\n')
w.write(line)
w.write(getStrFooter)
else:
with open(controllerfile, "w") as w:
w.write(importStr)
w.write('\n')
w.write(line)
w.write(getStrFooter)
def ret_filename(origfile):
#print (origfile)
args=''
retstr=''
str=''
for str in regexstr:
#print (origfile, str)
if re.search(str,origfile):
retstr=origfile.split(str)[0].replace('def ','')
if retstr =='':
retstr=origfile.split('(')[0].replace('def ','')
args=origfile.split('(')[1].split(')')[0]
if retstr == '':
retstr=origfile.split('(')[0].replace('def ','')
args=origfile.split('(')[1].split(')')[0]
return (retstr,str,args)
functionCall=None
## Start by copying all controller code to staging area
cmd='cp ' + folder + "/* " + folderstg
os.system(cmd)
for default_controller in default_folders:
#default_controller=folder + '/default_controller.py'
print ("Default controller " + default_controller)
default_controller_file=default_controller.split('/')[-1]
# /tmp/stage/temp/swagger_server/controllers/store_controller.py
print ("Writing to " + folderstg + '/' + default_controller_file)
fStgDefaultController = open(folderstg + '/' + default_controller_file, 'w')
## Read default controller that was generated
with open(default_controller) as infile:
defn=0
post=0
defline=''
## Read each line
for line in infile:
## See if it matches operationid
if (re.match(r'operationId', line)):
l=re.findall(r"(\w)(get|post|put)\(", line)[0]
if l[0] != '_':
# Introduce a _ before get
line=re.sub(r'((get|post|put|delete))', r'_\1', line)
## Match the start of a function
if (re.match(r'^def', line)):
defn=1
defline=line
#print (ret_filename(line))
# Each line beginning def is a
## Different types of function definitions
# 1. binary_branch_get(branch)
# 2. binary_branch_post(body, branch)
# 3. clinic_city_city_get(city):
# 4. clinic_get():
## This is to handle mangled def with get|post. Example, idget instead of id_get
l=[]
try:
l=re.findall(r"(\w)(get|post|put)\(", line)[0]
if l[0] != '_':
# Introduce a _ before get
line=re.sub(r'((get|post|put))', r'_\1', line)
except:
l=[]
#print ("Line is " + line)
try:
newfile,operation,args=ret_filename(line)
#print (line,newfile,operation,args)
except:
print (sys.exc_info())
continue
#print ("Operation is " + operation)
## Get Arguments to the function
result=line.partition('(')[-1].rpartition(')')[0]
## Get the function name
impFn=line.split('(')[0].replace('def ', '')
## Construct calling new function
##from swagger_server.controllers.users import user_users_post
if operation == '_post':
importlocal=" from swagger_server.controllers." + newfile + " import " + impFn + "\n"
#functionCall=" return(" + impFn + "(" + result + "))\n"
functionCall=" res=" + impFn + "(" + result + ")\n" + resLogicPost
else:
operation = '_get'
importlocal="from swagger_server.controllers." + newfile + " import " + impFn + "\n"
#functionCall=" return(" + impFn + "(" + result + "))\n"
functionCall="res=" + impFn + "(" + result + ")\n" + resLogicGet
## New logic
## if connexion.request.is_json
#importlocal=" from swagger_server.controllers." + newfile + " import " + impFn + "\n"
#functionCall=" " + impFn + "(" + result + ")\n"
## Controller file
controllerfile=folderstg + '/' + newfile + '.py'
"""
## Check if new file exists
if os.path.exists(controllerfile):
## Check if operation exists
if line in open(controllerfile).read():
print("controller file Here " + controllerfile + " exists and no changes for " + line)
else:
print ("Writing line " + line + "to file " + controllerfile)
## Create stub
createStub(controllerfile,operation, line)
else:
## Create file and stub
createStub(controllerfile,operation, line)
"""
fStgDefaultController.write(line)
#elif (re.match(r'return \'do some magic!\'',line)):
## new Only POST has request.is_json in it
elif ('if connexion.request.is_json' in line):
post=1
## THis means it is post
importlocal=" from swagger_server.controllers." + newfile + " import " + impFn + "\n"
#functionCall=" return(" + impFn + "(" + result + "))\n"
functionCall=" res=" + impFn + "(" + result + ")\n" + resLogicPost
controllerfile=folderstg + '/' + newfile + '.py'
operation="_post"
newline=defline + '\n' + line
## Check if new file exists
if os.path.exists(controllerfile):
## Check if operation exists
if line in open(controllerfile).read():
print("controller file " + controllerfile + " exists and no changes for " + line)
else:
## Create stub
createStub(controllerfile,operation, newline)
else:
## Create file and stub
createStub(controllerfile,operation, newline)
fStgDefaultController.write(line)
elif ('return \'do some magic!\'' in line):
if post == 0:
importlocal=" from swagger_server.controllers." + newfile + " import " + impFn + "\n"
#functionCall=" return(" + impFn + "(" + result + "))\n"
functionCall=" res=" + impFn + "(" + result + ")\n" + resLogicGet
## Controller file
controllerfile=folderstg + '/' + newfile + '.py'
operation='_get'
newline=defline + '\n' + line
## Check if new file exists
if os.path.exists(controllerfile):
## Check if operation exists
if line in open(controllerfile).read():
print("controller file here1 " + controllerfile + " exists and no changes for " + newline)
else:
print ("post " + str(post), " def " + str(defn), "defline " + newline)
## Create stub
createStub(controllerfile,operation, newline)
else:
## Create file and stub
print ("Writing line " + line + "to file " + controllerfile)
createStub(controllerfile,operation, newline)
fStgDefaultController.write(line)
try:
fStgDefaultController.write(importlocal)
fStgDefaultController.write(functionCall)
except:
fStgDefaultController.write(line)
defn=0
post=0
else:
fStgDefaultController.write(line)
#defn=0
#post=0
fStgDefaultController.close()