Skip to content

Commit

Permalink
Make BarCode object do its own translation of .uri → .path
Browse files Browse the repository at this point in the history
But leave behind .uri for backwards-compatibility.
  • Loading branch information
dlenski committed Oct 6, 2021
1 parent 561fa4b commit e941beb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ The `BarCodeReader` class is used to decode images:
3.4.1 (3, 4, 1)
>>> barcode = reader.decode("test/barcodes/QR_CODE-easy.png")
>>> print(barcode)
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)])
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)])
```

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

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).
Expand Down
5 changes: 3 additions & 2 deletions test/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_decoding_multiple():

def test_parsing():
dec = zxing.BarCode.parse("""
file:///tmp/default.png (format: FAKE_DATA, type: TEXT):
file:///tmp/default%20file.png (format: FAKE_DATA, type: TEXT):
Raw result:
Élan|\tthe barcode is taking off
Parsed result:
Expand All @@ -87,7 +87,8 @@ def test_parsing():
Point 2: (201.0,198.0)
Point 3: (205.23952,21.0)
""".encode())
assert dec.uri == 'file:///tmp/default.png'
assert dec.uri == 'file:///tmp/default%20file.png'
assert dec.path == '/tmp/default file.png'
assert dec.format == 'FAKE_DATA'
assert dec.type == 'TEXT'
assert dec.raw == 'Élan|\tthe barcode is taking off'
Expand Down
13 changes: 11 additions & 2 deletions zxing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def __init__(self, uri, format, type, raw, parsed, points):
self.type = type
self.points = points

@property
def path(self):
try:
return file_uri_to_path(self.uri)
except ValueError:
pass

def __repr__(self):
return '{}(raw={!r}, parsed={!r}, uri={!r}, format={!r}, type={!r}, points={!r})'.format(
self.__class__.__name__, self.raw, self.parsed, self.uri, self.format, self.type, self.points)
return '{}(raw={!r}, parsed={!r}, {}={!r}, format={!r}, type={!r}, points={!r})'.format(
self.__class__.__name__, self.raw, self.parsed,
'path' if self.path else 'uri', self.path or self.uri,
self.format, self.type, self.points)

0 comments on commit e941beb

Please sign in to comment.