From 18e4c1b0ac634057ffd3f172f7c6df2be740824a Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 00:34:45 +0200 Subject: [PATCH 1/6] add new flake8-plugins and revise config --- setup.cfg | 10 +++++++++- tox.ini | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index ea43f22b..64dd7c79 100644 --- a/setup.cfg +++ b/setup.cfg @@ -9,4 +9,12 @@ universal=1 [flake8] exclude = .git,.tox,.github,.eggs,__pycache__,doc/conf.py,build,dist,capabilities-data,test,src/escpos/constants.py max-line-length = 120 -# future-imports = absolute_import, division, print_function, unicode_literals # we are not there yet +accept-encoding = utf-8, utf-16 +require-code = True +# FI12 __future__ import "with_statement" missing +# FI15 __future__ import "generator_stop" missing +# FI16 __future__ import "nested_scopes" missing +# FI17 __future__ import "generators" missing +# FI5x __future__ import "xxx" present +ignore = FI12,FI15,FI16,FI17,FI5 +copyright-check = True diff --git a/tox.ini b/tox.ini index 142ca439..7dae2a42 100644 --- a/tox.ini +++ b/tox.ini @@ -25,7 +25,12 @@ commands = sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html [testenv:flake8] basepython = python -# TODO add flake8-future +# TODO add flake8-print after adding logging # TODO add flake8-docstrings deps = flake8 + flake8-coding + flake8-copyright + flake8-future-import + flake8-quotes + flake8-import-order commands = flake8 From c0af9aaaf4fa9829b40197323ad1536091ee2c41 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 01:09:48 +0200 Subject: [PATCH 2/6] remove bad quotes --- README.rst | 6 +- doc/user/usage.rst | 18 +++--- examples/barcodes.py | 2 +- examples/codepage_tables.py | 12 ++-- examples/qr_code.py | 4 +- examples/software_barcode.py | 2 +- examples/weather.py | 28 ++++----- setup.py | 6 +- src/escpos/__init__.py | 2 +- src/escpos/cli.py | 2 +- src/escpos/escpos.py | 114 +++++++++++++++++------------------ src/escpos/exceptions.py | 54 ++++++++--------- src/escpos/image.py | 8 +-- src/escpos/katakana.py | 2 +- src/escpos/magicencode.py | 6 +- src/escpos/printer.py | 20 +++--- 16 files changed, 143 insertions(+), 143 deletions(-) diff --git a/README.rst b/README.rst index fc551e1f..238ad598 100644 --- a/README.rst +++ b/README.rst @@ -59,9 +59,9 @@ The basic usage is: from escpos.printer import Usb """ Seiko Epson Corp. Receipt Printer (EPSON TM-T88III) """ - p = Usb(0x04b8, 0x0202, 0, profile="TM-T88III") - p.text("Hello World\n") - p.image("logo.gif") + p = Usb(0x04b8, 0x0202, 0, profile='TM-T88III') + p.text('Hello World\n') + p.image('logo.gif') p.barcode('1324354657687', 'EAN13', 64, 2, '', '') p.cut() diff --git a/doc/user/usage.rst b/doc/user/usage.rst index 203389b0..f243aad3 100644 --- a/doc/user/usage.rst +++ b/doc/user/usage.rst @@ -67,7 +67,7 @@ IP by DHCP or you set it manually. :: - Epson = printer.Network("192.168.1.99") + Epson = printer.Network('192.168.1.99') Serial printer ^^^^^^^^^^^^^^ @@ -81,7 +81,7 @@ to. :: - Epson = printer.Serial("/dev/tty0") + Epson = printer.Serial('/dev/tty0') Other printers ^^^^^^^^^^^^^^ @@ -93,7 +93,7 @@ passing the device node name. :: - Epson = printer.File("/dev/usb/lp1") + Epson = printer.File('/dev/usb/lp1') The default is "/dev/usb/lp0", so if the printer is located on that node, then you don't necessary need to pass the node name. @@ -110,11 +110,11 @@ on a USB interface. """ Seiko Epson Corp. Receipt Printer M129 Definitions (EPSON TM-T88IV) """ Epson = printer.Usb(0x04b8,0x0202) # Print text - Epson.text("Hello World\n") + Epson.text('Hello World\n') # Print image - Epson.image("logo.gif") + Epson.image('logo.gif') # Print QR Code - Epson.qr("You can readme from your smartphone") + Epson.qr('You can readme from your smartphone') # Print barcode Epson.barcode('1324354657687','EAN13',64,2,'','') # Cut paper @@ -214,7 +214,7 @@ advantage of the fact that `_raw()` accepts binary strings.) from escpos import printer p = printer.Serial() # adapt this to your printer model - file = open("binary-blob.bin", "rb") # read in the file containing your commands in binary-mode + file = open('binary-blob.bin', 'rb') # read in the file containing your commands in binary-mode data = file.read() file.close() @@ -273,8 +273,8 @@ This is probably best explained by an example: d = Dummy() # create ESC/POS for the print job, this should go really fast - d.text("This is my image:\n") - d.image("funny_cat.png") + d.text('This is my image:\n') + d.image('funny_cat.png') d.cut() # send code to printer diff --git a/examples/barcodes.py b/examples/barcodes.py index 7e9c2425..86bc77b6 100644 --- a/examples/barcodes.py +++ b/examples/barcodes.py @@ -2,7 +2,7 @@ # Adapt to your needs -p = Usb(0x0416, 0x5011, profile="POS-5890") +p = Usb(0x0416, 0x5011, profile='POS-5890') # Print software and then hardware barcode with the same content p.soft_barcode('code39', '123456') diff --git a/examples/codepage_tables.py b/examples/codepage_tables.py index a071bf50..2b5e98a4 100644 --- a/examples/codepage_tables.py +++ b/examples/codepage_tables.py @@ -17,9 +17,9 @@ def main(): for codepage in sys.argv[1:] or ['USA']: dummy.set(height=2, width=2) - dummy._raw(codepage + "\n\n\n") + dummy._raw(codepage + '\n\n\n') print_codepage(dummy, codepage) - dummy._raw("\n\n") + dummy._raw('\n\n') dummy.cut() @@ -30,22 +30,22 @@ def print_codepage(printer, codepage): if codepage.isdigit(): codepage = int(codepage) printer._raw(CODEPAGE_CHANGE + six.int2byte(codepage)) - printer._raw("after") + printer._raw('after') else: printer.charcode(codepage) - sep = "" + sep = '' # Table header printer.set(font='b') - printer._raw(" {}\n".format(sep.join(map(lambda s: hex(s)[2:], range(0, 16))))) + printer._raw(' {}\n'.format(sep.join(map(lambda s: hex(s)[2:], range(0, 16))))) printer.set() # The table for x in range(0, 16): # First column printer.set(font='b') - printer._raw("{} ".format(hex(x)[2:])) + printer._raw('{} '.format(hex(x)[2:])) printer.set() for y in range(0, 16): diff --git a/examples/qr_code.py b/examples/qr_code.py index 325f8452..15120b46 100644 --- a/examples/qr_code.py +++ b/examples/qr_code.py @@ -4,7 +4,7 @@ def usage(): - print("usage: qr_code.py ") + print('usage: qr_code.py ') if __name__ == '__main__': @@ -15,5 +15,5 @@ def usage(): content = sys.argv[1] # Adapt to your needs - p = Usb(0x0416, 0x5011, profile="POS-5890") + p = Usb(0x0416, 0x5011, profile='POS-5890') p.qr(content, center=True) diff --git a/examples/software_barcode.py b/examples/software_barcode.py index 2fd3c181..93c8e309 100644 --- a/examples/software_barcode.py +++ b/examples/software_barcode.py @@ -2,7 +2,7 @@ # Adapt to your needs -p = Usb(0x0416, 0x5011, profile="POS-5890") +p = Usb(0x0416, 0x5011, profile='POS-5890') # Some software barcodes p.soft_barcode('code128', 'Hello') diff --git a/examples/weather.py b/examples/weather.py index 31ef4886..4dc9923f 100644 --- a/examples/weather.py +++ b/examples/weather.py @@ -25,22 +25,22 @@ """ Setting up the main pathing """ this_dir, this_filename = os.path.split(__file__) -GRAPHICS_PATH = os.path.join(this_dir, "graphics/climacons/") +GRAPHICS_PATH = os.path.join(this_dir, 'graphics/climacons/') # Adapt to your needs -printer = Usb(0x0416, 0x5011, profile="POS-5890") +printer = Usb(0x0416, 0x5011, profile='POS-5890') # You can get your API Key on www.darksky.net and register a dev account. # Technically you can use any other weather service, of course :) -API_KEY = "YOUR API KEY" +API_KEY = 'YOUR API KEY' -LAT = "22.345490" # Your Location -LONG = "114.189945" # Your Location +LAT = '22.345490' # Your Location +LONG = '114.189945' # Your Location def forecast_icon(idx): icon = data['daily']['data'][idx]['icon'] - image = GRAPHICS_PATH + icon + ".png" + image = GRAPHICS_PATH + icon + '.png' return image @@ -80,23 +80,23 @@ def forecast(idx): def icon(): icon = data['currently']['icon'] - image = GRAPHICS_PATH + icon + ".png" + image = GRAPHICS_PATH + icon + '.png' return image deg = ' C' # Degree symbol on thermal printer, need to find a better way to use a proper degree symbol # if you want Fahrenheit change units= to 'us' -url = "https://api.darksky.net/forecast/" + API_KEY + "/" + LAT + "," + LONG + \ - "?exclude=[alerts,minutely,hourly,flags]&units=si" # change last bit to 'us' for Fahrenheit +url = 'https://api.darksky.net/forecast/' + API_KEY + '/' + LAT + ',' + LONG + \ + '?exclude=[alerts,minutely,hourly,flags]&units=si' # change last bit to 'us' for Fahrenheit response = urllib.urlopen(url) data = json.loads(response.read()) printer.print_and_feed(n=1) -printer.control("LF") +printer.control('LF') printer.set(font='a', height=2, align='center', bold=True, double_height=True) -printer.text("Weather Forecast") -printer.text("\n") +printer.text('Weather Forecast') +printer.text('\n') printer.set(align='center') @@ -104,7 +104,7 @@ def icon(): printer.set(font='a', height=2, align='center', bold=True, double_height=False) printer.text('Current conditions: \n') printer.image(icon()) -printer.text("\n") +printer.text('\n') printer.set(font='a', height=2, align='left', bold=False, double_height=False) temp = data['currently']['temperature'] @@ -124,4 +124,4 @@ def icon(): forecast(0) forecast(1) printer.cut() -printer.control("LF") +printer.control('LF') diff --git a/setup.py b/setup.py index 6b214506..d09c4726 100755 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ base_dir = os.path.dirname(__file__) -src_dir = os.path.join(base_dir, "src") +src_dir = os.path.join(base_dir, 'src') # When executing the setup.py, we need to be able to import ourselves, this # means that we need to add the src/ directory to the sys.path. @@ -54,8 +54,8 @@ def read(fname): 'receipt,', ], platforms='any', - package_dir={"": "src"}, - packages=find_packages(where="src", exclude=["tests", "tests.*"]), + package_dir={'': 'src'}, + packages=find_packages(where='src', exclude=['tests', 'tests.*']), package_data={'': ['COPYING', 'src/escpos/capabilities.json']}, include_package_data=True, classifiers=[ diff --git a/src/escpos/__init__.py b/src/escpos/__init__.py index 3fbc8080..ddf81ba8 100644 --- a/src/escpos/__init__.py +++ b/src/escpos/__init__.py @@ -7,7 +7,7 @@ from __future__ import print_function from __future__ import unicode_literals -__all__ = ["constants", "escpos", "exceptions", "printer"] +__all__ = ['constants', 'escpos', 'exceptions', 'printer'] try: from .version import version as __version__ # noqa diff --git a/src/escpos/cli.py b/src/escpos/cli.py index 665e3b82..dcc377d1 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -550,7 +550,7 @@ def main(): # print command with args getattr(printer, target_command)(**command_arguments) if target_command in REQUIRES_NEWLINE: - printer.text("\n") + printer.text('\n') else: command_arguments['printer'] = printer globals()[target_command](**command_arguments) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 98a6e938..f7a9fa5b 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -89,7 +89,7 @@ def _read(self): """ raise NotImplementedError() - def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl="bitImageRaster", + def image(self, img_source, high_density_vertical=True, high_density_horizontal=True, impl='bitImageRaster', fragment_height=960, center=False): """ Print an image @@ -141,14 +141,14 @@ def image(self, img_source, high_density_vertical=True, high_density_horizontal= fragment_height=fragment_height) return - if impl == "bitImageRaster": + if impl == 'bitImageRaster': # GS v 0, raster format bit image density_byte = (0 if high_density_horizontal else 1) + (0 if high_density_vertical else 2) - header = GS + b"v0" + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) +\ + header = GS + b'v0' + six.int2byte(density_byte) + self._int_low_high(im.width_bytes, 2) +\ self._int_low_high(im.height, 2) self._raw(header + im.to_raster_format()) - if impl == "graphics": + if impl == 'graphics': # GS ( L raster format graphics img_header = self._int_low_high(im.width, 2) + self._int_low_high(im.height, 2) tone = b'0' @@ -160,14 +160,14 @@ def image(self, img_source, high_density_vertical=True, high_density_horizontal= self._image_send_graphics_data(b'0', b'p', header + raster_data) self._image_send_graphics_data(b'0', b'2', b'') - if impl == "bitImageColumn": + if impl == 'bitImageColumn': # ESC *, column format bit image density_byte = (1 if high_density_horizontal else 0) + (32 if high_density_vertical else 0) - header = ESC + b"*" + six.int2byte(density_byte) + self._int_low_high(im.width, 2) - outp = [ESC + b"3" + six.int2byte(16)] # Adjust line-feed size + header = ESC + b'*' + six.int2byte(density_byte) + self._int_low_high(im.width, 2) + outp = [ESC + b'3' + six.int2byte(16)] # Adjust line-feed size for blob in im.to_column_format(high_density_vertical): - outp.append(header + blob + b"\n") - outp.append(ESC + b"2") # Reset line-feed size + outp.append(header + blob + b'\n') + outp.append(ESC + b'2') # Reset line-feed size self._raw(b''.join(outp)) def _image_send_graphics_data(self, m, fn, data): @@ -182,7 +182,7 @@ def _image_send_graphics_data(self, m, fn, data): self._raw(GS + b'(L' + header + m + fn + data) def qr(self, content, ec=QR_ECLEVEL_L, size=3, model=QR_MODEL_2, - native=False, center=False, impl="bitImageRaster"): + native=False, center=False, impl='bitImageRaster'): """ Print QR Code for the provided string :param content: The content of the code. Numeric data will be more efficiently compacted. @@ -198,18 +198,18 @@ def qr(self, content, ec=QR_ECLEVEL_L, size=3, model=QR_MODEL_2, """ # Basic validation if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]: - raise ValueError("Invalid error correction level") + raise ValueError('Invalid error correction level') if not 1 <= size <= 16: - raise ValueError("Invalid block size (must be 1-16)") + raise ValueError('Invalid block size (must be 1-16)') if model not in [QR_MODEL_1, QR_MODEL_2, QR_MICRO]: - raise ValueError("Invalid QR model (must be one of QR_MODEL_1, QR_MODEL_2, QR_MICRO)") - if content == "": + raise ValueError('Invalid QR model (must be one of QR_MODEL_1, QR_MODEL_2, QR_MICRO)') + if content == '': # Handle edge case by printing nothing. return if not native: # Map ESC/POS error correction levels to python 'qrcode' library constant and render to an image if model != QR_MODEL_2: - raise ValueError("Invalid QR model for qrlib rendering (must be QR_MODEL_2)") + raise ValueError('Invalid QR model for qrlib rendering (must be QR_MODEL_2)') python_qr_ec = { QR_ECLEVEL_H: qrcode.constants.ERROR_CORRECT_H, QR_ECLEVEL_L: qrcode.constants.ERROR_CORRECT_L, @@ -220,7 +220,7 @@ def qr(self, content, ec=QR_ECLEVEL_L, size=3, model=QR_MODEL_2, qr_code.add_data(content) qr_code.make(fit=True) qr_img = qr_code.make_image() - im = qr_img._img.convert("RGB") + im = qr_img._img.convert('RGB') # Convert the RGB image in printable image self.text('\n') @@ -230,7 +230,7 @@ def qr(self, content, ec=QR_ECLEVEL_L, size=3, model=QR_MODEL_2, return if center: - raise NotImplementedError("Centering not implemented for native QR rendering") + raise NotImplementedError('Centering not implemented for native QR rendering') # Native 2D code printing cn = b'1' # Code type for QR code @@ -253,7 +253,7 @@ def _send_2d_code_data(self, fn, cn, data, m=b''): :param m: Modifier/variant for function. Often '0' where used. """ if len(m) > 1 or len(cn) != 1 or len(fn) != 1: - raise ValueError("cn and fn must be one byte each.") + raise ValueError('cn and fn must be one byte each.') header = self._int_low_high(len(data) + len(m) + 2, 2) self._raw(GS + b'(k' + header + cn + fn + m + data) @@ -266,16 +266,16 @@ def _int_low_high(inp_number, out_bytes): """ max_input = (256 << (out_bytes * 8) - 1) if not 1 <= out_bytes <= 4: - raise ValueError("Can only output 1-4 bytes") + raise ValueError('Can only output 1-4 bytes') if not 0 <= inp_number <= max_input: - raise ValueError("Number too large. Can only output up to {0} in {1} bytes".format(max_input, out_bytes)) + raise ValueError('Number too large. Can only output up to {0} in {1} bytes'.format(max_input, out_bytes)) outp = b'' for _ in range(0, out_bytes): outp += six.int2byte(inp_number % 256) inp_number //= 256 return outp - def charcode(self, code="AUTO"): + def charcode(self, code='AUTO'): """ Set Character Code Table Sets the control sequence from ``CHARCODE`` in :py:mod:`escpos.constants` as active. It will be sent with @@ -285,7 +285,7 @@ def charcode(self, code="AUTO"): :param code: Name of CharCode :raises: :py:exc:`~escpos.exceptions.CharCodeError` """ - if code.upper() == "AUTO": + if code.upper() == 'AUTO': self.magic.force_encoding(False) else: self.magic.force_encoding(code) @@ -316,7 +316,7 @@ def check_barcode(bc, code): bounds, regex = BARCODE_FORMATS[bc] return any(bound[0] <= len(code) <= bound[1] for bound in bounds) and re_match(regex, code) - def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", + def barcode(self, code, bc, height=64, width=3, pos='BELOW', font='A', align_ct=True, function_type=None, check=True): """ Print Barcode @@ -409,25 +409,25 @@ def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", if bc in BARCODE_TYPES['B']: if not self.profile.supports(BARCODE_B): raise BarcodeTypeError(( - "Barcode type '{bc} not supported for " - "the current printer profile").format(bc=bc)) + 'Barcode type '{bc} not supported for ' + 'the current printer profile').format(bc=bc)) function_type = 'B' else: raise BarcodeTypeError(( - "Barcode type '{bc} is not valid").format(bc=bc)) + 'Barcode type '{bc} is not valid').format(bc=bc)) bc_types = BARCODE_TYPES[function_type.upper()] if bc.upper() not in bc_types.keys(): raise BarcodeTypeError(( - "Barcode '{bc}' not valid for barcode function type " - "{function_type}").format( + 'Barcode '{bc}' not valid for barcode function type ' + '{function_type}').format( bc=bc, function_type=function_type, )) if check and not self.check_barcode(bc, code): raise BarcodeCodeError(( - "Barcode '{code}' not in a valid format for type '{bc}'").format( + 'Barcode '{code}' not in a valid format for type '{bc}'').format( code=code, bc=bc, )) @@ -439,30 +439,30 @@ def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", if 1 <= height <= 255: self._raw(BARCODE_HEIGHT + six.int2byte(height)) else: - raise BarcodeSizeError("height = {height}".format(height=height)) + raise BarcodeSizeError('height = {height}'.format(height=height)) # Width if 2 <= width <= 6: self._raw(BARCODE_WIDTH + six.int2byte(width)) else: - raise BarcodeSizeError("width = {width}".format(width=width)) + raise BarcodeSizeError('width = {width}'.format(width=width)) # Font - if font.upper() == "B": + if font.upper() == 'B': self._raw(BARCODE_FONT_B) else: # DEFAULT FONT: A self._raw(BARCODE_FONT_A) # Position - if pos.upper() == "OFF": + if pos.upper() == 'OFF': self._raw(BARCODE_TXT_OFF) - elif pos.upper() == "BOTH": + elif pos.upper() == 'BOTH': self._raw(BARCODE_TXT_BTH) - elif pos.upper() == "ABOVE": + elif pos.upper() == 'ABOVE': self._raw(BARCODE_TXT_ABV) else: # DEFAULT POSITION: BELOW self._raw(BARCODE_TXT_BLW) self._raw(bc_types[bc.upper()]) - if function_type.upper() == "B": + if function_type.upper() == 'B': self._raw(six.int2byte(len(code))) # Print Code @@ -471,7 +471,7 @@ def barcode(self, code, bc, height=64, width=3, pos="BELOW", font="A", else: raise BarcodeCodeError() - if function_type.upper() == "A": + if function_type.upper() == 'A': self._raw(NUL) def soft_barcode(self, barcode_type, data, impl='bitImageColumn', @@ -489,7 +489,7 @@ def soft_barcode(self, barcode_type, data, impl='bitImageColumn', barcode_class = barcode.get_barcode_class(barcode_type) my_code = barcode_class(data, writer=image_writer) - with open(os.devnull, "wb") as nullfile: + with open(os.devnull, 'wb') as nullfile: my_code.write(nullfile, { 'module_height': module_height, 'module_width': module_width, @@ -640,12 +640,12 @@ def line_spacing(self, spacing=None, divisor=180): return if divisor not in LINESPACING_FUNCS: - raise ValueError("divisor must be either 360, 180 or 60") + raise ValueError('divisor must be either 360, 180 or 60') if (divisor in [360, 180] and (not(0 <= spacing <= 255))): - raise ValueError("spacing must be a int between 0 and 255 when divisor is 360 or 180") + raise ValueError('spacing must be a int between 0 and 255 when divisor is 360 or 180') if divisor == 60 and (not(0 <= spacing <= 85)): - raise ValueError("spacing must be a int between 0 and 85 when divisor is 60") + raise ValueError('spacing must be a int between 0 and 85 when divisor is 60') self._raw(LINESPACING_FUNCS[divisor] + six.int2byte(spacing)) @@ -671,14 +671,14 @@ def cut(self, mode='FULL', feed=True): mode = mode.upper() if mode not in ('FULL', 'PART'): - raise ValueError("Mode must be one of ('FULL', 'PART')") + raise ValueError('Mode must be one of ('FULL', 'PART')') - if mode == "PART": + if mode == 'PART': if self.profile.supports('paperPartCut'): self._raw(PAPER_PART_CUT) elif self.profile.supports('paperFullCut'): self._raw(PAPER_FULL_CUT) - elif mode == "FULL": + elif mode == 'FULL': if self.profile.supports('paperFullCut'): self._raw(PAPER_FULL_CUT) elif self.profile.supports('paperPartCut'): @@ -748,11 +748,11 @@ def hw(self, hw): * SELECT * RESET """ - if hw.upper() == "INIT": + if hw.upper() == 'INIT': self._raw(HW_INIT) - elif hw.upper() == "SELECT": + elif hw.upper() == 'SELECT': self._raw(HW_SELECT) - elif hw.upper() == "RESET": + elif hw.upper() == 'RESET': self._raw(HW_RESET) else: # DEFAULT: DOES NOTHING pass @@ -767,9 +767,9 @@ def print_and_feed(self, n=1): """ if 0 <= n <= 255: # ESC d n - self._raw(ESC + b"d" + six.int2byte(n)) + self._raw(ESC + b'd' + six.int2byte(n)) else: - raise ValueError("n must be betwen 0 and 255") + raise ValueError('n must be betwen 0 and 255') def control(self, ctl, count=5, tab_size=8): """ Feed control sequences @@ -787,13 +787,13 @@ def control(self, ctl, count=5, tab_size=8): :raises: :py:exc:`~escpos.exceptions.TabPosError` """ # Set position - if ctl.upper() == "LF": + if ctl.upper() == 'LF': self._raw(CTL_LF) - elif ctl.upper() == "FF": + elif ctl.upper() == 'FF': self._raw(CTL_FF) - elif ctl.upper() == "CR": + elif ctl.upper() == 'CR': self._raw(CTL_CR) - elif ctl.upper() == "HT": + elif ctl.upper() == 'HT': if not (0 <= count <= 32 and 1 <= tab_size <= 255 and count * tab_size < 256): @@ -804,7 +804,7 @@ def control(self, ctl, count=5, tab_size=8): for iterator in range(1, count): self._raw(six.int2byte(iterator * tab_size)) self._raw(NUL) - elif ctl.upper() == "VT": + elif ctl.upper() == 'VT': self._raw(CTL_VT) def panel_buttons(self, enable=True): @@ -930,16 +930,16 @@ def writelines(self, text, **kwargs): elif isinstance(text, list) or isinstance(text, tuple): lines = text else: - lines = ["{0}".format(text), ] + lines = ['{0}'.format(text), ] # TODO check unicode handling # TODO flush? or on print? (this should prob rather be handled by the _raw-method) for line in lines: self.printer.set(**params) if isinstance(text, six.text_type): - self.printer.text(u"{0}\n".format(line)) + self.printer.text(u'{0}\n'.format(line)) else: - self.printer.text("{0}\n".format(line)) + self.printer.text('{0}\n'.format(line)) def close(self): """ called upon closing the `with`-statement diff --git a/src/escpos/exceptions.py b/src/escpos/exceptions.py index b82e4e09..4d55e461 100644 --- a/src/escpos/exceptions.py +++ b/src/escpos/exceptions.py @@ -51,13 +51,13 @@ class BarcodeTypeError(Error): one of those specified in :py:meth:`escpos.escpos.Escpos.barcode`. The returned error code is `10`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 10 def __str__(self): - return "No Barcode type is defined ({msg})".format(msg=self.msg) + return 'No Barcode type is defined ({msg})'.format(msg=self.msg) class BarcodeSizeError(Error): @@ -67,13 +67,13 @@ class BarcodeSizeError(Error): The size of the barcode has to be in the range that is specified in :py:meth:`escpos.escpos.Escpos.barcode`. The resulting returncode is `20`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 20 def __str__(self): - return "Barcode size is out of range ({msg})".format(msg=self.msg) + return 'Barcode size is out of range ({msg})'.format(msg=self.msg) class BarcodeCodeError(Error): @@ -83,13 +83,13 @@ class BarcodeCodeError(Error): was True and the check failed. The returncode for this exception is `30`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 30 def __str__(self): - return "No Barcode code was supplied ({msg})".format(msg=self.msg) + return 'No Barcode code was supplied ({msg})'.format(msg=self.msg) class ImageSizeError(Error): @@ -97,7 +97,7 @@ class ImageSizeError(Error): The returncode for this exception is `40`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 40 @@ -111,13 +111,13 @@ class ImageWidthError(Error): The return code for this exception is `41`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 41 def __str__(self): - return "Image width is too large ({msg})".format(msg=self.msg) + return 'Image width is too large ({msg})'.format(msg=self.msg) class TextError(Error): @@ -126,13 +126,13 @@ class TextError(Error): This exception is raised when an empty string is passed to :py:meth:`escpos.escpos.Escpos.text`. The returncode for this exception is `50`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 50 def __str__(self): - return "Text string must be supplied to the text() method ({msg})".format(msg=self.msg) + return 'Text string must be supplied to the text() method ({msg})'.format(msg=self.msg) class CashDrawerError(Error): @@ -141,13 +141,13 @@ class CashDrawerError(Error): A valid pin number has to be passed onto the method :py:meth:`escpos.escpos.Escpos.cashdraw`. The returncode for this exception is `60`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 60 def __str__(self): - return "Valid pin must be set to send pulse ({msg})".format(msg=self.msg) + return 'Valid pin must be set to send pulse ({msg})'.format(msg=self.msg) class TabPosError(Error): @@ -157,13 +157,13 @@ class TabPosError(Error): This exception is raised by :py:meth:`escpos.escpos.Escpos.control`. The returncode for this exception is `70`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 70 def __str__(self): - return "Valid tab positions must be in the range 0 to 16 ({msg})".format(msg=self.msg) + return 'Valid tab positions must be in the range 0 to 16 ({msg})'.format(msg=self.msg) class CharCodeError(Error): @@ -172,13 +172,13 @@ class CharCodeError(Error): The supplied charcode-name in :py:meth:`escpos.escpos.Escpos.charcode` is unknown. Ths returncode for this exception is `80`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 80 def __str__(self): - return "Valid char code must be set ({msg})".format(msg=self.msg) + return 'Valid char code must be set ({msg})'.format(msg=self.msg) class USBNotFoundError(Error): @@ -187,13 +187,13 @@ class USBNotFoundError(Error): The USB device seems to be not plugged in. Ths returncode for this exception is `90`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 90 def __str__(self): - return "USB device not found ({msg})".format(msg=self.msg) + return 'USB device not found ({msg})'.format(msg=self.msg) class SetVariableError(Error): @@ -202,13 +202,13 @@ class SetVariableError(Error): Check set variables against minimum and maximum values Ths returncode for this exception is `100`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 100 def __str__(self): - return "Set variable out of range ({msg})".format(msg=self.msg) + return 'Set variable out of range ({msg})'.format(msg=self.msg) # Configuration errors @@ -219,13 +219,13 @@ class ConfigNotFoundError(Error): The default or passed configuration file could not be read Ths returncode for this exception is `200`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 200 def __str__(self): - return "Configuration not found ({msg})".format(msg=self.msg) + return 'Configuration not found ({msg})'.format(msg=self.msg) class ConfigSyntaxError(Error): @@ -234,13 +234,13 @@ class ConfigSyntaxError(Error): The syntax is incorrect Ths returncode for this exception is `210`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 210 def __str__(self): - return "Configuration syntax is invalid ({msg})".format(msg=self.msg) + return 'Configuration syntax is invalid ({msg})'.format(msg=self.msg) class ConfigSectionMissingError(Error): @@ -249,10 +249,10 @@ class ConfigSectionMissingError(Error): The part of the config asked for doesn't exist in the loaded configuration Ths returncode for this exception is `220`. """ - def __init__(self, msg=""): + def __init__(self, msg=''): Error.__init__(self, msg) self.msg = msg self.resultcode = 220 def __str__(self): - return "Configuration section is missing ({msg})".format(msg=self.msg) + return 'Configuration section is missing ({msg})'.format(msg=self.msg) diff --git a/src/escpos/image.py b/src/escpos/image.py index 76b75fd8..e4280484 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -42,14 +42,14 @@ def __init__(self, img_source): # Convert to white RGB background, paste over white background # to strip alpha. img_original = img_original.convert('RGBA') - im = Image.new("RGB", img_original.size, (255, 255, 255)) + im = Image.new('RGB', img_original.size, (255, 255, 255)) im.paste(img_original, mask=img_original.split()[3]) # Convert down to greyscale - im = im.convert("L") + im = im.convert('L') # Invert: Only works on 'L' images im = ImageOps.invert(im) # Pure black and white - self._im = im.convert("1") + self._im = im.convert('1') @property def width(self): @@ -125,7 +125,7 @@ def center(self, max_width): old_width, height = self._im.size new_size = (max_width, height) - new_im = Image.new("1", new_size) + new_im = Image.new('1', new_size) paste_x = int((max_width - old_width) / 2) new_im.paste(self._im, (paste_x, 0)) diff --git a/src/escpos/katakana.py b/src/escpos/katakana.py index 927a59b3..652b720b 100644 --- a/src/escpos/katakana.py +++ b/src/escpos/katakana.py @@ -33,7 +33,7 @@ def encode_katakana(text): # TODO doesn't this discard all that is not in the map? Can we be sure that the input does contain only # encodable characters? We could at least throw an exception if encoding is not possible. pass - return b"".join(encoded) + return b''.join(encoded) TXT_ENC_KATAKANA_MAP = { diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index c87b20c9..12eb0fb8 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -75,11 +75,11 @@ def _get_codepage_char_list(encoding): """ codepage = CodePages.get_encoding(encoding) if 'data' in codepage: - encodable_chars = list("".join(codepage['data'])) + encodable_chars = list(''.join(codepage['data'])) assert(len(encodable_chars) == 128) return encodable_chars elif 'python_encode' in codepage: - encodable_chars = [u" "] * 128 + encodable_chars = [u' '] * 128 for i in range(0, 128): codepoint = i + 128 try: @@ -280,7 +280,7 @@ def _handle_character_failed(self, char): def write_with_encoding(self, encoding, text): if text is not None and type(text) is not six.text_type: - raise Error("The supplied text has to be unicode, but is of type {type}.".format( + raise Error('The supplied text has to be unicode, but is of type {type}.'.format( type=type(text) )) diff --git a/src/escpos/printer.py b/src/escpos/printer.py index a652b98a..1d7e644c 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -54,7 +54,7 @@ def open(self): """ Search device on USB tree and set it as escpos device """ self.device = usb.core.find(idVendor=self.idVendor, idProduct=self.idProduct) if self.device is None: - raise USBNotFoundError("Device not found or cable not plugged in.") + raise USBNotFoundError('Device not found or cable not plugged in.') check_driver = None @@ -68,13 +68,13 @@ def open(self): self.device.detach_kernel_driver(0) except usb.core.USBError as e: if check_driver is not None: - print("Could not detatch kernel driver: {0}".format(str(e))) + print('Could not detatch kernel driver: {0}'.format(str(e))) try: self.device.set_configuration() self.device.reset() except usb.core.USBError as e: - print("Could not set configuration: {0}".format(str(e))) + print('Could not set configuration: {0}'.format(str(e))) def _raw(self, msg): """ Print any command sent in raw format @@ -107,7 +107,7 @@ class Serial(Escpos): """ - def __init__(self, devfile="/dev/ttyS0", baudrate=9600, bytesize=8, timeout=1, + def __init__(self, devfile='/dev/ttyS0', baudrate=9600, bytesize=8, timeout=1, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, xonxoff=False, dsrdtr=True, *args, **kwargs): """ @@ -143,9 +143,9 @@ def open(self): xonxoff=self.xonxoff, dsrdtr=self.dsrdtr) if self.device is not None: - print("Serial printer enabled") + print('Serial printer enabled') else: - print("Unable to open serial printer on: {0}".format(str(self.devfile))) + print('Unable to open serial printer on: {0}'.format(str(self.devfile))) def _raw(self, msg): """ Print any command sent in raw format @@ -209,7 +209,7 @@ def open(self): self.device.connect((self.host, self.port)) if self.device is None: - print("Could not open socket for {0}".format(self.host)) + print('Could not open socket for {0}'.format(self.host)) def _raw(self, msg): """ Print any command sent in raw format @@ -240,7 +240,7 @@ class File(Escpos): """ - def __init__(self, devfile="/dev/usb/lp0", auto_flush=True, *args, **kwargs): + def __init__(self, devfile='/dev/usb/lp0', auto_flush=True, *args, **kwargs): """ :param devfile: Device file under dev filesystem @@ -253,10 +253,10 @@ def __init__(self, devfile="/dev/usb/lp0", auto_flush=True, *args, **kwargs): def open(self): """ Open system file """ - self.device = open(self.devfile, "wb") + self.device = open(self.devfile, 'wb') if self.device is None: - print("Could not open the specified file {0}".format(self.devfile)) + print('Could not open the specified file {0}'.format(self.devfile)) def flush(self): """ Flush printing content """ From 9b8e56cd59c45cb241343444cc9663d92042474f Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 01:23:04 +0200 Subject: [PATCH 3/6] cleanup imports, first part --- examples/barcodes.py | 5 +++++ examples/codepage_tables.py | 9 ++++++--- examples/qr_code.py | 5 +++++ examples/software_barcode.py | 5 +++++ examples/weather.py | 11 +++++++---- setup.py | 6 ++++++ src/escpos/capabilities.py | 5 +++++ 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/examples/barcodes.py b/examples/barcodes.py index 86bc77b6..9eec4f55 100644 --- a/examples/barcodes.py +++ b/examples/barcodes.py @@ -1,3 +1,8 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + from escpos.printer import Usb diff --git a/examples/codepage_tables.py b/examples/codepage_tables.py index 2b5e98a4..e5b0d666 100644 --- a/examples/codepage_tables.py +++ b/examples/codepage_tables.py @@ -1,13 +1,16 @@ """Prints code page tables. """ - +from __future__ import absolute_import +from __future__ import division from __future__ import print_function +from __future__ import unicode_literals -import six import sys from escpos import printer -from escpos.constants import CODEPAGE_CHANGE, ESC, CTL_LF, CTL_FF, CTL_CR, CTL_HT, CTL_VT +from escpos.constants import CODEPAGE_CHANGE, CTL_CR, CTL_FF, CTL_HT, CTL_LF, CTL_VT, ESC + +import six def main(): diff --git a/examples/qr_code.py b/examples/qr_code.py index 15120b46..8fa93415 100644 --- a/examples/qr_code.py +++ b/examples/qr_code.py @@ -1,3 +1,8 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + import sys from escpos.printer import Usb diff --git a/examples/software_barcode.py b/examples/software_barcode.py index 93c8e309..458cff0e 100644 --- a/examples/software_barcode.py +++ b/examples/software_barcode.py @@ -1,3 +1,8 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + from escpos.printer import Usb diff --git a/examples/weather.py b/examples/weather.py index 4dc9923f..b3708202 100644 --- a/examples/weather.py +++ b/examples/weather.py @@ -12,14 +12,17 @@ # Icons taken from http://adamwhitcroft.com/climacons/ # Check out his github: https://github.com/AdamWhitcroft/climacons - +from __future__ import absolute_import +from __future__ import division from __future__ import print_function -from datetime import datetime +from __future__ import unicode_literals + import calendar -import urllib import json -import time import os +import time +import urllib +from datetime import datetime from escpos.printer import Usb diff --git a/setup.py b/setup.py index d09c4726..dbdcaae4 100755 --- a/setup.py +++ b/setup.py @@ -1,7 +1,13 @@ #!/usr/bin/env python +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +# from __future__ import unicode_literals + import os import sys + from setuptools import find_packages, setup diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index 0cf76e12..c39d8c8b 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -1,3 +1,8 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + import re from os import environ, path import pickle From 594ab83fc336fb59e17c3410c1e42cbbf19d681e Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 01:57:37 +0200 Subject: [PATCH 4/6] cleanup imports, second part --- setup.cfg | 3 ++- src/escpos/capabilities.py | 12 ++++----- src/escpos/cli.py | 4 ++- src/escpos/codepages.py | 5 ++++ src/escpos/config.py | 4 ++- src/escpos/escpos.py | 51 +++++++++++++++++++------------------- src/escpos/image.py | 1 + src/escpos/magicencode.py | 6 +++-- src/escpos/printer.py | 6 +++-- 9 files changed, 53 insertions(+), 39 deletions(-) diff --git a/setup.cfg b/setup.cfg index 64dd7c79..5e7ceb6b 100644 --- a/setup.cfg +++ b/setup.cfg @@ -16,5 +16,6 @@ require-code = True # FI16 __future__ import "nested_scopes" missing # FI17 __future__ import "generators" missing # FI5x __future__ import "xxx" present -ignore = FI12,FI15,FI16,FI17,FI5 +# I101 Imported names are in the wrong order. +ignore = FI12,FI15,FI16,FI17,FI5, I101 copyright-check = True diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index c39d8c8b..cebcfe87 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -3,17 +3,17 @@ from __future__ import print_function from __future__ import unicode_literals -import re -from os import environ, path -import pickle import logging +import pickle +import platform +import re import time +from os import environ, path +from tempfile import gettempdir import six -import yaml -from tempfile import gettempdir -import platform +import yaml logging.basicConfig() logger = logging.getLogger(__name__) diff --git a/src/escpos/cli.py b/src/escpos/cli.py index dcc377d1..7728c5bf 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -15,13 +15,15 @@ from __future__ import unicode_literals import argparse +import sys + try: import argcomplete except ImportError: # this CLI works nevertheless without argcomplete pass # noqa -import sys import six + from . import config from . import version diff --git a/src/escpos/codepages.py b/src/escpos/codepages.py index ca63ef3c..a015ea46 100644 --- a/src/escpos/codepages.py +++ b/src/escpos/codepages.py @@ -1,3 +1,8 @@ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + from .capabilities import CAPABILITIES diff --git a/src/escpos/config.py b/src/escpos/config.py index 7528934a..d6a708fa 100644 --- a/src/escpos/config.py +++ b/src/escpos/config.py @@ -10,11 +10,13 @@ from __future__ import unicode_literals import os + import appdirs + import yaml -from . import printer from . import exceptions +from . import printer class Config(object): diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index f7a9fa5b..f6ff53b8 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -15,42 +15,41 @@ from __future__ import print_function from __future__ import unicode_literals -import qrcode +import os import textwrap -import six import time +from abc import ABCMeta, abstractmethod # abstract base class support from re import match as re_match import barcode from barcode.writer import ImageWriter -import os +import qrcode -from .constants import ESC, GS, NUL, QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q -from .constants import QR_MODEL_1, QR_MODEL_2, QR_MICRO, BARCODE_TYPES, BARCODE_HEIGHT, BARCODE_WIDTH +import six + +from .capabilities import BARCODE_B, get_profile from .constants import BARCODE_FONT_A, BARCODE_FONT_B, BARCODE_FORMATS from .constants import BARCODE_TXT_OFF, BARCODE_TXT_BTH, BARCODE_TXT_ABV, BARCODE_TXT_BLW -from .constants import TXT_SIZE, TXT_NORMAL -from .constants import SET_FONT -from .constants import LINESPACING_FUNCS, LINESPACING_RESET -from .constants import LINE_DISPLAY_OPEN, LINE_DISPLAY_CLEAR, LINE_DISPLAY_CLOSE +from .constants import BARCODE_TYPES, BARCODE_HEIGHT, BARCODE_WIDTH from .constants import CD_KICK_DEC_SEQUENCE, CD_KICK_5, CD_KICK_2, PAPER_FULL_CUT, PAPER_PART_CUT -from .constants import HW_RESET, HW_SELECT, HW_INIT from .constants import CTL_VT, CTL_CR, CTL_FF, CTL_LF, CTL_SET_HT, PANEL_BUTTON_OFF, PANEL_BUTTON_ON -from .constants import TXT_STYLE -from .constants import RT_STATUS_ONLINE, RT_MASK_ONLINE +from .constants import ESC, GS, NUL, QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q +from .constants import HW_INIT, HW_RESET, HW_SELECT +from .constants import LINESPACING_FUNCS, LINESPACING_RESET +from .constants import LINE_DISPLAY_OPEN, LINE_DISPLAY_CLEAR, LINE_DISPLAY_CLOSE +from .constants import QR_MODEL_1, QR_MODEL_2, QR_MICRO +from .constants import RT_MASK_ONLINE, RT_STATUS_ONLINE from .constants import RT_STATUS_PAPER, RT_MASK_PAPER, RT_MASK_LOWPAPER, RT_MASK_NOPAPER - -from .exceptions import BarcodeTypeError, BarcodeSizeError, TabPosError -from .exceptions import CashDrawerError, SetVariableError, BarcodeCodeError -from .exceptions import ImageWidthError - +from .constants import SET_FONT +from .constants import TXT_SIZE, TXT_NORMAL +from .constants import TXT_STYLE +from .exceptions import BarcodeCodeError, BarcodeSizeError, BarcodeTypeError +from .exceptions import CashDrawerError, ImageWidthError +from .exceptions import SetVariableError, TabPosError +from .image import EscposImage from .magicencode import MagicEncode -from abc import ABCMeta, abstractmethod # abstract base class support -from escpos.image import EscposImage -from escpos.capabilities import get_profile, BARCODE_B - @six.add_metaclass(ABCMeta) class Escpos(object): @@ -409,17 +408,17 @@ def barcode(self, code, bc, height=64, width=3, pos='BELOW', font='A', if bc in BARCODE_TYPES['B']: if not self.profile.supports(BARCODE_B): raise BarcodeTypeError(( - 'Barcode type '{bc} not supported for ' + "Barcode type '{bc}' not supported for " 'the current printer profile').format(bc=bc)) function_type = 'B' else: raise BarcodeTypeError(( - 'Barcode type '{bc} is not valid').format(bc=bc)) + "Barcode type '{bc}' is not valid").format(bc=bc)) bc_types = BARCODE_TYPES[function_type.upper()] if bc.upper() not in bc_types.keys(): raise BarcodeTypeError(( - 'Barcode '{bc}' not valid for barcode function type ' + "Barcode '{bc}' not valid for barcode function type " '{function_type}').format( bc=bc, function_type=function_type, @@ -427,7 +426,7 @@ def barcode(self, code, bc, height=64, width=3, pos='BELOW', font='A', if check and not self.check_barcode(bc, code): raise BarcodeCodeError(( - 'Barcode '{code}' not in a valid format for type '{bc}'').format( + "Barcode '{code}' not in a valid format for type '{bc}'").format( code=code, bc=bc, )) @@ -671,7 +670,7 @@ def cut(self, mode='FULL', feed=True): mode = mode.upper() if mode not in ('FULL', 'PART'): - raise ValueError('Mode must be one of ('FULL', 'PART')') + raise ValueError("Mode must be one of ('FULL', 'PART')") if mode == 'PART': if self.profile.supports('paperPartCut'): diff --git a/src/escpos/image.py b/src/escpos/image.py index e4280484..55563365 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -14,6 +14,7 @@ from __future__ import unicode_literals import math + from PIL import Image, ImageOps diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 12eb0fb8..d80bc006 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -18,10 +18,12 @@ from __future__ import unicode_literals from builtins import bytes + +import six + +from .codepages import CodePages from .constants import CODEPAGE_CHANGE from .exceptions import Error -from .codepages import CodePages -import six class Encoder(object): diff --git a/src/escpos/printer.py b/src/escpos/printer.py index 1d7e644c..40e7c43a 100644 --- a/src/escpos/printer.py +++ b/src/escpos/printer.py @@ -13,10 +13,12 @@ from __future__ import print_function from __future__ import unicode_literals +import socket + +import serial + import usb.core import usb.util -import serial -import socket from .escpos import Escpos from .exceptions import USBNotFoundError From 87b33367c0801dde06941f23e836cb11ba682d9b Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 02:02:35 +0200 Subject: [PATCH 5/6] add magic coding comment --- examples/barcodes.py | 1 + examples/codepage_tables.py | 1 + examples/qr_code.py | 1 + examples/software_barcode.py | 1 + examples/weather.py | 4 ++-- setup.py | 1 + src/escpos/capabilities.py | 3 +++ src/escpos/cli.py | 1 + src/escpos/codepages.py | 3 +++ src/escpos/config.py | 1 + src/escpos/escpos.py | 2 +- src/escpos/image.py | 1 + src/escpos/magicencode.py | 2 +- 13 files changed, 18 insertions(+), 4 deletions(-) diff --git a/examples/barcodes.py b/examples/barcodes.py index 9eec4f55..96e7f74e 100644 --- a/examples/barcodes.py +++ b/examples/barcodes.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function diff --git a/examples/codepage_tables.py b/examples/codepage_tables.py index e5b0d666..a43d04ea 100644 --- a/examples/codepage_tables.py +++ b/examples/codepage_tables.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """Prints code page tables. """ from __future__ import absolute_import diff --git a/examples/qr_code.py b/examples/qr_code.py index 8fa93415..6af606c6 100644 --- a/examples/qr_code.py +++ b/examples/qr_code.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function diff --git a/examples/software_barcode.py b/examples/software_barcode.py index 458cff0e..b8b95951 100644 --- a/examples/software_barcode.py +++ b/examples/software_barcode.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division from __future__ import print_function diff --git a/examples/weather.py b/examples/weather.py index b3708202..ccd05f4b 100644 --- a/examples/weather.py +++ b/examples/weather.py @@ -1,5 +1,5 @@ -#!/usr/bin/python - +#!/usr/bin/env python +# -*- coding: utf-8 -*- # Adapted script from Adafruit # Weather forecast for Raspberry Pi w/Adafruit Mini Thermal Printer. diff --git a/setup.py b/setup.py index dbdcaae4..6d929fcc 100755 --- a/setup.py +++ b/setup.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- from __future__ import absolute_import from __future__ import division diff --git a/src/escpos/capabilities.py b/src/escpos/capabilities.py index cebcfe87..a4a9e81e 100644 --- a/src/escpos/capabilities.py +++ b/src/escpos/capabilities.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + from __future__ import absolute_import from __future__ import division from __future__ import print_function diff --git a/src/escpos/cli.py b/src/escpos/cli.py index 7728c5bf..15f08cee 100644 --- a/src/escpos/cli.py +++ b/src/escpos/cli.py @@ -1,4 +1,5 @@ #!/usr/bin/env python +# -*- coding: utf-8 -*- # PYTHON_ARGCOMPLETE_OK """ CLI diff --git a/src/escpos/codepages.py b/src/escpos/codepages.py index a015ea46..5f2a17c5 100644 --- a/src/escpos/codepages.py +++ b/src/escpos/codepages.py @@ -1,3 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + from __future__ import absolute_import from __future__ import division from __future__ import print_function diff --git a/src/escpos/config.py b/src/escpos/config.py index d6a708fa..686584a5 100644 --- a/src/escpos/config.py +++ b/src/escpos/config.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ ESC/POS configuration manager. This module contains the implentations of abstract base class :py:class:`Config`. diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index f6ff53b8..84a7aa53 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- """ Main class diff --git a/src/escpos/image.py b/src/escpos/image.py index 55563365..51edaf9b 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Image format handling class This module contains the image format handler :py:class:`EscposImage`. diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index d80bc006..29c05ff2 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/env python # -*- coding: utf-8 -*- """ Magic Encode From 4d479337d3463aa5bd5fa9db82468f9969198a22 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Wed, 16 May 2018 02:15:52 +0200 Subject: [PATCH 6/6] other minor problems --- src/escpos/escpos.py | 10 ++++------ src/escpos/image.py | 2 +- src/escpos/magicencode.py | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 84a7aa53..2444b957 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -421,8 +421,8 @@ def barcode(self, code, bc, height=64, width=3, pos='BELOW', font='A', "Barcode '{bc}' not valid for barcode function type " '{function_type}').format( bc=bc, - function_type=function_type, - )) + function_type=function_type,) + ) if check and not self.check_barcode(bc, code): raise BarcodeCodeError(( @@ -590,8 +590,7 @@ def set(self, align='left', font='a', bold=False, underline=0, width=1, """ if custom_size: - if 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and\ - isinstance(height, int): + if 1 <= width <= 8 and 1 <= height <= 8 and isinstance(width, int) and isinstance(height, int): size_byte = TXT_STYLE['width'][width] + TXT_STYLE['height'][height] self._raw(TXT_SIZE + six.int2byte(size_byte)) else: @@ -640,8 +639,7 @@ def line_spacing(self, spacing=None, divisor=180): if divisor not in LINESPACING_FUNCS: raise ValueError('divisor must be either 360, 180 or 60') - if (divisor in [360, 180] - and (not(0 <= spacing <= 255))): + if (divisor in [360, 180] and (not(0 <= spacing <= 255))): raise ValueError('spacing must be a int between 0 and 255 when divisor is 360 or 180') if divisor == 60 and (not(0 <= spacing <= 85)): raise ValueError('spacing must be a int between 0 and 85 when divisor is 60') diff --git a/src/escpos/image.py b/src/escpos/image.py index 51edaf9b..d121d3f5 100644 --- a/src/escpos/image.py +++ b/src/escpos/image.py @@ -107,7 +107,7 @@ def split(self, fragment_height): :param fragment_height: height of fragment :return: list of PIL objects """ - passes = int(math.ceil(self.height/fragment_height)) + passes = int(math.ceil(self.height // fragment_height)) fragments = [] for n in range(0, passes): left = 0 diff --git a/src/escpos/magicencode.py b/src/escpos/magicencode.py index 29c05ff2..8e67d9c7 100644 --- a/src/escpos/magicencode.py +++ b/src/escpos/magicencode.py @@ -64,7 +64,7 @@ def get_encoding_name(self, encoding): raise ValueError(( 'Encoding "{}" cannot be used for the current profile. ' 'Valid encodings are: {}' - ).format(encoding, ','.join(self.codepages.keys()))) + ).format(encoding, ','.join(self.codepages.keys()))) return encoding @staticmethod