-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabelmaker.py
More file actions
357 lines (342 loc) · 13.5 KB
/
labelmaker.py
File metadata and controls
357 lines (342 loc) · 13.5 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
import sys
import string
from datetime import date
import os
import urllib
import math
import re
import logging
SESSION_STATE = "state"
SESSION_LINES = "lines"
SESSION_LENGTH = "length"
SESSION_CURRENT = "current"
SESSION_LABEL = "label"
SESSION_COPIES = "copies"
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
## State Description
## 0 Asking for the number of lines to put on the label
## 1 Asking for the text of the line to put on the label
## 2 Asking for the length of the label
## 3 Asking for number of copies of label
## 4 Asking for approval to print label
@ask.launch
def launch():
state = 0 ## Initial state engine
session.attributes[SESSION_STATE] = state
welcome = render_template('welcome')
reprompt = render_template('reprompt')
return question(welcome).reprompt(reprompt)
@ask.intent('RouteNumber')
def RouteNumber(wholenumber, decimal, numerator, denominator):
divtable = ['half','third','thirds','fourth','fourths','quarter', \
'quarters','fifth','fifths','sixth','sixths','seventh', \
'sevenths','eighth','eigths','nineth','nineths','tenth','tenths']
divvalue = [2,3,3,4,4,4,4,5,5,6,6,7,7,8,8,9,9,10,10]
value = 0
ntype = 0 ## integer = 0; float = 1
# Let's see what got filled in
if wholenumber is not None:
if wholenumber.isdigit():
wn = wholenumber
ntype = 0 ## So far, it's an integer
else:
askline = render_template('asknumber')
return question(askline)
else:
wn = '0'
ntype = -1 ## So far, we don't have a valid number
if decimal is not None: ## See if this is a decimal number
if decimal.isdigit():
value = float(wn + '.' + decimal) ## Add it to whole number
ntype = 1 ## we have a decimal number
else:
askline = render_template('asknumber')
return question(askline)
else: ## It's not. See if it is a fraction
if (denominator is not None):
if (numerator is not None):
fdividend = int(numerator)
else:
fdividend = 1
fdivisor = 0
for frac in range(len(divtable)):
if divtable[frac] == denominator:
fdivisor = divvalue[frac]
if fdivisor != 0:
fracstr = str(float(fdividend)/float(fdivisor)) # convert to decimal string
value = float(wn)+float(fracstr)
ntype = 1 ## we have a fraction
else:
askline = render_template('asknumber')
return question(askline)
if ntype == -1: ## We didn't get a valid number
askline = render_template('asknumber')
return question(askline)
if ntype == 0:
value = int(wn)
state = session.attributes[SESSION_STATE]
if state == 0:
if ntype == 0:
if value == 0: ## Has to be at least one line
askline = render_template('zeroline')
return question(askline)
session.attributes[SESSION_LINES] = value
state = 1 ## Set up to receive label text
current = 0
label = ''
session.attributes[SESSION_STATE] = state
session.attributes[SESSION_CURRENT] = current
session.attributes[SESSION_LABEL] = label
if value == 1:
askline = render_template('oneline')
else:
askline = render_template('multiline')
linehelp = render_template('linehelp')
return question(askline).reprompt(linehelp)
else: ## Expected an integer line count
askline = render_template('asknumber')
return question(askline)
if state == 1:
label = session.attributes[SESSION_LABEL]
current = session.attributes[SESSION_CURRENT]
lines = session.attributes[SESSION_LINES]
if current > 0:
label = label + '\n'
label = label + str(value) ###### WORK ON THIS
current = current + 1
session.attributes[SESSION_CURRENT] = current
session.attributes[SESSION_LABEL] = label
if current < lines:
askline = render_template('nextline') ## ask for next line
else:
askline = render_template('asksize') ## got all the lines
state = 2 ## ask for label length
session.attributes[SESSION_STATE] = state
return question(askline)
if state == 2:
if ntype == 0:
length = float(value)
else:
length = value
session.attributes[SESSION_LENGTH] = length
askline = render_template('askcopies') ## got all the lines
state = 3 ## ask for number of copies
session.attributes[SESSION_STATE] = state
return question(askline)
if state == 3:
if ntype == 0:
copies = value
if copies == 0: ## Has to be at least one copy
askline = render_template('zerocopy')
return question(askline)
session.attributes[SESSION_COPIES] = copies
length = session.attributes[SESSION_LENGTH]
label = session.attributes[SESSION_LABEL]
if length == 0.0:
if copies > 1:
askline = render_template('messages', msg=label, copies=copies)
else:
askline = render_template('message', msg=label)
else:
if copies > 1:
askline = render_template('sizemsgs', labelsize=length, msg=label, copies=copies)
else:
askline = render_template('sizemsg', labelsize=length, msg=label)
state = 4 ## ready for printing
session.attributes[SESSION_STATE] = state
return question(askline)
else: ## Expected an integer copy count
askline = render_template('asknumber')
return question(askline)
@ask.intent('LinesIntent', convert={'lines': int})
def GetLineCount(lines):
if lines == 0: ## Has to be at least one line
askline = render_template('zeroline')
return question(askline)
session.attributes[SESSION_LINES] = lines
state = 1
current = 0
label = ''
session.attributes[SESSION_STATE] = state
session.attributes[SESSION_CURRENT] = current
session.attributes[SESSION_LABEL] = label
if lines == 1:
askline = render_template('oneline')
else:
askline = render_template('multiline')
linehelp = render_template('linehelp')
return question(askline).reprompt(linehelp)
@ask.intent('CopiesIntent', convert={'copies': int})
def GetCopyCount(copies):
if copies == 0: ## Has to be at least one copy
askline = render_template('zerocopy')
return question(askline)
session.attributes[SESSION_COPIES] = copies
length = session.attributes[SESSION_LENGTH]
label = session.attributes[SESSION_LABEL]
if length == 0.0:
if copies > 1:
askline = render_template('messages', msg=label, copies=copies)
else:
askline = render_template('message', msg=label)
else:
if copies > 1:
askline = render_template('sizemsgs', labelsize=length, msg=label, copies=copies)
else:
askline = render_template('sizemsg', labelsize=length, msg=label)
state = 4 ## ready for printing
session.attributes[SESSION_STATE] = state
return question(askline)
@ask.intent('YesIntent')
def PrintLabel():
state = session.attributes[SESSION_STATE]
if state == 4:
length = session.attributes[SESSION_LENGTH]
copies = session.attributes[SESSION_COPIES]
label = session.attributes[SESSION_LABEL]
label = '"' + label + '"'
if length == 0.0:
pixstr = ''
label = 'label:' + label
else:
lenpix = (length / 0.00555556) # assumes 180 pixels per inch
pixstr = str(int(lenpix))
label = 'caption:' + label
cmd = '/usr/bin/convert -gravity center -monochrome -negate -size ' + \
pixstr + 'x76 ' + label + ' /tmp/lm.png'
if os.system(cmd) != 0:
return statement(render_template('imfail'))
cmd = '/usr/local/bin/ptouch-print'
for i in range(0, copies):
if i > 0:
cmd = cmd + ' --cutmark'
cmd = cmd + ' --image /tmp/lm.png'
os.system(cmd)
askline = render_template('result')
return statement(askline)
@ask.intent('NoIntent')
def nogo():
## close_user_session()
return statement(render_template('cancel'))
@ask.intent('CatchAllIntent')
def workflow(anythinga,anythingb,anythingc,anythingd,anythinge,anythingf,anythingg,anythingh):
anything = ''
if anythinga is not None:
anything = anythinga.upper()
if anythingb is not None:
anything = anything + ' ' + anythingb.upper()
if anythingc is not None:
anything = anything + ' ' + anythingc.upper()
if anythingd is not None:
anything = anything + ' ' + anythingd.upper()
if anythinge is not None:
anything = anything + ' ' + anythinge.upper()
if anythingf is not None:
anything = anything + ' ' + anythingf.upper()
if anythingg is not None:
anything = anything + ' ' + anythingg.upper()
if anythingh is not None:
anything = anything + ' ' + anythingh.upper()
state = session.attributes[SESSION_STATE]
if state == 0: ## extract number of lines in label
match = re.search(r'(\w+)', anything)
if match:
lines = int(match.group(1))
if lines == 0: ## Has to be at least one line
askline = render_template('zeroline')
return question(askline)
session.attributes[SESSION_LINES] = lines
state = 1
current = 0
label = ''
session.attributes[SESSION_STATE] = state
session.attributes[SESSION_CURRENT] = current
session.attributes[SESSION_LABEL] = label
if lines == 1:
askline = render_template('oneline')
else:
askline = render_template('multiline')
linehelp = render_template('linehelp')
return question(askline).reprompt(linehelp)
else:
return question(reprompt)
if state == 1: ## add text to label
label = session.attributes[SESSION_LABEL]
current = session.attributes[SESSION_CURRENT]
lines = session.attributes[SESSION_LINES]
if current > 0:
label = label + '\n'
if anything == 'TIMESTAMP': ## timestamp must be only word in line
today = date.today()
label = label + today.strftime('%m/%d/%Y')
elif anything == 'NOTHING': ## must be only word in line
label = label + ' ' ## just add a blank space
else:
label = label + anything
current = current + 1
session.attributes[SESSION_CURRENT] = current
session.attributes[SESSION_LABEL] = label
if current < lines:
askline = render_template('nextline') ## ask for next line
else:
askline = render_template('asksize') ## got all the lines
state = 2 ## ask for label length
session.attributes[SESSION_STATE] = state
return question(askline)
if state == 2:
label = session.attributes[SESSION_LABEL]
match = re.search(r'(\w+)', anything)
if match:
length = float(match.group(1))
session.attributes[SESSION_LENGTH] = length
state = 3 ## ask for number of copies
session.attributes[SESSION_STATE] = state
return question(askline)
else:
askline = render_template('asknumber')
return question(askline)
if state == 3:
match = re.search(r'(\w+)', anything)
if match:
copies = int(match.group(1))
if copies == 0: ## Has to be at least one copy
askline = render_template('zerocopy')
return question(askline)
session.attributes[SESSION_COPIES] = copies
length = session.attributes[SESSION_LENGTH]
label = session.attributes[SESSION_LABEL]
lines = session.attributes[SESSION_LINES]
if length == 0.0:
if copies > 1:
askline = render_template('messages', msg=label, copies=copies)
else:
askline = render_template('message', msg=label)
else:
if copies > 1:
askline = render_template('sizemsgs', labelsize=length, msg=label, copies=copies)
else:
askline = render_template('sizemsg', labelsize=length, msg=label)
state = 4 ## ready for printing
session.attributes[SESSION_STATE] = state
return question(askline)
else:
askline = render_template('asknumber')
return question(askline)
@ask.intent('AMAZON.CancelIntent')
def cancel():
## close_user_session()
return statement(render_template('cancel'))
@ask.intent('AMAZON.StopIntent')
def stop():
## close_user_session()
return statement(render_template('stop'))
@ask.session_ended
def session_ended():
## close_user_session()
return "", 200
if __name__ == '__main__':
app.run(debug=True)