Skip to content

Commit

Permalink
add tests for the labels module and work around brother_ql issues
Browse files Browse the repository at this point in the history
  • Loading branch information
FriedrichFroebel committed Nov 22, 2023
1 parent 87ee072 commit 6822d03
Show file tree
Hide file tree
Showing 12 changed files with 419 additions and 48 deletions.
38 changes: 38 additions & 0 deletions brother_ql_web/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from __future__ import annotations

import logging
from typing import Any


def patch_deprecation_warning() -> None:
"""
Avoid the deprecation warning from `brother_ql.devicedependent`. This has been
fixed in the Git version, but not in PyPI one:
https://github.com/pklaus/brother_ql/commit/5c2b72b18bcf436c116f180a9147cbb6805958f5
"""
original_logger = logging.getLogger("brother_ql.devicedependent").warning

def warn(message: str, *args: Any, **kwargs: Any) -> None:
if (
message
== "deprecation warning: brother_ql.devicedependent is deprecated and will be removed in a future release" # noqa: E501
):
return
original_logger(message, *args, **kwargs)

logging.getLogger("brother_ql.devicedependent").warn = warn # type: ignore[assignment,method-assign] # noqa: E501


patch_deprecation_warning()


import brother_ql.conversion # noqa: E402
import PIL # noqa: E402


# Renamed in version 2.7.0:
# https://pillow.readthedocs.io/en/stable/releasenotes/2.7.0.html#antialias-renamed-to-lanczos
brother_ql.conversion.Image.ANTIALIAS = PIL.Image.LANCZOS # type: ignore[attr-defined]


__all__: list[str] = []
21 changes: 13 additions & 8 deletions brother_ql_web/labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
class LabelParameters:
configuration: Configuration

font_family: str
font_style: str
font_family: str | None = None
font_style: str | None = None
text: str = ""
font_size: int = 100
label_size: str = "62"
Expand All @@ -40,7 +40,9 @@ class LabelParameters:
margin_left: int = 35
margin_right: int = 35
label_count: int = 1
high_quality: bool = True
# TODO: Not yet taken into account. The number of dots in each direction has to be
# doubled. The generator/calculation methods have to be updated accordingly.
high_quality: bool = False

@property
def kind(self) -> FormFactor:
Expand Down Expand Up @@ -73,12 +75,13 @@ def fill_color(self) -> tuple[int, int, int]:
def font_path(self) -> str:
try:
if self.font_family is None or self.font_style is None:
assert self.configuration.label.default_font is not None
self.font_family = self.configuration.label.default_font.family
self.font_style = self.configuration.label.default_font.style
fonts = utils.collect_fonts(self.configuration)
path = fonts[self.font_family][self.font_style]
except KeyError:
raise LookupError("Couln't find the font & style")
raise LookupError("Couldn't find the font & style")
return path

@property
Expand Down Expand Up @@ -209,13 +212,15 @@ def generate_label(
if save_image_to:
image.save(save_image_to)

red: bool = False
red: bool = "red" in parameters.label_size
rotate: int | str = 0
if parameters.kind == ENDLESS_LABEL:
rotate = 0 if parameters.orientation == "standard" else 90
elif parameters.kind in (ROUND_DIE_CUT_LABEL, DIE_CUT_LABEL):
rotate = "auto"
red = "red" in parameters.label_size

if parameters.high_quality:
logger.warning("High quality mode is not implemented for now.")

qlr = BrotherQLRaster(configuration.printer.model)
create_label(
Expand All @@ -226,7 +231,7 @@ def generate_label(
threshold=parameters.threshold,
cut=True,
rotate=rotate,
dpi_600=parameters.high_quality,
dpi_600=False,
)

return qlr
Expand All @@ -240,7 +245,7 @@ def print_label(
) -> None:
backend = backend_class(configuration.printer.printer)
for i in range(parameters.label_count):
logger.info("Printing label %d of %d ...", i, parameters.label_count)
logger.info("Printing label %d of %d ...", i + 1, parameters.label_count)
backend.write(qlr.data)
backend.dispose()
del backend
24 changes: 0 additions & 24 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,10 @@
import logging
from functools import cached_property
from pathlib import Path
from unittest import TestCase as _TestCase
from typing import Any

from brother_ql_web.configuration import Configuration


def patch_deprecation_warning() -> None:
"""
Avoid the deprecation warning from `brother_ql.devicedependent`. This has been
fixed in the Git version, but not in PyPI one:
https://github.com/pklaus/brother_ql/commit/5c2b72b18bcf436c116f180a9147cbb6805958f5
"""
original_logger = logging.getLogger("brother_ql.devicedependent").warning

def warn(message: str, *args: Any, **kwargs: Any) -> None:
if (
message
== "deprecation warning: brother_ql.devicedependent is deprecated and will be removed in a future release" # noqa: E501
):
return
original_logger(message, *args, **kwargs)

logging.getLogger("brother_ql.devicedependent").warn = warn # type: ignore[assignment,method-assign] # noqa: E501


patch_deprecation_warning()


class TestCase(_TestCase):
@cached_property
def example_configuration_path(self) -> str:
Expand Down
Binary file added tests/data/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added tests/data/hello_world__label_size_62x29.data
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 6822d03

Please sign in to comment.