Skip to content

Commit e941beb

Browse files
committed
Make BarCode object do its own translation of .uri → .path
But leave behind .uri for backwards-compatibility.
1 parent 561fa4b commit e941beb

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ The `BarCodeReader` class is used to decode images:
2525
3.4.1 (3, 4, 1)
2626
>>> barcode = reader.decode("test/barcodes/QR_CODE-easy.png")
2727
>>> print(barcode)
28-
BarCode(raw='This should be QR_CODE', parsed='This should be QR_CODE', uri='file:///path/to/test/barcodes/QR_CODE-easy.png, format='QR_CODE', type='TEXT', points=[(15.0, 87.0), (15.0, 15.0), (87.0, 15.0), (75.0, 75.0)])
28+
BarCode(raw='This should be QR_CODE', parsed='This should be QR_CODE', path='test/barcodes/QR_CODE-easy.png', format='QR_CODE', type='TEXT', points=[(15.0, 87.0), (15.0, 15.0), (87.0, 15.0), (75.0, 75.0)])
2929
```
3030

31-
The attributes of the decoded `BarCode` object are `raw`, `parsed`, `uri`, `format`, `type`, and `points`. The list of formats which ZXing can decode is
31+
The attributes of the decoded `BarCode` object are `raw`, `parsed`, `path`, `format`, `type`, and `points`. The list of formats which ZXing can decode is
3232
[here](https://zxing.github.io/zxing/apidocs/com/google/zxing/BarcodeFormat.html).
3333

3434
The `decode()` method accepts an image path (or list of paths) and takes optional parameters `try_harder` (boolean), `possible_formats` (list of formats to consider), and `pure_barcode` (boolean).

test/test_all.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_decoding_multiple():
7575

7676
def test_parsing():
7777
dec = zxing.BarCode.parse("""
78-
file:///tmp/default.png (format: FAKE_DATA, type: TEXT):
78+
file:///tmp/default%20file.png (format: FAKE_DATA, type: TEXT):
7979
Raw result:
8080
Élan|\tthe barcode is taking off
8181
Parsed result:
@@ -87,7 +87,8 @@ def test_parsing():
8787
Point 2: (201.0,198.0)
8888
Point 3: (205.23952,21.0)
8989
""".encode())
90-
assert dec.uri == 'file:///tmp/default.png'
90+
assert dec.uri == 'file:///tmp/default%20file.png'
91+
assert dec.path == '/tmp/default file.png'
9192
assert dec.format == 'FAKE_DATA'
9293
assert dec.type == 'TEXT'
9394
assert dec.raw == 'Élan|\tthe barcode is taking off'

zxing/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,15 @@ def __init__(self, uri, format, type, raw, parsed, points):
168168
self.type = type
169169
self.points = points
170170

171+
@property
172+
def path(self):
173+
try:
174+
return file_uri_to_path(self.uri)
175+
except ValueError:
176+
pass
177+
171178
def __repr__(self):
172-
return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r})'.format(
173-
self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points)
179+
return '{}(raw={!r}, parsed={!r}, {}={!r}, format={!r}, type={!r}, points={!r})'.format(
180+
self.__class__.__name__, self.raw, self.parsed,
181+
'path' if self.path else 'uri', self.path or self.uri,
182+
self.format, self.type, self.points)

0 commit comments

Comments
 (0)