Skip to content

Commit

Permalink
--decode
Browse files Browse the repository at this point in the history
Because I want a warning when libzbar is not available, but I don't want
that warning to be shown by default, unless the user explicitly asks for
--decode.
  • Loading branch information
mgedmin committed Jan 21, 2022
1 parent 6bf9515 commit 6faf281
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
26 changes: 18 additions & 8 deletions qr2text.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
from typing import BinaryIO, Iterable, List, Optional, Tuple, Union


try:
from pyzbar import pyzbar
except ImportError: # pragma: nocover
pyzbar = None


__version__ = '1.0.1.dev0'


Expand Down Expand Up @@ -301,20 +307,17 @@ def from_svg(cls, fileobj: FileNameOrFileObject) -> 'QR':
return qr

def decode(self) -> Optional[bytes]:
try:
from pyzbar.pyzbar import ZBarSymbol, decode
except ImportError: # pragma: nocover
# should I print a warning about libzbar not being available?
if pyzbar is None:
return None

# Note: experiments with pyqrcode and zbarimg show that I need
# pyqrcode.create('text').svg('file.svg', background='#fff', scale=2)
# to have recognizable qr codes. no background or scale=1 make zbarimg
# fail to find any codes.
scale = 2
res = decode((self.canvas.to_bytes(xscale=scale, yscale=scale),
self.size * scale, self.size * scale),
symbols=[ZBarSymbol.QRCODE])
image_data = (self.canvas.to_bytes(xscale=scale, yscale=scale),
self.size * scale, self.size * scale)
res = pyzbar.decode(image_data, symbols=[pyzbar.ZBarSymbol.QRCODE])
assert 0 <= len(res) <= 1
if not res:
return None
Expand All @@ -341,13 +344,20 @@ def main() -> None:
help='remove empty border')
parser.add_argument("--pad", type=int, default=0,
help='pad with empty border')
parser.add_argument("--decode", action="store_true",
default=(pyzbar is not None),
help=("decode the QR codes"
" (default if libzbar is available)"))
parser.add_argument("--no-decode", action="store_false",
dest="decode", default=True,
dest="decode",
help="don't decode the QR codes")
parser.add_argument("filename", type=argparse.FileType('rb'), nargs='+',
help='SVG file with the QR code (use - for stdin)')
args = parser.parse_args()

if args.decode and pyzbar is None:
print("libzbar is not available, --decode ignored", file=sys.stderr)

rc = 0
try:
for filename in args.filename:
Expand Down
14 changes: 14 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pyqrcode
import pytest

import qr2text
from qr2text import QR, Canvas, Error, Path, PathParser, main


Expand Down Expand Up @@ -317,6 +318,19 @@ def test_main(monkeypatch, tmp_path, capsys):
]) + '\n'


def test_main_no_libzbar(monkeypatch, tmp_path, capsys):
filename = str(tmp_path / 'hello.svg')
pyqrcode.create('hello').svg(filename)
monkeypatch.setattr(sys, 'argv', ['qr2text', '--decode', filename])
monkeypatch.setattr(qr2text, 'pyzbar', None)
with pytest.raises(SystemExit) as exc:
main()
assert exc.value.code == 0
assert capsys.readouterr().err == '\n'.join([
'libzbar is not available, --decode ignored',
]) + '\n'


def test_main_error(monkeypatch, tmp_path, capsys):
hello_svg = tmp_path / 'hello.svg'
hello_svg.write_text('this is not an SVG file\n')
Expand Down

0 comments on commit 6faf281

Please sign in to comment.