Skip to content

Commit 2850bf1

Browse files
authored
Merge pull request #26 from maresb/dont-assume-default-barcode-type
Don't assume the default barcode type when handling empty string
2 parents 9a3b9bf + d740b80 commit 2850bf1

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/labelle/lib/render_engines/barcode.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44
from PIL import Image
55

66
from labelle.lib.barcode_writer import BarcodeImageWriter
7-
from labelle.lib.constants import DEFAULT_BARCODE_TYPE
7+
from labelle.lib.constants import DEFAULT_BARCODE_TYPE, BarcodeType
88
from labelle.lib.render_engines.render_context import RenderContext
99
from labelle.lib.render_engines.render_engine import (
1010
RenderEngine,
1111
RenderEngineException,
1212
)
1313

14+
if DEFAULT_BARCODE_TYPE != BarcodeType.CODE128:
15+
# Ensure that we fail fast if the default barcode type is adjusted
16+
# and the code below hasn't been updated.
17+
raise RuntimeError(
18+
"The conditional below assumes that the default barcode type is CODE128. "
19+
"Different barcodes have different quirks, so we should manually test the "
20+
"new default to ensure a good user experience in the GUI when the initial "
21+
"value is an empty string."
22+
)
23+
1424

1525
class BarcodeRenderError(RenderEngineException):
1626
def __init__(self) -> None:
@@ -25,7 +35,10 @@ def __init__(self, content: str, barcode_type: str | None) -> None:
2535
self.barcode_type = barcode_type or DEFAULT_BARCODE_TYPE
2636

2737
def render(self, context: RenderContext) -> Image.Image:
28-
if self.barcode_type == "code128" and self.content == "":
38+
if (
39+
self.barcode_type == DEFAULT_BARCODE_TYPE == BarcodeType.CODE128
40+
and self.content == ""
41+
):
2942
# An exception is raised on the empty string. Since this is
3043
# the default code, we really don't want to trigger a popup
3144
# in the GUI before the user entered a barcode.

0 commit comments

Comments
 (0)