Skip to content

Commit

Permalink
V1.1.0 Rewrite compression_type as format
Browse files Browse the repository at this point in the history
Deprecated compression_type in favor of format. Fixed the issue of ^GFB
and ^GFC fields being incorrectly used instead of ^GFA.

Closes #2
  • Loading branch information
miikanissi authored Mar 24, 2024
2 parents 1df45f8 + 1c72f36 commit 6862008
Show file tree
Hide file tree
Showing 21 changed files with 283 additions and 178 deletions.
14 changes: 7 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
:target: https://zebrafy.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status

.. image:: https://img.shields.io/badge/pypi-1.0.1-blue
.. image:: https://img.shields.io/badge/pypi-1.1.0-blue
:target: https://pypi.org/project/zebrafy/
:alt: PyPi Package

Expand All @@ -36,11 +36,11 @@ If you want more control over the resulting ZPL data, **ZebrafyImage** and
+----------------------+--------------------------------------------------------------------------------------------------------------+
| Parameter | Description |
+======================+==============================================================================================================+
| ``compression_type`` | ZPL graphic field compression type (default ``"A"``) |
| ``format`` | ZPL graphic field format type (default ``"ASCII"``) |
| | |
| | - ``"A"`` — ASCII hexadecimal (most compatible) |
| | - ``"B"`` — Base64 Binary |
| | - ``"C"`` — Z64 compressed binary (best compression) |
| | - ``"ASCII"`` — ASCII hexadecimal (most compatible) |
| | - ``"B64"`` — Base64 Binary |
| | - ``"Z64"`` — Z64 compressed binary (best compression) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
| ``invert`` | Invert the black and white in the image/PDF output. (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -106,7 +106,7 @@ Example usage with optional parameters:
with open("source.png", "rb") as image:
zpl_string = ZebrafyImage(
image.read(),
compression_type="C",
format="Z64",
invert=True,
dither=False,
threshold=128,
Expand Down Expand Up @@ -160,7 +160,7 @@ conversion:
with open("source.pdf", "rb") as pdf:
zpl_string = ZebrafyPDF(
pdf.read(),
compression_type="C",
format="Z64",
invert=True,
dither=False,
threshold=128,
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
project = "Zebrafy"
copyright = "2023, Miika Nissi"
author = "Miika Nissi"
version = "1.0.1"
version = "1.1.0"
release = version

# -- General configuration ---------------------------------------------------
Expand Down
13 changes: 7 additions & 6 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ ZebrafyPDF and ZebrafyImage Parameters
+----------------------+--------------------------------------------------------------------------------------------------------------+
| Parameter | Description |
+======================+==============================================================================================================+
| ``compression_type`` | ZPL graphic field compression type (default ``"A"``) |
| ``format`` | ZPL graphic field format type (default ``"ASCII"``) |
| | |
| | - ``"A"`` — ASCII hexadecimal (most compatible) |
| | - ``"B"`` — Base64 Binary |
| | - ``"C"`` — Z64 compressed binary (best compression) |
| | - ``"ASCII"`` — ASCII hexadecimal (most compatible) |
| | - ``"B64"`` — Base64 Binary |
| | - ``"Z64"`` — Z64 compressed binary (best compression) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
+----------------------+--------------------------------------------------------------------------------------------------------------+
| ``invert`` | Invert the black and white in the image/PDF output. (``True`` or ``False``, default ``False``) |
+----------------------+--------------------------------------------------------------------------------------------------------------+
Expand Down Expand Up @@ -69,7 +70,7 @@ Example usage with optional parameters:
with open("source.png", "rb") as image:
zpl_string = ZebrafyImage(
image.read(),
compression_type="C",
format="Z64",
invert=True,
dither=False,
threshold=128,
Expand Down Expand Up @@ -123,7 +124,7 @@ conversion:
with open("source.pdf", "rb") as pdf:
zpl_string = ZebrafyPDF(
pdf.read(),
compression_type="C",
format="Z64",
invert=True,
dither=False,
threshold=128,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "zebrafy"
version = "1.0.1"
version = "1.1.0"
description = "Python library for converting PDF and images to Zebra Programming Language (ZPL)"
classifiers = [
"Development Status :: 4 - Beta",
Expand Down
File renamed without changes
File renamed without changes.
File renamed without changes

Large diffs are not rendered by default.

File renamed without changes

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

File renamed without changes.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

181 changes: 105 additions & 76 deletions tests/test_zebrafy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def _read_static_file(self, file_name: str) -> Union[bytes, str]:

def test_version(self):
"""Test package version."""
self.assertEqual(__version__, "1.0.1")
self.assertEqual(__version__, "1.1.0")

###########
# CRC Tests
Expand All @@ -78,15 +78,24 @@ def test_graphic_field_image(self):
with self.assertRaises(TypeError):
GraphicField(123)

def test_graphic_field_compression_type(self):
def test_graphic_field_format(self):
im = Image.new(mode="RGB", size=(200, 200))
gf = GraphicField(im)
with self.assertRaises(ValueError):
gf.compression_type = None
gf.format = None
with self.assertRaises(TypeError):
gf.compression_type = 123
gf.format = 123
with self.assertRaises(ValueError):
gf.compression_type = "D"
gf.format = "D"

def test_graphic_field_deprecated_compression_type(self):
im = Image.new(mode="RGB", size=(200, 200))
gfa = GraphicField(im, compression_type="A")
self.assertEqual(gfa.format, "ASCII")
gfb = GraphicField(im, compression_type="B")
self.assertEqual(gfb.format, "B64")
gfc = GraphicField(im, compression_type="C")
self.assertEqual(gfc.format, "Z64")

####################
# ZebrafyImage Tests
Expand All @@ -99,16 +108,25 @@ def test_zebrafy_image_image(self):
with self.assertRaises(TypeError):
ZebrafyImage(123)

def test_zebrafy_image_compression_type(self):
"""Test ZebrafyImage compression_type input"""
def test_zebrafy_image_format(self):
"""Test ZebrafyImage format input"""
im = Image.new(mode="RGB", size=(200, 200))
zebrafy_image = ZebrafyImage(im)
with self.assertRaises(ValueError):
zebrafy_image.compression_type = None
zebrafy_image.format = None
with self.assertRaises(TypeError):
zebrafy_image.compression_type = 123
zebrafy_image.format = 123
with self.assertRaises(ValueError):
zebrafy_image.compression_type = "D"
zebrafy_image.format = "D"

def test_zebrafy_image_deprecated_compression_type(self):
im = Image.new(mode="RGB", size=(200, 200))
gfa = ZebrafyImage(im, compression_type="A")
self.assertEqual(gfa.format, "ASCII")
gfb = ZebrafyImage(im, compression_type="B")
self.assertEqual(gfb.format, "B64")
gfc = ZebrafyImage(im, compression_type="C")
self.assertEqual(gfc.format, "Z64")

def test_zebrafy_image_invert(self):
"""Test ZebrafyImage invert input"""
Expand Down Expand Up @@ -190,28 +208,28 @@ def test_zebrafy_image_complete_zpl(self):
def test_image_to_default_zpl(self):
"""Test image to ZPL with default options."""
default_zpl = ZebrafyImage(self._read_static_file("test_image.png")).to_zpl()
self.assertEqual(default_zpl, self._read_static_file("test_image_gfa.zpl"))
self.assertEqual(default_zpl, self._read_static_file("test_image_ascii.zpl"))

def test_image_to_gfa_zpl(self):
def test_image_to_ascii_zpl(self):
"""Test image to ZPL with A (ASCII) compression."""
gfa_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), compression_type="A"
ascii_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), format="ASCII"
).to_zpl()
self.assertEqual(gfa_zpl, self._read_static_file("test_image_gfa.zpl"))
self.assertEqual(ascii_zpl, self._read_static_file("test_image_ascii.zpl"))

def test_image_to_gfb_zpl(self):
def test_image_to_b64_zpl(self):
"""Test image to ZPL with B (B64 Binary) compression."""
gfb_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), compression_type="B"
b64_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), format="B64"
).to_zpl()
self.assertEqual(gfb_zpl, self._read_static_file("test_image_gfb.zpl"))
self.assertEqual(b64_zpl, self._read_static_file("test_image_b64.zpl"))

def test_image_to_gfc_zpl(self):
def test_image_to_z64_zpl(self):
"""Test image to ZPL with C (Z64 Binary) compression."""
gfc_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), compression_type="C"
z64_zpl = ZebrafyImage(
self._read_static_file("test_image.png"), format="Z64"
).to_zpl()
self.assertEqual(gfc_zpl, self._read_static_file("test_image_gfc.zpl"))
self.assertEqual(z64_zpl, self._read_static_file("test_image_z64.zpl"))

def test_image_to_zpl_invert(self):
"""Test image to ZPL inverting the image."""
Expand Down Expand Up @@ -288,16 +306,25 @@ def test_zebrafy_pdf_pdf_bytes(self):
with self.assertRaises(TypeError):
ZebrafyPDF(123)

def test_zebrafy_pdf_compression_type(self):
"""Test ZebrafyPDF compression_type input"""
def test_zebrafy_pdf_format(self):
"""Test ZebrafyPDF format input"""
pdf = self._read_static_file("test_pdf.pdf")
zebrafy_pdf = ZebrafyPDF(pdf)
with self.assertRaises(ValueError):
zebrafy_pdf.compression_type = None
zebrafy_pdf.format = None
with self.assertRaises(TypeError):
zebrafy_pdf.compression_type = 123
zebrafy_pdf.format = 123
with self.assertRaises(ValueError):
zebrafy_pdf.compression_type = "D"
zebrafy_pdf.format = "D"

def test_zebrafy_pdf_deprecated_compression_type(self):
pdf = self._read_static_file("test_pdf.pdf")
gfa = ZebrafyPDF(pdf, compression_type="A")
self.assertEqual(gfa.format, "ASCII")
gfb = ZebrafyPDF(pdf, compression_type="B")
self.assertEqual(gfb.format, "B64")
gfc = ZebrafyPDF(pdf, compression_type="C")
self.assertEqual(gfc.format, "Z64")

def test_zebrafy_pdf_invert(self):
"""Test ZebrafyPDF invert input"""
Expand Down Expand Up @@ -378,28 +405,28 @@ def test_zebrafy_pdf_complete_zpl(self):
def test_pdf_to_default_zpl(self):
"""Test PDF to ZPL with default options."""
default_zpl = ZebrafyPDF(self._read_static_file("test_pdf.pdf")).to_zpl()
self.assertEqual(default_zpl, self._read_static_file("test_pdf_gfa.zpl"))
self.assertEqual(default_zpl, self._read_static_file("test_pdf_ascii.zpl"))

def test_pdf_to_gfa_zpl(self):
def test_pdf_to_ascii_zpl(self):
"""Test PDF to ZPL with A (ASCII) compression."""
gfa_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), compression_type="A"
ascii_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), format="ASCII"
).to_zpl()
self.assertEqual(gfa_zpl, self._read_static_file("test_pdf_gfa.zpl"))
self.assertEqual(ascii_zpl, self._read_static_file("test_pdf_ascii.zpl"))

def test_pdf_to_gfb_zpl(self):
def test_pdf_to_b64_zpl(self):
"""Test PDF to ZPL with B (B64 Binary) compression."""
gfb_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), compression_type="B"
b64_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), format="B64"
).to_zpl()
self.assertEqual(gfb_zpl, self._read_static_file("test_pdf_gfb.zpl"))
self.assertEqual(b64_zpl, self._read_static_file("test_pdf_b64.zpl"))

def test_pdf_to_gfc_zpl(self):
def test_pdf_to_z64_zpl(self):
"""Test PDF to ZPL with C (Z64 Binary) compression."""
gfc_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), compression_type="C"
z64_zpl = ZebrafyPDF(
self._read_static_file("test_pdf.pdf"), format="Z64"
).to_zpl()
self.assertEqual(gfc_zpl, self._read_static_file("test_pdf_gfc.zpl"))
self.assertEqual(z64_zpl, self._read_static_file("test_pdf_z64.zpl"))

def test_pdf_to_zpl_no_dither(self):
"""Test PDF to ZPL without dithering the PDF."""
Expand Down Expand Up @@ -438,31 +465,33 @@ def test_zebrafy_zpl_zpl_data(self):
with self.assertRaises(TypeError):
ZebrafyZPL(123)

def test_gfa_zpl_to_image(self):
"""Test ZPL GFA to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_gfa.zpl")).to_images()[0]
def test_ascii_zpl_to_image(self):
"""Test ZPL GFA ASCII to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_ascii.zpl")).to_images()[
0
]
image_bytes = io.BytesIO()
image.save(image_bytes, format="PNG")
self.assertEqual(
image_bytes.getvalue(), self._read_static_file("test_image_gfa.png")
image_bytes.getvalue(), self._read_static_file("test_image_ascii.png")
)

def test_gfb_zpl_to_image(self):
"""Test ZPL GFB to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_gfb.zpl")).to_images()[0]
def test_b64_zpl_to_image(self):
"""Test ZPL GFA B64 to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_b64.zpl")).to_images()[0]
image_bytes = io.BytesIO()
image.save(image_bytes, format="PNG")
self.assertEqual(
image_bytes.getvalue(), self._read_static_file("test_image_gfb.png")
image_bytes.getvalue(), self._read_static_file("test_image_b64.png")
)

def test_gfc_zpl_to_image(self):
"""Test ZPL GFC to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_gfc.zpl")).to_images()[0]
def test_z64_zpl_to_image(self):
"""Test ZPL GFA Z64 to image bytes."""
image = ZebrafyZPL(self._read_static_file("test_image_z64.zpl")).to_images()[0]
image_bytes = io.BytesIO()
image.save(image_bytes, format="PNG")
self.assertEqual(
image_bytes.getvalue(), self._read_static_file("test_image_gfc.png")
image_bytes.getvalue(), self._read_static_file("test_image_z64.png")
)

def test_broken_zpl_gf_to_image(self):
Expand All @@ -473,36 +502,36 @@ def test_broken_zpl_gf_to_image(self):
with self.assertRaises(ValueError):
zebrafy_broken_zpl.to_images()

def test_broken_zpl_gfc_crc_to_image(self):
"""Test broken ZPL gfc to image bytes - resulting in ValueError."""
def test_broken_zpl_z64_crc_to_image(self):
"""Test broken ZPL GFA Z64 to image bytes - resulting in ValueError."""
zebrafy_broken_zpl = ZebrafyZPL(
self._read_static_file("test_image_gfc_broken_crc.zpl"),
self._read_static_file("test_image_z64_broken_crc.zpl"),
)
with self.assertRaises(ValueError):
zebrafy_broken_zpl.to_images()

def test_broken_zpl_gfc_compression_to_image(self):
"""Test broken ZPL gfc to image bytes - resulting in ValueError."""
def test_broken_zpl_z64_compression_to_image(self):
"""Test broken ZPL GFA Z64 to image bytes - resulting in ValueError."""
zebrafy_broken_zpl = ZebrafyZPL(
self._read_static_file("test_image_gfc_broken_compression.zpl"),
self._read_static_file("test_image_z64_broken_compression.zpl"),
)
with self.assertRaises(ValueError):
zebrafy_broken_zpl.to_images()

def test_gfa_zpl_to_pdf(self):
"""Test ZPL GFA to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_gfa.zpl")).to_pdf()
gfa_zpl = ZebrafyPDF(pdf_bytes, compression_type="A").to_zpl()
self.assertEqual(gfa_zpl, self._read_static_file("test_pdf_gfa.zpl"))

def test_gfb_zpl_to_pdf(self):
"""Test ZPL GFB to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_gfb.zpl")).to_pdf()
gfb_zpl = ZebrafyPDF(pdf_bytes, compression_type="B").to_zpl()
self.assertEqual(gfb_zpl, self._read_static_file("test_pdf_gfb.zpl"))

def test_gfc_zpl_to_pdf(self):
"""Test ZPL GFC to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_gfc.zpl")).to_pdf()
gfc_zpl = ZebrafyPDF(pdf_bytes, compression_type="C").to_zpl()
self.assertEqual(gfc_zpl, self._read_static_file("test_pdf_gfc.zpl"))
def test_ascii_zpl_to_pdf(self):
"""Test ZPL GFA ASCII to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_ascii.zpl")).to_pdf()
ascii_zpl = ZebrafyPDF(pdf_bytes, format="ASCII").to_zpl()
self.assertEqual(ascii_zpl, self._read_static_file("test_pdf_ascii.zpl"))

def test_b64_zpl_to_pdf(self):
"""Test ZPL GFA B64 to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_b64.zpl")).to_pdf()
b64_zpl = ZebrafyPDF(pdf_bytes, format="B64").to_zpl()
self.assertEqual(b64_zpl, self._read_static_file("test_pdf_b64.zpl"))

def test_z64_zpl_to_pdf(self):
"""Test ZPL GFA Z64 to PDF bytes."""
pdf_bytes = ZebrafyZPL(self._read_static_file("test_pdf_z64.zpl")).to_pdf()
z64_zpl = ZebrafyPDF(pdf_bytes, format="Z64").to_zpl()
self.assertEqual(z64_zpl, self._read_static_file("test_pdf_z64.zpl"))
2 changes: 1 addition & 1 deletion zebrafy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
from .zebrafy_pdf import ZebrafyPDF
from .zebrafy_zpl import ZebrafyZPL

__version__ = "1.0.1"
__version__ = "1.1.0"
Loading

0 comments on commit 6862008

Please sign in to comment.