From ba7e854a6fa43b763c880c9a1de1f5eea1fe51b4 Mon Sep 17 00:00:00 2001 From: n07070 Date: Fri, 13 Nov 2026 03:01:21 +0100 Subject: [PATCH] Match case for printer type discovery --- src/printers.py | 74 ++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/src/printers.py b/src/printers.py index e69333f..e45ba0d 100644 --- a/src/printers.py +++ b/src/printers.py @@ -90,47 +90,45 @@ class Printers: hex(dev.idProduct), ) - if manufacturer == "EPSON": - try: - # We create a new EscPosPrinter() - self.app.logger.debug("Trying to create a new EPSON printer") - prid = dev.idProduct - vendir = dev.idVendor - escpos_printer = EscPosPrinter( - self.app, vendor_id=vendir, device_id=prid - ) - except Exception as e: - raise e + match (manufacturer): + case ("EPSON"): + try: + # We create a new EscPosPrinter() + self.app.logger.debug("Trying to create a new EPSON printer") + prid = dev.idProduct + vendir = dev.idVendor + 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) + # 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) + case ("Brother"): + try: + # We create a new BrotherPrinter() + self.app.logger.debug("Trying to create a new BROTHER printer") - # We already found the type of printer, - # we don't need an extra comparaison. - continue + brother_printer = BrotherPrinter( + self.app, vendor_id=dev.idVendor, device_id=dev.idProduct + ) + except Exception as e: + self.app.logger.error( + "Could not create a %s printer class with %s:%s", + product, + dev.idVendor, + dev.idProduct, + ) + raise e - # or a Brother Printer - if manufacturer == "Brother": - try: - # We create a new BrotherPrinter() - self.app.logger.debug("Trying to create a new BROTHER printer") - - brother_printer = BrotherPrinter( - self.app, vendor_id=dev.idVendor, device_id=dev.idProduct - ) - except Exception as e: - 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) + # 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) + case (_): + self.app.logger.debug("Found a printer of unknown manufacturer : %s", manufacturer) + # end match self.app.logger.debug("Found %s printers", len(printers))