|
| 1 | +"""Module: barcode.codabar |
| 2 | +
|
| 3 | +:Provided barcodes: Codabar (NW-7) |
| 4 | +""" |
| 5 | +__docformat__ = "restructuredtext en" |
| 6 | + |
| 7 | +from barcode.base import Barcode |
| 8 | +from barcode.charsets import codabar |
| 9 | +from barcode.errors import BarcodeError |
| 10 | +from barcode.errors import IllegalCharacterError |
| 11 | + |
| 12 | + |
| 13 | +class CODABAR(Barcode): |
| 14 | + """Initializes a new CODABAR instance. |
| 15 | +
|
| 16 | + :parameters: |
| 17 | + code : String |
| 18 | + Codabar (NW-7) string that matches [ABCD][0-9$:/.+-]+[ABCD] |
| 19 | + writer : barcode.writer Instance |
| 20 | + The writer to render the barcode (default: SVGWriter). |
| 21 | + narrow: Integer |
| 22 | + Width of the narrow elements (default: 2) |
| 23 | + wide: Integer |
| 24 | + Width of the wide elements (default: 5) |
| 25 | + wide/narrow must be in the range 2..3 |
| 26 | + """ |
| 27 | + |
| 28 | + name = "Codabar (NW-7)" |
| 29 | + |
| 30 | + def __init__(self, code, writer=None, narrow=2, wide=5): |
| 31 | + self.code = code |
| 32 | + self.writer = writer or self.default_writer() |
| 33 | + self.narrow = narrow |
| 34 | + self.wide = wide |
| 35 | + |
| 36 | + def __str__(self): |
| 37 | + return self.code |
| 38 | + |
| 39 | + def get_fullcode(self): |
| 40 | + return self.code |
| 41 | + |
| 42 | + def build(self): |
| 43 | + try: |
| 44 | + data = ( |
| 45 | + codabar.STARTSTOP[self.code[0]] + "n" |
| 46 | + ) # Start with [A-D], followed by a narrow space |
| 47 | + |
| 48 | + except KeyError: |
| 49 | + raise BarcodeError("Codabar should start with either A,B,C or D") |
| 50 | + |
| 51 | + try: |
| 52 | + data += "n".join( |
| 53 | + [codabar.CODES[c] for c in self.code[1:-1]] |
| 54 | + ) # separated by a narrow space |
| 55 | + except KeyError: |
| 56 | + raise IllegalCharacterError("Codabar can only contain numerics or $:/.+-") |
| 57 | + |
| 58 | + try: |
| 59 | + data += "n" + codabar.STARTSTOP[self.code[-1]] # End with [A-D] |
| 60 | + except KeyError: |
| 61 | + raise BarcodeError("Codabar should end with either A,B,C or D") |
| 62 | + |
| 63 | + raw = "" |
| 64 | + for e in data: |
| 65 | + if e == "W": |
| 66 | + raw += "1" * self.wide |
| 67 | + if e == "w": |
| 68 | + raw += "0" * self.wide |
| 69 | + if e == "N": |
| 70 | + raw += "1" * self.narrow |
| 71 | + if e == "n": |
| 72 | + raw += "0" * self.narrow |
| 73 | + return [raw] |
0 commit comments