Mercurial > hg > zebra
changeset 0:5bd9ee5c0cae
Initial version
author | Ben Croston <ben@fuzzyduckbrewery.co.uk> |
---|---|
date | Fri, 12 Aug 2011 21:40:12 +0100 |
parents | |
children | 21c3229ed401 |
files | .hgignore INSTALL LICENCE MANIFEST MANIFEST.in README setup.py zebra.py |
diffstat | 8 files changed, 200 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,3 @@ +syntax: glob +*.pyc +*.*~
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INSTALL Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,4 @@ +python setup.py install + or +python3 setup.py install +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LICENCE Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,20 @@ +Copyright (c) 2011 Ben Croston + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MANIFEST Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,7 @@ +# file GENERATED by distutils, do NOT edit +INSTALL +LICENCE +MANIFEST.in +README +setup.py +zebra.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MANIFEST.in Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,1 @@ +include MANIFEST.in INSTALL README LICENCE
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,31 @@ +============ +Zebra-0.0.1a +============ + +Note: + +- Windows support is not yet available but is currently being developed. +- Mac OSX may be supported but has not yet been tested. + +Usage: + +:: + + z = zebra( [printerqueue] ) + - Constructor with optional printerqueue + + z.getqueues() + - Return a list containing a list of printer queues + + z.setqueue( printerqueue ) + - Set the printer queue + + z.setup(self, direct_transfer=None, label_height=None, label_width=None) + - Set up the printer + + z.store_graphic(self, name, filename) + - Store a graphics .PCX file on printer + + z.output(commands) + - Output EPL2 commands to the printer +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.py Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,43 @@ +#!/usr/bin/env python +import sys +from distutils.core import setup + +try: + from distutils.command.build_py import build_py_2to3 as build_py +except ImportError: + from distutils.command.build_py import build_py + +PLATFORM_IS_WINDOWS = sys.platform.lower().startswith('win') + +classifiers = ['Development Status :: 3 - Alpha', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: Unix', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.2', + 'Topic :: Printing'] + +long_description = open('README').read() + +if PLATFORM_IS_WINDOWS: + requires = ['win32print'] +else: + requires = [] + +setup(name = 'zebra', + version = '0.0.1a', + py_modules = ['zebra'], + author = 'Ben Croston', + author_email = 'ben@croston.org', + maintainer = 'Ben Croston', + maintainer_email = 'ben@croston.org', +# url = 'http://www.wyre-it.co.uk/zebra/', + description = 'A package to communicate with (Zebra) label printers using EPL2', + long_description = long_description, + platforms = 'Windows, Unix', + classifiers = classifiers, + license = 'MIT', + cmdclass = {'build_py': build_py}, + requires = requires, + ) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/zebra.py Fri Aug 12 21:40:12 2011 +0100 @@ -0,0 +1,91 @@ +#!/usr/bin/env python +# NOTE: this package is Linux specific at the moment + +import subprocess +import os.path +import sys +if sys.platform.lower().startswith('win'): + import win32print + +class zebra(object): + def __init__(self, queue=None): + """queue - name of the printer queue as returned by lpstat -d""" + self.queue = queue + + def _output_unix(self, commands): + if self.queue == 'zebra_python_unittest': + p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) + else: + p = subprocess.Popen(['lpr','-P%s'%self.queue], stdin=subprocess.PIPE) + if type(commands) == bytes: + p.communicate(commands) + else: + p.communicate(str(commands).encode()) + p.stdin.close() + + def _output_win(self, commands): + raise Exception('Not yet implemented') + + def output(self, commands): + assert self.queue is not None + if sys.platform.startswith('win'): + self._output_win(commands) + else: + self._output_unix(commmands) + + def _getqueues_unix(self): + queues = [] + output = subprocess.check_output(['lpstat','-p'], universal_newlines=True) + for line in output.split('\n'): + if line.startswith('printer'): + queues.append(line.split(' ')[1]) + return queues + + def _getqueues_win(self): + raise Exception('Not yet implemented') + + def getqueues(self): + if sys.platform.startswith('win'): + return self._getqueues_win() + else: + return self._getqueues_unix() + + def setqueue(self,queue): + self.queue = queue + + def setup(self, direct_transfer=None, label_height=None, label_width=None): + commands = '\n' + if direct_transfer: + commands += ('OD\n') + if label_height: + commands += ('Q%s,%s\n'%(label_height[0],label_height[1])) + if label_width: + commands += ('q%s\n'%label_width) + self.output(commands) + + def store_graphic(self, name, filename): + assert filename.endswith('.pcx') + commands = '\nGK"%s"\n'%name + commands += 'GK"%s"\n'%name + size = os.path.getsize(filename) + commands += 'GM"%s"%s\n'%(name,size) + self.output(commands) + self.output(open(filename,'rb').read()) + +if __name__ == '__main__': + z = zebra() + print 'Printer queues found:',z.getqueues() + z.setqueue('zebra_python_unittest') +# z.setup(direct_transfer=True, label_height=(406,32), label_width=609) # 3" x 2" label +# z.store_graphic('logo','logo.pcx') + label = """ +N +GG419,40,"logo" +A40,80,0,4,1,1,N,"Tangerine Duck 4.4%" +A40,198,0,3,1,1,N,"Duty paid on 39.9l" +A40,240,0,3,1,1,N,"Gyle: 123 Best Before: 16/09/2011" +A40,320,0,4,1,1,N,"Pump & Truncheon" +P1 +""" + z.output(label) +