/
printing.py
executable file
·672 lines (579 loc) · 25.3 KB
/
printing.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# For now the internal nametag templates will be hardcoded. At least a [default]
# section is required; if a named section is missing the HTML will be ignored,
# otherwise the following sections and files are allowed:
# - [parent]
# - [report]
# - [volunteer]
# TODO: respect locale's date/time format when doing substitution. Hard coded for now.
# TODO: abstract barcode generation in themes. Hard coded for now.
# TODO: a bunch of caching and restructuring.
# TODO: code for multiple copies, if needed.
# TODO: speed up batch printing with multi-page patched wkhtmltopdf?
"""Handles generation of HTML for nametags, saving/reading printer config, etc"""
import os
import sys
import re
import platform
import logging
import subprocess
import tempfile
import datetime
from configobj import ConfigObj
from codebar import codebar
# Platforms using the CUPS printing system (UNIX):
unix = ['Linux', 'linux2', 'Darwin']
wkhtmltopdf = '/usr/bin/wkhtmltopdf' #path to wkhtmltopdf binary
#TODO: Option to select native or builtin wkhtmltopdf
#TODO: Determine whether to use ~/.taxidi/resources/ (config.ini in ~/.taxidi/)
# or $PWD/resources/ (config.ini in pwd).
nametags = os.path.join('resources', 'nametag') #path where html can be found.
#For distribution this may be moved to /usr/share/taxidi/ in UNIX, but should be
#copied to user's ~/.taxidi/ as write access is needed.
lpr = '/usr/bin/lpr' #path to LPR program (CUPS/Unix only).
class Printer:
def __init__(self):
self.log = logging.getLogger(__name__)
if platform.system() in unix:
self.log.info("System is type UNIX. Using CUPS.")
self.con = _CUPS()
elif platform.system() == 'win32':
self.log.info("System is type WIN32. Using GDI.")
#TODO implement win32 printing code
else:
self.log.warning("Unsupported platform. Printing by preview only.")
def buildArguments(self, config, section='default'):
"""Returns list of arguments to pass to wkhtmltopdf. Requires config
dictionary and section name to read (if None reads [default]).
Works all in lowercase. If section name does not exist, will use
values from [default] instead.
"""
try: #attempt to read passed section. If invalid, use default instead.
self.log.debug('Attempting to read print configuration for section {0}...'
.format(section))
piece = config[section]
except KeyError:
self.log.warn('No section for {0}: using default instead.'.format(section))
piece = config['default']
args = []
if len(piece) == 0:
return [] #No options in section
for arg in piece.keys():
if arg.lower() == 'zoom':
args.append('--zoom')
args.append(piece[arg])
elif arg.lower() == 'size':
args.append('-s')
args.append(piece[arg])
elif arg.lower() == 'height':
args.append('--page-height')
args.append(piece[arg])
elif arg.lower() == 'width':
args.append('--page-width')
args.append(piece[arg])
elif arg.lower() == 'left':
args.append('--margin-left')
args.append(piece[arg])
elif arg.lower() == 'right':
args.append('--margin-right')
args.append(piece[arg])
elif arg.lower() == 'top':
args.append('--margin-top')
args.append(piece[arg])
elif arg.lower() == 'bottom':
args.append('--margin-bottom')
args.append(piece[arg])
elif arg.lower() == 'orientation':
args.append('--orientation')
args.append(piece[arg].lower())
else:
self.log.warning("Unexpected key encountered in {0}: {1} = {2}"
.format(section, arg, piece[arg]))
return args
def writePdf(self, args, html, copies=1, collate=True):
"""
Calls wkhtmltopdf and generates a pdf. Accepts args as a list, path
to html file, and returns path to temporary file. Temp file
should be unlinked when no longer needed.
Also accepts copies=1, collate=True
"""
#Build arguments
if copies < 0: copies = 1
if copies != 1:
args.append("--copies")
args.append(copies)
if collate:
args.append("--collate")
args.append(html)
#create temp file to write to
out = tempfile.NamedTemporaryFile(delete=False)
out.close()
args.append(out.name) #append output file name
self.log.debug('Calling {0} with arguments <'.format(wkhtmltopdf))
self.log.debug('{0} >'.format(args))
if args[0] != wkhtmltopdf: args.insert(0, wkhtmltopdf) #prepend program name
ret = subprocess.check_call(args)
if ret != 0:
self.log.error('Called process error:')
self.log.error('Program returned exit code {0}'.
format(ret))
return 0
self.log.debug('Generated pdf {0}'.format(out.name))
return out.name
def preview(self, fileName):
"""Opens a file using the default application. (Print preview)"""
if os.name == 'posix':
if sys.platform == 'darwin': #OS X
self.log.debug('Attempting to preview file {0} via open'
.format(fileName))
ret = subprocess.call(['/usr/bin/open', fileName])
if ret != 0:
self.log.error('open returned non-zero exit code {0}'.format(ret))
return 1
return 0
elif sys.platform == 'linux2': #linux
try: #attempt to use evince-previewer instead
self.log.debug('Attempting to preview file {0} with evince.'
.format(fileName))
ret=subprocess.Popen(('/usr/bin/evince-previewer', fileName))
if ret != 0:
self.log.error('evince returned non-zero exit code {0}'
.format(ret))
return 1
return 0
except OSError:
self.log.debug('evince unavailable.')
self.log.debug('Attempting to preview file {0} via xdg-open'
.format(fileName))
ret = subprocess.call(('/usr/bin/xdg-open', fileName))
if ret != 0:
self.log.error('xdg-open returned non-zero exit code {0}'.format(ret))
return 1
return 0
else:
self.log.warning('Unable to determine default viewer for POSIX platform {0}'.format(sys.platform))
self.log.warning('Please file a bug and attach a copy of this log.')
if os.name == 'nt': #UNTESTED
self.log.debug('Attempting to preview file {0} via win32.startfile'
.format(fileName))
os.startfile(filepath)
return 0
def printout(self, filename, printer=None, orientation=None):
if os.name == 'posix' or os.name == 'mac': #use CUPS/lpr
self.con.printout(filename, printer, orientation)
#---- printing proxy methods -----
def listPrinters(self):
"""Returns list of names of available system printers (proxy)."""
return self.con.listPrinters()
def getPrinters(self):
"""
Returns dictionary of printer name, description, location, and URI (proxy)
"""
return self.con.getPrinters()
class Nametag:
def __init__(self, barcode=False):
"""
Format nametags with data using available HTML templates. Will
fetch barcode encoding options from global config; otherwise accepts
barcode=False as the initialization argument.
"""
#TODO: source config options for barcode, etc. from global config.
self.barcodeEnable = barcode
self.log = logging.getLogger(__name__)
#Setup and compile regex replacements:
self.date_re = re.compile(r'%DATE%', re.IGNORECASE) #date
self.time_re = re.compile(r'%TIME%', re.IGNORECASE) #time
self.room_re = re.compile(r'%ROOM%', re.IGNORECASE) #room
self.first_re = re.compile(r'%FIRST%', re.IGNORECASE) #name
self.last_re = re.compile(r'%LAST%', re.IGNORECASE) #surname
self.medical_re = re.compile(r'%MEDICAL%', re.IGNORECASE) #medical
self.code_re = re.compile(r'%CODE%', re.IGNORECASE) #paging code
self.secure_re = re.compile(r'%S%', re.IGNORECASE) #security code
self.medicalIcon_re = re.compile(
r"window\.onload\s=\shide\('medical'\)\;", re.IGNORECASE)
self.volunteerIcon_re = re.compile(
r"window\.onload\s=\shide\('volunteer'\)\;", re.IGNORECASE)
def listTemplates(self, directory=nametags):
"""Returns a list of the installed nametag themes"""
#TODO: add more stringent validation against config and html.
directory = os.path.abspath(directory)
self.log.debug("Searching for html templates in {0}".format(directory))
try:
resource = os.listdir(directory)
except OSError as e:
logger.error('({0})'.format(e))
return []
themes = []
#Remove any files from themes[] that are not directories:
for i in resource:
if os.path.isdir(os.path.join(directory, i)):
themes.append(i)
valid = []
#Check that each folder has the corresponding .conf file:
for i in themes:
if os.path.isfile(self._getTemplateFile(i)):
valid.append(i)
valid.sort()
del resource, themes
self.log.debug("Found templates {0}".format(valid))
return valid
def _getTemplateFile(self, theme, directory=nametags):
"""
Returns full path to .conf file for an installed template pack and
check it exists.
"""
directory = os.path.abspath(directory)
path = os.path.join(directory, theme, '{0}.conf'.format(theme))
#TODO: more stringent checks (is there a matching HTML file?)
if os.path.isfile(path):
return path
else:
self.log.warning("{0} does not exist or is not file.".format(path))
def _getTemplatePath(self, theme, directory=nametags):
"""Returns absolute path only of template pack"""
return os.path.join(directory, theme)
def readConfig(self, theme):
"""
Reads the configuration for a specified template pack. Returns
dictionary.
"""
inifile = self._getTemplateFile(theme)
self.log.info("Reading template configuration from '{0}'"
.format(inifile))
config = ConfigObj(inifile)
try: #check for default section
default = config['default']
except KeyError as e:
self.log.error(e)
self.log.error("{0} contains no [default] section and is invalid."
.format(inifile))
raise KeyError("{0} contains no [default] section and is invalid."
.format(inifile))
del default
return config
def nametag(self, template='default', room='', first='', last='', medical='',
code='', secure='', barcode=True):
"""
Convenience function for generating nametag html and the coresponding
barcodes. Applies data to a nametag template and returns the resulting
HTML. Does not query database; pass barcode bool to disable.
Accepts template theme from listTemplates() as first argument; 'default'
if none. Accepts the following formats (defaults are blank):
- room
- first
- last
- medical
- code
- secure
- Barcode=True
This method processes default.html only.
Disabled barcodes will be replaced with white.gif
"""
#Check that theme specified is valid:
if template != 'default':
themes = self.listTemplates()
if template not in themes:
self.log.error("Bad theme specified. Using default instead.")
template = 'default'
#Read in the HTML from template.
try:
directory = self._getTemplatePath(template)
except KeyError:
self.log.error("Unable to process template '{0}'. Aborting."
.format(template))
return None
#read in the HTML
self.log.debug('Generating nametag with nametag()')
self.log.debug('Reading {0}...'.format(os.path.join(directory,
'default.html')))
f = open(os.path.join(directory, 'default.html'))
html = f.read()
f.close()
if len(html) == 0: #template file is empty: use default instead.
self.log.warn('HTML template file {0} is blank.'.format(
os.path.join(directory, 'default.html')))
#generate barcode of secure code, code128:
if barcode:
try:
if secure == '':
codebar.gen('code128', os.path.join(directory,
'default-secure.png'), code)
else:
codebar.gen('code128', os.path.join(directory,
'default-secure.png'), secure)
except NotImplementedError as e:
self.log.error('Unable to generate barcode: {0}'.format(e))
html = html.replace(u'default-secure.png', u'white.gif')
else:
#replace barcode image with white.gif
html = html.replace(u'default-secure.png', u'white.gif')
self.log.debug("Disabled barcode.")
#get the current date/time
now = datetime.datetime.now()
#Perform substitutions:
html = self.date_re.sub(now.strftime("%a %d %b, %Y"), html)
html = self.time_re.sub(now.strftime("%H:%M:%S"), html)
html = self.room_re.sub(room, html)
#Fix for if database returns None instead of empty string:
if medical == None: medical = ''
html = self.first_re.sub(str(first), html)
html = self.last_re.sub(str(last), html)
html = self.medical_re.sub(str(medical), html)
html = self.code_re.sub(code, html)
html = self.secure_re.sub(secure, html)
#show medical icon
if len(medical) > 0:
html = self.medicalIcon_re.sub('', html)
return html
def parent(self, template='default', room='', first='', last='',
code='', secure='', link=None, barcode=True):
"""
Applies data to a nametag template and returns the resulting HTML.
Accepts template theme from listTemplates() as first argument; 'default'
if None. Accepts the following formats (defaults are blank):
- room
- first
- last
- medical
- code
- secure
This method processes parent.html only.
"""
#Check that theme specified is valid:
if template != 'default':
themes = self.listTemplates()
if template not in themes:
self.log.error("Bad theme specified. Using default instead.")
template = 'default'
#Read in the HTML from template.
try:
directory = self._getTemplatePath(template)
except KeyError:
self.log.error("Unable to process template '{0}'. Aborting."
.format(template))
return None
self.log.debug('Generating child tag using {0} from {1}'
.format('parent.html', directory))
f = open(os.path.join(directory, 'parent.html'))
inp = f.read()
f.close()
#Generate barcodes if needed:
if barcode:
try:
if secure == '':
codebar.gen('code128', os.path.join(directory,
'parent-secure.png'), code)
else:
codebar.gen('code128', os.path.join(directory,
'parent-secure.png'), secure)
except NotImplementedError as e:
self.log.error('Unable to generate barcode: {0}'.format(e))
inp = inp.replace(u'parent-secure.png', u'white.gif')
else:
#replace barcode image with white.gif
inp = inp.replace(u'parent-secure.png', u'white.gif')
self.log.debug("Disabled barcode.")
#Generate QR code if needed:
if link == '': link = None
if link != None:
codebar.gen('qr', os.path.join(directory, 'parent-link.png'), link)
#get the current date/time
now = datetime.datetime.now()
#Perform substitutions:
inp = self.date_re.sub(now.strftime("%a %d %b, %Y"), inp) #date
inp = self.time_re.sub(now.strftime("%H:%M:%S"), inp) #time
inp = self.room_re.sub(room, inp) #room
inp = self.first_re.sub(first, inp) #first name
inp = self.last_re.sub(last, inp) #surname
inp = self.code_re.sub(code, inp) #paging code
inp = self.secure_re.sub(secure, inp) #security code
return inp
def volunteer(self, template='default', room='', first='', last='',
ministry=''):
"""
Applies data to a nametag template and returns the resulting HTML.
Accepts template theme from listTemplates() as first argument; 'default'
if none. Accepts the following formats (defaults are blank):
- room
- first
- last
- ministry
This method processes volunteer.html only.
"""
#Check that theme specified is valid:
if template != 'default':
themes = self.listTemplates()
if template not in themes:
self.log.error("Bad theme specified. Using default instead.")
template = 'default'
#Read in the HTML from template.
try:
directory = self._getTemplatePath(template)
except KeyError:
self.log.error("Unable to process template '{0}'. Aborting.".format(template))
return None
self.log.debug('Generating child tag using {0} from {1}'
.format('volunteer.html', directory))
f = open(os.path.join(directory, 'volunteer.html'))
html = f.read()
f.close()
now = datetime.datetime.now()
#Perform substitutions:
html = self.date_re.sub(now.strftime("%a %d %b, %Y"), html)
html = self.time_re.sub(now.strftime("%H:%M:%S"), html)
html = self.room_re.sub(room, html)
html = self.first_re.sub(first, html)
html = self.last_re.sub(last, html)
html = self.medical_re.sub(ministry, html)
#show icon
if len(ministry) > 0:
html = self.volunteerIcon_re.sub('', html)
return html
class _CUPS:
def __init__(self):
self.log = logging.getLogger(__name__)
self.log.info("Connecting to CUPS server on localhost...")
try:
import cups
except ImportError as e:
self.log.error("CUPS module not available. Is CUPS installed? {0}".format(e))
self.con = cups.Connection()
def listPrinters(self):
"""
Returns a list of the names of available system printers.
"""
return self.con.getPrinters().keys()
def getPrinters(self):
"""
Returns dictionary of printer name, description, location, and URI.
"""
a = dict()
printers = self.con.getPrinters()
for item in printers:
info = printers[item]['printer-info']
location = printers[item]['printer-location']
uri = printers[item]['device-uri']
a[item] = { 'info' : info, 'location' : location, 'uri' : uri}
return a
def getDefault(self):
"""Determines the user's default system or personal printer."""
return self.con.getDests()[None, None].name
def printout(self, filename, printer=None, orientation=None):
if printer != None: #set printer; if none, then use default (leave argument blank)
if printer not in self.listPrinters():
raise PrinterError('Specified printer is not available on this system')
printArgs = ['-P', printer]
else:
printArgs = []
if orientation: #set orientation option
if orientation not in ['landscape', 'portrait']:
raise PrinterError('Bad orientation specification: {0}'.format(orientation))
printArgs.append('-o')
printArgs.append(orientation)
try: #see if file exists
f = open(filename)
except IOError:
raise PrinterError('The specified file does not exist: {0}'.format(filename))
else:
f.close()
printArgs.append(filename) #append file to print.
ret = subprocess.check_call([lpr,] + printArgs)
if ret != 0:
raise PrinterError('{0} exited non-zero ({1}). Error spooling.'.format(lpr, ret))
class PrinterError(Exception):
def __init__(self, value=''):
if value == '':
self.error = 'Generic spooling error.'
else:
self.error = value
def __str__(self):
return repr(self.error)
class Main:
def __init__(self):
self.log = logging.getLogger(__name__)
self.con = Printer()
self.tag = Nametag(True)
self.section = ''
def nametag(self, theme='default', room='', first='', last='', medical='',
code='', secure='', barcode=True, section='default'):
"""Note: section= not fully implemented in Nametag.nametag method"""
self.section = section
self.conf = self.tag.readConfig(theme) #theme
self.args = self.con.buildArguments(self.conf, section) #section
tmpname = os.path.join(self.tag._getTemplatePath(theme), 'temp.html')
stuff = self.tag.nametag(room=room, first=first, last=last, medical=medical,
code=code, secure=secure, barcode=barcode, template=theme)
html = open(tmpname, 'w')
html.write(stuff)
html.close()
self.pdf = self.con.writePdf(self.args, tmpname)
os.unlink(tmpname)
return self.pdf
def parent(self, theme='default', room='', first='', last='', code='',
secure='', link='', barcode=True, section='parent'):
self.section = section
self.conf = self.tag.readConfig(theme) #theme
self.args = self.con.buildArguments(self.conf, section) #section
tmpname = os.path.join(self.tag._getTemplatePath(theme), 'temp.html')
stuff = self.tag.parent(template=theme, room=room, first=first, last=last,
code=code, secure=secure, link=link, barcode=barcode)
html = open(tmpname, 'w')
html.write(stuff)
html.close()
self.pdf = self.con.writePdf(self.args, tmpname)
os.unlink(tmpname)
return self.pdf
def volunteer(self, theme='default', room='', first='', last='',
ministry='', section='volunteer'):
self.section = section
self.conf = self.tag.readConfig(theme) #theme
self.args = self.con.buildArguments(self.conf, section) #section
tmpname = os.path.join(self.tag._getTemplatePath(theme), 'temp.html')
stuff = self.tag.volunteer(template=theme, room=room, first=first,
last=last, ministry=ministry)
html = open(tmpname, 'w')
html.write(stuff)
html.close()
self.pdf = self.con.writePdf(self.args, tmpname)
os.unlink(tmpname)
return self.pdf
def preview(self, filename=None):
if filename == None:
filename = self.pdf
self.con.preview(filename)
def printout(self, filename=None, printer=None, orientation=None):
if filename == None:
filename = self.pdf
if orientation == None:
orientation = self.conf[self.section]['orientation']
self.con.printout(filename, printer, orientation)
def cleanup(self, trash=None):
if trash != None:
for item in trash:
os.unlink(item)
else:
os.unlink(self.pdf)
self.section = ''
if __name__ == '__main__':
con = Main()
con.nametag(theme='newlife', room='Green Room', first='Johnothan',
last='Churchgoer', code='O-9999', medical=None,
secure='5C55', barcode=True)
#~ con.nametag(theme='default', code='BUS',
#~ first=' ',
#~ last="Driver Name", room="123456790",
#~ secure="Z1Z", barcode=True)
con.preview()
#~ con.printout(printer="Brother-QL-570")
#con.volunteer(room='Jungle Room', first='Zac', last='Sturgeon', ministry='Tech')
#con.printout()
#con.preview()
#con.cleanup()
#~ con.parent(room='Green Room', first='Johnothan', last='Churchgoer',
#~ code='O-9999', secure='9999:Z99', link='http://blahblahblah.com/church/taxidi/parent.cgi?id=12682862')
#~ con.preview()
#~ con.printout()
#con.cleanup()
raw_input(">")
con.cleanup()