Formatting

This commit is contained in:
n07070 committed 2026-11-28 01:27:00 +01:00
1 parent 12508bda83
commit 99f9eccf00
7 files changed
+187 -85

No files matched your search

+65 -33
View File
@@ -28,6 +28,7 @@ from brother_ql import create_label
# Used for typing
from task import PrintTask
class PrinterType(Enum):
"""
What are the capacities of a Printer ?
@@ -115,7 +116,7 @@ class EscPosPrinter(Printer):
Making sure it has paper,
Define default print settings
"""
super().__init__(app, vendor_id, device_id,printer_type=PrinterType.EPSON)
super().__init__(app, vendor_id, device_id, printer_type=PrinterType.EPSON)
self.printer = None
self.usb_args = {}
self.usb_args["idVendor"] = self.vendor_id
@@ -257,7 +258,6 @@ class EscPosPrinter(Printer):
if do_cut:
self._cut()
self.app.logger.info("Printed text")
self.ready = True
@@ -383,11 +383,16 @@ class EscPosPrinter(Printer):
match (task.task_type.value):
case "text":
self._print_txt(data["txt"], signature=data["sign"], do_cut=data["do_cut"])
self._print_txt(
data["txt"], signature=data["sign"], do_cut=data["do_cut"]
)
self.ready = True
case "image":
self._print_img(
data["img"], signature=data["sign"], process=data["process"], do_cut=data["do_cut"]
data["img"],
signature=data["sign"],
process=data["process"],
do_cut=data["do_cut"],
)
self.ready = True
case "qr":
@@ -428,9 +433,7 @@ class BrotherPrinter(Printer):
# Match vendor and product IDs so this instance doesn't accidentally
# use the identifier belonging to the last device in the backend list.
expected_prefix = (
f"usb://0x{vendor_id:04x}:0x{device_id:04x}/"
).lower()
expected_prefix = (f"usb://0x{vendor_id:04x}:0x{device_id:04x}/").lower()
matching_identifiers = []
@@ -451,8 +454,16 @@ class BrotherPrinter(Printer):
matching_identifiers.append(identifier)
if len(matching_identifiers) != 1:
self.app.logger.error("Expected one Brother backend device matching %s, found %s", expected_prefix, len(matching_identifiers) )
raise RuntimeError("Expected one Brother backend device matching %s, found %s", expected_prefix, len(matching_identifiers) )
self.app.logger.error(
"Expected one Brother backend device matching %s, found %s",
expected_prefix,
len(matching_identifiers),
)
raise RuntimeError(
"Expected one Brother backend device matching %s, found %s",
expected_prefix,
len(matching_identifiers),
)
# Extract connection details from the identifier for this printer only.
identifier = matching_identifiers[0]
@@ -496,7 +507,6 @@ class BrotherPrinter(Printer):
self.app.logger.debug("Created a Brother printer!")
self.ready = True
def _has_paper(self):
raise NotImplementedError("This printer model does not support this.")
@@ -512,21 +522,20 @@ class BrotherPrinter(Printer):
if do_cut:
self._cut()
def _print_img(
self,
data,
signature="",
process=False,
do_cut=False
):
def _print_img(self, data, signature="", process=False, do_cut=False):
"""
Print a raster image via a Brother QL printer
"""
self.app.logger.debug("Printing an image %s via Brother printer ( %s )" , data, self.printer_info.model )
self.app.logger.debug(
"Printing an image %s via Brother printer ( %s )",
data,
self.printer_info.model,
)
if process:
try:
self.app.logger.debug("\t[⚙ %s] Proccessing the image", threading.get_ident())
self.app.logger.debug(
"\t[⚙ %s] Proccessing the image", threading.get_ident()
)
processed_path = _process_image(self, data)
except RuntimeError as e:
self.app.logger.error(
@@ -547,8 +556,6 @@ class BrotherPrinter(Printer):
)
raise e
try:
qlr = BrotherQLRaster(self.printer_info.model)
@@ -570,7 +577,7 @@ class BrotherPrinter(Printer):
compress=False,
dpi_600=False,
cut=False,
)
)
success = send(
instructions=instructions,
@@ -679,29 +686,47 @@ class BrotherPrinter(Printer):
"""Execute actual print based on task type"""
i_m_ready = self._state()
while not i_m_ready:
self.app.logger.debug("\t[⚙ %s] Waiting for the printer to become ready, current state %s ", self.id, str(i_m_ready))
self.app.logger.debug(
"\t[⚙ %s] Waiting for the printer to become ready, current state %s ",
self.id,
str(i_m_ready),
)
i_m_ready = self._state()
time.sleep(0.3)
self.app.logger.debug("\t[⚙ %s] Checked state to start printing %s ", self.id, self._state())
self.app.logger.debug(
"\t[⚙ %s] Checked state to start printing %s ", self.id, self._state()
)
self.ready = False
try:
match (task.task_type.value):
case "image":
self._print_img(data["img"], signature=data["sign"], do_cut=data["do_cut"])
self._print_img(
data["img"], signature=data["sign"], do_cut=data["do_cut"]
)
case "text":
self._print_txt(data["txt"], signature=data["sign"], do_cut=data["do_cut"])
self._print_txt(
data["txt"], signature=data["sign"], do_cut=data["do_cut"]
)
case _:
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 ",self.id, task.task_id, task.value, str(ve))
self.app.logger.error(
"\t[⚙ %s] The printer couldn't be found, the printing failed for task %s of type , error : %s ",
self.id,
task.task_id,
task.value,
str(ve),
)
raise ve
except Exception as e:
raise e
self.app.logger.debug("\t[⚙ %s] Finished consuming task %s", self.id, task.task_id)
self.app.logger.debug(
"\t[⚙ %s] Finished consuming task %s", self.id, task.task_id
)
self.ready = True
@@ -733,7 +758,9 @@ def _process_image(self, path, max_width=575, max_height=1000):
"Image is dark; increasing brightness by %.2f",
brightness_factor,
)
grayscale_image = ImageEnhance.Brightness(grayscale_image).enhance(brightness_factor)
grayscale_image = ImageEnhance.Brightness(grayscale_image).enhance(
brightness_factor
)
contrast = float(np.asarray(grayscale_image, dtype=np.float32).std())
self.app.logger.debug("Grayscale standard deviation: %.2f", contrast)
@@ -758,7 +785,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)
@@ -829,7 +856,13 @@ def text_to_image(
return str(image_path)
def _resize_image(self,label_type: str, image_path: str, device_pixel_width: int, printer_model: object):
def _resize_image(
self,
label_type: str,
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
# """
@@ -845,7 +878,6 @@ def _resize_image(self,label_type: str, image_path: str, device_pixel_width: int
# self.app.logger.debug("Found label %s with maximum dimensions %s", label_type, max_image_dimension)
# # Open image
# with Image.open(image_path ) as img:
# self.app.logger.debug("Image current dimensions before resizing are %s", img.size)