Apply linting
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
|
||||
# import brother_ql
|
||||
from time import sleep
|
||||
import os.path
|
||||
|
||||
from PIL import Image, ImageEnhance
|
||||
import numpy as np
|
||||
|
||||
# Importing the module to manage the connection to the printer.
|
||||
import escpos.printer
|
||||
# import brother_ql
|
||||
from time import sleep, gmtime, strftime
|
||||
import os.path
|
||||
from PIL import Image, ImageEnhance, ImageOps
|
||||
import numpy as np
|
||||
|
||||
|
||||
class Printer(object):
|
||||
"""
|
||||
@@ -75,10 +79,14 @@ class Printer(object):
|
||||
try:
|
||||
# This also calls open(), which we need to close()
|
||||
# or else the device will appear as busy.
|
||||
p = escpos.printer.Usb(self.device_id, self.vendor_id, 0, profile="TM-P80")
|
||||
p = escpos.printer.Usb(
|
||||
self.device_id, self.vendor_id, 0, profile="TM-P80"
|
||||
)
|
||||
except Exception as e:
|
||||
self.app.logger.error(
|
||||
"The USB device is not plugged in, trying again %s : %s",waiting_elapsed, str(e)
|
||||
"The USB device is not plugged in, trying again %s : %s",
|
||||
waiting_elapsed,
|
||||
str(e),
|
||||
)
|
||||
pass
|
||||
|
||||
@@ -87,7 +95,10 @@ class Printer(object):
|
||||
self.ready = True
|
||||
self.app.logger.debug("Printer online !")
|
||||
except Exception as e:
|
||||
self.app.logger.error("Error while getting the printer online %s : %s",waiting_elapsed, str(e)
|
||||
self.app.logger.error(
|
||||
"Error while getting the printer online %s : %s",
|
||||
waiting_elapsed,
|
||||
str(e),
|
||||
)
|
||||
pass
|
||||
|
||||
@@ -128,8 +139,10 @@ class Printer(object):
|
||||
|
||||
def _print_sms(self, msg, signature="", bold=False):
|
||||
|
||||
if not isinstance(msg,str):
|
||||
self.app.logger.error("It is not possible to print a " + str(type(msg)) + ", only strings.")
|
||||
if not isinstance(msg, str):
|
||||
self.app.logger.error(
|
||||
"It is not possible to print a " + str(type(msg)) + ", only strings."
|
||||
)
|
||||
raise ValueError
|
||||
|
||||
# We make sure that the signature is not something too goofy
|
||||
@@ -168,7 +181,9 @@ class Printer(object):
|
||||
self.printer.close()
|
||||
except Exception as e:
|
||||
self.app.logger.error("Unable to print because : " + str(e))
|
||||
raise RuntimeError("Unable to print a SMS, the printer couldn't do it.") from e
|
||||
raise RuntimeError(
|
||||
"Unable to print a SMS, the printer couldn't do it."
|
||||
) from e
|
||||
|
||||
self.app.logger.info("Printed text")
|
||||
return True
|
||||
@@ -201,7 +216,9 @@ class Printer(object):
|
||||
self.app.logger.debug("Proccessing the image")
|
||||
path = _process_image(self, path)
|
||||
except RuntimeError as e:
|
||||
self.app.logger.error("Error while processing the image, aborting print : %s",str(e))
|
||||
self.app.logger.error(
|
||||
"Error while processing the image, aborting print : %s", str(e)
|
||||
)
|
||||
raise e
|
||||
else:
|
||||
self.app.logger.warning("Not proccessing the image")
|
||||
@@ -226,7 +243,9 @@ class Printer(object):
|
||||
try:
|
||||
self.printer.close()
|
||||
except Exception as e:
|
||||
self.app.logger.error("Could not close the printer connexion %s", str(e))
|
||||
self.app.logger.error(
|
||||
"Could not close the printer connexion %s", str(e)
|
||||
)
|
||||
raise RuntimeError("Could not close the printer connexion. ") from e
|
||||
|
||||
self.app.logger.info("Printed a picture")
|
||||
@@ -261,11 +280,13 @@ class Printer(object):
|
||||
def print_task(self, task_type, data):
|
||||
"""Execute actual print based on task type"""
|
||||
match (task_type.value):
|
||||
case ("text"):
|
||||
self._print_sms(data["txt"],signature=data["sign"])
|
||||
case ("image"):
|
||||
self._print_img(data["img"], signature=data["sign"],process=data["process"])
|
||||
case ("cut"):
|
||||
case "text":
|
||||
self._print_sms(data["txt"], signature=data["sign"])
|
||||
case "image":
|
||||
self._print_img(
|
||||
data["img"], signature=data["sign"], process=data["process"]
|
||||
)
|
||||
case "cut":
|
||||
self._cut()
|
||||
case _:
|
||||
raise RuntimeError("This task type is not supported")
|
||||
@@ -324,6 +345,7 @@ def _process_image(self, path):
|
||||
|
||||
return jpeg_path
|
||||
|
||||
|
||||
def discover_printers():
|
||||
"""
|
||||
We try to find all the connected printers ( 0 or n ) to this system.
|
||||
@@ -336,8 +358,8 @@ def discover_printers():
|
||||
04f9 Brother Industries, Ltd
|
||||
"""
|
||||
|
||||
def find_and_parse_borther_ql_printer():
|
||||
|
||||
def find_and_parse_borther_ql_printer():
|
||||
|
||||
## We might be able to no use this because there is a `discover` command in https://github.com/pklaus/brother_ql#usage
|
||||
|
||||
@@ -352,18 +374,18 @@ def find_and_parse_borther_ql_printer():
|
||||
|
||||
for backend_name in ["pyusb", "linux_kernel"]:
|
||||
try:
|
||||
#print(f"Trying backend: {backend_name}")
|
||||
# print(f"Trying backend: {backend_name}")
|
||||
backend = backend_factory(backend_name)
|
||||
available_devices = backend["list_available_devices"]()
|
||||
#print(f"Found {len(available_devices)} devices with {backend_name} backend")
|
||||
# print(f"Found {len(available_devices)} devices with {backend_name} backend")
|
||||
|
||||
for printer in available_devices:
|
||||
#print(f"Found device: {printer}")
|
||||
# print(f"Found device: {printer}")
|
||||
identifier = printer["identifier"]
|
||||
parts = identifier.split("/")
|
||||
|
||||
if len(parts) < 4:
|
||||
#print(f"Skipping device with invalid identifier format: {identifier}")
|
||||
# print(f"Skipping device with invalid identifier format: {identifier}")
|
||||
continue
|
||||
|
||||
protocol = parts[0]
|
||||
@@ -373,7 +395,7 @@ def find_and_parse_borther_ql_printer():
|
||||
try:
|
||||
vendor_id, product_id = device_info.split(":")
|
||||
except ValueError:
|
||||
#print(f"Invalid device info format: {device_info}")
|
||||
# print(f"Invalid device info format: {device_info}")
|
||||
continue
|
||||
|
||||
# Default model
|
||||
@@ -386,9 +408,9 @@ def find_and_parse_borther_ql_printer():
|
||||
if m.product_id == product_id_int:
|
||||
model = m.identifier
|
||||
break
|
||||
#print(f"Matched printer model: {model}")
|
||||
# print(f"Matched printer model: {model}")
|
||||
except ValueError:
|
||||
#print(f"Invalid product ID format: {product_id}")
|
||||
# print(f"Invalid product ID format: {product_id}")
|
||||
continue
|
||||
|
||||
printer_info = {
|
||||
@@ -400,15 +422,16 @@ def find_and_parse_borther_ql_printer():
|
||||
"product_id": product_id,
|
||||
"serial_number": serial_number,
|
||||
}
|
||||
#print(f"Found printer: {printer_info}")
|
||||
# print(f"Found printer: {printer_info}")
|
||||
return printer_info
|
||||
|
||||
except Exception as e:
|
||||
#print(f"Error with backend {backend_name}: {str(e)}")
|
||||
# print(f"Error with backend {backend_name}: {str(e)}")
|
||||
continue
|
||||
|
||||
print("No Brother QL printer found")
|
||||
return None
|
||||
|
||||
|
||||
def fint_and_parse_epson_printer():
|
||||
pass
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user