-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
322 lines (233 loc) · 9.69 KB
/
api.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
import os
import json
import tempfile
import tornado.web
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.autoreload
from tornado.options import define, options
import renderer
try:
import pymongo as pm
import pymongo.json_util as json_util
except:
print("pymongo not installed; frontend functionaliy not available")
define("port", default=9999, help="run on the given port", type=int)
class App(tornado.web.Application):
"""
Tornado app which serves the API.
"""
def __init__(self,ioloop):
handlers = [(r"/3dmodels",ThreeDModelHandler),
(r"/backgrounds",BackgroundHandler),
(r"/render",RenderHandler),
(r"/renderq",QsubRenderHandler),
(r"/db/([\S]+)/([\S]+)",DataHandler),
(r"/file/([\S]+)/([\S]+)/([\S]+)",FileHandler),
(r"/dicarlocox-3dmodels-v1/(.*)",
tornado.web.StaticFileHandler,
{'path':os.path.join(os.path.dirname(__file__),
"dicarlocox-3dmodels-v1")})
]
settings = dict(debug=True,io_loop=ioloop)
tornado.web.Application.__init__(self, handlers, **settings)
def main():
"""
function which starts up the tornado IO loop and the app.
"""
tornado.options.parse_command_line()
ioloop = tornado.ioloop.IOLoop.instance()
http_server = tornado.httpserver.HTTPServer(App(ioloop))
http_server.listen(options.port)
tornado.autoreload.start()
ioloop.start()
def getQuerySequence(args):
"""
simple REST interface for MongoDB.
"""
querySequence = args.pop('querySequence',None)
if querySequence is None:
querySequence = []
field = args.get('distinct')
if field:
action = 'distinct'
else:
action = args.get('action','find')
assert action in ['find','find_one','count','distinct',"group"]
posargs = ()
kargs = {}
if action in ['find','find_one']:
posargs = (json.loads(args.get('query','{}')),)
fields = args.get('fields',None)
if fields:
kargs['fields'] = json.loads(fields)
elif action == "group":
query = json.loads(args['query'])
keys = json.loads(args['keys'])
initialize = json.loads(args['initialize'])
reduce = args['reduce']
finalize = args.get('finalize',None)
posargs = (keys,query,initialize,reduce)
kargs = {"finalize":finalize}
else:
if args.get('query'):
d = {'action':'find','args':(json.loads(args['query']),)}
querySequence.append(d)
if action == 'distinct':
posargs = (field or args['field'],)
else:
posargs = ()
actionDict = {'action' : action}
if posargs:
actionDict['args'] = posargs
if kargs:
actionDict['kargs'] = kargs
querySequence.append(actionDict)
skip = args.get('skip')
if skip:
querySequence.append({'action':'skip','args':(int(skip),)})
limit = args.get('limit')
if limit:
querySequence.append({'action':'limit','args':(int(limit),)})
else:
querySequence = json.loads(querySequence)
for (i,x) in enumerate(querySequence):
querySequence[i] = (x.get('action'),[x.get('args',()),x.get('kargs',{})])
return querySequence
import gridfs
class FileHandler(tornado.web.RequestHandler):
def get(self,dbname,fsname,filename):
connection = pm.Connection()
db = connection[dbname]
fs = gridfs.GridFS(db,fsname)
fh = fs.get_version(filename)
s = fh.read()
args = self.request.arguments
for k in args:
args[k] = args[k][0]
ct = args.get('Content-Type',"x-gzip")
cd = args.get('Content-Disposition','attachment; filename='+filename)
self.set_header("Content-Type", ct)
self.set_header("Content-Disposition", cd)
self.write(s)
class MongoDBHandler(tornado.web.RequestHandler):
"""
base handler for mongodb access.
"""
def get(self,*args):
self.get_connected(*args)
args = self.request.arguments
for k in args.keys():
args[k] = args[k][0]
args = dict([(str(x),y) for (x,y) in args.items()])
callback = args.pop('callback',None)
querySequence = getQuerySequence(args)
c = self.collection
for (a,[pos,kw]) in querySequence:
c = getattr(c,a)(*pos,**kw)
if isinstance(c,pm.cursor.Cursor):
resp = list(c)
else:
resp = c
if callback:
self.write(callback + '(')
self.write(json.dumps(resp,default = json_util.default))
if callback:
self.write(')')
class DataHandler(MongoDBHandler):
def get_connected(self,dbname,colname):
self.connection = pm.Connection()
self.db = self.connection[dbname]
self.collection = self.db[colname]
class ThreeDModelHandler(MongoDBHandler):
"""
DB handler for model DB
This handler is accessible via get requests on localhost:9999/3dmodels?
"""
def get_connected(self):
self.connection = pm.Connection()
self.db = self.connection['dicarlocox_3dmodels']
self.collection = self.db['3ds_test_images']
class BackgroundHandler(MongoDBHandler):
"""
DB handler for backgrounds DB
This handler is accessible via get requests on localhost:9999/backgrounds?
"""
def get_connected(self):
self.connection = pm.Connection()
self.db = self.connection['dicarlocox_3dmodels']
self.collection = self.db['3d_spherical_backgrounds']
class RenderHandler(tornado.web.RequestHandler):
"""
main renderer handler
This handler is accessible via get requests on localhost:9999/render?
This API allows you to specify N images to be rendered and returned as a .zip archive
Each image consists of one or more 3d models rendered on an HDR background, with various
variance parameters set for each model.
The basic syntax for these get requests is, to get N images:
params_list=[img_1,img_2, ...., img_N]]
where img_i is a dictionary specifying how to render the ith image.
Each such dictionary is of the following form:
{"model_params":[{model_spec_1},{model_spec_2}, ..., {model_spec_n}]
"bg_id" : bg_id_val,
"bg_phi" : bg_phi_val
"bg_psi" : bg_psi_val
"kenv": kenv_val}
where:
a) the "model_spec" elements are dictionaries passing parameters
specifying how to render each model within the image
b) the bg_id parameter specifies which background image to choose.
Optional -- default is to choose randomly.
c) bg_phi, bg_psi are the angles within the background image (which is a HDR).
Optional -- default is 0,0.
d) kenv is the optional vslue for the background lighting, default is 7.
The "model_spec" format is:
{"model_id" : model_id,
"tx" : tx,"ty" : ty, "tz":tz,
"rxy" : rxy
&c}
e.g. all the model-related params saying which model to render, and how.
An example:
http://localhost:9999/render?params_list=[{"model_params":[{"model_id":"MB31635","rxy":3.14}]}]
"""
@tornado.web.asynchronous
def get(self):
args = self.request.arguments
for k in args.keys():
args[k] = args[k][0]
try:
args[k] = json.loads(args[k])
except:
pass
params_list = args.pop('params_list')
self.temp_dir = self.get_tempdir()
self.render(params_list)
def get_tempdir(self):
return tempfile.mkdtemp()
def callback(self):
temp_dir = self.temp_dir
temp_cdir, temp_idir = os.path.split(temp_dir)
os.system('cd ' + temp_cdir + '; tar -czvf ' + temp_idir + '.tar.gz ' + temp_idir)
self.set_header("Content-Type", "x-gzip")
self.set_header("Content-Disposition", "attachment; filename="+temp_idir + '.tar.gz')
outfile = os.path.join(temp_cdir,temp_idir + '.tar.gz')
self.write(open(outfile).read())
os.system('rm -rf ' + temp_dir)
self.finish()
def render(self,params_list):
renderer.render(self.temp_dir,params_list,callback=self.callback)
import hashlib
import numpy as np
def random_id():
return hashlib.sha1(str(np.random.randint(10,size=(32,)))).hexdigest()
class QsubRenderHandler(RenderHandler):
def render(self,params_list):
renderer.render_qsub(self.temp_dir,params_list,callback=self.callback)
def get_tempdir(self):
d = os.path.join('/home/render/render_wd',random_id())
os.mkdir(d)
return d
if __name__ == "__main__":
main()