-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathExport_Tubes.py
286 lines (244 loc) · 8.97 KB
/
Export_Tubes.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
# -*- coding: utf-8 -*-
'''
Export GUI
Description: This script will write the exported tubes into a csv file so that
tubes can be tracked as they are exported to UMich. This script also checks
if one of the tubes have not passed a test. It will notify the user that one
of the tests has not been passed.
To run, first install Tkinter and pickle modules into your python library. After
that, running is as simple as: python "Export_Tubes.py" or double clicking in
Windows 10.
@author: Jason Gombas
'''
import tkinter as tk
from tkinter import StringVar
import os
from datetime import datetime
import pickle
import time
import sys
DROPBOX_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(DROPBOX_DIR)
from sMDT import db, tube
from sMDT.data import swage
from sMDT.data.status import Status
database = db.db()
# Returns a list of the status of the tube, in this order:
# swage status, tension status, leak status, dark current status
def getStatus(code):
#raise NotImplementedError
#return 1 # for now, this is not implemented correctly
try:
tube1 = database.get_tube(code)
sStatus = tube1.swage.status()
tStatus = tube1.tension.status()
lStatus = tube1.leak.status()
dStatus = tube1.dark_current.status()
return [sStatus, tStatus, lStatus, dStatus]
except KeyError:
return [Status.INCOMPLETE,Status.INCOMPLETE,Status.INCOMPLETE,Status.INCOMPLETE]
# Returns a list of the data of the tube, in this order:
# first tension date, last tension, last tension date, tension frequency, dark current
def getData(code):
data = [None, None, None, None, None]
try:
database = db.db()
tube1 = database.get_tube(code)
try:
tRecord_first = tube1.tension.get_record()
tRecord_last = tube1.tension.get_record()
data[0] = tRecord_first.date
data[1] = tRecord_last.tension
data[2] = tRecord_last.date
data[3] = tRecord_last.frequency
except:
data[0] = None
try:
data[4] = tube1.dark_current.get_record().dark_current
except:
data[4] = None
return data
except KeyError:
return data
# Returns booleans to indicate which tests failed
def getFailedTests(code):
try:
tube1 = database.get_tube(code)
sfail = tube1.swage.fail()
tfail = tube1.tension.fail()
lfail = tube1.leak.fail()
dfail = tube1.dark_current.fail()
return sfail,tfail,lfail,dfail
except KeyError:
return True,True,True,True
# Returns a list of barcodes found in the text
def textToList(barcodeList):
if '\n' in barcodeList:
barcodeList.replace('\n','')
lenBarcodes = int(len(barcodeList)/8)
barcodes = [barcodeList[i*8:i*8+8] for i in range(lenBarcodes)]
return list(filter(lambda a: a != '', barcodes))
# Check if one of the codes in the text is bad and display it
def checkCodes(Event):
barcodeList = text_list.get('1.0',tk.END)[0:-1]
if len(barcodeList)%8!=0: return
text_serrors.config(state=tk.NORMAL)
text_serrors.delete("1.0", tk.END)
text_terrors.config(state=tk.NORMAL)
text_terrors.delete("1.0", tk.END)
text_lerrors.config(state=tk.NORMAL)
text_lerrors.delete("1.0", tk.END)
text_derrors.config(state=tk.NORMAL)
text_derrors.delete("1.0", tk.END)
barcodes = textToList(barcodeList)
text_serrors.insert(tk.INSERT, 'Failed:\n')
text_terrors.insert(tk.INSERT, 'Failed:\n')
text_lerrors.insert(tk.INSERT, 'Failed:\n')
text_derrors.insert(tk.INSERT, 'Failed:\n')
for tube in barcodes:
sfail, tfail, lfail, dfail = getFailedTests(tube)
if sfail:
text_serrors.insert(tk.INSERT, tube)
if tfail:
text_terrors.insert(tk.INSERT, tube)
if lfail:
text_lerrors.insert(tk.INSERT, tube)
if dfail:
text_derrors.insert(tk.INSERT, tube)
text_serrors.insert(tk.INSERT, '\n\nIncompl:\n')
text_terrors.insert(tk.INSERT, '\n\nIncompl:\n')
text_lerrors.insert(tk.INSERT, '\n\nIncompl:\n')
text_derrors.insert(tk.INSERT, '\n\nIncompl:\n')
for tube in barcodes:
status = getStatus(tube)
if status[0] == Status.INCOMPLETE:
text_serrors.insert(tk.INSERT, tube)
if status[1] == Status.INCOMPLETE:
text_terrors.insert(tk.INSERT, tube)
if status[2] == Status.INCOMPLETE:
text_lerrors.insert(tk.INSERT, tube)
if status[3] == Status.INCOMPLETE:
text_derrors.insert(tk.INSERT, tube)
text_serrors.config(state=tk.DISABLED)
text_terrors.config(state=tk.DISABLED)
text_lerrors.config(state=tk.DISABLED)
text_derrors.config(state=tk.DISABLED)
# Write data for tubes to disk
def write(name, barcodeList):
#raise NotImplementedError
filename = DROPBOX_DIR + "\\Exported_Tubes\\" + f"{datetime.now().strftime('%m.%d.%Y_%H_%M_%S.csv')}"
filename = "Exported_Tubes/" + f"{datetime.now().strftime('%m.%d.%Y_%H_%M_%S.csv')}"
f = open(filename,'w')
f.write("Logger,Barcode,First Tension Date,Final Tension Measurement,Final Tension Date,Frequency,Dark Current\n")
barcodes = textToList(barcodeList)
badTubeList = []
for code in barcodes:
data = getData(code)
f.write(f"{name},{code},{data[0]},{data[1]},{data[2]},{data[3]},{data[4]}\n")
return badTubeList
#######################################
##### Submit Codes to Export ########
#######################################
def handle_enter(event):
text_serrors.config(state=tk.NORMAL)
text_serrors.delete("1.0", tk.END) # Turn text editing on and clear the textbox
text_terrors.config(state=tk.NORMAL)
text_terrors.delete("1.0", tk.END)
text_lerrors.config(state=tk.NORMAL)
text_lerrors.delete("1.0", tk.END)
text_derrors.config(state=tk.NORMAL)
text_derrors.delete("1.0", tk.END)
entry_name.config({"background": "White"})
if entry_name.get() == '':
entry_name.config({"background": "Red"})
else:
name = entry_name.get()
barcodeList = text_list.get('1.0',tk.END)[0:-1]
write(name, barcodeList)
text_list.delete("1.0", tk.END)
text_serrors.insert('1.0',"Tubes \nwritten")
text_serrors.config(state=tk.DISABLED)
def handle_update(event):
database = db.db()
window = tk.Tk()
window.title("Export Log GUI")
window.columnconfigure(0, weight=1, minsize=75)
window.rowconfigure(0, weight=1, minsize=50)
########### Generate Frames ###############
frame_entry = tk.Frame(
master=window,
width = 20,
relief=tk.RAISED,
borderwidth=1
)
frame_entry.grid(row=0,column=0)
frame_error = tk.Frame(master=window)
frame_error.grid(row=0,column=1)
########### Entry Frame ###############
label_name = tk.Label(master=frame_entry, text="Name")
entry_name = tk.Entry(master=frame_entry)
label_list = tk.Label(master=frame_entry, text='List of Tubes')
text_list = tk.Text(master=frame_entry,
width=8,
height=30)
text_list.bind('<KeyRelease>',checkCodes)
button = tk.Button(
master=frame_entry,
text="Submit Tubes",
width=20,
height=2,
bg="blue",
fg="yellow",
)
button.bind("<Button-1>", handle_enter)
update_button = tk.Button(
master=frame_entry,
text="Update Database",
width=20,
height=2,
bg="blue",
fg="yellow",
)
update_button.bind("<Button-1>", handle_update)
############ Swage Error Frame ################
label_serrors = tk.Label(master=frame_error, text='Swage \n Errors')
text_serrors = tk.Text(master=frame_error,
width=8,
height=30)
text_serrors.config(state=tk.DISABLED)
############ Tension Error Frame ################
label_terrors = tk.Label(master=frame_error, text='Tension \n Errors')
text_terrors = tk.Text(master=frame_error,
width=8,
height=30)
text_terrors.config(state=tk.DISABLED)
############ Leak Rate Error Frame ################
label_lerrors = tk.Label(master=frame_error, text='Leak Rate \n Errors')
text_lerrors = tk.Text(master=frame_error,
width=8,
height=30)
text_lerrors.config(state=tk.DISABLED)
############ Dark Current Error Frame ################
label_derrors = tk.Label(master=frame_error, text='Dark C\n Errors')
text_derrors = tk.Text(master=frame_error,
width=8,
height=30)
text_derrors.config(state=tk.DISABLED)
############ Pack Everything Together ##########
entry = tk.Entry()
update_button.pack()
label_name.pack()
entry_name.pack()
label_list.pack()
text_list.pack()
label_serrors.grid(row=0,column=0)
text_serrors.grid(row=1,column=0)
label_terrors.grid(row=0,column=1)
text_terrors.grid(row=1,column=1)
label_lerrors.grid(row=0,column=2)
text_lerrors.grid(row=1,column=2)
label_derrors.grid(row=0,column=3)
text_derrors.grid(row=1,column=3)
button.pack()
# Execute mainloop
window.mainloop()