1
0
mirror of https://github.com/python-escpos/python-escpos synced 2025-08-24 09:03:34 +00:00

start removal of six and improve type annotation (#607)

* fix unfinished Python2 -> 3 translation
* remove some six usage
* annotate
* fix regression in Six removal
* mypy: self.enf is always defined
* fix return type of cups.py
* Usb idVendor/idProduct are integers
* self.default_args is always defined
* tweak usb_args, PEP589 is better
* lp.py: reassure mypy
* correctly cast call to CashDrawerError()
* update CUPS test
* add missing close() method in metaclass
* document a bug in typeshed
* query_status() returns bytes as seen in constants.py
* remove more SIX usage
* test examples too
* remove type comment where type is annotated
* adapt behavior of cups printer to match other implementations

---------

Co-authored-by: Patrick Kanzler <dev@pkanzler.de>
Co-authored-by: Patrick Kanzler <4189642+patkan@users.noreply.github.com>
This commit is contained in:
Alexandre Detiste
2023-12-16 22:02:24 +01:00
committed by GitHub
parent 06bdb56937
commit 66a2e78e16
31 changed files with 245 additions and 237 deletions

View File

@@ -84,7 +84,7 @@ class CupsPrinter(Escpos):
return is_usable()
@dependency_pycups
def __init__(self, printer_name: str = "", *args, **kwargs):
def __init__(self, printer_name: str = "", *args, **kwargs) -> None:
"""Class constructor for CupsPrinter.
:param printer_name: CUPS printer name (Optional)
@@ -163,11 +163,10 @@ class CupsPrinter(Escpos):
return
logging.info("CupsPrinter printer enabled")
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Append any command sent in raw format to temporary file.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
self.pending_job = True
try:
@@ -176,8 +175,9 @@ class CupsPrinter(Escpos):
self.pending_job = False
raise TypeError("Bytes required. Printer job not opened")
def send(self):
def send(self) -> None:
"""Send the print job to the printer."""
assert self.device
if self.pending_job:
# Rewind tempfile
self.tmpfile.seek(0)
@@ -190,7 +190,7 @@ class CupsPrinter(Escpos):
)
self._clear()
def _clear(self):
def _clear(self) -> None:
"""Finish the print job.
Remove temporary file.
@@ -198,18 +198,18 @@ class CupsPrinter(Escpos):
self.tmpfile.close()
self.pending_job = False
def _read(self):
def _read(self) -> bytes:
"""Return a single-item array with the accepting state of the print queue.
states: idle = [3], printing a job = [4], stopped = [5]
"""
printer = self.printers.get(self.printer_name, {})
state = printer.get("printer-state")
if not state:
return []
return [state]
if not state or state in [4, 5]:
return b"8" # offline
return b"0" # online
def close(self):
def close(self) -> None:
"""Close CUPS connection.
Send pending job to the printer if needed.

View File

@@ -7,6 +7,7 @@
:copyright: Copyright (c) 2012-2023 Bashlinux and python-escpos
:license: MIT
"""
from typing import List
from ..escpos import Escpos
@@ -39,25 +40,24 @@ class Dummy(Escpos):
"""
return is_usable()
def __init__(self, *args, **kwargs):
def __init__(self, *args, **kwargs) -> None:
"""Init with empty output list."""
Escpos.__init__(self, *args, **kwargs)
self._output_list = []
self._output_list: List[bytes] = []
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
self._output_list.append(msg)
@property
def output(self):
def output(self) -> bytes:
"""Get the data that was sent to this printer."""
return b"".join(self._output_list)
def clear(self):
def clear(self) -> None:
"""Clear the buffer of the printer.
This method can be called if you send the contents to a physical printer
@@ -65,6 +65,6 @@ class Dummy(Escpos):
"""
del self._output_list[:]
def close(self):
def close(self) -> None:
"""Close not implemented for Dummy printer."""
pass

View File

@@ -88,12 +88,12 @@ class File(Escpos):
if self.device:
self.device.flush()
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
assert self.device
self.device.write(msg)
if self.auto_flush:
self.flush()

View File

@@ -182,12 +182,13 @@ class LP(Escpos):
if not self._is_closing:
self.open(_close_opened=False)
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Write raw command(s) to the printer.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
assert self.device is not None
assert self.device.stdin is not None
if self.device.stdin.writable():
self.device.stdin.write(msg)
else:

View File

@@ -110,16 +110,17 @@ class Network(Escpos):
return
logging.info("Network printer enabled")
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
assert self.device
self.device.sendall(msg)
def _read(self):
def _read(self) -> bytes:
"""Read data from the TCP socket."""
assert self.device
return self.device.recv(16)
def close(self) -> None:

View File

@@ -155,16 +155,17 @@ class Serial(Escpos):
return
logging.info("Serial printer enabled")
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
assert self.device
self.device.write(msg)
def _read(self):
def _read(self) -> bytes:
"""Read the data buffer and return it to the caller."""
assert self.device
return self.device.read(16)
def close(self) -> None:

View File

@@ -74,9 +74,9 @@ class Usb(Escpos):
def __init__(
self,
idVendor: str = "",
idProduct: str = "",
usb_args: Dict[str, str] = {},
idVendor: Optional[int] = None,
idProduct: Optional[int] = None,
usb_args: Dict[str, Union[str, int]] = {},
timeout: Union[int, float] = 0,
in_ep: int = 0x82,
out_ep: int = 0x01,
@@ -181,16 +181,17 @@ class Usb(Escpos):
except usb.core.USBError as e:
logging.error("Could not set configuration: %s", str(e))
def _raw(self, msg):
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
assert self.device
self.device.write(self.out_ep, msg, self.timeout)
def _read(self):
def _read(self) -> bytes:
"""Read a data buffer and return it to the caller."""
assert self.device
return self.device.read(self.in_ep, 16)
@dependency_usb

View File

@@ -76,7 +76,7 @@ class Win32Raw(Escpos):
return is_usable()
@dependency_win32print
def __init__(self, printer_name: str = "", *args, **kwargs):
def __init__(self, printer_name: str = "", *args, **kwargs) -> None:
"""Initialize default printer."""
Escpos.__init__(self, *args, **kwargs)
self.printer_name = printer_name
@@ -148,14 +148,17 @@ class Win32Raw(Escpos):
win32print.ClosePrinter(self._device)
self._device = False
def _raw(self, msg) -> None:
def _raw(self, msg: bytes) -> None:
"""Print any command sent in raw format.
:param msg: arbitrary code to be printed
:type msg: bytes
"""
if self.printer_name is None:
raise DeviceNotFoundError("Printer not found")
if not self.device:
raise DeviceNotFoundError("Printer job not opened")
win32print.WritePrinter(self.device, msg)
win32print.WritePrinter(self.device, msg) # type: ignore
# there is a bug in the typeshed
# https://github.com/mhammond/pywin32/blob/main/win32/src/win32print/win32print.cpp#L976
# https://github.com/python/typeshed/blob/main/stubs/pywin32/win32/win32print.pyi#L27C4-L27C4