Skip to content

Commit

Permalink
Make PNG library optional.
Browse files Browse the repository at this point in the history
Refs #338
  • Loading branch information
bartTC committed Aug 13, 2024
1 parent dee7e1d commit dee4a35
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
7 changes: 7 additions & 0 deletions qrcode/compat/png.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Try to import png library.
PngWriter = None

try:
from png import Writer as PngWriter # type: ignore # noqa: F401
except ImportError: # pragma: no cover
pass
9 changes: 4 additions & 5 deletions qrcode/image/pure.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from itertools import chain

import png
from qrcode.compat.png import PngWriter
from qrcode.image.base import BaseImage

import qrcode.image.base


class PyPNGImage(qrcode.image.base.BaseImage):
class PyPNGImage(BaseImage):
"""
pyPNG image builder.
"""
Expand All @@ -15,7 +14,7 @@ class PyPNGImage(qrcode.image.base.BaseImage):
needs_drawrect = False

def new_image(self, **kwargs):
return png.Writer(self.pixel_size, self.pixel_size, greyscale=True, bitdepth=1)
return PngWriter(self.pixel_size, self.pixel_size, greyscale=True, bitdepth=1)

def drawrect(self, row, col):
"""
Expand Down
8 changes: 5 additions & 3 deletions qrcode/tests/test_qrcode.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from tempfile import mkdtemp
from unittest import mock

import png

import qrcode
import qrcode.util
from qrcode.compat.pil import Image as pil_Image
from qrcode.compat.png import PngWriter
from qrcode.exceptions import DataOverflowError
from qrcode.image.base import BaseImage
from qrcode.image.pure import PyPNGImage
Expand Down Expand Up @@ -174,20 +174,22 @@ class MockFactory(BaseImage):
self.assertTrue(MockFactory.new_image.called)
self.assertTrue(MockFactory.drawrect.called)

@unittest.skipIf(not PngWriter, "Requires PNG")
def test_render_pypng(self):
qr = qrcode.QRCode()
qr.add_data(UNICODE_TEXT)
img = qr.make_image(image_factory=PyPNGImage)
self.assertIsInstance(img.get_image(), png.Writer)
self.assertIsInstance(img.get_image(), PngWriter)

print(img.width, img.box_size, img.border)
img.save(io.BytesIO())

@unittest.skipIf(not PngWriter, "Requires PNG")
def test_render_pypng_to_str(self):
qr = qrcode.QRCode()
qr.add_data(UNICODE_TEXT)
img = qr.make_image(image_factory=PyPNGImage)
self.assertIsInstance(img.get_image(), png.Writer)
self.assertIsInstance(img.get_image(), PngWriter)

mock_open = mock.mock_open()
with mock.patch("qrcode.image.pure.open", mock_open, create=True):
Expand Down
3 changes: 2 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tox]
distribute = False
envlist = py{39,310,311,312}-{pil,nopil}
envlist = py{39,310,311,312}-{pil,png,none}
skip_missing_interpreters = True

[gh-actions]
Expand All @@ -15,5 +15,6 @@ commands =
pytest {envsitepackagesdir}/qrcode
deps =
pil: pillow>=9.1.0
png: pypng
pytest
pytest-cov

0 comments on commit dee4a35

Please sign in to comment.