-
-
Notifications
You must be signed in to change notification settings - Fork 125
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
Basic support for MSI / Plessey #191
base: main
Are you sure you want to change the base?
Changes from 5 commits
46d61ed
037bf5b
ce27899
92e90d5
2d56b80
7505382
201c07d
bac523d
f5118db
03dbda8
3b26a75
e38637c
079d2b5
fe993d7
523d2cd
71d3fe1
4bc63a6
097591f
51e1a79
ba6ce09
59f0776
84881a4
1428670
a3e3aec
4a822b1
47f557c
854a1ff
02c5f44
aed1d4b
2cfaad4
eebc2c5
943acae
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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,72 @@ def get_fullcode(self): | |||||||||||||||||||||||||||||||||||
return super().get_fullcode()[1:] | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
class MSI(Barcode): | ||||||||||||||||||||||||||||||||||||
"""Initializes a new MSI (Modified Plessey) 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 | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use regular sphinx format here so docs render properly.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had copied the format from Code128... last commit fixes other docstrings in codex.py |
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
limitations: | ||||||||||||||||||||||||||||||||||||
- only one check digit (Luhn Mod10) | ||||||||||||||||||||||||||||||||||||
- only standard prefix/suffix | ||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
name = "MSI/Modified Plessey" | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
def __init__(self, code, writer=None, byteorder=None, encoding=None): | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type annotations missing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please help me with this? I don't know how to do it when multiple types are allowed. I have severe back and joint issues that are made much worse when sitting for extended periods of time, so I will not dig further into an obscure doc to find out. |
||||||||||||||||||||||||||||||||||||
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): | ||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
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)]) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops :-s my keyboard is depressed