From 6697922b74b9500bd8033149daf566efc3709c04 Mon Sep 17 00:00:00 2001 From: Patrick Kanzler Date: Sun, 19 Jun 2016 12:25:40 +0200 Subject: [PATCH] refactor PEP8 and code style --- escpos/cli.py | 6 ++++-- escpos/config.py | 2 +- escpos/constants.py | 42 +++++++++++++++++++++--------------------- escpos/escpos.py | 23 ++++++++++++++--------- escpos/exceptions.py | 2 ++ escpos/printer.py | 2 ++ setup.py | 8 ++++---- 7 files changed, 48 insertions(+), 37 deletions(-) diff --git a/escpos/cli.py b/escpos/cli.py index 8a18676..e2e8a83 100644 --- a/escpos/cli.py +++ b/escpos/cli.py @@ -18,6 +18,7 @@ import sys import six from . import config + # Must be defined before it's used in DEMO_FUNCTIONS def str_to_bool(string): """ Used as a type in argparse so that we get back a proper @@ -34,7 +35,7 @@ REQUIRES_NEWLINE = ('qr', 'barcode', 'text', 'block_text') # Value: A list of dictionaries to pass to the escpos function as arguments. DEMO_FUNCTIONS = { 'text': [ - {'txt': 'Hello, World!\n',} + {'txt': 'Hello, World!\n', } ], 'qr': [ {'content': 'This tests a QR code'}, @@ -427,6 +428,7 @@ ESCPOS_COMMANDS = [ }, ] + def main(): """ @@ -504,7 +506,6 @@ def main(): saved_config.load(config_path) printer = saved_config.printer() - if not printer: raise Exception('No printers loaded from config') @@ -519,6 +520,7 @@ def main(): command_arguments['printer'] = printer globals()[target_command](**command_arguments) + def demo(printer, **kwargs): """ Prints specificed demos. Called when CLI is passed `demo`. This function diff --git a/escpos/config.py b/escpos/config.py index 4a5d077..7528934 100644 --- a/escpos/config.py +++ b/escpos/config.py @@ -16,6 +16,7 @@ import yaml from . import printer from . import exceptions + class Config(object): """ Configuration handler class. @@ -115,4 +116,3 @@ class Config(object): self._printer = getattr(printer, self._printer_name)(**self._printer_config) return self._printer - diff --git a/escpos/constants.py b/escpos/constants.py index 0e1ce82..74b26eb 100644 --- a/escpos/constants.py +++ b/escpos/constants.py @@ -31,21 +31,21 @@ FS = b'\x1c' GS = b'\x1d' # Feed control sequences -CTL_LF = b'\n' # Print and line feed -CTL_FF = b'\f' # Form feed -CTL_CR = b'\r' # Carriage return -CTL_HT = b'\t' # Horizontal tab -CTL_SET_HT = ESC + b'\x44' # Set horizontal tab positions -CTL_VT = b'\v' # Vertical tab +CTL_LF = b'\n' # Print and line feed +CTL_FF = b'\f' # Form feed +CTL_CR = b'\r' # Carriage return +CTL_HT = b'\t' # Horizontal tab +CTL_SET_HT = ESC + b'\x44' # Set horizontal tab positions +CTL_VT = b'\v' # Vertical tab # Printer hardware HW_INIT = ESC + b'@' # Clear data in buffer and reset modes HW_SELECT = ESC + b'=\x01' # Printer select -HW_RESET = ESC + b'\x3f\x0a\x00' # Reset printer hardware - # (TODO: Where is this specified?) +HW_RESET = ESC + b'\x3f\x0a\x00' # Reset printer hardware + # (TODO: Where is this specified?) -#{ Cash Drawer (ESC p ) +# Cash Drawer (ESC p ) _CASH_DRAWER = lambda m, t1='', t2='': ESC + b'p' + m + six.int2byte(t1) + six.int2byte(t2) CD_KICK_2 = _CASH_DRAWER(b'\x00', 50, 50) # Sends a pulse to pin 2 [] CD_KICK_5 = _CASH_DRAWER(b'\x01', 50, 50) # Sends a pulse to pin 5 [] @@ -137,7 +137,7 @@ BARCODE_FONT_B = _SET_HRI_FONT(b'\x01') # Font type B for HRI barcode chars BARCODE_HEIGHT = GS + b'h' # Barcode Height [1-255] BARCODE_WIDTH = GS + b'w' # Barcode Width [2-6] -#NOTE: This isn't actually an ESC/POS command. It's the common prefix to the +# NOTE: This isn't actually an ESC/POS command. It's the common prefix to the # two "print bar code" commands: # - Type A: "GS k NUL" # - TYPE B: "GS k " @@ -184,13 +184,13 @@ BARCODE_TYPES = { 'B': BARCODE_TYPE_B, } -## QRCode error correction levels +# QRCode error correction levels QR_ECLEVEL_L = 0 QR_ECLEVEL_M = 1 QR_ECLEVEL_Q = 2 QR_ECLEVEL_H = 3 -## QRcode models +# QRcode models QR_MODEL_1 = 1 QR_MODEL_2 = 2 QR_MICRO = 3 @@ -205,12 +205,12 @@ S_RASTER_2H = _PRINT_RASTER_IMG(b'\x02') # Set raster image double height S_RASTER_Q = _PRINT_RASTER_IMG(b'\x03') # Set raster image quadruple # Printing Density -PD_N50 = GS + b'\x7c\x00' # Printing Density -50% -PD_N37 = GS + b'\x7c\x01' # Printing Density -37.5% -PD_N25 = GS + b'\x7c\x02' # Printing Density -25% -PD_N12 = GS + b'\x7c\x03' # Printing Density -12.5% -PD_0 = GS + b'\x7c\x04' # Printing Density 0% -PD_P50 = GS + b'\x7c\x08' # Printing Density +50% -PD_P37 = GS + b'\x7c\x07' # Printing Density +37.5% -PD_P25 = GS + b'\x7c\x06' # Printing Density +25% -PD_P12 = GS + b'\x7c\x05' # Printing Density +12.5% +PD_N50 = GS + b'\x7c\x00' # Printing Density -50% +PD_N37 = GS + b'\x7c\x01' # Printing Density -37.5% +PD_N25 = GS + b'\x7c\x02' # Printing Density -25% +PD_N12 = GS + b'\x7c\x03' # Printing Density -12.5% +PD_0 = GS + b'\x7c\x04' # Printing Density 0% +PD_P50 = GS + b'\x7c\x08' # Printing Density +50% +PD_P37 = GS + b'\x7c\x07' # Printing Density +37.5% +PD_P25 = GS + b'\x7c\x06' # Printing Density +25% +PD_P12 = GS + b'\x7c\x05' # Printing Density +12.5% diff --git a/escpos/escpos.py b/escpos/escpos.py index 40137f7..081130a 100644 --- a/escpos/escpos.py +++ b/escpos/escpos.py @@ -24,6 +24,7 @@ from .exceptions import * from abc import ABCMeta, abstractmethod # abstract base class support from escpos.image import EscposImage + @six.add_metaclass(ABCMeta) class Escpos(object): """ ESC/POS Printer object @@ -122,11 +123,14 @@ class Escpos(object): """ Print QR Code for the provided string :param content: The content of the code. Numeric data will be more efficiently compacted. - :param ec: Error-correction level to use. One of QR_ECLEVEL_L (default), QR_ECLEVEL_M, QR_ECLEVEL_Q or QR_ECLEVEL_H. + :param ec: Error-correction level to use. One of QR_ECLEVEL_L (default), QR_ECLEVEL_M, QR_ECLEVEL_Q or + QR_ECLEVEL_H. Higher error correction results in a less compact code. :param size: Pixel size to use. Must be 1-16 (default 3) - :param model: QR code model to use. Must be one of QR_MODEL_1, QR_MODEL_2 (default) or QR_MICRO (not supported by all printers). - :param native: True to render the code on the printer, False to render the code as an image and send it to the printer (Default) + :param model: QR code model to use. Must be one of QR_MODEL_1, QR_MODEL_2 (default) or QR_MICRO (not supported + by all printers). + :param native: True to render the code on the printer, False to render the code as an image and send it to the + printer (Default) """ # Basic validation if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]: @@ -143,11 +147,11 @@ class Escpos(object): if model != 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, - QR_ECLEVEL_M: qrcode.constants.ERROR_CORRECT_M, - QR_ECLEVEL_Q: qrcode.constants.ERROR_CORRECT_Q - } + QR_ECLEVEL_H: qrcode.constants.ERROR_CORRECT_H, + QR_ECLEVEL_L: qrcode.constants.ERROR_CORRECT_L, + QR_ECLEVEL_M: qrcode.constants.ERROR_CORRECT_M, + QR_ECLEVEL_Q: qrcode.constants.ERROR_CORRECT_Q + } qr_code = qrcode.QRCode(version=None, box_size=size, border=1, error_correction=python_qr_ec[ec]) qr_code.add_data(content) qr_code.make(fit=True) @@ -435,7 +439,8 @@ class Escpos(object): col_count = self.columns if columns is None else columns self.text(textwrap.fill(txt, col_count)) - def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False, flip=False): + def set(self, align='left', font='a', text_type='normal', width=1, height=1, density=9, invert=False, smooth=False, + flip=False): """ Set text properties by sending them to the printer :param align: horizontal position for text, possible values are: diff --git a/escpos/exceptions.py b/escpos/exceptions.py index 629b94f..d0e2bd6 100644 --- a/escpos/exceptions.py +++ b/escpos/exceptions.py @@ -210,6 +210,7 @@ class ConfigNotFoundError(Error): def __str__(self): return "Configuration not found ({msg})".format(msg=self.msg) + class ConfigSyntaxError(Error): """ The configuration file is invalid @@ -224,6 +225,7 @@ class ConfigSyntaxError(Error): def __str__(self): return "Configuration syntax is invalid ({msg})".format(msg=self.msg) + class ConfigSectionMissingError(Error): """ The configuration file is missing a section diff --git a/escpos/printer.py b/escpos/printer.py index b6ef158..4e10c1a 100644 --- a/escpos/printer.py +++ b/escpos/printer.py @@ -21,6 +21,7 @@ import socket from .escpos import Escpos from .exceptions import * + class Usb(Escpos): """ USB printer @@ -261,6 +262,7 @@ class File(Escpos): self.device.flush() self.device.close() + class Dummy(Escpos): """ Dummy printer diff --git a/setup.py b/setup.py index b949e2d..1e4e383 100755 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ import os import sys from setuptools import setup -from setuptools.command.test import test as TestCommand +from setuptools.command.test import test as test_command def read(fname): @@ -11,18 +11,18 @@ def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() -class Tox(TestCommand): +class Tox(test_command): """proxy class that enables tox to be run with setup.py test""" user_options = [('tox-args=', 'a', "Arguments to pass to tox")] def initialize_options(self): """initialize the user-options""" - TestCommand.initialize_options(self) + test_command.initialize_options(self) self.tox_args = None def finalize_options(self): """finalize user-options""" - TestCommand.finalize_options(self) + test_command.finalize_options(self) self.test_args = [] self.test_suite = True