
* Add self-open mechanism * self-open mechanism through the 'device' property * Separate open() and store connection in 'device' * Restore device status to False on close() * Add default value to all params + type annotations * Add generic DeviceNotFoundError exception * Update USBNotFoundError return code * Enhance connectors consistency * Fix LP printer stall * Fix LP waste of paper due to auto-flush + flush on close * Move platform dependent printers' guard to init --------- Co-authored-by: Patrick Kanzler <4189642+patkan@users.noreply.github.com>
160 lines
4.8 KiB
Python
160 lines
4.8 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
"""This module contains the implementation of the Win32Raw printer driver.
|
|
|
|
:author: python-escpos developers
|
|
:organization: `python-escpos <https://github.com/python-escpos>`_
|
|
:copyright: Copyright (c) 2012-2023 Bashlinux and python-escpos
|
|
:license: MIT
|
|
"""
|
|
|
|
import functools
|
|
import logging
|
|
from typing import Literal, Optional, Type, Union
|
|
|
|
from ..escpos import Escpos
|
|
from ..exceptions import DeviceNotFoundError
|
|
|
|
#: keeps track if the win32print dependency could be loaded (:py:class:`escpos.printer.Win32Raw`)
|
|
_DEP_WIN32PRINT = False
|
|
|
|
|
|
try:
|
|
import win32print
|
|
|
|
_DEP_WIN32PRINT = True
|
|
except ImportError:
|
|
pass
|
|
|
|
|
|
def is_usable() -> bool:
|
|
"""Indicate whether this component can be used due to dependencies."""
|
|
usable = False
|
|
if _DEP_WIN32PRINT:
|
|
usable = True
|
|
return usable
|
|
|
|
|
|
def dependency_win32print(func):
|
|
"""Indicate dependency on win32print."""
|
|
|
|
@functools.wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
"""Throw a RuntimeError if win32print not installed."""
|
|
if not is_usable():
|
|
raise RuntimeError(
|
|
"Printing with Win32Raw requires a win32print library to"
|
|
"be installed. Please refer to the documentation on"
|
|
"what to install and install the dependencies for win32print."
|
|
)
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
class Win32Raw(Escpos):
|
|
"""Printer binding for win32 API.
|
|
|
|
Uses the module pywin32 for printing.
|
|
|
|
inheritance:
|
|
|
|
.. inheritance-diagram:: escpos.printer.Win32Raw
|
|
:parts: 1
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
def is_usable() -> bool:
|
|
"""Indicate whether this printer class is usable.
|
|
|
|
Will return True if dependencies are available.
|
|
Will return False if not.
|
|
"""
|
|
return is_usable()
|
|
|
|
@dependency_win32print
|
|
def __init__(self, printer_name: str = "", *args, **kwargs):
|
|
"""Initialize default printer."""
|
|
Escpos.__init__(self, *args, **kwargs)
|
|
self.printer_name = printer_name
|
|
self.job_name = ""
|
|
|
|
self._device: Union[
|
|
Literal[False],
|
|
Literal[None],
|
|
Type[win32print.OpenPrinter],
|
|
] = False
|
|
|
|
@property
|
|
def printers(self) -> dict:
|
|
"""Available Windows printers."""
|
|
return {
|
|
printer["pPrinterName"]: printer
|
|
for printer in win32print.EnumPrinters(win32print.PRINTER_ENUM_NAME, "", 4)
|
|
}
|
|
|
|
def open(
|
|
self, job_name: str = "python-escpos", raise_not_found: bool = True
|
|
) -> None:
|
|
"""Open connection to default printer.
|
|
|
|
By default raise an exception if device is not found.
|
|
|
|
:param raise_not_found: Default True.
|
|
False to log error but do not raise exception.
|
|
|
|
:raises: :py:exc:`~escpos.exceptions.DeviceNotFoundError`
|
|
"""
|
|
if self._device:
|
|
self.close()
|
|
|
|
self.job_name = job_name
|
|
try:
|
|
# Name validation, set default if no given name
|
|
self.printer_name = self.printer_name or win32print.GetDefaultPrinter()
|
|
assert self.printer_name in self.printers, "Incorrect printer name"
|
|
# Open device
|
|
self.device: Optional[
|
|
Type[win32print.OpenPrinter]
|
|
] = win32print.OpenPrinter(self.printer_name)
|
|
if self.device:
|
|
self.current_job = win32print.StartDocPrinter(
|
|
self.device, 1, (job_name, None, "RAW")
|
|
)
|
|
win32print.StartPagePrinter(self.device)
|
|
except AssertionError as e:
|
|
# Raise exception or log error and cancel
|
|
self.device = None
|
|
if raise_not_found:
|
|
raise DeviceNotFoundError(
|
|
f"Unable to start a print job for the printer {self.printer_name}:"
|
|
+ f"\n{e}"
|
|
)
|
|
else:
|
|
logging.error("Win32Raw printing %s not available", self.printer_name)
|
|
return
|
|
logging.info("Win32Raw printer enabled")
|
|
|
|
def close(self) -> None:
|
|
"""Close connection to default printer."""
|
|
if not self._device:
|
|
return
|
|
logging.info("Closing Win32Raw connection to printer %s", self.printer_name)
|
|
win32print.EndPagePrinter(self._device)
|
|
win32print.EndDocPrinter(self._device)
|
|
win32print.ClosePrinter(self._device)
|
|
self._device = False
|
|
|
|
def _raw(self, msg):
|
|
"""Print any command sent in raw format.
|
|
|
|
:param msg: arbitrary code to be printed
|
|
:type msg: bytes
|
|
"""
|
|
if self.printer_name is None:
|
|
raise Exception("Printer not found")
|
|
if not self.device:
|
|
raise Exception("Printer job not opened")
|
|
win32print.WritePrinter(self.device, msg)
|