Skip to content

Commit

Permalink
Merge pull request #26 from maresb/dont-assume-default-barcode-type
Browse files Browse the repository at this point in the history
Don't assume the default barcode type when handling empty string
  • Loading branch information
tomers authored Apr 28, 2024
2 parents 9a3b9bf + d740b80 commit 2850bf1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/labelle/lib/render_engines/barcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@
from PIL import Image

from labelle.lib.barcode_writer import BarcodeImageWriter
from labelle.lib.constants import DEFAULT_BARCODE_TYPE
from labelle.lib.constants import DEFAULT_BARCODE_TYPE, BarcodeType
from labelle.lib.render_engines.render_context import RenderContext
from labelle.lib.render_engines.render_engine import (
RenderEngine,
RenderEngineException,
)

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


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

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

0 comments on commit 2850bf1

Please sign in to comment.