1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-09-13 09:09:58 +00:00

reformat codebase

This commit is contained in:
Patrick Kanzler
2021-10-30 18:15:22 +02:00
parent 109a5d8a92
commit 435f2bba24
41 changed files with 1706 additions and 1398 deletions

View File

@@ -19,7 +19,7 @@ from .exceptions import USBNotFoundError
class Usb(Escpos):
""" USB printer
"""USB printer
This class describes a printer that natively speaks USB.
@@ -30,8 +30,17 @@ class Usb(Escpos):
"""
def __init__(self, idVendor, idProduct, usb_args=None, timeout=0, in_ep=0x82, out_ep=0x01,
*args, **kwargs): # noqa: N803
def __init__(
self,
idVendor,
idProduct,
usb_args=None,
timeout=0,
in_ep=0x82,
out_ep=0x01,
*args,
**kwargs
): # noqa: N803
"""
:param idVendor: Vendor ID
:param idProduct: Product ID
@@ -47,13 +56,13 @@ class Usb(Escpos):
usb_args = usb_args or {}
if idVendor:
usb_args['idVendor'] = idVendor
usb_args["idVendor"] = idVendor
if idProduct:
usb_args['idProduct'] = idProduct
usb_args["idProduct"] = idProduct
self.open(usb_args)
def open(self, usb_args):
""" Search device on USB tree and set it as escpos device.
"""Search device on USB tree and set it as escpos device.
:param usb_args: USB arguments
"""
@@ -92,7 +101,7 @@ class Usb(Escpos):
print("Could not set configuration: {0}".format(str(e)))
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes
@@ -100,18 +109,18 @@ class Usb(Escpos):
self.device.write(self.out_ep, msg, self.timeout)
def _read(self):
""" Reads a data buffer and returns it to the caller. """
"""Reads a data buffer and returns it to the caller."""
return self.device.read(self.in_ep, 16)
def close(self):
""" Release USB interface """
"""Release USB interface"""
if self.device:
usb.util.dispose_resources(self.device)
self.device = None
class Serial(Escpos):
""" Serial printer
"""Serial printer
This class describes a printer that is connected by serial interface.
@@ -122,9 +131,19 @@ class Serial(Escpos):
"""
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):
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
):
"""
:param devfile: Device file under dev filesystem
@@ -149,13 +168,19 @@ class Serial(Escpos):
self.open()
def open(self):
""" Setup serial port and set is as escpos device """
"""Setup serial port and set is as escpos device"""
if self.device is not None and self.device.is_open:
self.close()
self.device = serial.Serial(port=self.devfile, baudrate=self.baudrate,
bytesize=self.bytesize, parity=self.parity,
stopbits=self.stopbits, timeout=self.timeout,
xonxoff=self.xonxoff, dsrdtr=self.dsrdtr)
self.device = serial.Serial(
port=self.devfile,
baudrate=self.baudrate,
bytesize=self.bytesize,
parity=self.parity,
stopbits=self.stopbits,
timeout=self.timeout,
xonxoff=self.xonxoff,
dsrdtr=self.dsrdtr,
)
if self.device is not None:
print("Serial printer enabled")
@@ -163,7 +188,7 @@ class Serial(Escpos):
print("Unable to open serial printer on: {0}".format(str(self.devfile)))
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes
@@ -171,18 +196,18 @@ class Serial(Escpos):
self.device.write(msg)
def _read(self):
""" Reads a data buffer and returns it to the caller. """
"""Reads a data buffer and returns it to the caller."""
return self.device.read(16)
def close(self):
""" Close Serial interface """
"""Close Serial interface"""
if self.device is not None and self.device.is_open:
self.device.flush()
self.device.close()
class Network(Escpos):
""" Network printer
"""Network printer
This class is used to attach to a networked printer. You can also use this in order to attach to a printer that
is forwarded with ``socat``.
@@ -218,7 +243,7 @@ class Network(Escpos):
self.open()
def open(self):
""" Open TCP socket with ``socket``-library and set it as escpos device """
"""Open TCP socket with ``socket``-library and set it as escpos device"""
self.device = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.device.settimeout(self.timeout)
self.device.connect((self.host, self.port))
@@ -227,7 +252,7 @@ class Network(Escpos):
print("Could not open socket for {0}".format(self.host))
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes
@@ -235,12 +260,12 @@ class Network(Escpos):
self.device.sendall(msg)
def _read(self):
""" Read data from the TCP socket """
"""Read data from the TCP socket"""
return self.device.recv(16)
def close(self):
""" Close TCP connection """
"""Close TCP connection"""
if self.device is not None:
try:
self.device.shutdown(socket.SHUT_RDWR)
@@ -250,7 +275,7 @@ class Network(Escpos):
class File(Escpos):
""" Generic file printer
"""Generic file printer
This class is used for parallel port printer or other printers that are directly attached to the filesystem.
Note that you should stay away from using USB-to-Parallel-Adapter since they are unreliable
@@ -275,18 +300,18 @@ class File(Escpos):
self.open()
def open(self):
""" Open system file """
"""Open system file"""
self.device = open(self.devfile, "wb")
if self.device is None:
print("Could not open the specified file {0}".format(self.devfile))
def flush(self):
""" Flush printing content """
"""Flush printing content"""
self.device.flush()
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes
@@ -296,14 +321,14 @@ class File(Escpos):
self.flush()
def close(self):
""" Close system file """
"""Close system file"""
if self.device is not None:
self.device.flush()
self.device.close()
class Dummy(Escpos):
""" Dummy printer
"""Dummy printer
This class is used for saving commands to a variable, for use in situations where
there is no need to send commands to an actual printer. This includes
@@ -317,13 +342,12 @@ class Dummy(Escpos):
"""
def __init__(self, *args, **kwargs):
"""
"""
""" """
Escpos.__init__(self, *args, **kwargs)
self._output_list = []
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes
@@ -332,11 +356,11 @@ class Dummy(Escpos):
@property
def output(self):
""" Get the data that was sent to this printer """
return b''.join(self._output_list)
"""Get the data that was sent to this printer"""
return b"".join(self._output_list)
def clear(self):
""" Clear the buffer of the printer
"""Clear the buffer of the printer
This method can be called if you send the contents to a physical printer
and want to use the Dummy printer for new output.
@@ -356,6 +380,7 @@ except ImportError:
pass
if _WIN32PRINT:
class Win32Raw(Escpos):
def __init__(self, printer_name=None, *args, **kwargs):
Escpos.__init__(self, *args, **kwargs)
@@ -370,7 +395,9 @@ if _WIN32PRINT:
if self.printer_name is None:
raise Exception("Printer not found")
self.hPrinter = win32print.OpenPrinter(self.printer_name)
self.current_job = win32print.StartDocPrinter(self.hPrinter, 1, (job_name, None, "RAW"))
self.current_job = win32print.StartDocPrinter(
self.hPrinter, 1, (job_name, None, "RAW")
)
win32print.StartPagePrinter(self.hPrinter)
def close(self):
@@ -382,7 +409,7 @@ if _WIN32PRINT:
self.hPrinter = None
def _raw(self, msg):
""" Print any command sent in raw format
"""Print any command sent in raw format
:param msg: arbitrary code to be printed
:type msg: bytes