refactor PEP8 and code style

This commit is contained in:
Patrick Kanzler 2016-06-19 12:25:40 +02:00
parent e814396bd8
commit 6697922b74
7 changed files with 48 additions and 37 deletions

View File

@ -18,6 +18,7 @@ import sys
import six import six
from . import config from . import config
# Must be defined before it's used in DEMO_FUNCTIONS # Must be defined before it's used in DEMO_FUNCTIONS
def str_to_bool(string): def str_to_bool(string):
""" Used as a type in argparse so that we get back a proper """ 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. # Value: A list of dictionaries to pass to the escpos function as arguments.
DEMO_FUNCTIONS = { DEMO_FUNCTIONS = {
'text': [ 'text': [
{'txt': 'Hello, World!\n',} {'txt': 'Hello, World!\n', }
], ],
'qr': [ 'qr': [
{'content': 'This tests a QR code'}, {'content': 'This tests a QR code'},
@ -427,6 +428,7 @@ ESCPOS_COMMANDS = [
}, },
] ]
def main(): def main():
""" """
@ -504,7 +506,6 @@ def main():
saved_config.load(config_path) saved_config.load(config_path)
printer = saved_config.printer() printer = saved_config.printer()
if not printer: if not printer:
raise Exception('No printers loaded from config') raise Exception('No printers loaded from config')
@ -519,6 +520,7 @@ def main():
command_arguments['printer'] = printer command_arguments['printer'] = printer
globals()[target_command](**command_arguments) globals()[target_command](**command_arguments)
def demo(printer, **kwargs): def demo(printer, **kwargs):
""" """
Prints specificed demos. Called when CLI is passed `demo`. This function Prints specificed demos. Called when CLI is passed `demo`. This function

View File

@ -16,6 +16,7 @@ import yaml
from . import printer from . import printer
from . import exceptions from . import exceptions
class Config(object): class Config(object):
""" Configuration handler class. """ Configuration handler class.
@ -115,4 +116,3 @@ class Config(object):
self._printer = getattr(printer, self._printer_name)(**self._printer_config) self._printer = getattr(printer, self._printer_name)(**self._printer_config)
return self._printer return self._printer

View File

@ -31,21 +31,21 @@ FS = b'\x1c'
GS = b'\x1d' GS = b'\x1d'
# Feed control sequences # Feed control sequences
CTL_LF = b'\n' # Print and line feed CTL_LF = b'\n' # Print and line feed
CTL_FF = b'\f' # Form feed CTL_FF = b'\f' # Form feed
CTL_CR = b'\r' # Carriage return CTL_CR = b'\r' # Carriage return
CTL_HT = b'\t' # Horizontal tab CTL_HT = b'\t' # Horizontal tab
CTL_SET_HT = ESC + b'\x44' # Set horizontal tab positions CTL_SET_HT = ESC + b'\x44' # Set horizontal tab positions
CTL_VT = b'\v' # Vertical tab CTL_VT = b'\v' # Vertical tab
# Printer hardware # Printer hardware
HW_INIT = ESC + b'@' # Clear data in buffer and reset modes HW_INIT = ESC + b'@' # Clear data in buffer and reset modes
HW_SELECT = ESC + b'=\x01' # Printer select HW_SELECT = ESC + b'=\x01' # Printer select
HW_RESET = ESC + b'\x3f\x0a\x00' # Reset printer hardware HW_RESET = ESC + b'\x3f\x0a\x00' # Reset printer hardware
# (TODO: Where is this specified?) # (TODO: Where is this specified?)
#{ Cash Drawer (ESC p <pin> <on time: 2*ms> <off time: 2*ms>) # Cash Drawer (ESC p <pin> <on time: 2*ms> <off time: 2*ms>)
_CASH_DRAWER = lambda m, t1='', t2='': ESC + b'p' + m + six.int2byte(t1) + six.int2byte(t2) _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_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 [] 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_HEIGHT = GS + b'h' # Barcode Height [1-255]
BARCODE_WIDTH = GS + b'w' # Barcode Width [2-6] 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: # two "print bar code" commands:
# - Type A: "GS k <type as integer> <data> NUL" # - Type A: "GS k <type as integer> <data> NUL"
# - TYPE B: "GS k <type as letter> <data length> <data>" # - TYPE B: "GS k <type as letter> <data length> <data>"
@ -184,13 +184,13 @@ BARCODE_TYPES = {
'B': BARCODE_TYPE_B, 'B': BARCODE_TYPE_B,
} }
## QRCode error correction levels # QRCode error correction levels
QR_ECLEVEL_L = 0 QR_ECLEVEL_L = 0
QR_ECLEVEL_M = 1 QR_ECLEVEL_M = 1
QR_ECLEVEL_Q = 2 QR_ECLEVEL_Q = 2
QR_ECLEVEL_H = 3 QR_ECLEVEL_H = 3
## QRcode models # QRcode models
QR_MODEL_1 = 1 QR_MODEL_1 = 1
QR_MODEL_2 = 2 QR_MODEL_2 = 2
QR_MICRO = 3 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 S_RASTER_Q = _PRINT_RASTER_IMG(b'\x03') # Set raster image quadruple
# Printing Density # Printing Density
PD_N50 = GS + b'\x7c\x00' # Printing Density -50% PD_N50 = GS + b'\x7c\x00' # Printing Density -50%
PD_N37 = GS + b'\x7c\x01' # Printing Density -37.5% PD_N37 = GS + b'\x7c\x01' # Printing Density -37.5%
PD_N25 = GS + b'\x7c\x02' # Printing Density -25% PD_N25 = GS + b'\x7c\x02' # Printing Density -25%
PD_N12 = GS + b'\x7c\x03' # Printing Density -12.5% PD_N12 = GS + b'\x7c\x03' # Printing Density -12.5%
PD_0 = GS + b'\x7c\x04' # Printing Density 0% PD_0 = GS + b'\x7c\x04' # Printing Density 0%
PD_P50 = GS + b'\x7c\x08' # Printing Density +50% PD_P50 = GS + b'\x7c\x08' # Printing Density +50%
PD_P37 = GS + b'\x7c\x07' # Printing Density +37.5% PD_P37 = GS + b'\x7c\x07' # Printing Density +37.5%
PD_P25 = GS + b'\x7c\x06' # Printing Density +25% PD_P25 = GS + b'\x7c\x06' # Printing Density +25%
PD_P12 = GS + b'\x7c\x05' # Printing Density +12.5% PD_P12 = GS + b'\x7c\x05' # Printing Density +12.5%

View File

@ -24,6 +24,7 @@ from .exceptions import *
from abc import ABCMeta, abstractmethod # abstract base class support from abc import ABCMeta, abstractmethod # abstract base class support
from escpos.image import EscposImage from escpos.image import EscposImage
@six.add_metaclass(ABCMeta) @six.add_metaclass(ABCMeta)
class Escpos(object): class Escpos(object):
""" ESC/POS Printer object """ ESC/POS Printer object
@ -122,11 +123,14 @@ class Escpos(object):
""" Print QR Code for the provided string """ Print QR Code for the provided string
:param content: The content of the code. Numeric data will be more efficiently compacted. :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. Higher error correction results in a less compact code.
:param size: Pixel size to use. Must be 1-16 (default 3) :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 model: QR code model to use. Must be one of QR_MODEL_1, QR_MODEL_2 (default) or QR_MICRO (not supported
: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) 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 # Basic validation
if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]: 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: 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 = { python_qr_ec = {
QR_ECLEVEL_H: qrcode.constants.ERROR_CORRECT_H, QR_ECLEVEL_H: qrcode.constants.ERROR_CORRECT_H,
QR_ECLEVEL_L: qrcode.constants.ERROR_CORRECT_L, QR_ECLEVEL_L: qrcode.constants.ERROR_CORRECT_L,
QR_ECLEVEL_M: qrcode.constants.ERROR_CORRECT_M, QR_ECLEVEL_M: qrcode.constants.ERROR_CORRECT_M,
QR_ECLEVEL_Q: qrcode.constants.ERROR_CORRECT_Q 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 = qrcode.QRCode(version=None, box_size=size, border=1, error_correction=python_qr_ec[ec])
qr_code.add_data(content) qr_code.add_data(content)
qr_code.make(fit=True) qr_code.make(fit=True)
@ -435,7 +439,8 @@ class Escpos(object):
col_count = self.columns if columns is None else columns col_count = self.columns if columns is None else columns
self.text(textwrap.fill(txt, col_count)) 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 """ Set text properties by sending them to the printer
:param align: horizontal position for text, possible values are: :param align: horizontal position for text, possible values are:

View File

@ -210,6 +210,7 @@ class ConfigNotFoundError(Error):
def __str__(self): def __str__(self):
return "Configuration not found ({msg})".format(msg=self.msg) return "Configuration not found ({msg})".format(msg=self.msg)
class ConfigSyntaxError(Error): class ConfigSyntaxError(Error):
""" The configuration file is invalid """ The configuration file is invalid
@ -224,6 +225,7 @@ class ConfigSyntaxError(Error):
def __str__(self): 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): class ConfigSectionMissingError(Error):
""" The configuration file is missing a section """ The configuration file is missing a section

View File

@ -21,6 +21,7 @@ import socket
from .escpos import Escpos from .escpos import Escpos
from .exceptions import * from .exceptions import *
class Usb(Escpos): class Usb(Escpos):
""" USB printer """ USB printer
@ -261,6 +262,7 @@ class File(Escpos):
self.device.flush() self.device.flush()
self.device.close() self.device.close()
class Dummy(Escpos): class Dummy(Escpos):
""" Dummy printer """ Dummy printer

View File

@ -3,7 +3,7 @@
import os import os
import sys import sys
from setuptools import setup from setuptools import setup
from setuptools.command.test import test as TestCommand from setuptools.command.test import test as test_command
def read(fname): def read(fname):
@ -11,18 +11,18 @@ def read(fname):
return open(os.path.join(os.path.dirname(__file__), fname)).read() 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""" """proxy class that enables tox to be run with setup.py test"""
user_options = [('tox-args=', 'a', "Arguments to pass to tox")] user_options = [('tox-args=', 'a', "Arguments to pass to tox")]
def initialize_options(self): def initialize_options(self):
"""initialize the user-options""" """initialize the user-options"""
TestCommand.initialize_options(self) test_command.initialize_options(self)
self.tox_args = None self.tox_args = None
def finalize_options(self): def finalize_options(self):
"""finalize user-options""" """finalize user-options"""
TestCommand.finalize_options(self) test_command.finalize_options(self)
self.test_args = [] self.test_args = []
self.test_suite = True self.test_suite = True