ean-tools 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ean_tools/__init__.py +0 -0
- ean_tools/barcode_info.py +57 -0
- ean_tools/barcode_prefixes.py +61 -0
- ean_tools/check_digits.py +44 -0
- ean_tools/data/gs1-8-prefixes.yaml +28 -0
- ean_tools/data/gs1-prefixes.yaml +655 -0
- ean_tools/data/isbn-prefixes.yaml +691 -0
- ean_tools/normalization.py +72 -0
- ean_tools-0.1.0.dist-info/METADATA +62 -0
- ean_tools-0.1.0.dist-info/RECORD +11 -0
- ean_tools-0.1.0.dist-info/WHEEL +4 -0
@@ -0,0 +1,72 @@
|
|
1
|
+
import re
|
2
|
+
from typing import Optional
|
3
|
+
|
4
|
+
from ean_tools.check_digits import get_correct_check_digit, has_correct_check_digit, isbn10_has_correct_check_digit
|
5
|
+
|
6
|
+
|
7
|
+
def normalize_barcode(barcode: str, is_isbn: Optional[bool] = None) -> str:
|
8
|
+
"""Normalizes a barcode.
|
9
|
+
|
10
|
+
Args:
|
11
|
+
barcode (str): The barcode string.
|
12
|
+
is_isbn (Optional[bool]): Flag to indicate whether the barcode is an ISBN
|
13
|
+
(to resolve possible ambiguity between ISBN-10 and EAN barcodes).
|
14
|
+
|
15
|
+
Returns:
|
16
|
+
str: The normalized barcode string.
|
17
|
+
|
18
|
+
Raises:
|
19
|
+
ValueError: If the barcode is invalid or ambiguous.
|
20
|
+
"""
|
21
|
+
barcode = re.sub(r'[\s-]', '', barcode)
|
22
|
+
if not barcode or not barcode.isdigit() or barcode == '0' * len(barcode):
|
23
|
+
raise ValueError("Doesn't look like a barcode")
|
24
|
+
|
25
|
+
barcode_len = len(barcode)
|
26
|
+
barcode_stripped = barcode.lstrip('0')
|
27
|
+
barcode_stripped_len = len(barcode_stripped)
|
28
|
+
|
29
|
+
if len(barcode) < 8:
|
30
|
+
raise ValueError('Too short for a barcode')
|
31
|
+
|
32
|
+
if barcode_stripped_len > 14:
|
33
|
+
raise ValueError('Too long for a barcode')
|
34
|
+
|
35
|
+
is_valid_isbn10 = barcode_len == 10 and isbn10_has_correct_check_digit(barcode)
|
36
|
+
|
37
|
+
if is_isbn:
|
38
|
+
if is_valid_isbn10:
|
39
|
+
return convert_isbn10_to_isbn13(barcode)
|
40
|
+
|
41
|
+
if barcode_len == 10 and not is_valid_isbn10:
|
42
|
+
raise ValueError('Invalid ISBN')
|
43
|
+
|
44
|
+
if barcode_len != 13 or not re.match(r'^97[8-9]', barcode):
|
45
|
+
raise ValueError('Invalid ISBN')
|
46
|
+
|
47
|
+
if barcode_len == 10 and is_isbn is None:
|
48
|
+
is_valid_ean = has_correct_check_digit(barcode)
|
49
|
+
|
50
|
+
if not is_valid_ean and is_valid_isbn10:
|
51
|
+
return convert_isbn10_to_isbn13(barcode)
|
52
|
+
|
53
|
+
if is_valid_ean is is_valid_isbn10:
|
54
|
+
raise ValueError('Ambiguous ISBN-10 / EAN')
|
55
|
+
|
56
|
+
if barcode_stripped_len in (13, 14, 8):
|
57
|
+
return barcode_stripped
|
58
|
+
|
59
|
+
return barcode_stripped.rjust(13, '0')
|
60
|
+
|
61
|
+
|
62
|
+
def convert_isbn10_to_isbn13(isbn10: str) -> str:
|
63
|
+
"""Converts an ISBN-10 barcode to ISBN-13 format.
|
64
|
+
|
65
|
+
Args:
|
66
|
+
isbn10 (str): The ISBN-10 barcode string.
|
67
|
+
|
68
|
+
Returns:
|
69
|
+
str: The corresponding ISBN-13 barcode string.
|
70
|
+
"""
|
71
|
+
barcode = '978' + isbn10
|
72
|
+
return barcode[:-1] + get_correct_check_digit(barcode)
|
@@ -0,0 +1,62 @@
|
|
1
|
+
Metadata-Version: 2.3
|
2
|
+
Name: ean-tools
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: Collection of tools for validating and getting information about EAN (UPC, GTIN) and ISBN barcodes.
|
5
|
+
Home-page: https://github.com/ean-db/ean-tools
|
6
|
+
License: MIT
|
7
|
+
Keywords: barcode,ean,upc,isbn,gtin
|
8
|
+
Author: EAN-DB
|
9
|
+
Author-email: support@ean-db.com
|
10
|
+
Requires-Python: >=3.12
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
15
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
16
|
+
Requires-Dist: PyYaml (>=6)
|
17
|
+
Requires-Dist: affix-tree (>=0.1.1)
|
18
|
+
Project-URL: Repository, https://github.com/ean-db/ean-tools
|
19
|
+
Description-Content-Type: text/markdown
|
20
|
+
|
21
|
+
# ean-tools
|
22
|
+
|
23
|
+
Collection of tools for validating and getting information about EAN (UPC, GTIN) and ISBN barcodes.
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
```commandline
|
28
|
+
pip install affix-tree
|
29
|
+
```
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Barcode normalization
|
34
|
+
|
35
|
+
```pycon
|
36
|
+
>>> from ean_tools.normalization import normalize_barcode
|
37
|
+
|
38
|
+
>>> normalize_barcode('978-84865-4608-3')
|
39
|
+
|
40
|
+
'9788486546083'
|
41
|
+
```
|
42
|
+
|
43
|
+
### Check digit validation
|
44
|
+
|
45
|
+
```pycon
|
46
|
+
>>> from ean_tools.check_digits import has_correct_check_digit
|
47
|
+
|
48
|
+
>>> has_correct_check_digit('8510000076279')
|
49
|
+
|
50
|
+
False
|
51
|
+
```
|
52
|
+
|
53
|
+
### Getting additional barcode information
|
54
|
+
|
55
|
+
```pycon
|
56
|
+
>>> from ean_tools.barcode_info import get_barcode_info, BarcodeType
|
57
|
+
|
58
|
+
>>> get_barcode_info('4000000001140')
|
59
|
+
|
60
|
+
BarcodeInfo(barcode_type=BarcodeType.REGULAR, description='GS1 Germany', country='de')
|
61
|
+
```
|
62
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
ean_tools/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
2
|
+
ean_tools/barcode_info.py,sha256=AYr9muXXai8NBYeLR5Usls94R3Yifh5bxqWoJ0gPtMQ,1948
|
3
|
+
ean_tools/barcode_prefixes.py,sha256=hG6wPO4-3GkIp8iIC0wT4c1fKM6kRkHmYPtEmEeJKco,1843
|
4
|
+
ean_tools/check_digits.py,sha256=UZKDWJm6mSqNCOk_DDx9yrwCar70RCZsu9B86gHHwc8,1270
|
5
|
+
ean_tools/data/gs1-8-prefixes.yaml,sha256=8_QfQic9KoEd91nTqT8UdbA5gDtKKSEkmvYepmyYh_A,374
|
6
|
+
ean_tools/data/gs1-prefixes.yaml,sha256=rEwlAPPCOgph-4PKuW5IkTE3EXbNmyvwfO9PHVKPsBA,10876
|
7
|
+
ean_tools/data/isbn-prefixes.yaml,sha256=ABFMVsiKa7TXLl9UPVJReN-DB6_0ka0qGfwSA71RC2U,11790
|
8
|
+
ean_tools/normalization.py,sha256=SIAd4MSawVhTnDIQvu5uyqDhcnRprkhCvaFS2sKHdyc,2234
|
9
|
+
ean_tools-0.1.0.dist-info/METADATA,sha256=1Lm5R4IJAYQ5C-H9hMsjbH3ObBzZaFXzfhOVLzUtGeg,1525
|
10
|
+
ean_tools-0.1.0.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
|
11
|
+
ean_tools-0.1.0.dist-info/RECORD,,
|