Better error management, change signature print

This commit is contained in:
n07070 committed 2026-11-28 23:56:54 +01:00
1 parent ac2f3fe969
commit 5574fc89ef
1 file changed
+30 -56
+30 -56
View File
@@ -513,14 +513,12 @@ class BrotherPrinter(Printer):
def _state(self):
return self.ready
def _print_txt(self, msg, signature="", bold=False, do_cut=False):
def _print_txt(self, msg, signature="", do_cut=False):
"""Print text after converting it to an image"""
self._print_img(text_to_image(msg))
if signature != "":
self._print_img(text_to_image(signature))
if do_cut:
self._cut()
if signature == "":
self._print_img(text_to_image(msg), do_cut=do_cut)
else:
self._print_img(text_to_image(msg), signature, do_cut=do_cut)
def _print_img(self, data, signature="", process=False, do_cut=False):
"""
@@ -533,9 +531,7 @@ class BrotherPrinter(Printer):
)
if process:
try:
self.app.logger.debug(
"\t[⚙ %s] Proccessing the image", threading.get_ident()
)
self.app.logger.debug("\t[⚙ %s] Proccessing the image", self.id)
processed_path = _process_image(self, data)
except RuntimeError as e:
self.app.logger.error(
@@ -547,8 +543,8 @@ class BrotherPrinter(Printer):
self.app.logger.warning("Not proccessing the image")
try:
selected_backend = guess_backend(self.printer_info.identifier)
BACKEND_CLASS = backend_factory(selected_backend)["backend_class"]
# selected_backend = guess_backend(self.printer_info.identifier)
# BACKEND_CLASS = backend_factory(selected_backend)["backend_class"]
BACKEND_STRING_DESCR = self.printer_info.identifier
except Exception as e:
self.app.logger.error(
@@ -576,31 +572,38 @@ class BrotherPrinter(Printer):
dither=True,
compress=False,
dpi_600=False,
cut=False,
cut=do_cut,
)
success = send(
return_from_printer = send(
instructions=instructions,
printer_identifier=BACKEND_STRING_DESCR,
backend_identifier="pyusb",
blocking=True,
)
self.app.logger.debug(str(success))
self.app.logger.debug(str(return_from_printer))
match (return_from_printer.get("outcome")):
case ("printed"):
self.app.logger.debug("\t[⚙ %s] Printed image", self.id)
time.sleep(2)
case ("error"):
state = return_from_printer.get("printer_state")
errors = [str(error) for error in state.get("errors") if error]
raise RuntimeError(errors)
case (_):
raise RuntimeError("Unknown print error")
except usb.core.USBError as e:
if "timeout error" in str(e):
self.app.logger.error("USB Timeout Error %s", str(e))
raise e
self.app.logger.error("USB Error : %s", str(e))
raise e
except Exception as e:
raise e
# finally:
# try:
# os.remove(processed_path)
# self.app.logger.debug("Removed image : " + str(processed_path))
# except OSError as e:
# raise e
if signature != "":
self.app.logger.debug("Printing signature for the image %s", data)
@@ -641,42 +644,13 @@ class BrotherPrinter(Printer):
raise e
finally:
try:
# os.remove(signature_file)
os.remove(signature_file)
self.app.logger.debug("Removed image : " + str(signature_file))
except OSError as e:
raise e
if do_cut:
self._cut()
self.ready = True
def _cut(self):
self.app.logger.debug("Cutting via Brother Printer")
try:
selected_backend = guess_backend(self.printer_info.identifier)
BACKEND_CLASS = backend_factory(selected_backend)["backend_class"]
BACKEND_STRING_DESCR = self.printer_info.identifier
except Exception as e:
self.app.logger.error(
"Couldn't guess the backend to use from the printer string descriptor"
)
raise e
data = "src/static/images/1x1.png"
try:
im = Image.open(data)
qlr = BrotherQLRaster(self.printer_info.model)
create_label(
qlr, im, self.printer_info.label_type, threshold=70, cut=True, rotate=0
)
# noinspection PyCallingNonCallable
be = BACKEND_CLASS(BACKEND_STRING_DESCR)
be.write(qlr.data)
be.dispose()
del be
self.app.logger.debug("Finished cutting via Brother Printer")
except Exception as e:
raise e
def _qr(self):
# This should not be reached by a print on a BrotherQL
@@ -714,10 +688,10 @@ class BrotherPrinter(Printer):
raise RuntimeError("This task type is not supported")
except ValueError as ve:
self.app.logger.error(
"\t[⚙ %s] The printer couldn't be found, the printing failed for task %s of type , error : %s ",
"\t[⚙ %s] The printer couldn't be found, the printing failed for task %s of type %s , error : %s ",
self.id,
task.task_id,
task.value,
task.task_type,
str(ve),
)
raise ve
@@ -785,7 +759,7 @@ def text_to_image(
background="white",
fill="black",
output_dir="src/static/uploads",
):
):
"""Converts a string to an image"""
font = ImageFont.truetype(font_path, font_size)
@@ -862,7 +836,7 @@ def _resize_image(
image_path: str,
device_pixel_width: int,
printer_model: object,
):
):
# """
# Resize an image to the specs needed for a label. Only used in Brother printers
# """