Apply black formatter

This commit is contained in:
n07070
2026-06-04 00:31:04 +02:00
parent a2d1779e2b
commit 3c490e10b4
8 changed files with 103 additions and 49 deletions

View File

@@ -3,15 +3,18 @@ A collection of Printers.
It has methods to discover printers, and provides an interface for the methods expected from printers.
"""
from collections.abc import Mapping, Set
import usb.core
import usb.util
from printer import Printer, EscPosPrinter, BrotherPrinter, PrinterType
class Printers():
class Printers:
"""
Finds and creates a set of Printer that can be used by the Workers to print.
"""
def __init__(self, app):
"""
Discover printers connected to the computer and return a Collection of Printer()
@@ -36,10 +39,14 @@ class Printers():
printers = set()
# Find all connected USB devices
devices = usb.core.find(find_all=True,custom_match=_FindClass(7))
devices = usb.core.find(find_all=True, custom_match=_FindClass(7))
if not devices:
self.app.logger.warning("No USB devices of class 7 ( printers ) found or pyusb could not access the bus.")
raise RuntimeError("No USB devices of class 7 ( printers ) found or pyusb could not access the bus.")
self.app.logger.warning(
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
)
raise RuntimeError(
"No USB devices of class 7 ( printers ) found or pyusb could not access the bus."
)
for dev in devices:
# Attempt to get the manufacturer and product strings
@@ -52,7 +59,13 @@ class Printers():
product = usb.util.get_string(dev, dev.iProduct)
except Exception:
product = "Unknown"
self.app.logger.debug("Looking at %s %s (%s:%s)", manufacturer, product, hex(dev.idVendor), hex(dev.idProduct))
self.app.logger.debug(
"Looking at %s %s (%s:%s)",
manufacturer,
product,
hex(dev.idVendor),
hex(dev.idProduct),
)
if manufacturer == "EPSON":
try:
@@ -60,13 +73,15 @@ class Printers():
self.app.logger.debug("Trying to creat a new EPSON printer")
prid = dev.idProduct
vendir = dev.idVendor
escpos_printer = EscPosPrinter(self.app, vendor_id=vendir, device_id=prid)
escpos_printer = EscPosPrinter(
self.app, vendor_id=vendir, device_id=prid
)
except Exception as e:
raise e
# If the object creation is successfull, we add it to the list of Printers
printers.add(escpos_printer)
self.app.logger.debug("Found a %s printer" % manufacturer )
self.app.logger.debug("Found a %s printer" % manufacturer)
# We already found the type of printer,
# we don't need an extra comparaison.
@@ -79,14 +94,20 @@ class Printers():
self.app.logger.debug("Trying to creat a new BROTHER printer")
prid = dev.idProduct
vendir = dev.idVendor
brother_printer = BrotherPrinter(self.app, vendor_id=vendir,device_id=prid)
brother_printer = BrotherPrinter(
self.app, vendor_id=vendir, device_id=prid
)
except Exception as e:
self.app.logger.error("Could not create a %s printer class with %s:%s" % product, dev.idVendor, dev.idProduct)
self.app.logger.error(
"Could not create a %s printer class with %s:%s" % product,
dev.idVendor,
dev.idProduct,
)
raise e
# If the object creation is successfull, we add it to the list of Printers
printers.add(brother_printer)
self.app.logger.debug("Found a %s printer" % manufacturer )
self.app.logger.debug("Found a %s printer" % manufacturer)
self.app.logger.debug("Found %s printers" % len(printers))
return printers
@@ -110,9 +131,10 @@ class Printers():
return NotImplementedError()
class _FindClass():
class _FindClass:
def __init__(self, class_):
self._class = class_
def __call__(self, device):
# first, let's check the device
if device.bDeviceClass == self._class:
@@ -121,10 +143,7 @@ class _FindClass():
# interface that matches our class
for cfg in device:
# find_descriptor: what's it?
intf = usb.util.find_descriptor(
cfg,
bInterfaceClass=self._class
)
intf = usb.util.find_descriptor(cfg, bInterfaceClass=self._class)
if intf is not None:
return True