-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.py
More file actions
282 lines (220 loc) · 7.33 KB
/
labels.py
File metadata and controls
282 lines (220 loc) · 7.33 KB
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
import csv
import os
import subprocess
import time
import json
PRODUCTION = os.getenv("PRODUCTION")
LABEL_DIR = os.path.join(os.path.dirname(__file__), "glabels")
TMP_DIR = os.path.join(os.path.dirname(__file__), "tmp")
LABEL_CSV = os.path.join(TMP_DIR, "label_data.csv")
PRINT_PDF = os.path.join(TMP_DIR, "labels.pdf")
MERGE_CSV = os.path.join(TMP_DIR, "merge.csv")
SETTINGS_JSON = os.path.join(TMP_DIR, "settings.json")
class LabelFileError(Exception):
pass
class LabelSet(object):
def __init__(self, customer, cultivar, lot, trays):
self.Customer = customer
self.Cultivar = cultivar
self.Lot = lot
self.Trays = trays
self.Printed = 0
self.Seeds = 0
def __repr__(self):
return "%s: %s - %s, %s"%(self.Customer, self.Cultivar, self.Lot, self.Trays)
class CultivarData(object):
def __init__(self, cultivar, sow_date):
self.SowDate = sow_date
self.Cultivar = cultivar
self.LotNumber = None
self.TrayCount = None
self.Printed = 0
self.LabelSets = []
def _print(label_type):
# generate the pdf to print
if PRODUCTION:
label_file = os.path.join(LABEL_DIR, label_type+".glabels")
subprocess.run(["glabels-3-batch", "-i", MERGE_CSV, "-o", PRINT_PDF, label_file])
subprocess.run(["lp", PRINT_PDF])
else:
print("Printed!")
time.sleep(1.5)
# glabels-3-batch -i <input.csv> -o <output.pdf> <label.glabel>
# lp <output.pdf>
def printLabel(label_set_groups, label_type=None):
'''
Prints one label per given label set
[(label_set, tray_number)], label_type
'''
if label_type is None:
with open(SETTINGS_JSON) as f:
label_type = json.load(f).get('label_type', "commercial")
# Generate the temporary csv file to merge with the labels
contents = [["Customer", "Cultivar", "Tray", "Lot"]]
for label_set, tray in label_set_groups:
contents.append([label_set.Customer, label_set.Cultivar, tray, label_set.Lot])
with open(MERGE_CSV, "w") as f:
w = csv.writer(f)
for row in contents:
w.writerow(row)
_print(label_type)
return
def printCSV(csv_file, label_type):
csv_file.save(MERGE_CSV)
rows = []
with open(MERGE_CSV) as f:
reader = csv.reader(f)
rows = [row for row in reader]
if len(rows) < 2:
raise LabelFileError("Not enough rows")
if "Customer" not in rows[0]:
raise LabelFileError("No Customer in header")
if "Cultivar" not in rows[0]:
raise LabelFileError("No Cultivar in header")
if "Tray" not in rows[0]:
raise LabelFileError("No Tray in header")
if "Lot" not in rows[0]:
raise LabelFileError("No Lot in header")
_print(label_type)
def saveLabelDataFile(csv_file, label_type):
with open(SETTINGS_JSON, "w") as f:
json.dump({"label_type": label_type}, f)
csv_file.save(LABEL_CSV)
getFileContents()
def getFileContents():
if not os.path.isfile(LABEL_CSV):
raise LabelFileError("No file uploaded")
try:
contents = []
with open(LABEL_CSV) as f:
reader = csv.reader(f)
for row in reader:
contents.append(row)
except:
raise LabelFileError("Not a CSV File")
# Validate the data
# if not "Sow Date" in contents[0][0]:
# raise LabelFileError("Bad file. Sow Date not found")
# if not "Lots" in contents[1][2]:
# raise LabelFileError("Bad file. Lot information not found")
# if not "Customer" in contents[4][0]:
# raise LabelFileError("Bad file. Customer information not found")
return contents
# def parseFile():
# contents = getFileContents()
# #
# # Organize the data
# #
# sow_date = contents[0][1]
# # Get each unique cultivar name
# cultivars = []
# for idx in range(len(contents[3])):
# c = contents[3][idx]
# if c == "" and not cultivars:
# continue
# elif c == "" and cultivars:
# cultivar_start_idx = idx+2
# break
# else:
# cultivars.append(CultivarData(c, sow_date))
# # attach the lot numbers
# idx = 3
# for c in cultivars:
# c.LotNumber = contents[1][idx]
# idx += 1
# # get cultivar positions in the customer table
# cultivar_idxs = {}
# idx = cultivar_start_idx
# for c in cultivars:
# cultivar_idxs[idx] = c.Cultivar
# idx += 2
# # Cultivar map
# cultivar_map = {}
# for c in cultivars:
# cultivar_map[c.Cultivar] = c
# # Build Customer label sets
# for row in contents[5:]:
# customer = row[0]
# for idx,c in cultivar_idxs.items():
# if row[idx] not in ["0", ""]:
# trays = int(row[idx])
# cultivar = cultivar_map[c]
# cultivar.LabelSets.append(LabelSet(customer, c, cultivar.LotNumber, trays))
# # Sum the tray counts
# for c in cultivars:
# c.TrayCount = sum([ls.Trays for ls in c.LabelSets])
# return cultivars
def parseFile():
"""
Reads the temporary file and returns a list of CultivarData objects
"""
contents = getFileContents()
#
# Collect header data
#
sow_date = contents[0][1]
# base_lot = contents[1][1]
customer = contents[2][1]
#
# Cultivars start on row 6, which is row 5 with 0 indexing
# The table is: Cultivar Name, Lot Number, Seeds, Trays
header_row = 5-1
cultivar_row = 6-1
# Rows:
name_idx = 0
lot_idx = 1
seed_idx = 2
tray_idx = 3
sown_idx = 4
# Confirm header and columns
# raise LabelFileError("")
# Process the cultivar table
cultivars = []
for row in contents[cultivar_row:]:
name = row[name_idx]
if name == "":
break
else:
lot_number = row[lot_idx]
trays = int(row[tray_idx])
cd = CultivarData(name, sow_date)
ls = LabelSet(customer, name, lot_number, trays)
ls.Seeds = row[seed_idx]
cd.LabelSets.append(ls)
cd.TrayCount = ls.Trays
if len(row) > sown_idx:
ls.Printed = int(row[sown_idx])
cd.Printed = ls.Printed
cultivars.append(cd)
return cultivars
def saveFile(cultivars):
"""
Write the progress back to the file
"""
contents = getFileContents()
#
# Cultivars start on row 6, which is row 5 with 0 indexing
# The table is: Cultivar Name, Lot Number, Seeds, Trays
header_row = 5-1
cultivar_row = 6-1
# Rows:
name_idx = 0
lot_idx = 1
seed_idx = 2
tray_idx = 3
sown_idx = 4
with open(LABEL_CSV, "w") as f:
csv_writer = csv.writer(f)
# Write the pre-amble
for x in range(header_row):
csv_writer.writerow(contents[x])
# write the header row
if len(contents[x+1]) < sown_idx:
csv_writer.writerow(contents[x+1]+["Sown"])
else:
csv_writer.writerow(contents[x+1])
# Write the new culitvar rows
for cultivar_data in cultivars:
for label_set in cultivar_data.LabelSets:
row = [label_set.Cultivar, label_set.Lot, label_set.Seeds, label_set.Trays, label_set.Printed]
csv_writer.writerow(row)