Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pathlib.Path objects as filenames #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions test/test_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import os
from operator import attrgetter
from pathlib import Path
from tempfile import mkdtemp

from PIL import Image
Expand Down Expand Up @@ -174,3 +176,33 @@ 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(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


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`.
]
)
7 changes: 4 additions & 3 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down