From c1256677c6fad0e9190fc3aaae11f3c49922b448 Mon Sep 17 00:00:00 2001 From: stefan6419846 <96178532+stefan6419846@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:06:58 +0100 Subject: [PATCH 1/2] Add support for pathlib.Path objects as filenames --- test/test_all.py | 29 +++++++++++++++++++++++++++++ zxing/__init__.py | 7 ++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/test/test_all.py b/test/test_all.py index 044c026..fc766c3 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -1,5 +1,7 @@ import logging import os +from operator import attrgetter +from pathlib import Path from tempfile import mkdtemp from PIL import Image @@ -174,3 +176,30 @@ def test_bad_file_format_error(): global test_reader with helper.assertRaises(zxing.BarCodeReaderException): test_reader.decode(os.path.join(test_barcode_dir, 'bad_format.png')) + + +@with_setup(setup_reader) +def _check_filenames_type(filenames, expected_raw): + result = test_reader.decode(filenames) + if isinstance(expected_raw, list): + assert list(map(attrgetter('raw'), result)) == expected_raw + else: + assert result.raw == expected_raw + + +def test_filenames_types(): + multi_paths = [ + Path(test_barcode_dir, test_barcodes[0][0]), + os.path.join(test_barcode_dir, test_barcodes[1][0]) + ] + multi_expected = [test_barcodes[0][2], test_barcodes[1][2]] + yield from ( + (_check_filenames_type, filenames, expected_raw) for filenames, expected_raw in [ + (os.path.join(test_barcode_dir, test_barcodes[0][0]), test_barcodes[0][2]), + (Path(test_barcode_dir, test_barcodes[0][0]), test_barcodes[0][2]), + (multi_paths, multi_expected), + (set(multi_paths), multi_expected), + (tuple(multi_paths), multi_expected), + ((x for x in multi_paths), multi_expected), # Generator, inline version of `yield`. + ] + ) diff --git a/zxing/__init__.py b/zxing/__init__.py index 27421a4..508fea9 100644 --- a/zxing/__init__.py +++ b/zxing/__init__.py @@ -17,6 +17,7 @@ from enum import Enum from io import IOBase from itertools import chain +from types import GeneratorType try: from PIL.Image import Image @@ -69,11 +70,11 @@ def __init__(self, classpath=None, java=None): def decode(self, filenames, try_harder=False, possible_formats=None, pure_barcode=False, products_only=False): possible_formats = (possible_formats,) if isinstance(possible_formats, str) else possible_formats - if isinstance(filenames, (str, IOBase, Image) if have_pil else (str, IOBase)): + if isinstance(filenames, (list, set, tuple, GeneratorType)): + one_file = False + else: one_file = True filenames = filenames, - else: - one_file = False file_uris = [] temp_files = [] From 9ec14c47d9148d3f4a4981cf1be2f279ca368b60 Mon Sep 17 00:00:00 2001 From: stefan6419846 <96178532+stefan6419846@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:23:56 +0100 Subject: [PATCH 2/2] fix tests for set --- test/test_all.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/test_all.py b/test/test_all.py index fc766c3..4d6159e 100644 --- a/test/test_all.py +++ b/test/test_all.py @@ -181,7 +181,10 @@ def test_bad_file_format_error(): @with_setup(setup_reader) def _check_filenames_type(filenames, expected_raw): result = test_reader.decode(filenames) - if isinstance(expected_raw, list): + if isinstance(filenames, set): + # Might be in another order. + assert set(map(attrgetter('raw'), result)) == set(expected_raw) + elif isinstance(expected_raw, list): assert list(map(attrgetter('raw'), result)) == expected_raw else: assert result.raw == expected_raw