-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppserver.py
executable file
·270 lines (222 loc) · 9.71 KB
/
ppserver.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
#!/usr/bin/env python3
# ppserver.py, a part of PR-PR (previously known as PaR-PaR), a biology-friendly language for liquid-handling robots
# Author: Nina Stawski, [email protected], [email protected]
# Copyright 2012-2013, Lawrence Berkeley National Laboratory
# http://github.com/JBEI/prpr/blob/master/license.txt
__author__ = 'Nina Stawski'
__version__ = '1.1'
import bottle
from bottle import *
import os
from prparser import *
from tempfile import TemporaryFile
import glob
import importlib
global robotTips
global maxAm
robotTips = 8
maxAm = 150
@route('/')
def prpr():
human = __import__('prpr_human')
languages = list(human.PRPR.dictionary)
return template('pages' + os.sep + 'page.html', file='', btn='', text='', alerterror=[], alertsuccess=[], tables=GetDefaultTables(), selected='tecan', version=__version__, languages = languages)
@post('/preview')
def preview():
a = {'response' : 'error', 'config' : ['1', '2'], 'somethingelse' : {'somethign' : 'else'}}
return json_dumps(a)
@route('/dev')
def dev():
return template('pages' + os.sep + 'page_dev.html', version=__version__)
@route('/mf')
def mf():
return template('pages' + os.sep + 'dev-mf.html', version=__version__)
@route('/disclaimer')
def disclaimer():
return template('pages' + os.sep + 'disclaimer.html', version=__version__)
@route('/copyright')
def copyright_():
return template('pages' + os.sep + 'copyright.html', version=__version__)
@post('/table')
def table():
plateIndexes = {}
plateNicknames = {}
experiment = False
tablename = request.body.read().decode()
tabledirname = 'default_tables' + os.sep
plateFile = open(tabledirname + tablename, "r")
PlateFileParse(plateFile, experiment, plateNicknames, plateIndexes)
plates = []
for key in plateIndexes.keys():
plates.append([key, [plateIndexes[key][0], plateIndexes[key][1]], plateNicknames[key]])
tojs = json_dumps(plates)
return tojs
@post('/plates')
def plates():
a = request.body.read().decode().split('\n')[2:-2]
plateFile = TemporaryFile(mode='r+')
plateFile.writelines(a)
plateFile.seek(0)
plateIndexes = {}
plateNicknames = {}
experiment = False
PlateFileParse(plateFile, experiment, plateNicknames, plateIndexes)
plates = []
for key in plateIndexes.keys():
plates.append([key, [plateIndexes[key][0], plateIndexes[key][1]], plateNicknames[key]])
tojs = json_dumps(plates)
return tojs
@post('/mfplates')
def mfplates():
position = request.forms.get('position', '')
wells = request.forms.get('wells', '')
mywells = '\n'.join(json_loads(wells))
tablename = createMFPlate(mywells, position)
return tablename
def createMFPlate(wells, position):
directory = 'tables'
mydata = position + '\n' + wells
fileCounter = len(glob.glob1(directory, "tables_mf_*"))
tablename = 'tables_mf_' + str(fileCounter) + '.mfp'
tablefile = open(directory + os.sep + tablename, "wb")
tablefile.write(bytes(mydata, 'UTF-8'))
tablefile.close()
return tablename
@post('/mfparse')
def mfparse():
info = request.body.read().decode()
if info[-3:] == 'mfp':
tabledirname = 'default_tables' + os.sep
with open(tabledirname + info, "r") as plateFile:
a = plateFile.readlines()
else:
a = request.body.read().decode().split('\n')[4:-2]
a[-1] = a[-1].strip()
tojs = json_dumps(a)
return tojs
@post('/sample')
def sample():
platform = request.body.read().decode()
configFile = 'prpr_sample_' + platform + '.par'
config = open('samples/' + configFile, 'r')
return config.readlines()
@post('/getMethods')
def getMethods():
db = DBHandler()
methods = db.getMethods()
return json_dumps(methods)
@post('/getPlates')
def getPlates():
db = DBHandler()
plates = db.getPlates()
return json_dumps(plates)
@post('/getconfig')
def config():
human = __import__('prpr_human')
languages = list(human.PRPR.dictionary)
errorList = []
successList = []
platform = request.forms.get('deviceselect', '').strip()
getconfig = request.forms.get('text', '').strip()
language = request.forms.get('language', '').strip()
customMethods = request.forms.get('methods', '').strip().split(',')
if getconfig != '':
db = DBHandler()
global experiment
if customMethods != ['']:
experiment = Experiment(maxVolume=150, tips=8, db=db, platform=platform, userMethods=customMethods, language=language)
else:
experiment = Experiment(maxVolume=150, tips=8, platform=platform, db=db, language=language)
expID = experiment.ID
print('platform:', platform)
if platform == 'tecan':
preselected = request.forms.get('tableselect')
data = request.files.data
if data != '':
fileExtension = data.filename[-3:]
raw = data.file.read()
tablename = 'tables' + os.sep + 'tables_' + expID + '.' + fileExtension
tablefile = open(tablename, "wb")
tablefile.write(raw)
tablefile.close()
else:
if preselected != 'select':
tablename = 'default_tables' + os.sep + preselected
else:
errorList.append("Please select or upload the table file for your configuration script.")
return template('pages' + os.sep + 'page.html', file='', btn='', text=getconfig, alerterror=errorList, alertsuccess=successList,tables=GetDefaultTables(), selected=platform, version=__version__)
elif platform == 'microfluidics':
preselected = request.forms.get('mftableselect')
#note: if the platform is microfluidics
if preselected != 'select':
tablename = 'default_tables' + os.sep + preselected
else:
position = request.forms.get('position', '')
wells = '\n'.join(request.forms.get('wells', '').strip().split(';'))
tablename = 'tables' + os.sep + createMFPlate(wells, position)
dirname = 'incoming' + os.sep
filename = 'config_' + expID + '.par'
writefile = open(dirname + filename, "w", encoding="utf-8")
if getconfig.startswith('TABLE'):
getconfig = '\n'.join(getconfig.split('\n')[1:]) #removing the extra 'TABLE' from the config file
if (platform != 'human') and (platform != 'microscope'):
list_ = ['TABLE ', tablename, '\n', '\n', getconfig] #adding the chosen/uploaded table to the config file.
else:
list_ = getconfig
writefile.writelines(''.join(list_))
writefile.close()
readfile = open(dirname + filename, "r", encoding="utf-8")
platform = __import__('prpr_'+ experiment.platform)
ParseFile(readfile, experiment)
if len(experiment.errorLogger):
for item in experiment.errorLogger:
errorList.append(item)
return template('pages' + os.sep + 'page.html', file='', btn='btn-success', text=getconfig, alerterror=errorList, alertsuccess=successList, tables=GetDefaultTables(), selected=platform, version=__version__, languages=languages)
elif experiment.testindex:
if experiment.platform == 'human':
platformFileExtension = 'txt'
elif experiment.platform == 'microscope':
platformFileExtension = 'py'
else:
platformFileExtension = platform.defaults.fileExtensions[tablename[-3:]]
file = 'config' + str(expID) + '.' + platformFileExtension
prpr = platform.PRPR(experiment.ID)
log = 'experiment' + str(expID) + '.log'
successList.append("Your configuration file has been successfully processed.")
return template('pages' + os.sep + 'page.html', file=file, btn='btn-success', text=getconfig, alerterror=errorList, alertsuccess=successList, tables=GetDefaultTables(), selected=platform, version=__version__, languages=languages)
else:
errorList.append("Your configuration file doesn't contain any actions. Please refer to PR-PR howto guide.")
return template('pages' + os.sep + 'page.html', file='', btn='btn-success', text=getconfig, alerterror=errorList, alertsuccess=successList, tables=GetDefaultTables(), selected=platform, version=__version__, languages=languages)
else:
errorList.append("Your configuration file doesn't contain any actions. Please refer to PR-PR howto guide.")
return template('pages' + os.sep + 'page.html', file='', btn='btn-success', text=getconfig, alerterror=errorList, alertsuccess=successList, tables=GetDefaultTables(), selected=platform, version=__version__, languages=languages)
@route('/static/:path#.+#', name='static')
def static(path):
return static_file(path, root='static')
@route('/download/<filename>')
def download(filename):
if filename.startswith('config'):
root = 'esc'
elif filename.startswith('tables_mf_'):
root = 'tables'
else:
root = 'logs'
return static_file(filename, root, download=filename)
@route('/get/<filename>')
def download(filename):
return static_file(filename, root='tables', download=filename)
def GetDefaultTables():
"""
Getting a list of default tables from folder 'tables'
"""
tablesDir = os.listdir('default_tables')
tables = []
for name in tablesDir:
if not name.startswith('.') and (name.endswith('.ewt') or name.endswith('.gem') or name.endswith('.mfp')):
tables.append(name)
tables.sort()
jsonTables = json_dumps(tables)
return jsonTables
if __name__ == '__main__':
bottle.debug(True)
run(host='localhost', port=8080, reloader=True)