Mercurial > hg > zebra
annotate zebra.py @ 8:19fd067db7ca
Added Windows support
author | Ben Croston |
---|---|
date | Mon, 15 Aug 2011 00:20:31 +0100 |
parents | 0687a6666873 |
children | 6a1963ee4aff |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 # NOTE: this package is Linux specific at the moment | |
3 | |
4 import subprocess | |
5 import os.path | |
6 import sys | |
7 if sys.platform.lower().startswith('win'): | |
8 import win32print | |
9 | |
10 class zebra(object): | |
11 def __init__(self, queue=None): | |
1 | 12 """queue - name of the printer queue""" |
0 | 13 self.queue = queue |
14 | |
15 def _output_unix(self, commands): | |
16 if self.queue == 'zebra_python_unittest': | |
17 p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) | |
18 else: | |
19 p = subprocess.Popen(['lpr','-P%s'%self.queue], stdin=subprocess.PIPE) | |
20 if type(commands) == bytes: | |
21 p.communicate(commands) | |
22 else: | |
23 p.communicate(str(commands).encode()) | |
24 p.stdin.close() | |
25 | |
26 def _output_win(self, commands): | |
8 | 27 if self.queue == 'zebra_python_unittest': |
28 print commands | |
29 return | |
30 if type(commands) != bytes: | |
31 commands = str(commands).encode() | |
32 hPrinter = win32print.OpenPrinter(self.queue) | |
33 try: | |
34 hJob = win32print.StartDocPrinter(hPrinter, 1, ('Label',None,'RAW')) | |
35 try: | |
36 win32print.StartPagePrinter(hPrinter) | |
37 win32print.WritePrinter(hPrinter, commands) | |
38 win32print.EndPagePrinter(hPrinter) | |
39 finally: | |
40 win32print.EndDocPrinter(hPrinter) | |
41 finally: | |
42 win32print.ClosePrinter(hPrinter) | |
0 | 43 |
44 def output(self, commands): | |
45 assert self.queue is not None | |
7 | 46 if sys.platform.lower().startswith('win'): |
0 | 47 self._output_win(commands) |
48 else: | |
49 self._output_unix(commmands) | |
50 | |
51 def _getqueues_unix(self): | |
52 queues = [] | |
53 output = subprocess.check_output(['lpstat','-p'], universal_newlines=True) | |
54 for line in output.split('\n'): | |
55 if line.startswith('printer'): | |
56 queues.append(line.split(' ')[1]) | |
57 return queues | |
58 | |
59 def _getqueues_win(self): | |
8 | 60 printers = [] |
61 for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): | |
62 printers.append(name) | |
63 return printers | |
0 | 64 |
65 def getqueues(self): | |
7 | 66 if sys.platform.lower().startswith('win'): |
0 | 67 return self._getqueues_win() |
68 else: | |
69 return self._getqueues_unix() | |
70 | |
1 | 71 def setqueue(self, queue): |
0 | 72 self.queue = queue |
73 | |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
74 def setup(self, direct_thermal=None, label_height=None, label_width=None): |
0 | 75 commands = '\n' |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
76 if direct_thermal: |
0 | 77 commands += ('OD\n') |
78 if label_height: | |
79 commands += ('Q%s,%s\n'%(label_height[0],label_height[1])) | |
80 if label_width: | |
81 commands += ('q%s\n'%label_width) | |
82 self.output(commands) | |
83 | |
84 def store_graphic(self, name, filename): | |
85 assert filename.endswith('.pcx') | |
86 commands = '\nGK"%s"\n'%name | |
87 commands += 'GK"%s"\n'%name | |
88 size = os.path.getsize(filename) | |
89 commands += 'GM"%s"%s\n'%(name,size) | |
90 self.output(commands) | |
91 self.output(open(filename,'rb').read()) | |
92 | |
93 if __name__ == '__main__': | |
94 z = zebra() | |
95 print 'Printer queues found:',z.getqueues() | |
96 z.setqueue('zebra_python_unittest') | |
2
ad9d9bf61243
Change direct transfer to direct thermal
Ben Croston <ben@fuzzyduckbrewery.co.uk>
parents:
1
diff
changeset
|
97 z.setup(direct_thermal=True, label_height=(406,32), label_width=609) # 3" x 2" direct thermal label |
1 | 98 z.store_graphic('logo','logo.pcx') |
0 | 99 label = """ |
100 N | |
101 GG419,40,"logo" | |
102 A40,80,0,4,1,1,N,"Tangerine Duck 4.4%" | |
103 A40,198,0,3,1,1,N,"Duty paid on 39.9l" | |
1 | 104 A40,240,0,3,1,1,N,"Gyle: 127 Best Before: 16/09/2011" |
0 | 105 A40,320,0,4,1,1,N,"Pump & Truncheon" |
106 P1 | |
107 """ | |
108 z.output(label) | |
109 |