allow qr to set all arguments to image (#600)
* allow qr to set all arguments to image * increase coverage
This commit is contained in:
parent
86a715cf02
commit
5914c7c560
|
@ -12,6 +12,7 @@ This module contains the abstract base class :py:class:`Escpos`.
|
||||||
|
|
||||||
|
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import warnings
|
||||||
from abc import ABCMeta, abstractmethod # abstract base class support
|
from abc import ABCMeta, abstractmethod # abstract base class support
|
||||||
from re import match as re_match
|
from re import match as re_match
|
||||||
from typing import List, Literal, Optional, Union
|
from typing import List, Literal, Optional, Union
|
||||||
|
@ -304,7 +305,8 @@ class Escpos(object):
|
||||||
model=QR_MODEL_2,
|
model=QR_MODEL_2,
|
||||||
native=False,
|
native=False,
|
||||||
center=False,
|
center=False,
|
||||||
impl="bitImageRaster",
|
impl=None,
|
||||||
|
image_arguments: Optional[dict] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Print QR Code for the provided string.
|
"""Print QR Code for the provided string.
|
||||||
|
|
||||||
|
@ -319,6 +321,8 @@ class Escpos(object):
|
||||||
printer (Default)
|
printer (Default)
|
||||||
:param center: Centers the code *default:* False
|
:param center: Centers the code *default:* False
|
||||||
:param impl: Image-printing-implementation, refer to :meth:`.image()` for details
|
:param impl: Image-printing-implementation, refer to :meth:`.image()` for details
|
||||||
|
:param image_arguments: arguments passed to :meth:`.image()`.
|
||||||
|
Replaces `impl` and `center`. If `impl` or `center` are set, they will overwrite `image_arguments`.
|
||||||
"""
|
"""
|
||||||
# Basic validation
|
# Basic validation
|
||||||
if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]:
|
if ec not in [QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q]:
|
||||||
|
@ -333,6 +337,19 @@ class Escpos(object):
|
||||||
# Handle edge case by printing nothing.
|
# Handle edge case by printing nothing.
|
||||||
return
|
return
|
||||||
if not native:
|
if not native:
|
||||||
|
# impl is deprecated in favor of image_arguments
|
||||||
|
if impl:
|
||||||
|
warnings.warn(
|
||||||
|
"Parameter impl is deprecated in favor of image_arguments and will be dropped in a future release.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
# assemble arguments for image
|
||||||
|
if not image_arguments:
|
||||||
|
image_arguments = {}
|
||||||
|
if impl:
|
||||||
|
image_arguments["impl"] = impl
|
||||||
|
if "center" not in image_arguments:
|
||||||
|
image_arguments["center"] = center
|
||||||
# Map ESC/POS error correction levels to python 'qrcode' library constant and render to an image
|
# Map ESC/POS error correction levels to python 'qrcode' library constant and render to an image
|
||||||
if model != QR_MODEL_2:
|
if model != QR_MODEL_2:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
@ -354,7 +371,7 @@ class Escpos(object):
|
||||||
|
|
||||||
# Convert the RGB image in printable image
|
# Convert the RGB image in printable image
|
||||||
self.text("\n")
|
self.text("\n")
|
||||||
self.image(im, center=center, impl=impl)
|
self.image(im, **image_arguments)
|
||||||
self.text("\n")
|
self.text("\n")
|
||||||
self.text("\n")
|
self.text("\n")
|
||||||
return
|
return
|
||||||
|
|
|
@ -9,6 +9,8 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
import warnings
|
||||||
|
|
||||||
import mock
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
@ -29,10 +31,47 @@ def test_type_of_object_passed_to_image_function(img_function):
|
||||||
assert isinstance(args[0], Image.Image)
|
assert isinstance(args[0], Image.Image)
|
||||||
|
|
||||||
|
|
||||||
|
@mock.patch("escpos.printer.Dummy.image", spec=Dummy)
|
||||||
|
def test_parameter_image_arguments_passed_to_image_function(img_function):
|
||||||
|
"""Test the parameter passed to non-native qr printing."""
|
||||||
|
d = Dummy()
|
||||||
|
d.qr(
|
||||||
|
"LoremIpsum",
|
||||||
|
native=False,
|
||||||
|
image_arguments={
|
||||||
|
"impl": "bitImageColumn",
|
||||||
|
"high_density_vertical": False,
|
||||||
|
"center": True,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
args, kwargs = img_function.call_args
|
||||||
|
assert "impl" in kwargs
|
||||||
|
assert kwargs["impl"] == "bitImageColumn"
|
||||||
|
assert "high_density_vertical" in kwargs
|
||||||
|
assert kwargs["high_density_vertical"] is False
|
||||||
|
assert "center" in kwargs
|
||||||
|
assert kwargs["center"]
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def instance():
|
def instance():
|
||||||
return Dummy()
|
return Dummy()
|
||||||
|
|
||||||
|
|
||||||
def test_center(instance):
|
def test_center(instance):
|
||||||
|
"""Test printing qr codes."""
|
||||||
instance.qr("LoremIpsum", center=True)
|
instance.qr("LoremIpsum", center=True)
|
||||||
|
|
||||||
|
|
||||||
|
def test_deprecated_arguments(instance):
|
||||||
|
"""Test deprecation warning."""
|
||||||
|
with warnings.catch_warnings(record=True) as w:
|
||||||
|
# cause all warnings to always be triggered
|
||||||
|
warnings.simplefilter("always")
|
||||||
|
instance.qr(
|
||||||
|
"LoremIpsum",
|
||||||
|
impl="bitImageRaster",
|
||||||
|
image_arguments={"impl": "bitImageColumn"},
|
||||||
|
)
|
||||||
|
assert issubclass(w[-1].category, DeprecationWarning)
|
||||||
|
assert "deprecated" in str(w[-1].message)
|
||||||
|
|
Loading…
Reference in New Issue