-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
285 lines (240 loc) · 13.2 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
from fastapi import FastAPI, HTTPException, Body, Header, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, StreamingResponse
from starlette.background import BackgroundTask # Starlette underlies FastAPI
import os, shutil
import requests as r
import json
from requests import get
from urllib.parse import urlparse, parse_qsl, urlencode, urlunparse
import base64
import tempfile
import urllib.request
import zipfile
import traceback
from pathlib import Path
from zipfile import ZipFile
from api_helpers import *
from project_chameleon.rheedconverter import rheedconverter
from project_chameleon.brukerrawbackground import brukerrawbackground
from project_chameleon.brukerrawconverter import brukerrawconverter
from project_chameleon.mbeparser import mbeparser
from project_chameleon.non4dstem import non4dstem
from project_chameleon.stemarray4d import stemarray4d
from project_chameleon.ppmsmpms import ppmsmpmsparser
from project_chameleon.arpes import arpes_folder_workbook
from project_chameleon.hs2converter import hs2converter
from project_chameleon.rheed_video_converter import rheed_video_converter
from project_chameleon.jeol_sem_converter import sem_base_plot
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post('/rheedconverter')
async def rheed_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.img', '.png')
try:
rheedconverter(input_file, output_file)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "rheedconverter"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/rheed_video_converter')
def rheed_vide_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.imm', '.avi')
try:
rheed_video_converter(input_file, output_file, '.avi')
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "rheed_video_converter"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/brukerrawconverter')
def brukerraw_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.raw', '.csv')
cps = None
if 'cps' in data:
if data['cps']:
cps = True
else:
cps = False
try:
brukerrawconverter(input_file, output_file, cps)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "brukerrawconverter"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/mbeparser')
def MBE_parser_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_folder,output_folder,output_file = common_folder_handler_parse_request(request, data)
try:
mbeparser(input_folder)
return common_folder_handler_prepare_output(request, data, output_folder, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert folder: an error occurred running the function "mbeparser"')
finally:
common_handler_cleanup_request(request, data, None, input_folder)
@app.post('/non4dstem_folder')
def non4dstem_folder_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_folder,output_folder,output_file = common_folder_handler_parse_request(request, data)
try:
non4dstem(data_folder = input_folder,outputs_folder = output_folder)
return common_folder_handler_prepare_output(request, data, output_folder, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert folder: an error occurred running the function "non4dstem" on a folder')
finally:
common_handler_cleanup_request(request, data, None, input_folder)
@app.post('/non4dstem_file')
def non4dstem_file_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.emd', '.png')
try:
non4dstem(data_file = input_file, output_file = output_file)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "non4dstem" on a file')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/ppmsmpms')
def ppmsmpms_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.dat', '.csv')
if 'value_name' in data:
value = data['value_name']
else:
value = 1
try:
ppmsmpmsparser(input_file, output_file, value)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "ppmsmpmsparser"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/stemarray4d')
def stem4d_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.raw', '') # Output has no file extension, all files are in a folder
try:
stemarray4d(input_file, output_file)
return common_folder_handler_prepare_output(request, data, output_file, media_type='application/zip')
except:
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "stemarray4d"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/arpes_workbook')
def arpes_workbook_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_folder,output_folder,output_file = common_folder_handler_parse_request(request, data)
try:
arpes_folder_workbook(input_folder, output_file)
return common_file_handler_prepare_output(request, data, output_file, media_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert files: an error occurred running the function "arpes_folder_workbook"')
finally:
common_handler_cleanup_request(request, data, None, input_folder)
@app.post('/hs2converter')
async def hs2_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.hs2', '.png')
try:
hs2converter(input_file, output_file)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "hs2converter"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
@app.post('/brukerrawbackground')
def brukerbackground_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.raw', '.csv')
background = secondary_file_handler_parse_request(request, data, '.raw')
try:
brukerrawbackground(background, input_file, output_file)
return common_folder_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "brukerrawbackground"')
finally:
common_handler_cleanup_request(request, data, input_file, None)
if ('background_file_bytes' in data) or ('background_file_url' in data):
os.remove(background)
@app.post('/jeol_sem_converter')
async def jeol_sem_convert_route(request: Request, data: dict = Body(...), access_token: str = Header(default=''), x_auth_access_token: str = Header(default='')):
access_token = common_handler_access_token(request, data, access_token, x_auth_access_token)
er = common_handler_early_response(request, data)
if not (er is None):
return er
common_handler_method_auth_check(request, data, access_token)
input_file,output_file = common_file_handler_parse_request(request, data, '.EMSA', '.png')
try:
sem_base_plot(input_file, output_file)
return common_file_handler_prepare_output(request, data, output_file)
except:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f'Failed to convert file: an error occurred running the function "sem_base_plot"')
finally:
common_handler_cleanup_request(request, data, input_file, None)