-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRoboReturns-source.py
301 lines (242 loc) · 11.2 KB
/
RoboReturns-source.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
#!/usr/bin/env python3
from tkinter import *
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk
from openpyxl import Workbook
from openpyxl import load_workbook
import configparser
import requests
import xmltodict
import csv
import os
import datetime
# local modules
import alma
# main program ################################################################
def main(*args):
input_file = gui.openfile()
if input_file == "":
return
# clear tree view and progress bar
gui.clear_tree()
gui.progress_bar["value"] = 0
# check file extension
file_extension = os.path.splitext(input_file)[1]
# if plain text file, convert to XLSX
if file_extension.upper() == ".TXT" or file_extension.upper() == ".CSV":
# set delimiters
if file_extension.upper() == ".TXT":
delimiter = "\t"
if file_extension.upper() == ".CSV":
delimiter = ","
# convert to XLSX
with open(input_file, newline='\n', encoding='utf-8') as plain_text_file:
reader = csv.reader(plain_text_file, delimiter=delimiter, quotechar='"')
wb = Workbook()
ws = wb.active
for row in reader:
ws.append(row)
wb.save("Converted_PlainText_File.xlsx")
input_file_converted = "Converted_PlainText_File.xlsx"
# if already XLSX file
if file_extension.upper() == ".XLSX":
input_file_converted = input_file
# open excel file
wb = load_workbook(filename=input_file_converted)
ws = wb.get_sheet_by_name(wb.sheetnames[0])
row_count = ws.max_row - 1
# find barcode column
barcode_column = find_barcode_column(ws)
if barcode_column == None:
gui.msgbox("No Barcode column found.")
return
# loop through barcodes
counter = 0
for cell in ws[barcode_column]:
# skip header
if config.barcode_column_header != "" and counter == 0:
counter += 1
gui.insert_text(counter, ("n/a", "n/a", "Skipped", "Spreadsheet header."), 'success')
continue
# get item record
barcode = str(cell.value)
# clean up prefixes and suffixes
if config.prefix_trim > 0:
barcode = barcode[config.prefix_trim:]
if config.suffix_trim > 0:
barcode = barcode[:-config.suffix_trim]
# get information from item record in Alma
item = alma.item_record(barcode, config.apikey, config.apikey_region)
if item.found == False:
counter += 1
gui.insert_text(counter, (barcode, "n/a", "Error", item.error_msg),
'error')
continue
# return item
scan_return = alma.ret()
scan_return.post(config.apikey, config.apikey_region, config.library,
config.circ_desk,
config.register_in_house_use, item.mms_id,
item.holding_id, item.pid, item.xml)
if scan_return.successful == False:
counter += 1
gui.insert_text(counter, (barcode, item.title, "Error",
scan_return.error_msg), 'error')
continue
if scan_return.successful == True:
if "Queue: 0" in scan_return.additional_info:
counter += 1
# if file only has a single line of barcodes, you will get a div by zero error
try:
increment = 100 / row_count
except:
increment = 100 / 1
gui.insert_text(counter, (barcode, item.title, "Returned",
scan_return.additional_info), 'success')
gui.progress_bar.step(increment)
if "Queue: 0" not in scan_return.additional_info:
counter += 1
increment = 100 / row_count
gui.insert_text(counter, (barcode, item.title, "Returned-Queue",
scan_return.additional_info), 'attention')
gui.progress_bar.step(increment)
# delete source file
if config.delete_barcode_file.upper() == "TRUE":
try:
os.remove(input_file)
except OSError as e:
gui.msgbox(e)
# finish
gui.progress_bar["value"] = 100
gui.msgbox("Done.")
# functions ###################################################################
def find_barcode_column(ws):
barcode_column = None
# if no header specified
if config.barcode_column_header == "":
barcode_column = "A"
# if header is specified
if config.barcode_column_header != "":
for row in ws.iter_rows():
for cell in row:
if cell.value:
if cell.value.upper() == config.barcode_column_header.upper():
barcode_column = cell.column
break
# finish
return barcode_column
# configs #####################################################################
class configs:
def __init__(self, configfile):
self.configs = configs
c_dict = configparser.ConfigParser()
c_dict.read(configfile)
self.version = c_dict['misc']['version']
self.delete_barcode_file = c_dict['misc']['delete_barcode_file']
self.key = c_dict['apikey']['key']
self.apikey_region = c_dict['apikey']['region'].lower()
self.download_directory = c_dict['spreadsheet']['spreadsheet_directory'].replace('\\', '//')
self.barcode_column_header = c_dict['spreadsheet']['barcode_column_header']
self.library = c_dict['alma']['library']
self.circ_desk = c_dict['alma']['circ_desk']
self.register_in_house_use = c_dict['alma']['register_in_house_use']
self.prefix_trim = int(c_dict['barcodes']['prefix_trim'])
self.suffix_trim = int(c_dict['barcodes']['suffix_trim'])
self.log_directory = c_dict['log']['log_directory'].replace('\\', '//')
# set apikey
# -will first search for a matching environmental variable key.
# -if not found will set apikey as written in config.ini entry.
try:
self.apikey = os.environ[self.key]
except KeyError:
self.apikey = self.key
# gui #########################################################################
class gui:
def __init__(self, master):
self.master = master
master.title("RoboReturns "+config.version)
master.resizable(0, 0)
master.minsize(width=1150, height=500)
master.maxsize(width=1150, height=900)
master.iconbitmap(".\images\logo_small.ico")
logo = PhotoImage(file=".\images\logo_large.png")
self.logo = Label(image=logo)
self.logo.image = logo
self.logo.pack()
# frames
self.top_frame = Frame(master)
self.top_frame.pack(side='top', fill='both', expand=False)
self.run_button = Button(self.top_frame, text="OPEN FILE AND RUN", font="Arial 14", command=main, relief="groove")
self.run_button.pack(fill='both', side='left', expand=True)
self.save_img = PhotoImage(format = 'png', file= '.\images\save_icon.png')
self.save_button = Button(self.top_frame, text="SAVE LOG", image=self.save_img, font="Arial 14", command=self.save_log, relief="groove")
self.save_button.pack(fill='both', side='right', expand=False)
self.mid_frame = Frame(master)
self.mid_frame.pack(side='top', fill='both', expand=True)
# tree view
self.tree = ttk.Treeview(self.mid_frame, height=15)
style = ttk.Style()
style.theme_use('clam')
# headings
self.tree['columns'] = ('barcode', 'title', 'status', 'info')
self.tree.heading('#0', text='#', anchor='w')
self.tree.heading('barcode', text='Barcode', anchor="w")
self.tree.heading('title', text='Title', anchor="w")
self.tree.heading('status', text='Status', anchor="w")
self.tree.heading('info', text='Additional Info', anchor="w")
self.tree.column("#0", width=40)
self.tree.column("barcode", width=100)
self.tree.column("title", width=300)
self.tree.column("status", width=100)
self.tree.column("info", width=900)
self.tree.pack(fill="both", expand=False, side="left")
# scrollbar
v_scrollbar = ttk.Scrollbar(self.mid_frame, orient="vertical",
command=self.tree.yview)
v_scrollbar.place(x=1136, y=26, height=303)
self.tree.configure(yscrollcommand=v_scrollbar.set)
# tags
self.tree.tag_configure('error', background='pink')
self.tree.tag_configure('success', background='white')
self.tree.tag_configure('attention', background='khaki')
# progressbar
style.configure("red.Horizontal.TProgressbar", foreground='red',
background='#2381df')
self.progress_bar = ttk.Progressbar(master, style="red.Horizontal.TProgressbar",
orient='horizontal', mode='determinate')
self.progress_bar.pack(fill="both", expand=False, side="top")
def msgbox(self, msg):
messagebox.showinfo("Attention", msg)
def openfile(self):
self.filename = filedialog.askopenfilename(initialdir = config.download_directory,
title = "Select file",
filetypes = (("EXCEL, TXT, or CSV","*.xlsx *.txt *.csv"),("all files","*.*")))
filename = self.filename
return filename
def clear_tree(self):
self.tree.delete(*self.tree.get_children())
def insert_text(self, counter, msg, tags):
self.tree.insert("", "end", text=counter, values=(msg), tags=tags)
self.tree.yview_moveto(1)
root.update()
def save_log(self):
current_date = datetime.datetime.now()
current_date_formatted = current_date.strftime("%Y-%m-%d__%H%M%S")
try:
saved_log = open(f"{config.log_directory}robo_returns_log_{current_date_formatted}.csv", "w", encoding="utf-8", newline='')
except:
saved_log = open(f"robo_returns_log_{current_date_formatted}.csv", "w", encoding="utf-8", newline='')
children = self.tree.get_children()
for child in children:
list = self.tree.item(child)["values"]
w = csv.writer(saved_log, quoting=csv.QUOTE_ALL)
w.writerow(list)
saved_log.close()
self.msgbox("LOG SAVED SUCCESFULLY.")
# toplevel #########################################################################
config = configs('config.ini')
# gui
root = Tk()
gui = gui(root)
root.mainloop()