comparison zebra.py @ 0:5bd9ee5c0cae

Initial version
author Ben Croston <ben@fuzzyduckbrewery.co.uk>
date Fri, 12 Aug 2011 21:40:12 +0100
parents
children 21c3229ed401
comparison
equal deleted inserted replaced
-1:000000000000 0:5bd9ee5c0cae
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):
12 """queue - name of the printer queue as returned by lpstat -d"""
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):
27 raise Exception('Not yet implemented')
28
29 def output(self, commands):
30 assert self.queue is not None
31 if sys.platform.startswith('win'):
32 self._output_win(commands)
33 else:
34 self._output_unix(commmands)
35
36 def _getqueues_unix(self):
37 queues = []
38 output = subprocess.check_output(['lpstat','-p'], universal_newlines=True)
39 for line in output.split('\n'):
40 if line.startswith('printer'):
41 queues.append(line.split(' ')[1])
42 return queues
43
44 def _getqueues_win(self):
45 raise Exception('Not yet implemented')
46
47 def getqueues(self):
48 if sys.platform.startswith('win'):
49 return self._getqueues_win()
50 else:
51 return self._getqueues_unix()
52
53 def setqueue(self,queue):
54 self.queue = queue
55
56 def setup(self, direct_transfer=None, label_height=None, label_width=None):
57 commands = '\n'
58 if direct_transfer:
59 commands += ('OD\n')
60 if label_height:
61 commands += ('Q%s,%s\n'%(label_height[0],label_height[1]))
62 if label_width:
63 commands += ('q%s\n'%label_width)
64 self.output(commands)
65
66 def store_graphic(self, name, filename):
67 assert filename.endswith('.pcx')
68 commands = '\nGK"%s"\n'%name
69 commands += 'GK"%s"\n'%name
70 size = os.path.getsize(filename)
71 commands += 'GM"%s"%s\n'%(name,size)
72 self.output(commands)
73 self.output(open(filename,'rb').read())
74
75 if __name__ == '__main__':
76 z = zebra()
77 print 'Printer queues found:',z.getqueues()
78 z.setqueue('zebra_python_unittest')
79 # z.setup(direct_transfer=True, label_height=(406,32), label_width=609) # 3" x 2" label
80 # z.store_graphic('logo','logo.pcx')
81 label = """
82 N
83 GG419,40,"logo"
84 A40,80,0,4,1,1,N,"Tangerine Duck 4.4%"
85 A40,198,0,3,1,1,N,"Duty paid on 39.9l"
86 A40,240,0,3,1,1,N,"Gyle: 123 Best Before: 16/09/2011"
87 A40,320,0,4,1,1,N,"Pump & Truncheon"
88 P1
89 """
90 z.output(label)
91