Mercurial > hg > zebra
diff zebra.py @ 31:f77ca0963d6d 0.2.0
Fix language bug, convert to pyproject.toml
author | Ben Croston <ben@croston.org> |
---|---|
date | Wed, 12 Feb 2025 15:13:51 +0000 |
parents | 63d1260cc64e |
children |
line wrap: on
line diff
--- a/zebra.py Tue Sep 01 15:57:55 2020 +0100 +++ b/zebra.py Wed Feb 12 15:13:51 2025 +0000 @@ -1,6 +1,6 @@ #!/usr/bin/env python3 -# Copyright (c) 2011-2020 Ben Croston +# Copyright (c) 2011-2025 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 @@ -30,6 +30,7 @@ IS_WINDOWS = False import subprocess + class Zebra: """A class to communicate with (Zebra) label printers""" @@ -39,9 +40,11 @@ def _output_unix(self, commands): if self.queue == 'zebra_python_unittest': - p = subprocess.Popen(['cat','-'], stdin=subprocess.PIPE) + p = subprocess.Popen(['cat', '-'], stdin=subprocess.PIPE) else: - p = subprocess.Popen(['lpr','-P{}'.format(self.queue),'-oraw'], stdin=subprocess.PIPE) + p = subprocess.Popen( + ['lpr', '-P{}'.format(self.queue), '-oraw'], stdin=subprocess.PIPE + ) p.communicate(commands) p.stdin.close() @@ -51,7 +54,7 @@ return hPrinter = win32print.OpenPrinter(self.queue) try: - hJob = win32print.StartDocPrinter(hPrinter, 1, ('Label',None,'RAW')) + win32print.StartDocPrinter(hPrinter, 1, ('Label', None, 'RAW')) try: win32print.StartPagePrinter(hPrinter) win32print.WritePrinter(hPrinter, commands) @@ -68,7 +71,7 @@ encoding - Encoding used if 'commands' is not a byte string """ assert self.queue is not None - if type(commands) != bytes: + if not isinstance(commands, bytes): commands = str(commands).encode(encoding=encoding) if IS_WINDOWS: self._output_win(commands) @@ -84,7 +87,11 @@ def _getqueues_unix(self): queues = [] try: - output = subprocess.check_output(['lpstat','-p'], universal_newlines=True) + output = subprocess.check_output( + ['lpstat', '-p'], + universal_newlines=True, + env={"LANG": "en_US.UTF-8"} + ) except subprocess.CalledProcessError: return [] for line in output.split('\n'): @@ -94,7 +101,7 @@ def _getqueues_win(self): printers = [] - for (a,b,name,d) in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): + for a, b, name, d in win32print.EnumPrinters(win32print.PRINTER_ENUM_LOCAL): printers.append(name) return printers @@ -121,9 +128,9 @@ if direct_thermal: commands += 'OD\n' if label_height: - commands += 'Q%s,%s\n'%(label_height[0],label_height[1]) + commands += 'Q%s,%s\n' % (label_height[0], label_height[1]) if label_width: - commands += 'q%s\n'%label_width + commands += 'q%s\n' % label_width self.output(commands) def reset_default(self): @@ -135,10 +142,10 @@ self.output('\n^@\n') def autosense(self): - """Run AutoSense by sending an EPL2 command - Get the printer to detect label and gap length and set the sensor levels - """ - self.output('\nxa\n') + """Run AutoSense by sending an EPL2 command + Get the printer to detect label and gap length and set the sensor levels + """ + self.output('\nxa\n') def store_graphic(self, name, filename): """Store a 1 bit PCX file on the label printer, using EPL2. @@ -147,12 +154,12 @@ filename - local filename """ assert filename.lower().endswith('.pcx') - commands = '\nGK"%s"\n'%name - commands += 'GK"%s"\n'%name + commands = '\nGK"%s"\n' % name + commands += 'GK"%s"\n' % name size = os.path.getsize(filename) - commands += 'GM"%s"%s\n'%(name,size) + commands += 'GM"%s"%s\n' % (name, size) self.output(commands) - self.output(open(filename,'rb').read()) + self.output(open(filename, 'rb').read()) def print_graphic(self, x, y, width, length, data, qty): """Print a label from 1 bit data, using EPL2 @@ -163,18 +170,28 @@ data - raw graphical data, in bytes qty - number of labels to print """ - assert type(data) == bytes + assert isinstance(data, bytes) assert width % 8 == 0 # make sure width is a multiple of 8 - assert (width//8) * length == len(data) - commands = b"\nN\nGW%d,%d,%d,%d,%s\nP%d\n"%(x, y, width//8, length, data, qty) + assert (width // 8) * length == len(data) + commands = b'\nN\nGW%d,%d,%d,%d,%s\nP%d\n' % ( + x, + y, + width // 8, + length, + data, + qty, + ) self.output(commands) + if __name__ == '__main__': z = Zebra() - print('Printer queues found:',z.getqueues()) + print('Printer queues found:', z.getqueues()) z.setqueue('zebra_python_unittest') - z.setup(direct_thermal=True, label_height=(406,32), label_width=609) # 3" x 2" direct thermal label - z.store_graphic('logo','logo.pcx') + z.setup( + direct_thermal=True, label_height=(406, 32), label_width=609 + ) # 3" x 2" direct thermal label + z.store_graphic('logo', 'logo.pcx') label = """ N GG419,40,"logo" @@ -185,4 +202,3 @@ P1 """ z.output(label) -