Mercurial > hg > zebra
comparison zebra.py @ 10:6a1963ee4aff
Added docstrings
Minor tidying up
author | Ben Croston <ben@fuzzyduckbrewery.co.uk> |
---|---|
date | Mon, 15 Aug 2011 01:00:49 +0100 |
parents | 19fd067db7ca |
children | e6ea86e3d0e1 |
comparison
equal
deleted
inserted
replaced
9:19a28a953f39 | 10:6a1963ee4aff |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # NOTE: this package is Linux specific at the moment | |
3 | 2 |
4 import subprocess | 3 import subprocess |
5 import os.path | 4 import os.path |
6 import sys | 5 import sys |
6 | |
7 if sys.platform.lower().startswith('win'): | 7 if sys.platform.lower().startswith('win'): |
8 IS_WINDOWS = True | |
8 import win32print | 9 import win32print |
10 else: | |
11 IS_WINDOWS = False | |
9 | 12 |
10 class zebra(object): | 13 class zebra(object): |
14 """A class to communicate with (Zebra) label printers using EPL2""" | |
15 | |
11 def __init__(self, queue=None): | 16 def __init__(self, queue=None): |
12 """queue - name of the printer queue""" | 17 """queue - name of the printer queue (optional)""" |
13 self.queue = queue | 18 self.queue = queue |
14 | 19 |
15 def _output_unix(self, commands): | 20 def _output_unix(self, commands): |
16 if self.queue == 'zebra_python_unittest': | 21 if self.queue == 'zebra_python_unittest': |
17 p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) | 22 p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) |
40 win32print.EndDocPrinter(hPrinter) | 45 win32print.EndDocPrinter(hPrinter) |
41 finally: | 46 finally: |
42 win32print.ClosePrinter(hPrinter) | 47 win32print.ClosePrinter(hPrinter) |
43 | 48 |
44 def output(self, commands): | 49 def output(self, commands): |
50 """Output EPL2 commands to the label printer | |
51 | |
52 commands - EPL2 commands to send to the printer | |
53 """ | |
45 assert self.queue is not None | 54 assert self.queue is not None |
46 if sys.platform.lower().startswith('win'): | 55 if IS_WINDOWS: |
47 self._output_win(commands) | 56 self._output_win(commands) |
48 else: | 57 else: |
49 self._output_unix(commmands) | 58 self._output_unix(commmands) |
50 | 59 |
51 def _getqueues_unix(self): | 60 def _getqueues_unix(self): |
61 for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): | 70 for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): |
62 printers.append(name) | 71 printers.append(name) |
63 return printers | 72 return printers |
64 | 73 |
65 def getqueues(self): | 74 def getqueues(self): |
66 if sys.platform.lower().startswith('win'): | 75 """Returns a list of printer queues on local machine""" |
76 if IS_WINDOWS: | |
67 return self._getqueues_win() | 77 return self._getqueues_win() |
68 else: | 78 else: |
69 return self._getqueues_unix() | 79 return self._getqueues_unix() |
70 | 80 |
71 def setqueue(self, queue): | 81 def setqueue(self, queue): |
82 """Set the printer queue""" | |
72 self.queue = queue | 83 self.queue = queue |
73 | 84 |
74 def setup(self, direct_thermal=None, label_height=None, label_width=None): | 85 def setup(self, direct_thermal=None, label_height=None, label_width=None): |
86 """Set up the label printer. Parameters are not set if they are None. | |
87 | |
88 direct_thermal - True if using direct thermal labels | |
89 label_height - tuple (label height, label gap) in dots | |
90 label_width - in dots | |
91 """ | |
75 commands = '\n' | 92 commands = '\n' |
76 if direct_thermal: | 93 if direct_thermal: |
77 commands += ('OD\n') | 94 commands += ('OD\n') |
78 if label_height: | 95 if label_height: |
79 commands += ('Q%s,%s\n'%(label_height[0],label_height[1])) | 96 commands += ('Q%s,%s\n'%(label_height[0],label_height[1])) |
80 if label_width: | 97 if label_width: |
81 commands += ('q%s\n'%label_width) | 98 commands += ('q%s\n'%label_width) |
82 self.output(commands) | 99 self.output(commands) |
83 | 100 |
84 def store_graphic(self, name, filename): | 101 def store_graphic(self, name, filename): |
85 assert filename.endswith('.pcx') | 102 """Store a .PCX file on the label printer |
103 | |
104 name - name to be used on printer | |
105 filename - local filename | |
106 """ | |
107 assert filename.lower().endswith('.pcx') | |
86 commands = '\nGK"%s"\n'%name | 108 commands = '\nGK"%s"\n'%name |
87 commands += 'GK"%s"\n'%name | 109 commands += 'GK"%s"\n'%name |
88 size = os.path.getsize(filename) | 110 size = os.path.getsize(filename) |
89 commands += 'GM"%s"%s\n'%(name,size) | 111 commands += 'GM"%s"%s\n'%(name,size) |
90 self.output(commands) | 112 self.output(commands) |