From 46d61ed3326acc0b2cecb13160f3faf81978ff36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Fri, 9 Dec 2022 11:41:50 +0100 Subject: [PATCH 01/31] MSI barcodes, with bytes/strings encoding --- barcode/__init__.py | 2 ++ barcode/codex.py | 63 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/barcode/__init__.py b/barcode/__init__.py index d0d15cd..5772c80 100755 --- a/barcode/__init__.py +++ b/barcode/__init__.py @@ -13,6 +13,7 @@ from barcode.codex import Code39 from barcode.codex import Code128 from barcode.codex import Gs1_128 +from barcode.codex import MSI from barcode.ean import EAN8 from barcode.ean import EAN8_GUARD from barcode.ean import EAN13 @@ -50,6 +51,7 @@ "gs1_128": Gs1_128, "codabar": CODABAR, "nw-7": CODABAR, + "msi": MSI, } PROVIDED_BARCODES = list(__BARCODE_MAP) diff --git a/barcode/codex.py b/barcode/codex.py index 74665be..7a02cb1 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -1,6 +1,6 @@ """Module: barcode.codex -:Provided barcodes: Code 39, Code 128, PZN +:Provided barcodes: Code 39, Code 128, PZN, MSI """ from barcode.base import Barcode from barcode.charsets import code39 @@ -129,7 +129,7 @@ class Code128(Barcode): The writer to render the barcode (default: SVGWriter). """ - name = "Code 128" + ame = "Code 128" def __init__(self, code, writer=None): self.code = code @@ -271,5 +271,64 @@ def get_fullcode(self): return super().get_fullcode()[1:] +class MSI(Barcode): + """Initializes a new MSI (Modified Plessy) instance. The checksum is added automatically + when building the bars. + + :parameters: + code : int or bytes or string + Code MSI int without checksum (added automatically). + Code MSI bytes without checksum. requires byteorder for int conversion + Code MSI string without checksum. requires encoding and byteorder for int conversion + writer : barcode.writer Instance + The writer to render the barcode (default: SVGWriter). + byteorder : string + 'big' or 'little' ; to convert bytes to int + encoding : string + if set, convert bytes to string and use this as a label. defaults to utf-8 + """ + name = "MSI/Modified Plessy" + + def __init__(self, code, writer=None, byteorder = 'big', encoding = 'utf-8' ): + self.writer = writer or self.default_writer() + self._buffer = "" + + if type(code) is int: + self.code = str(code) + elif type(code) is bytes: + self.code = str( int.from_bytes( code, byteorder ) ) + if encoding is not None: + self.label = code.decode(encoding) + else: + self.label = self.code + elif type(code) is str: + self.code = str( int.from_bytes( bytes(code, encoding), byteorder ) ) + self.label = code + + def __str__(self): + return self.label + + @property + def encoded(self): + return self._build() + + def get_fullcode(self): + return self.label + + def _build(self): + from luhn import append as luhn_check_append + + bcd = ''.join([f"{bin(int(n))[2:]:0>4}" for n in luhn_check_append( self.code )]) + + conv = { + '0': '100', + '1': '110', + } + return ['110' + ''.join([conv[i] for i in bcd]) + '1001'] + + def build(self): + return self.encoded + + # For pre 0.8 compatibility PZN = PZN7 From 037bf5b24096798f289fd643d3dbdea401f17094 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 14:36:35 +0100 Subject: [PATCH 02/31] typo + better docstrings --- barcode/__init__.py | 10 ++++------ barcode/codex.py | 11 +++++++++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/barcode/__init__.py b/barcode/__init__.py index 5772c80..0be483f 100755 --- a/barcode/__init__.py +++ b/barcode/__init__.py @@ -1,12 +1,10 @@ -"""This package provides a simple way to create standard barcodes. -It needs no external packages to be installed, the barcodes are +"""This package provides a simple way to create standard barcodes. It needs no +external packages to be installed (exception: 'luhn' for MSI), the barcodes are created as SVG objects. If Pillow is installed, the barcodes can also be rendered as images (all formats supported by Pillow). """ -import os -from typing import BinaryIO -from typing import Dict -from typing import Union +import os from +typing import BinaryIO from typing import Dict from typing import Union from barcode.codabar import CODABAR from barcode.codex import PZN diff --git a/barcode/codex.py b/barcode/codex.py index 7a02cb1..409d96b 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -272,7 +272,7 @@ def get_fullcode(self): class MSI(Barcode): - """Initializes a new MSI (Modified Plessy) instance. The checksum is added automatically + """Initializes a new MSI (Modified Plessey) instance. The checksum is added automatically when building the bars. :parameters: @@ -286,8 +286,12 @@ class MSI(Barcode): 'big' or 'little' ; to convert bytes to int encoding : string if set, convert bytes to string and use this as a label. defaults to utf-8 + + limitations: + - only one check digit (Luhn Mod10) + - only standard prefix/suffix """ - name = "MSI/Modified Plessy" + name = "MSI/Modified Plessey" def __init__(self, code, writer=None, byteorder = 'big', encoding = 'utf-8' ): self.writer = writer or self.default_writer() @@ -316,6 +320,9 @@ def get_fullcode(self): return self.label def _build(self): + """ + check digit is computed with https://pypi.org/project/luhn/ + """ from luhn import append as luhn_check_append bcd = ''.join([f"{bin(int(n))[2:]:0>4}" for n in luhn_check_append( self.code )]) From ce2789918a98215cc8dc807b5d0cbd2c811b2ca4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 13:45:02 +0000 Subject: [PATCH 03/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/__init__.py | 5 +++-- barcode/codex.py | 17 +++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/barcode/__init__.py b/barcode/__init__.py index 0be483f..b9aae1b 100755 --- a/barcode/__init__.py +++ b/barcode/__init__.py @@ -3,15 +3,16 @@ created as SVG objects. If Pillow is installed, the barcodes can also be rendered as images (all formats supported by Pillow). """ -import os from +import os + typing import BinaryIO from typing import Dict from typing import Union from barcode.codabar import CODABAR +from barcode.codex import MSI from barcode.codex import PZN from barcode.codex import Code39 from barcode.codex import Code128 from barcode.codex import Gs1_128 -from barcode.codex import MSI from barcode.ean import EAN8 from barcode.ean import EAN8_GUARD from barcode.ean import EAN13 diff --git a/barcode/codex.py b/barcode/codex.py index 409d96b..598e57b 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -291,22 +291,23 @@ class MSI(Barcode): - only one check digit (Luhn Mod10) - only standard prefix/suffix """ + name = "MSI/Modified Plessey" - def __init__(self, code, writer=None, byteorder = 'big', encoding = 'utf-8' ): + def __init__(self, code, writer=None, byteorder="big", encoding="utf-8"): self.writer = writer or self.default_writer() self._buffer = "" if type(code) is int: self.code = str(code) elif type(code) is bytes: - self.code = str( int.from_bytes( code, byteorder ) ) + self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: self.label = code.decode(encoding) else: self.label = self.code elif type(code) is str: - self.code = str( int.from_bytes( bytes(code, encoding), byteorder ) ) + self.code = str(int.from_bytes(bytes(code, encoding), byteorder)) self.label = code def __str__(self): @@ -325,13 +326,13 @@ def _build(self): """ from luhn import append as luhn_check_append - bcd = ''.join([f"{bin(int(n))[2:]:0>4}" for n in luhn_check_append( self.code )]) + bcd = "".join([f"{bin(int(n))[2:]:0>4}" for n in luhn_check_append(self.code)]) conv = { - '0': '100', - '1': '110', - } - return ['110' + ''.join([conv[i] for i in bcd]) + '1001'] + "0": "100", + "1": "110", + } + return ["110" + "".join([conv[i] for i in bcd]) + "1001"] def build(self): return self.encoded From 92e90d5cfe9ca1612cb4567abf8c8a29a770275a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 15:13:29 +0100 Subject: [PATCH 04/31] fixed silly mistake, more sensible defaults byteorder and encoding parameters default to None ; user must specifically set them to enable conversion of strings or bytes --- barcode/__init__.py | 5 +++-- barcode/codex.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/barcode/__init__.py b/barcode/__init__.py index b9aae1b..dd8ad1e 100755 --- a/barcode/__init__.py +++ b/barcode/__init__.py @@ -4,8 +4,9 @@ rendered as images (all formats supported by Pillow). """ import os - -typing import BinaryIO from typing import Dict from typing import Union +from typing import BinaryIO +from typing import Dict +from typing import Union from barcode.codabar import CODABAR from barcode.codex import MSI diff --git a/barcode/codex.py b/barcode/codex.py index 598e57b..087abe4 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -294,7 +294,7 @@ class MSI(Barcode): name = "MSI/Modified Plessey" - def __init__(self, code, writer=None, byteorder="big", encoding="utf-8"): + def __init__(self, code, writer=None, byteorder=None, encoding=None ): self.writer = writer or self.default_writer() self._buffer = "" From 2d56b8037de6838e42bef2474380b7d3089422de Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 14:16:26 +0000 Subject: [PATCH 05/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 087abe4..e61ff40 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -294,7 +294,7 @@ class MSI(Barcode): name = "MSI/Modified Plessey" - def __init__(self, code, writer=None, byteorder=None, encoding=None ): + def __init__(self, code, writer=None, byteorder=None, encoding=None): self.writer = writer or self.default_writer() self._buffer = "" From 7505382044be3b6a6e15bdf55b23543579afa9e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:07:37 +0100 Subject: [PATCH 06/31] removed code-can-be-string, added type hints to encode strings, just use something else (such as Code128) --- barcode/codex.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index e61ff40..50ee2ae 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -276,16 +276,16 @@ class MSI(Barcode): when building the bars. :parameters: - code : int or bytes or string + code : int or bytes Code MSI int without checksum (added automatically). - Code MSI bytes without checksum. requires byteorder for int conversion - Code MSI string without checksum. requires encoding and byteorder for int conversion + Code MSI bytes without checksum. requires byteorder (WARNING: non-standard use) writer : barcode.writer Instance The writer to render the barcode (default: SVGWriter). byteorder : string 'big' or 'little' ; to convert bytes to int encoding : string if set, convert bytes to string and use this as a label. defaults to utf-8 + if unset, use integer value as label limitations: - only one check digit (Luhn Mod10) @@ -294,7 +294,7 @@ class MSI(Barcode): name = "MSI/Modified Plessey" - def __init__(self, code, writer=None, byteorder=None, encoding=None): + def __init__(self, code:(int,bytes), writer=None, byteorder:str=None, encoding:str='utf-8', label:str=None ): self.writer = writer or self.default_writer() self._buffer = "" @@ -303,12 +303,9 @@ def __init__(self, code, writer=None, byteorder=None, encoding=None): elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: - self.label = code.decode(encoding) + self.label = code.decode(encoding) if label is None else label else: - self.label = self.code - elif type(code) is str: - self.code = str(int.from_bytes(bytes(code, encoding), byteorder)) - self.label = code + self.label = self.code if label is None else label def __str__(self): return self.label From 201c07df05e66be3c4d48798b2f696145b96d326 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:07:56 +0000 Subject: [PATCH 07/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 50ee2ae..9c810cd 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -294,7 +294,14 @@ class MSI(Barcode): name = "MSI/Modified Plessey" - def __init__(self, code:(int,bytes), writer=None, byteorder:str=None, encoding:str='utf-8', label:str=None ): + def __init__( + self, + code: (int, bytes), + writer=None, + byteorder: str = None, + encoding: str = "utf-8", + label: str = None, + ): self.writer = writer or self.default_writer() self._buffer = "" From bac523d373a57818d4236ba8834131e7535b61f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:12:37 +0100 Subject: [PATCH 08/31] use regular sphinx format (also for other types in same file) --- barcode/codex.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 9c810cd..3482bb0 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -77,10 +77,9 @@ def render(self, writer_options=None, text=None): class PZN7(Code39): """Initializes new German number for pharmaceutical products. - :parameters: - pzn : String + :param pzn: String Code to render. - writer : barcode.writer Instance + :param writer: barcode.writer Instance The writer to render the barcode (default: SVGWriter). """ @@ -122,10 +121,9 @@ class Code128(Barcode): """Initializes a new Code128 instance. The checksum is added automatically when building the bars. - :parameters: - code : String + :param code: String Code 128 string without checksum (added automatically). - writer : barcode.writer Instance + :param writer: barcode.writer Instance The writer to render the barcode (default: SVGWriter). """ @@ -276,14 +274,14 @@ class MSI(Barcode): when building the bars. :parameters: - code : int or bytes + :param code: int or bytes Code MSI int without checksum (added automatically). Code MSI bytes without checksum. requires byteorder (WARNING: non-standard use) - writer : barcode.writer Instance + :param writer: barcode.writer Instance The writer to render the barcode (default: SVGWriter). - byteorder : string + :param byteorder: string 'big' or 'little' ; to convert bytes to int - encoding : string + :param encoding: string if set, convert bytes to string and use this as a label. defaults to utf-8 if unset, use integer value as label From f5118dbb713caf6f85d02c0acaadc44a2eea2fbf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:13:14 +0000 Subject: [PATCH 09/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 3482bb0..706d4c8 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -77,10 +77,10 @@ def render(self, writer_options=None, text=None): class PZN7(Code39): """Initializes new German number for pharmaceutical products. - :param pzn: String - Code to render. - :param writer: barcode.writer Instance - The writer to render the barcode (default: SVGWriter). + :param pzn: String + Code to render. + :param writer: barcode.writer Instance + The writer to render the barcode (default: SVGWriter). """ name = "Pharmazentralnummer" From 03dbda8948bf36588b8436c5cc93079661090e05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:15:04 +0100 Subject: [PATCH 10/31] depressed keyboard --- barcode/codex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 706d4c8..9f6236e 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -127,7 +127,7 @@ class Code128(Barcode): The writer to render the barcode (default: SVGWriter). """ - ame = "Code 128" + name = "Code 128" def __init__(self, code, writer=None): self.code = code From 3b26a751376511a70127f923a980bd95d7cff870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:22:41 +0100 Subject: [PATCH 11/31] fixes for failed test --- barcode/codex.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 9f6236e..b65ed9d 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -270,13 +270,13 @@ def get_fullcode(self): class MSI(Barcode): - """Initializes a new MSI (Modified Plessey) instance. The checksum is added automatically - when building the bars. + """Initializes a new MSI (Modified Plessey) instance. The checksum is added + automatically when building the bars. :parameters: :param code: int or bytes Code MSI int without checksum (added automatically). - Code MSI bytes without checksum. requires byteorder (WARNING: non-standard use) + Code bytes without checksum. requires byteorder (WARNING: non-standard use) :param writer: barcode.writer Instance The writer to render the barcode (default: SVGWriter). :param byteorder: string @@ -294,11 +294,11 @@ class MSI(Barcode): def __init__( self, - code: (int, bytes), + code: [int, bytes], writer=None, - byteorder: str = None, - encoding: str = "utf-8", - label: str = None, + byteorder: [NoneType,str] = None, + encoding: [NoneType,str] = "utf-8", + label: [NoneType,str] = None, ): self.writer = writer or self.default_writer() self._buffer = "" From e38637c92f8113a44531c3d3270c005b5e7d34d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:22:59 +0000 Subject: [PATCH 12/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index b65ed9d..f5906b8 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -270,7 +270,7 @@ def get_fullcode(self): class MSI(Barcode): - """Initializes a new MSI (Modified Plessey) instance. The checksum is added + """Initializes a new MSI (Modified Plessey) instance. The checksum is added automatically when building the bars. :parameters: @@ -296,9 +296,9 @@ def __init__( self, code: [int, bytes], writer=None, - byteorder: [NoneType,str] = None, - encoding: [NoneType,str] = "utf-8", - label: [NoneType,str] = None, + byteorder: [NoneType, str] = None, + encoding: [NoneType, str] = "utf-8", + label: [NoneType, str] = None, ): self.writer = writer or self.default_writer() self._buffer = "" From 079d2b5e8e922488b03432a8a15a59d76f93fb5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:24:00 +0100 Subject: [PATCH 13/31] NoneType is... not a type >>> type(None) /me confused --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index f5906b8..1ce4dd6 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -296,9 +296,9 @@ def __init__( self, code: [int, bytes], writer=None, - byteorder: [NoneType, str] = None, - encoding: [NoneType, str] = "utf-8", - label: [NoneType, str] = None, + byteorder: str = None, + encoding: str = "utf-8", + label: str = None, ): self.writer = writer or self.default_writer() self._buffer = "" From fe993d7a68939e97fd6c2863dd7add7bc3d75b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:25:23 +0100 Subject: [PATCH 14/31] stilly very new to this... --- barcode/codex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 1ce4dd6..2875255 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -294,7 +294,7 @@ class MSI(Barcode): def __init__( self, - code: [int, bytes], + code: Tuple(int, bytes), writer=None, byteorder: str = None, encoding: str = "utf-8", From 523d2cd50faf1acbd1ede7df436fb7697b6600bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:26:19 +0100 Subject: [PATCH 15/31] this is annoying --- barcode/codex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 2875255..b0279e8 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -294,7 +294,7 @@ class MSI(Barcode): def __init__( self, - code: Tuple(int, bytes), + code: Tuple[int, bytes], writer=None, byteorder: str = None, encoding: str = "utf-8", From 71d3fe1c0f7b45431e1c90b9f70d70987db3868d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:27:20 +0100 Subject: [PATCH 16/31] import --- barcode/codex.py | 1 + 1 file changed, 1 insertion(+) diff --git a/barcode/codex.py b/barcode/codex.py index b0279e8..c81f0eb 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -8,6 +8,7 @@ from barcode.errors import BarcodeError from barcode.errors import IllegalCharacterError from barcode.errors import NumberOfDigitsError +from typing import Tuple __docformat__ = "restructuredtext en" From 4bc63a615cbb464f877f480a0b42eae52012bf3b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:27:31 +0000 Subject: [PATCH 17/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index c81f0eb..6a8c56d 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -2,13 +2,14 @@ :Provided barcodes: Code 39, Code 128, PZN, MSI """ +from typing import Tuple + from barcode.base import Barcode from barcode.charsets import code39 from barcode.charsets import code128 from barcode.errors import BarcodeError from barcode.errors import IllegalCharacterError from barcode.errors import NumberOfDigitsError -from typing import Tuple __docformat__ = "restructuredtext en" From 097591ffa71bbb60b15464a0ee1de1338964a276 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:28:45 +0100 Subject: [PATCH 18/31] more errors --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 6a8c56d..bf0f8aa 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -298,9 +298,9 @@ def __init__( self, code: Tuple[int, bytes], writer=None, - byteorder: str = None, - encoding: str = "utf-8", - label: str = None, + byteorder: Tuple[None, str] = None, + encoding: Tuple[str, None] = "utf-8", + label: Tuple[None, str] = None, ): self.writer = writer or self.default_writer() self._buffer = "" From 51e1a79a019bf385f48443628444ee9e4a6788e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:30:19 +0100 Subject: [PATCH 19/31] don't know how to do that --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index bf0f8aa..eb693c9 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -298,9 +298,9 @@ def __init__( self, code: Tuple[int, bytes], writer=None, - byteorder: Tuple[None, str] = None, - encoding: Tuple[str, None] = "utf-8", - label: Tuple[None, str] = None, + byteorder = None, + encoding = "utf-8", + label = None, ): self.writer = writer or self.default_writer() self._buffer = "" From ba6ce09b0ba2cdc3367e9c0de7b295f6a89f17af Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 10 Dec 2022 17:30:36 +0000 Subject: [PATCH 20/31] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index eb693c9..15b8a2a 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -298,9 +298,9 @@ def __init__( self, code: Tuple[int, bytes], writer=None, - byteorder = None, - encoding = "utf-8", - label = None, + byteorder=None, + encoding="utf-8", + label=None, ): self.writer = writer or self.default_writer() self._buffer = "" From 59f07768d3e1d3d6611d0897ab338432cc30d5e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:33:28 +0100 Subject: [PATCH 21/31] more explicit --- barcode/codex.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 15b8a2a..0aff307 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -308,11 +308,15 @@ def __init__( if type(code) is int: self.code = str(code) elif type(code) is bytes: - self.code = str(int.from_bytes(code, byteorder)) - if encoding is not None: - self.label = code.decode(encoding) if label is None else label + try: + self.code = str(int.from_bytes(code, byteorder)) + except TypeError: + raise else: - self.label = self.code if label is None else label + if encoding is not None: + self.label = code.decode(encoding) if label is None else label + else: + self.label = self.code if label is None else label def __str__(self): return self.label From 84881a4352c12c48ca209f195429152f97bd0b9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:35:54 +0100 Subject: [PATCH 22/31] this is getting ridiculous. --- barcode/codex.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barcode/codex.py b/barcode/codex.py index 0aff307..17680c4 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -296,7 +296,7 @@ class MSI(Barcode): def __init__( self, - code: Tuple[int, bytes], + code, writer=None, byteorder=None, encoding="utf-8", From 1428670b5bde0ba320992c8fadb3881a1857b460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sat, 10 Dec 2022 18:36:40 +0100 Subject: [PATCH 23/31] whatever. those checks are getting on my nerves ad making things very slow --- barcode/codex.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 17680c4..eeb7e8e 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -2,8 +2,6 @@ :Provided barcodes: Code 39, Code 128, PZN, MSI """ -from typing import Tuple - from barcode.base import Barcode from barcode.charsets import code39 from barcode.charsets import code128 From a3e3aeca973507ba12affd84d90c49ba75f98c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:06:57 +0100 Subject: [PATCH 24/31] removed stupid try/except --- barcode/codex.py | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index eeb7e8e..23a742f 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -306,15 +306,11 @@ def __init__( if type(code) is int: self.code = str(code) elif type(code) is bytes: - try: - self.code = str(int.from_bytes(code, byteorder)) - except TypeError: - raise + self.code = str(int.from_bytes(code, byteorder)) + if encoding is not None: + self.label = code.decode(encoding) if label is None else label else: - if encoding is not None: - self.label = code.decode(encoding) if label is None else label - else: - self.label = self.code if label is None else label + self.label = self.code if label is None else label def __str__(self): return self.label From 4a822b14069f7f50a955db8936d17b0e863f62f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:18:47 +0100 Subject: [PATCH 25/31] setting label when code is int --- barcode/codex.py | 1 + 1 file changed, 1 insertion(+) diff --git a/barcode/codex.py b/barcode/codex.py index 23a742f..d12aaff 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -305,6 +305,7 @@ def __init__( if type(code) is int: self.code = str(code) + self.label = label if label is not None else self.code elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: From 47f557cfbcf9b9cdba995b9ec1ffc549fa076e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:27:30 +0100 Subject: [PATCH 26/31] background can be omitted --- barcode/writer.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/barcode/writer.py b/barcode/writer.py index 3a3dcd0..90247fc 100755 --- a/barcode/writer.py +++ b/barcode/writer.py @@ -322,14 +322,15 @@ def _init(self, code): attributes = {"id": "barcode_group"} _set_attributes(group, **attributes) self._group = self._root.appendChild(group) - background = self._document.createElement("rect") - attributes = { - "width": "100%", - "height": "100%", - "style": f"fill:{self.background}", - } - _set_attributes(background, **attributes) - self._group.appendChild(background) + if self.background: + background = self._document.createElement("rect") + attributes = { + "width": "100%", + "height": "100%", + "style": f"fill:{self.background}", + } + _set_attributes(background, **attributes) + self._group.appendChild(background) def _create_module(self, xpos, ypos, width, color): # Background rect has been provided already, so skipping "spaces" From 854a1ff65aff5c90d9659f091c34b0c41042c451 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:51:55 +0100 Subject: [PATCH 27/31] just found out about 'text' argument to save() --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index d12aaff..8ef56cb 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -284,10 +284,12 @@ class MSI(Barcode): :param encoding: string if set, convert bytes to string and use this as a label. defaults to utf-8 if unset, use integer value as label + Note: label can also be set when calling save() with param 'text' limitations: - only one check digit (Luhn Mod10) - only standard prefix/suffix + """ name = "MSI/Modified Plessey" @@ -298,14 +300,12 @@ def __init__( writer=None, byteorder=None, encoding="utf-8", - label=None, ): self.writer = writer or self.default_writer() self._buffer = "" if type(code) is int: - self.code = str(code) - self.label = label if label is not None else self.code + self.label = self.code = str(code) elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: From 02c5f4403ca54691529f2f98d8007dc27f7cd925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:51:55 +0100 Subject: [PATCH 28/31] just found out about 'text' argument to save() --- barcode/codex.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index d12aaff..8ef56cb 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -284,10 +284,12 @@ class MSI(Barcode): :param encoding: string if set, convert bytes to string and use this as a label. defaults to utf-8 if unset, use integer value as label + Note: label can also be set when calling save() with param 'text' limitations: - only one check digit (Luhn Mod10) - only standard prefix/suffix + """ name = "MSI/Modified Plessey" @@ -298,14 +300,12 @@ def __init__( writer=None, byteorder=None, encoding="utf-8", - label=None, ): self.writer = writer or self.default_writer() self._buffer = "" if type(code) is int: - self.code = str(code) - self.label = label if label is not None else self.code + self.label = self.code = str(code) elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: From aed1d4bd9db4982f2ade82f7b996426249514d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 10:21:55 +0100 Subject: [PATCH 29/31] meeh. doing label a bit more "right" --- barcode/codex.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 8ef56cb..6482887 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -284,7 +284,8 @@ class MSI(Barcode): :param encoding: string if set, convert bytes to string and use this as a label. defaults to utf-8 if unset, use integer value as label - Note: label can also be set when calling save() with param 'text' + Note: for convenience ; label can also be set when calling save() with param + 'text' limitations: - only one check digit (Luhn Mod10) @@ -309,7 +310,7 @@ def __init__( elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: - self.label = code.decode(encoding) if label is None else label + self.label = code.decode(encoding) else: self.label = self.code if label is None else label From 2cfaad4a1b8c2d3f835b7b34edbf6f01ef709f00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 09:27:30 +0100 Subject: [PATCH 30/31] background can be omitted --- barcode/writer.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/barcode/writer.py b/barcode/writer.py index 3a3dcd0..90247fc 100755 --- a/barcode/writer.py +++ b/barcode/writer.py @@ -322,14 +322,15 @@ def _init(self, code): attributes = {"id": "barcode_group"} _set_attributes(group, **attributes) self._group = self._root.appendChild(group) - background = self._document.createElement("rect") - attributes = { - "width": "100%", - "height": "100%", - "style": f"fill:{self.background}", - } - _set_attributes(background, **attributes) - self._group.appendChild(background) + if self.background: + background = self._document.createElement("rect") + attributes = { + "width": "100%", + "height": "100%", + "style": f"fill:{self.background}", + } + _set_attributes(background, **attributes) + self._group.appendChild(background) def _create_module(self, xpos, ypos, width, color): # Background rect has been provided already, so skipping "spaces" From eebc2c538c4927815c28ad4eff4283e992a8a963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jesus=20Zen=20Dro=C3=AFd?= Date: Sun, 11 Dec 2022 10:21:55 +0100 Subject: [PATCH 31/31] meeh. doing label a bit more "right" --- barcode/codex.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/barcode/codex.py b/barcode/codex.py index 8ef56cb..6482887 100755 --- a/barcode/codex.py +++ b/barcode/codex.py @@ -284,7 +284,8 @@ class MSI(Barcode): :param encoding: string if set, convert bytes to string and use this as a label. defaults to utf-8 if unset, use integer value as label - Note: label can also be set when calling save() with param 'text' + Note: for convenience ; label can also be set when calling save() with param + 'text' limitations: - only one check digit (Luhn Mod10) @@ -309,7 +310,7 @@ def __init__( elif type(code) is bytes: self.code = str(int.from_bytes(code, byteorder)) if encoding is not None: - self.label = code.decode(encoding) if label is None else label + self.label = code.decode(encoding) else: self.label = self.code if label is None else label