/
file_browser.py
651 lines (576 loc) · 24.8 KB
/
file_browser.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
'''
APIHandler documentation:
https://github.com/jupyter/notebook/blob/master/notebook/base/handlers.py#L583
Note APIHandler.finish() sets Content-Type handler to 'application/json'
Use finish() for json, write() for text
'''
from notebook.base.handlers import APIHandler
import json, os
import logging
from tornado import web
from shutil import move, rmtree
from .util.stochss_errors import StochSSAPIError
from .util.ls import ls
from .util.convert_to_notebook import convert_to_notebook
from .util.duplicate import duplicate, duplicate_wkfl_as_new
from .util.rename import rename
from .util.convert_to_smdl_mdl import convert_model
from .util.convert_to_sbml import convert_to_sbml
from .util.convert_sbml_to_model import convert_sbml_to_model
from .util.generate_zip_file import download_zip
from .util.upload_file import upload
log = logging.getLogger('stochss')
class ModelBrowserFileList(APIHandler):
'''
##############################################################################
Handler for interacting with the User's File Browser
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Reads the content of a directory and returns a jstree dictionary.
Attributes
----------
path : str
Path from the user directory to the target directory.
'''
log.info("Path to the directory: {0}".format(path))
try:
output = ls(path)
log.debug("Contents of the directory: {0}".format(output))
self.write(output)
except StochSSAPIError as err:
self.set_status(err.status_code)
self.set_header('Content-Type', 'application/json')
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class ModelToNotebookHandler(APIHandler):
'''
##############################################################################
Handler for handling conversions from model (.mdl) file to Jupyter Notebook
(.ipynb) file.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Runs the convert_to_notebook function from the convert_to_notebook script
to build a Jupyter Notebook version of the model and write it to a file.
Attributes
----------
path : str
Path from the user directory to the target model.
'''
self.set_header('Content-Type', 'application/json')
try:
log.debug("Path to the model file: {0}".format(path))
dest_file = convert_to_notebook(path)
log.debug("Notebook file path: {0}".format(dest_file))
self.write(dest_file)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class DeleteFileAPIHandler(APIHandler):
'''
##############################################################################
Handler for removing files and/or directoies from the User's file system.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Removes a single file or recursively remove a directory and its contents.
Attributes
----------
path : str
Path to removal target.
'''
file_path = os.path.join('/home/jovyan', path)
log.debug("Deleting path: {0}".format(file_path))
try:
try:
os.remove(file_path)
except OSError:
rmtree(file_path)
self.write("The file {0} was successfully deleted.".format(path.split('/').pop()))
except FileNotFoundError as err:
self.set_status(404)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"StochSS File Not Found","Message":"Could not find the file or directory: {0}".format(err)}
self.write(error)
except PermissionError as err:
self.set_status(403)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"Permission Denied","Message":"You do not have permission to delete this file: {0}".format(err)}
self.write(error)
self.finish()
class MoveFileAPIHandler(APIHandler):
'''
##############################################################################
Handler moving file locations in the User's file system.
##############################################################################
'''
@web.authenticated
async def get(self, data):
'''
Moves a file or a directory and its contents to a new location.
Attributes
----------
data : str
Data string containing old and new locations of target file.
'''
user_dir = "/home/jovyan/"
log.debug("File path and dest path: {0}".format(data))
old_path = os.path.join(user_dir, "{0}".format(data.split('/<--MoveTo-->/')[0]))
log.debug("Path to the file: {0}".format(old_path))
new_path = os.path.join(user_dir, "{0}".format(data.split('/<--MoveTo-->/').pop()))
log.debug("Destination path: {0}".format(new_path))
try:
if os.path.isdir(old_path):
move(old_path, new_path)
# If directory is wkfl and has been started, update wkfl model path
if old_path.endswith('.wkfl') and "RUNNING" in os.listdir(path=new_path):
old_parent_dir = os.path.dirname(old_path)
new_parent_dir = os.path.dirname(new_path)
with open(os.path.join(new_path, "info.json"), "r+") as info_file:
info = json.load(info_file)
info['model'] = info['model'].replace(old_parent_dir, new_parent_dir)
info_file.seek(0)
json.dump(info, info_file)
info_file.truncate()
else:
os.rename(old_path, new_path)
self.write("Success! {0} was moved to {1}.".format(old_path, new_path))
except FileNotFoundError as err:
self.set_status(404)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"StochSS File Not Found","Message":"Could not find the file or directory: {0}".format(err)}
self.write(error)
except PermissionError as err:
self.set_status(403)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"Permission Denied","Message":"You do not have permission to move this file: {0}".format(err)}
self.write(error)
self.finish()
class DuplicateModelHandler(APIHandler):
'''
##############################################################################
Handler for creating copies of a file.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Creates a copy of a file with a unique name and stores it in the same
directory as the original.
Attributes
----------
path : str
Path to target model.
'''
self.set_header('Content-Type', 'application/json')
log.debug("Copying file: {0}".format(path))
try:
resp = duplicate(path)
log.debug("Response message: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class DuplicateDirectoryHandler(APIHandler):
'''
##############################################################################
Handler for creating copies of a directory and its contents.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Creates a copy of a directory and its contents with a unique name and
stores it in the same parent directory as the original.
Attributes
----------
path : str
Path to target directory.
'''
self.set_header('Content-Type', 'application/json')
log.debug("Path to the file: {0}".format(path))
try:
resp = duplicate(path, True)
log.debug("Response message: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class RenameAPIHandler(APIHandler):
'''
##############################################################################
Handler for renaming files or directories.
##############################################################################
'''
@web.authenticated
async def get(self, data):
'''
Rename a file or directory. If the file or directory already exists
increment the file name using '(x)' naming convention to get a unique
name.
Attributes
----------
data : str
string of data containing rename information.
'''
log.debug("Renaming file: {0}".format(data))
path, new_name = data.split('/<--change-->/')
log.debug("Path to the file or directory: {0}".format(path))
log.debug("New filename: {0}".format(new_name))
self.set_header('Content-Type', 'application/json')
try:
resp = rename(path, new_name)
log.debug("Response message: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class ConvertToSpatialAPIHandler(APIHandler):
'''
##############################################################################
Handler for converting a model to a spatial model.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Creates a spatial model file with a unique name from a model file and
stores it in the same directory as the original.
Attributes
----------
path : str
Path from the user directory to the model.
'''
log.debug("Converting non-spatial model to spatial model: {0}".format(path))
self.set_header('Content-Type', 'application/json')
try:
resp = convert_model(path, to_spatial=True)
log.debug("Response: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class ConvertToModelAPIHandler(APIHandler):
'''
##############################################################################
Handler for converting a spatial model to a model.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Creates a model file with a unique name from a spatial model file and
stores it in the same directory as the original.
Attributes
----------
path : str
Path from the user directory to the spatial model.
'''
log.debug("Converting spatial model to non-spatial model: {0}".format(path))
self.set_header('Content-Type', 'application/json')
try:
resp = convert_model(path, to_spatial=False)
log.debug("Response: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class ModelToSBMLAPIHandler(APIHandler):
'''
##############################################################################
Handler for converting a StochSS model to a SBML model.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Create a SBML Model file with a unique name from a model file and
store it in the same directory as the original.
Attributes
----------
path : str
Path from the user directory to the target model file.
'''
self.set_header('Content-Type', 'application/json')
log.debug("Converting to SBML: {0}".format(path))
try:
resp = convert_to_sbml(path)
log.debug("Response: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class SBMLToModelAPIHandler(APIHandler):
'''
##############################################################################
Handler for converting a SBML model to a StochSS model.
##############################################################################
'''
async def get(self, path):
'''
Creates a StochSS model with a unique name from an sbml model and
store it in the same directory as the original.
Attributes
----------
path : str
Path from the user directory to the target sbml file.
'''
log.setLevel(logging.DEBUG)
log.debug("Converting SBML: {0}".format(path))
template_path ='/stochss/model_templates/nonSpatialModelTemplate.json'
log.debug("Using model template: {0}".format(template_path))
with open(template_path, "r") as template_file:
model_template = template_file.read()
log.debug("Model template: {0}".format(model_template))
self.set_header('Content-Type', 'application/json')
try:
resp = convert_sbml_to_model(path, model_template)
log.debug("Response: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
log.setLevel(logging.WARNING)
self.finish()
class DownloadAPIHandler(APIHandler):
'''
##############################################################################
Handler for downloading plain text files to the users download directory.
##############################################################################
'''
@web.authenticated
async def get(self, path):
'''
Read and return plain text files.
Attributes
----------
path : str
Path from the user directory to the target sbml file.
'''
log.debug("Path to the model: {0}".format(path))
full_path = os.path.join("/home/jovyan", path)
log.debug("Path to the model on disk: {0}".format(full_path))
try:
with open(full_path, 'r') as f:
resp = f.read()
self.write(resp)
except FileNotFoundError as err:
self.set_status(404)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"File Not Found","Message":"Could not find file: " + str(err)}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class DownloadZipFileAPIHandler(APIHandler):
'''
##############################################################################
Handler for downloading zip files to the users download directory.
##############################################################################
'''
@web.authenticated
async def get(self, action, path):
'''
Read and download a zip file or generate a zip file from a directory or
file and then download. Generated zip files are stored in the targets
parent directory with a unique name.
Attributes
----------
path : str
Path from the user directory to the target file or directory.
'''
log.debug("Path to the model: {0}".format(path))
log.debug("Action: {0}".format(action))
if action == "download":
file_name = "{0}.zip".format(path.split('/').pop().split('.')[0])
log.debug("Name of the download file: {0}".format(file_name))
self.set_header('Content-Type', 'application/zip')
self.set_header('Content_Disposition', 'attachment; filename="{0}"'.format(file_name))
else:
self.set_header('Content-Type', 'application/json')
try:
resp = download_zip(path, action)
log.debug("Response: {0}\n".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
if action == "download":
self.set_header('Content-Type', 'application/json')
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class CreateDirectoryHandler(APIHandler):
'''
##############################################################################
Handler for creating a new directory or path of directories.
##############################################################################
'''
@web.authenticated
async def get(self, directories):
'''
Creates a single directory or if the path od directories contains other
directories that do not exist, a path of directories.
Attributes
----------
directories : str
Directory or path of directories to be created if needed.
'''
log.debug("Path of directories: {0}".format(directories))
full_path = os.path.join("/home/jovyan", directories)
log.debug("Full path of directories: {0}".format(full_path))
try:
os.makedirs(full_path)
self.write("{0} was successfully created!".format(directories))
except FileExistsError as err:
self.set_status(406)
self.set_header('Content-Type', 'application/json')
error = {"Reason":"Directory Already Exists","Message":"Could not create your directory: "+str(err)}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class UploadFileAPIHandler(APIHandler):
'''
##############################################################################
Handler for uploading files.
##############################################################################
'''
@web.authenticated
async def post(self):
'''
Uploads the target file to the target directory. If the intended file
type is a StochSS Model or SBML Model, the original file is uploaded
with a converted model. If the file can't be uploaded to the intended
type no conversion is made and errors are sent to the user.
Attributes
----------
'''
log.setLevel(logging.DEBUG)
file_data = self.request.files['datafile'][0]
file_info = json.loads(self.request.body_arguments['fileinfo'][0].decode())
log.debug(file_info['type'])
log.debug(file_info['path'])
if file_info['name']:
log.debug(file_info['name'])
else:
log.debug("No name given")
file_name = file_data['filename']
log.debug(file_name)
log.debug(type(file_data['body']))
body = file_data['body']
resp = upload(file_data, file_info)
log.debug(resp)
self.write(json.dumps(resp))
log.setLevel(logging.WARNING)
self.finish()
class DuplicateWorkflowAsNewHandler(APIHandler):
'''
##############################################################################
Handler for duplicating a workflow as new and a workflows model.
##############################################################################
'''
@web.authenticated
async def get(self, target, time_stamp, path):
'''
Creates a duplicate of the model in the target workflow in its parent
directory. Creates a new workflow that uses the same model and has
the same type as the target workflow in its parent directory.
Attributes
----------
path : str
Path from the user directory to the target workflow.
'''
log.debug("Path to the workflow: {0}".format(path))
log.debug("The {0} is being copied".format(target))
if time_stamp == "None":
time_stamp = None
else:
log.debug("The time stamp for the new workflow: {0}".format(time_stamp))
only_model = target == "wkfl_model"
log.debug("only_model flag: {0}".format(only_model))
self.set_header('Content-Type', 'application/json')
try:
resp = duplicate_wkfl_as_new(path, only_model, time_stamp)
log.debug("Response: {0}".format(resp))
self.write(resp)
except StochSSAPIError as err:
self.set_status(err.status_code)
error = {"Reason":err.reason,"Message":err.message}
log.error("Exception information: {0}".format(error))
self.write(error)
self.finish()
class GetWorkflowModelPathAPIHandler(APIHandler):
'''
##############################################################################
Handler for getting the path to the workflow model.
##############################################################################
'''
async def get(self, path):
'''
Returns the path to the model used for the workflow to allow the user
to edit the model. If the model doesn't exist a 404 response is sent.
Attributes
----------
path : str
Path from the user directory to the target workflow.
'''
from json.decoder import JSONDecodeError
user_dir = "/home/jovyan"
self.set_header('Content-Type', 'application/json')
log.debug("The path to the workflow: {0}".format(path))
full_path = os.path.join(user_dir, path, "info.json")
log.debug("The full path to the workflow's info file: {0}".format(full_path))
try:
with open(full_path, "r") as info_file:
info = json.load(info_file)
log.debug("Workflow info: {0}".format(info))
model_path = info['model']
log.debug("Path to the workflow's model: {0}".format(model_path))
resp = {"file":model_path.replace(user_dir + "/", "")}
if not os.path.exists(os.path.join(user_dir, model_path)):
mdl_file = model_path.split('/').pop()
resp['error'] = "The model file {0} could not be found. To edit the model you will need to extract the model from the workflow or open the workflow and update the path to the model.".format(mdl_file)
log.debug("Response: {0}".format(resp))
self.write(resp)
except FileNotFoundError as err:
self.set_status(404)
error = {"Reason":"Workflow Info File Not Found","Message":"Could not find the workflow's info file: "+str(err)}
log.error(error)
self.write(error)
except JSONDecodeError as err:
self.set_status(404)
error = {"Reason":"Workflow Info File Not JSON Format","Message":"The workflow info file is not JSON decodable: "+str(err)}
log.error(error)
self.write(error)
self.finish()