diff options
Diffstat (limited to 'share/extensions/Barcode')
| -rw-r--r-- | share/extensions/Barcode/Base.py | 140 | ||||
| -rw-r--r-- | share/extensions/Barcode/BaseEan.py | 107 | ||||
| -rw-r--r-- | share/extensions/Barcode/Code128.py | 130 | ||||
| -rw-r--r-- | share/extensions/Barcode/Code25i.py | 57 | ||||
| -rw-r--r-- | share/extensions/Barcode/Code39.py | 116 | ||||
| -rw-r--r-- | share/extensions/Barcode/Code39Ext.py | 2 | ||||
| -rw-r--r-- | share/extensions/Barcode/Code93.py | 120 | ||||
| -rw-r--r-- | share/extensions/Barcode/Ean13.py | 13 | ||||
| -rw-r--r-- | share/extensions/Barcode/Ean2.py | 38 | ||||
| -rw-r--r-- | share/extensions/Barcode/Ean5.py | 37 | ||||
| -rw-r--r-- | share/extensions/Barcode/Ean8.py | 14 | ||||
| -rw-r--r-- | share/extensions/Barcode/Makefile.am | 1 | ||||
| -rw-r--r-- | share/extensions/Barcode/Rm4scc.py | 9 | ||||
| -rw-r--r-- | share/extensions/Barcode/Upca.py | 16 | ||||
| -rw-r--r-- | share/extensions/Barcode/Upce.py | 66 | ||||
| -rw-r--r-- | share/extensions/Barcode/__init__.py | 18 |
16 files changed, 468 insertions, 416 deletions
diff --git a/share/extensions/Barcode/Base.py b/share/extensions/Barcode/Base.py index b7429f84f..bfa0a774f 100644 --- a/share/extensions/Barcode/Base.py +++ b/share/extensions/Barcode/Base.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Base module for rendering barcodes for Inkscape. @@ -23,114 +23,140 @@ import itertools import sys from lxml import etree +(TEXT_POS_BOTTOM, TEXT_POS_TOP) = range(2) (WHITE_BAR, BLACK_BAR, TALL_BAR) = range(3) TEXT_TEMPLATE = 'font-size:%dpx;text-align:center;text-anchor:middle;' +SVG_URI = u'http://www.w3.org/2000/svg' +# pylint: disable=abstract-class-not-used class Barcode(object): """Provide a base class for all barcode renderers""" + default_height = 30 + font_size = 9 name = None - def error(self, bar, msg): + def error(self, text, msg): """Cause an error to be reported""" sys.stderr.write( - "Error encoding '%s' as %s barcode: %s\n" % (bar, self.name, msg)) + "Error encoding '%s' as %s barcode: %s\n" % (text, self.name, msg)) return "ERROR" - def __init__(self, param={}): + def encode(self, text): + """ + Replace this with the encoding function, it should return + a string of ones and zeros + """ + raise NotImplementedError("You need to write an encode() function.") + + def __init__(self, param): + param = param or {} self.document = param.get('document', None) - self.x = int(param.get('x', 0)) - self.y = int(param.get('y', 0)) - self.scale = param.get('scale', 1) - self.height = param.get('height', 30) - self.label = param.get('text', None) - self.string = self.encode( self.label ) - - if not self.string: - return + self.known_ids = [] + self._extra = [] - self.width = len(self.string) - self.data = self.graphicalArray(self.string) + self.pos_x = int(param.get('x', 0)) + self.pos_y = int(param.get('y', 0)) + self.text = param.get('text', None) + self.scale = param.get('scale', 1) + self.height = param.get('height', self.default_height) + self.pos_text = param.get('text_pos', TEXT_POS_BOTTOM) - def get_id(self): - """Get the next useful id""" - if not self.document: - return "barcode" - doc_ids = {} - docIdNodes = self.document.xpath('//@id') - for m in docIdNodes: - doc_ids[m] = 1 + if self.document: + self.known_ids = list(self.document.xpath('//@id')) - name = 'barcode' + if not self.text: + raise ValueError("No string specified for barcode.") + def get_id(self, name='element'): + """Get the next useful id (and claim it)""" index = 0 - while (doc_ids.has_key(name)): + while name in self.known_ids: index += 1 name = 'barcode%d' % index + self.known_ids.append(name) return name + def add_extra_barcode(self, barcode, **kw): + """Add an extra barcode along side this one, used for ean13 extras""" + from . import getBarcode + kw['height'] = self.height + kw['document'] = self.document + kw['scale'] = None + self._extra.append(getBarcode(barcode, **kw).generate()) + def generate(self): """Generate the actual svg from the coding""" - svg_uri = u'http://www.w3.org/2000/svg' - if self.string == 'ERROR': + string = self.encode(self.text) + + if string == 'ERROR': return - if not self.string or not self.data: - raise ValueError("No string specified for barcode.") - data = self.data - name = self.get_id() + name = self.get_id('barcode') # use an svg group element to contain the barcode - barcode = etree.Element('{%s}%s' % (svg_uri,'g')) + barcode = etree.Element('{%s}g' % SVG_URI) barcode.set('id', name) barcode.set('style', 'fill: black;') - barcode.set('transform', 'translate(%d,%d) scale(%f)' % (self.x, self.y, self.scale)) - + if self.scale: + barcode.set('transform', 'translate(%d,%d) scale(%f)' % ( + self.pos_x, self.pos_y, self.scale)) + else: + barcode.set('transform', 'translate(%d,%d)' % ( + self.pos_x, self.pos_y)) + + bar_id = 1 bar_offset = 0 - bar_id = 1 + tops = set() - for datum in data: + for datum in self.graphical_array(string): # Datum 0 tells us what style of bar is to come next - style = self.getStyle(int(datum[0])) + style = self.get_style(int(datum[0])) # Datum 1 tells us what width in units, # style tells us how wide a unit is width = int(datum[1]) * int(style['width']) if style['write']: - rect = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'rect')) - rect.set('x', str(bar_offset)) - rect.set('y', str(style['top'])) - rect.set('width', str(width)) + tops.add(style['top']) + rect = etree.SubElement(barcode, '{%s}rect' % SVG_URI) + rect.set('x', str(bar_offset)) + rect.set('y', str(style['top'])) + if self.pos_text == TEXT_POS_TOP: + rect.set('y', str(style['top'] + self.font_size)) + rect.set('id', "%s_bar%d" % (name, bar_id)) + rect.set('width', str(width)) rect.set('height', str(style['height'])) - rect.set('id', "%s_bar%d" % (name, bar_id)) bar_offset += width bar_id += 1 + for extra in self._extra: + if extra is not None: + barcode.append(extra) + bar_width = bar_offset # Add text at the bottom of the barcode - text = etree.SubElement(barcode,'{%s}%s' % (svg_uri,'text')) - text.set( 'x', str(int(bar_width / 2))) - text.set( 'y', str(self.height + self.fontSize() )) - text.set( 'style', TEXT_TEMPLATE % self.fontSize() ) - text.set( '{http://www.w3.org/XML/1998/namespace}space', 'preserve' ) - text.set( 'id', '%s_text' % name ) - text.text = str(self.label) + text = etree.SubElement(barcode, '{%s}text' % SVG_URI) + text.set('x', str(int(bar_width / 2))) + text.set('y', str(min(tops) + self.font_size - 1)) + if self.pos_text == TEXT_POS_BOTTOM: + text.set('y', str(self.height + max(tops) + self.font_size)) + text.set('style', TEXT_TEMPLATE % self.font_size) + text.set('{http://www.w3.org/XML/1998/namespace}space', 'preserve') + text.set('id', '%s_text' % name) + text.text = str(self.text) return barcode - def graphicalArray(self, code): + def graphical_array(self, code): """Converts black and white markets into a space array""" - return [(x,len(list(y))) for x, y in itertools.groupby(code)] + return [(x, len(list(y))) for x, y in itertools.groupby(code)] - def getStyle(self, index): + def get_style(self, index): """Returns the styles that should be applied to each bar""" - result = { 'width' : 1, 'top' : 0, 'write' : True } + result = {'width' : 1, 'top' : 0, 'write' : True} if index == BLACK_BAR: result['height'] = int(self.height) if index == TALL_BAR: - result['height'] = int(self.height) + int(self.fontSize() / 2) + result['height'] = int(self.height) + int(self.font_size / 2) if index == WHITE_BAR: result['write'] = False return result - def fontSize(self): - """Return the ideal font size, defaults to 9px""" - return 9 diff --git a/share/extensions/Barcode/BaseEan.py b/share/extensions/Barcode/BaseEan.py index 05c9b1c39..2c3ab0c09 100644 --- a/share/extensions/Barcode/BaseEan.py +++ b/share/extensions/Barcode/BaseEan.py @@ -13,40 +13,39 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Some basic common code shared between EAN and UCP generators. """ -from Base import Barcode -import sys +from .Base import Barcode, TEXT_POS_TOP MAPPING = [ # Left side of barcode Family '0' - [ "0001101", "0011001", "0010011", "0111101", "0100011", - "0110001", "0101111", "0111011", "0110111", "0001011" ], + ["0001101", "0011001", "0010011", "0111101", "0100011", + "0110001", "0101111", "0111011", "0110111", "0001011"], # Left side of barcode Family '1' and flipped to right side. - [ "0100111", "0110011", "0011011", "0100001", "0011101", - "0111001", "0000101", "0010001", "0001001", "0010111" ], + ["0100111", "0110011", "0011011", "0100001", "0011101", + "0111001", "0000101", "0010001", "0001001", "0010111"], ] # This chooses which of the two encodings above to use. -FAMILIES = [ '000000', '001011', '001101', '001110', '010011', - '011001', '011100', '010101', '010110', '011010' ] - -GUARD_BAR = '202' -CENTER_BAR = '02020' +FAMILIES = ('000000', '001011', '001101', '001110', '010011', + '011001', '011100', '010101', '010110', '011010') class EanBarcode(Barcode): """Simple base class for all EAN type barcodes""" - length = None lengths = None - checks = [] + length = None + checks = [] + extras = {} + magic = 10 + guard_bar = '202' + center_bar = '02020' def intarray(self, number): """Convert a string of digits into an array of ints""" - return [ int(i) for i in number ] - + return [int(i) for i in number] def encode_interleaved(self, family, number, fams=FAMILIES): """Encode any side of the barcode, interleaved""" @@ -54,27 +53,24 @@ class EanBarcode(Barcode): encset = self.intarray(fams[family]) for i in range(len(number)): thismap = MAPPING[encset[i]] - result.append( thismap[number[i]] ) + result.append(thismap[number[i]]) return result - def encode_right(self, number): """Encode the right side of the barcode, non-interleaved""" result = [] - for n in number: + for num in number: # The right side is always the reverse of the left's family '1' - result.append( MAPPING[1][n][::-1] ) + result.append(MAPPING[1][num][::-1]) return result - def encode_left(self, number): """Encode the left side of the barcode, non-interleaved""" result = [] - for n in number: - result.append( MAPPING[0][n] ) + for num in number: + result.append(MAPPING[0][num]) return result - def space(self, *spacing): """Space out an array of numbers""" result = '' @@ -86,60 +82,75 @@ class EanBarcode(Barcode): result += ' ' * space return result - - def getLengths(self): + def get_lengths(self): """Return a list of acceptable lengths""" if self.length: - return [ self.length ] + return [self.length] return self.lengths[:] - def encode(self, code): """Encode any EAN barcode""" + code = code.replace(' ', '').strip() + if not code.isdigit(): return self.error(code, 'Not a Number, must be digits 0-9 only') - lengths = self.getLengths() + self.checks + lengths = self.get_lengths() + self.checks + # Allow extra barcodes after the first one if len(code) not in lengths: - return self.error(code, 'Wrong size, must be %s digits' % - (', '.join(self.space(lengths)))) + for extra in self.extras: + sep = len(code) - extra + if sep in lengths: + # Generate a barcode along side this one. + self.add_extra_barcode(self.extras[extra], text=code[sep:], + x=self.pos_x + 400 * self.scale, text_pos=TEXT_POS_TOP) + code = code[:sep] + + if len(code) not in lengths: + return self.error(code, 'Wrong size %d, must be %s digits' % + (len(code), ', '.join([str(length) for length in lengths]))) if self.checks: if len(code) not in self.checks: - code = self.appendChecksum(code) - elif not self.verifyChecksum(code): + code = self.append_checksum(code) + elif not self.verify_checksum(code): return self.error(code, 'Checksum failed, omit for new sum') return self._encode(self.intarray(code)) - - def _encode(self, n): + def _encode(self, num): + """ + Write your EAN encoding function, it's passed in an array of int and + it should return a string on 1 and 0 for black and white parts + """ raise NotImplementedError("_encode should be provided by parent EAN") - def enclose(self, left, right=[], guard=GUARD_BAR, center=CENTER_BAR): + def enclose(self, left, right=()): """Standard Enclosure""" - parts = [ guard ] + left + [ center ] + right + [ guard ] - return ''.join( parts ) + parts = [self.guard_bar] + left + parts.append(self.center_bar) + parts += list(right) + [self.guard_bar] + return ''.join(parts) - def getChecksum(self, number, magic=10): + def get_checksum(self, number): """Generate a UPCA/EAN13/EAN8 Checksum""" - weight = [3,1] * len(number) + weight = [3, 1] * len(number) result = 0 # We need to work from left to right so reverse number = number[::-1] # checksum based on first digits. for i in range(len(number)): - result += int(number[i]) * weight[i] + result += int(number[i]) * weight[i] # Modulous result to a single digit checksum - checksum = magic - (result % magic) - if checksum < 0 or checksum >= magic: - return '0' + checksum = self.magic - (result % self.magic) + if checksum < 0 or checksum >= self.magic: + return '0' return str(checksum) - def appendChecksum(self, number): + def append_checksum(self, number): """Apply the checksum to a short number""" - return number + self.getChecksum(number) + return number + self.get_checksum(number) - def verifyChecksum(self, number): + def verify_checksum(self, number): """Verify any checksum""" - return self.getChecksum(number[:-1]) == number[-1] + return self.get_checksum(number[:-1]) == number[-1] diff --git a/share/extensions/Barcode/Code128.py b/share/extensions/Barcode/Code128.py index 618ce7817..b90e3bcf6 100644 --- a/share/extensions/Barcode/Code128.py +++ b/share/extensions/Barcode/Code128.py @@ -17,42 +17,42 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ -Python barcode renderer for Code128/EAN128 barcodes. Designed for use with Inkscape. +Renderer for Code128/EAN128 codes. Designed for use with Inkscape. """ -from Base import Barcode -import math +from .Base import Barcode import re -codeMap = [ - '11011001100','11001101100','11001100110','10010011000','10010001100', - '10001001100','10011001000','10011000100','10001100100','11001001000', - '11001000100','11000100100','10110011100','10011011100','10011001110', - '10111001100','10011101100','10011100110','11001110010','11001011100', - '11001001110','11011100100','11001110100','11101101110','11101001100', - '11100101100','11100100110','11101100100','11100110100','11100110010', - '11011011000','11011000110','11000110110','10100011000','10001011000', - '10001000110','10110001000','10001101000','10001100010','11010001000', - '11000101000','11000100010','10110111000','10110001110','10001101110', - '10111011000','10111000110','10001110110','11101110110','11010001110', - '11000101110','11011101000','11011100010','11011101110','11101011000', - '11101000110','11100010110','11101101000','11101100010','11100011010', - '11101111010','11001000010','11110001010','10100110000','10100001100', - '10010110000','10010000110','10000101100','10000100110','10110010000', - '10110000100','10011010000','10011000010','10000110100','10000110010', - '11000010010','11001010000','11110111010','11000010100','10001111010', - '10100111100','10010111100','10010011110','10111100100','10011110100', - '10011110010','11110100100','11110010100','11110010010','11011011110', - '11011110110','11110110110','10101111000','10100011110','10001011110', - '10111101000','10111100010','11110101000','11110100010','10111011110', - '10111101110','11101011110','11110101110','11010000100','11010010000', - '11010011100','11000111010','11' ] - -def mapExtra(sd, chars): - result = list(sd) +CODE_MAP = [ + '11011001100', '11001101100', '11001100110', '10010011000', '10010001100', + '10001001100', '10011001000', '10011000100', '10001100100', '11001001000', + '11001000100', '11000100100', '10110011100', '10011011100', '10011001110', + '10111001100', '10011101100', '10011100110', '11001110010', '11001011100', + '11001001110', '11011100100', '11001110100', '11101101110', '11101001100', + '11100101100', '11100100110', '11101100100', '11100110100', '11100110010', + '11011011000', '11011000110', '11000110110', '10100011000', '10001011000', + '10001000110', '10110001000', '10001101000', '10001100010', '11010001000', + '11000101000', '11000100010', '10110111000', '10110001110', '10001101110', + '10111011000', '10111000110', '10001110110', '11101110110', '11010001110', + '11000101110', '11011101000', '11011100010', '11011101110', '11101011000', + '11101000110', '11100010110', '11101101000', '11101100010', '11100011010', + '11101111010', '11001000010', '11110001010', '10100110000', '10100001100', + '10010110000', '10010000110', '10000101100', '10000100110', '10110010000', + '10110000100', '10011010000', '10011000010', '10000110100', '10000110010', + '11000010010', '11001010000', '11110111010', '11000010100', '10001111010', + '10100111100', '10010111100', '10010011110', '10111100100', '10011110100', + '10011110010', '11110100100', '11110010100', '11110010010', '11011011110', + '11011110110', '11110110110', '10101111000', '10100011110', '10001011110', + '10111101000', '10111100010', '11110101000', '11110100010', '10111011110', + '10111101110', '11101011110', '11110101110', '11010000100', '11010010000', + '11010011100', '11000111010', '11'] + +def map_extra(data, chars): + """Maps the data into the chars""" + result = list(data) for char in chars: result.append(chr(char)) result.append('FNC3') @@ -60,18 +60,18 @@ def mapExtra(sd, chars): result.append('SHIFT') return result -# The mapExtra method is used to slim down the amount +# The map_extra method is used to slim down the amount # of pre code and instead we generate the lists -charAB = list(' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_') -charA = mapExtra(charAB, range(0, 31)) # Offset 64 -charB = mapExtra(charAB, range(96, 125)) # Offset -32 +CHAR_AB = list(" !\"#$%&\'()*+,-./0123456789:;<=>?@" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_") +CHAR_A = map_extra(CHAR_AB, range(0, 31)) # Offset 64 +CHAR_B = map_extra(CHAR_AB, range(96, 125)) # Offset -32 class Code128(Barcode): """Main barcode object, generates the encoding bits here""" def encode(self, text): - result = '' blocks = [] - block = '' + block = '' # Split up into sections of numbers, or charicters # This makes sure that all the charicters are encoded @@ -81,42 +81,42 @@ class Code128(Barcode): block = block + datum else: if block: - blocks.append(self.bestBlock(block)) + blocks.append(self.best_block(block)) block = '' - blocks.append( [ 'C', datum ] ) + blocks.append(['C', datum]) if block: - blocks.append(self.bestBlock(block)) - block = ''; + blocks.append(self.best_block(block)) + block = '' - self.inclabel = text - return self.encodeBlocks(blocks) + return self.encode_blocks(blocks) - def bestBlock(self, block): - # If this has lower case then select B over A + def best_block(self, block): + """If this has lower case then select B over A""" if block.upper() == block: - return [ 'A', block ] - return [ 'B', block ] + return ['A', block] + return ['B', block] - def encodeBlocks(self, blocks): - total = 0 - pos = 0 - encode = ''; + def encode_blocks(self, blocks): + """Encode the given blocks into A, B or C codes""" + encode = '' + total = 0 + pos = 0 for block in blocks: - set = block[0] + b_set = block[0] datum = block[1] # POS : 0, 1 # A : 101, 103 # B : 100, 104 # C : 99, 105 - num = 0; - if set == 'A': + num = 0 + if b_set == 'A': num = 103 - elif set == 'B': + elif b_set == 'B': num = 104 - elif set == 'C': + elif b_set == 'C': num = 105 i = pos @@ -126,28 +126,28 @@ class Code128(Barcode): i = 1 total = total + num * i - encode = encode + codeMap[num] + encode = encode + CODE_MAP[num] pos = pos + 1 - if set == 'A' or set == 'B': - chars = charB - if set == 'A': - chars = charA + if b_set == 'A' or b_set == 'B': + chars = CHAR_B + if b_set == 'A': + chars = CHAR_A for char in datum: total = total + (chars.index(char) * pos) - encode = encode + codeMap[chars.index(char)] + encode = encode + CODE_MAP[chars.index(char)] pos = pos + 1 else: for char in (datum[i:i+2] for i in range(0, len(datum), 2)): total = total + (int(char) * pos) - encode = encode + codeMap[int(char)] + encode = encode + CODE_MAP[int(char)] pos = pos + 1 checksum = total % 103 - encode = encode + codeMap[checksum] - encode = encode + codeMap[106] - encode = encode + codeMap[107] + encode = encode + CODE_MAP[checksum] + encode = encode + CODE_MAP[106] + encode = encode + CODE_MAP[107] return encode diff --git a/share/extensions/Barcode/Code25i.py b/share/extensions/Barcode/Code25i.py index 51346be60..2c751559c 100644 --- a/share/extensions/Barcode/Code25i.py +++ b/share/extensions/Barcode/Code25i.py @@ -13,53 +13,48 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Generate barcodes for Code25-interleaved 2 of 5, for Inkscape. """ -from Base import Barcode -import sys +from .Base import Barcode # 1 means thick, 0 means thin -encoding = { - '0' : '00110', - '1' : '10001', - '2' : '01001', - '3' : '11000', - '4' : '00101', - '5' : '10100', - '6' : '01100', - '7' : '00011', - '8' : '10010', - '9' : '01010', +ENCODE = { + '0': '00110', + '1': '10001', + '2': '01001', + '3': '11000', + '4': '00101', + '5': '10100', + '6': '01100', + '7': '00011', + '8': '10010', + '9': '01010', } -# Start and stop code are already encoded into white (0) and black(1) bars -start_code = '1010' -stop_code = '1101' class Code25i(Barcode): - # Convert a text into string binary of black and white markers + """Convert a text into string binary of black and white markers""" + # Start and stop code are already encoded into white (0) and black(1) bars def encode(self, number): - self.label = number - if not number.isdigit(): - sys.stderr.write("CODE25 can only encode numbers.\n") - return + return self.error(number, "CODE25 can only encode numbers.") - # Number of figures to encode must be even, a 0 is added to the left in case it's odd. - if len(number) % 2 > 0 : + # Number of figures to encode must be even, + # a 0 is added to the left in case it's odd. + if len(number) % 2 > 0: number = '0' + number # Number is encoded by pairs of 2 figures - size = len(number) / 2; - encoded = start_code; + size = len(number) / 2 + encoded = '1010' for i in range(size): # First in the pair is encoded in black (1), second in white (0) - black = encoding[number[i*2]] - white = encoding[number[i*2+1]] + black = ENCODE[number[i*2]] + white = ENCODE[number[i*2+1]] for j in range(5): if black[j] == '1': encoded += '11' @@ -69,9 +64,5 @@ class Code25i(Barcode): encoded += '00' else: encoded += '0' - - encoded += stop_code - - self.inclabel = number - return encoded; + return encoded + '1101' diff --git a/share/extensions/Barcode/Code39.py b/share/extensions/Barcode/Code39.py index ade397463..0d4d445b1 100644 --- a/share/extensions/Barcode/Code39.py +++ b/share/extensions/Barcode/Code39.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for Code39 barcodes. Designed for use with Inkscape. @@ -21,73 +21,70 @@ Python barcode renderer for Code39 barcodes. Designed for use with Inkscape. from Base import Barcode -encoding = { - '0' : '000110100', - '1' : '100100001', - '2' : '001100001', - '3' : '101100000', - '4' : '000110001', - '5' : '100110000', - '6' : '001110000', - '7' : '000100101', - '8' : '100100100', - '9' : '001100100', - 'A' : '100001001', - 'B' : '001001001', - 'C' : '101001000', - 'D' : '000011001', - 'E' : '100011000', - 'F' : '001011000', - 'G' : '000001101', - 'H' : '100001100', - 'I' : '001001100', - 'J' : '000011100', - 'K' : '100000011', - 'L' : '001000011', - 'M' : '101000010', - 'N' : '000010011', - 'O' : '100010010', - 'P' : '001010010', - 'Q' : '000000111', - 'R' : '100000110', - 'S' : '001000110', - 'T' : '000010110', - 'U' : '110000001', - 'V' : '011000001', - 'W' : '111000000', - 'X' : '010010001', - 'Y' : '110010000', - 'Z' : '011010000', - '-' : '010000101', - '*' : '010010100', - '+' : '010001010', - '$' : '010101000', - '%' : '000101010', - '/' : '010100010', - '.' : '110000100', - ' ' : '011000100', +ENCODE = { + '0': '000110100', + '1': '100100001', + '2': '001100001', + '3': '101100000', + '4': '000110001', + '5': '100110000', + '6': '001110000', + '7': '000100101', + '8': '100100100', + '9': '001100100', + 'A': '100001001', + 'B': '001001001', + 'C': '101001000', + 'D': '000011001', + 'E': '100011000', + 'F': '001011000', + 'G': '000001101', + 'H': '100001100', + 'I': '001001100', + 'J': '000011100', + 'K': '100000011', + 'L': '001000011', + 'M': '101000010', + 'N': '000010011', + 'O': '100010010', + 'P': '001010010', + 'Q': '000000111', + 'R': '100000110', + 'S': '001000110', + 'T': '000010110', + 'U': '110000001', + 'V': '011000001', + 'W': '111000000', + 'X': '010010001', + 'Y': '110010000', + 'Z': '011010000', + '-': '010000101', + '*': '010010100', + '+': '010001010', + '$': '010101000', + '%': '000101010', + '/': '010100010', + '.': '110000100', + ' ': '011000100', } class Code39(Barcode): - # Convert a text into string binary of black and white markers + """Convert a text into string binary of black and white markers""" def encode(self, text): - text = text.upper() - self.label = text - text = '*' + text + '*' - result = '' + self.text = text.upper() + result = '' # It isposible for us to encode code39 # into full ascii, but this feature is # not enabled here - for char in text: - if not encoding.has_key(char): - char = '-'; - - result = result + encoding[char] + '0'; + for char in '*' + self.text + '*': + if not ENCODE.has_key(char): + char = '-' + result = result + ENCODE[char] + '0' # Now we need to encode the code39, best read # the code to understand what it's up to: - encoded = ''; - colour = '1'; # 1 = Black, 0 = White + encoded = '' + colour = '1' # 1 = Black, 0 = White for data in result: if data == '1': encoded = encoded + colour + colour @@ -95,6 +92,5 @@ class Code39(Barcode): encoded = encoded + colour colour = colour == '1' and '0' or '1' - self.inclabel = text - return encoded; + return encoded diff --git a/share/extensions/Barcode/Code39Ext.py b/share/extensions/Barcode/Code39Ext.py index b6df47de2..3edf82d2e 100644 --- a/share/extensions/Barcode/Code39Ext.py +++ b/share/extensions/Barcode/Code39Ext.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # """ Python barcode renderer for Code39 Extended barcodes. Designed for Inkscape. diff --git a/share/extensions/Barcode/Code93.py b/share/extensions/Barcode/Code93.py index 866ec6036..939a739dd 100644 --- a/share/extensions/Barcode/Code93.py +++ b/share/extensions/Barcode/Code93.py @@ -13,109 +13,101 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for Code93 barcodes. Designed for use with Inkscape. """ -from Base import Barcode +from .Base import Barcode -chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%' -encode = list(chars) -encode.append('($)') -encode.append('(/)') -encode.append('(+)') -encode.append('(%)') -encode.append('MARKER') +PALLET = list('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%') +PALLET.append('($)') +PALLET.append('(/)') +PALLET.append('(+)') +PALLET.append('(%)') +PALLET.append('MARKER') -map = {} - -i = 0 -for char in encode: - map[char] = i - i = i + 1 - -# Extended encoding maps for full ASCII Code93 -def getMap(array): +MAP = dict((PALLET[i], i) for i in range(len(PALLET))) +def get_map(array): + """Extended ENCODE maps for full ASCII Code93""" result = {} - y = 10 - - for x in array: - result[chr(x)] = encode[y] - y = y + 1 - - return result; + pos = 10 + for char in array: + result[chr(char)] = PALLET[pos] + pos = pos + 1 + return result # MapA is eclectic, but B, C, D are all ASCII ranges -mapA = getMap([27,28,29,30,31,59,60,61,62,63,91,92,93,94,95,123,124,125,126,127,0,64,96,127,127,127]) # % -mapB = getMap(range(1, 26)) # $ -mapC = getMap(range(33, 58)) # / -mapD = getMap(range(97, 122)) # + - -encoding = '100010100 101001000 101000100 101000010 100101000 100100100 100100010 101010000 100010010 100001010 110101000 110100100 110100010 110010100 110010010 110001010 101101000 101100100 101100010 100110100 100011010 101011000 101001100 101000110 100101100 100010110 110110100 110110010 110101100 110100110 110010110 110011010 101101100 101100110 100110110 100111010 100101110 111010100 111010010 111001010 101101110 101110110 110101110 100100110 111011010 111010110 100110010 101011110'.split() +MAP_A = get_map([27, 28, 29, 30, 31, 59, 60, 61, 62, 63, 91, 92, 93, 94, 95, + 123, 124, 125, 126, 127, 0, 64, 96, 127, 127, 127]) # % +MAP_B = get_map(range(1, 26)) # $ +MAP_C = get_map(range(33, 58)) # / +MAP_D = get_map(range(97, 122)) # + + +ENCODE = [ + '100010100', '101001000', '101000100', '101000010', '100101000', + '100100100', '100100010', '101010000', '100010010', '100001010', + '110101000', '110100100', '110100010', '110010100', '110010010', + '110001010', '101101000', '101100100', '101100010', '100110100', + '100011010', '101011000', '101001100', '101000110', '100101100', + '100010110', '110110100', '110110010', '110101100', '110100110', + '110010110', '110011010', '101101100', '101100110', '100110110', + '100111010', '100101110', '111010100', '111010010', '111001010', + '101101110', '101110110', '110101110', '100100110', '111011010', + '111010110', '100110010', '101011110', '' +] class Code93(Barcode): def encode(self, text): - # start marker - bits = self.encode93('MARKER') + # start marker + bits = ENCODE[MAP.get('MARKER', -1)] # Extend to ASCII charset ( return Array ) - text = self.encodeAscii(text) + text = self.encode_ascii(text) # Calculate the checksums text.append(self.checksum(text, 20)) # C text.append(self.checksum(text, 15)) # K - # Now convert text into the encoding bits (black and white stripes) + # Now convert text into the ENCODE bits (black and white stripes) for char in text: - bits = bits + self.encode93(char) - - # end marker - bits = bits + self.encode93('MARKER') + bits = bits + ENCODE[MAP.get(char, -1)] - # termination bar - bits = bits + '1' - - self.inclabel = text - return bits + # end marker and termination bar + return bits + ENCODE[MAP.get('MARKER', -1)] + '1' def checksum(self, text, mod): + """Generate a code 93 checksum""" weight = len(text) % mod - check = 0 + check = 0 for char in text: - check = check + (map[char] * weight) + check = check + (MAP[char] * weight) # Reset the weight is required weight = weight - 1 if weight == 0: weight = mod - return encode[check % 47] + return PALLET[check % 47] - # Some charicters need re-encoding into the code93 specification - def encodeAscii(self, text): + # Some charicters need re-ENCODE into the code93 specification + def encode_ascii(self, text): result = [] for char in text: - if map.has_key(char): + if MAP.has_key(char): result.append(char) - elif mapA.has_key(char): + elif MAP_A.has_key(char): result.append('(%)') - result.append(mapA[char]) - elif mapB.has_key(char): + result.append(MAP_A[char]) + elif MAP_B.has_key(char): result.append('($)') - result.append(mapB[char]) - elif mapC.has_key(char): + result.append(MAP_B[char]) + elif MAP_C.has_key(char): result.append('(/)') - result.append(mapC[char]) - elif mapD.has_key(char): + result.append(MAP_C[char]) + elif MAP_D.has_key(char): result.append('(+)') - result.append(mapD[char]) - + result.append(MAP_D[char]) return result - def encode93(self, char): - if map.has_key(char): - return encoding[map[char]] - return '' - diff --git a/share/extensions/Barcode/Ean13.py b/share/extensions/Barcode/Ean13.py index 3bad5d6e5..41f4df826 100644 --- a/share/extensions/Barcode/Ean13.py +++ b/share/extensions/Barcode/Ean13.py @@ -15,24 +15,25 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for EAN13 barcodes. Designed for use with Inkscape. """ -from BaseEan import EanBarcode +from .BaseEan import EanBarcode class Ean13(EanBarcode): """Provide an Ean13 barcode generator""" name = 'ean13' - lengths = [ 12 ] - checks = [ 13 ] + extras = {2: 'Ean2', 5: 'Ean5'} + checks = [13] + lengths = [12] def _encode(self, n): """Encode an ean13 barcode""" - self.label = self.space(n[0:1], 4, n[1:7], 5, n[7:], 7) + self.text = self.space(n[0:1], 4, n[1:7], 5, n[7:], 7) return self.enclose( - self.encode_interleaved(n[0], n[1:7]), self.encode_right(n[7:]) ) + self.encode_interleaved(n[0], n[1:7]), self.encode_right(n[7:])) diff --git a/share/extensions/Barcode/Ean2.py b/share/extensions/Barcode/Ean2.py new file mode 100644 index 000000000..9aec06a27 --- /dev/null +++ b/share/extensions/Barcode/Ean2.py @@ -0,0 +1,38 @@ +# +# Copyright (C) 2016 Martin Owens +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. +# +""" +Python barcode renderer for EAN2 barcodes. Designed for use with Inkscape. +""" + +from .BaseEan import EanBarcode + +FAMS = ['00', '01', '10', '11'] +START = '01011' + +class Ean2(EanBarcode): + """Provide an Ean5 barcode generator""" + length = 2 + name = 'ean5' + + def _encode(self, number): + if len(number) != 2: + number = ([0, 0] + number)[-2:] + self.text = ' '.join(self.space(number)) + family = ((number[0] * 10) + number[1]) % 4 + return START + '01'.join(self.encode_interleaved(family, number, FAMS)) + diff --git a/share/extensions/Barcode/Ean5.py b/share/extensions/Barcode/Ean5.py index 1bd26a4bd..c6f8555fb 100644 --- a/share/extensions/Barcode/Ean5.py +++ b/share/extensions/Barcode/Ean5.py @@ -1,39 +1,38 @@ -# +# # Copyright (C) 2009 Aaron C Spike # 2010 Martin Owens # -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2 of the License, or -# (at your option) any later version. +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. # -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for EAN5 barcodes. Designed for use with Inkscape. """ -from BaseEan import EanBarcode +from .BaseEan import EanBarcode -FAMS = [ '11000','10100','10010','10001','01100','00110','00011','01010','01001','00101' ] +FAMS = ['11000', '10100', '10010', '10001', '01100', + '00110', '00011', '01010', '01001', '00101'] START = '01011' class Ean5(EanBarcode): """Provide an Ean5 barcode generator""" - name = 'ean5' + name = 'ean5' length = 5 def _encode(self, number): - self.x += 110.0*self.scale # horiz offset so it does not overlap EAN13 - self.y -= (self.height + 5)*self.scale # move the text to the top - self.label = ' '.join(self.space(number)) - family = sum([int(n)*int(m) for n,m in zip(number, '39393')]) % 10 + self.text = ' '.join(self.space(number)) + family = sum([int(n)*int(m) for n, m in zip(number, '39393')]) % 10 return START + '01'.join(self.encode_interleaved(family, number, FAMS)) diff --git a/share/extensions/Barcode/Ean8.py b/share/extensions/Barcode/Ean8.py index 83e82814a..562952e42 100644 --- a/share/extensions/Barcode/Ean8.py +++ b/share/extensions/Barcode/Ean8.py @@ -13,22 +13,22 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for EAN8 barcodes. Designed for use with Inkscape. """ -from BaseEan import EanBarcode +from .BaseEan import EanBarcode class Ean8(EanBarcode): """Provide an EAN8 barcode generator""" - name = 'ean8' - lengths = [ 7 ] - checks = [ 8 ] + name = 'ean8' + checks = [8] + lengths = [7] def _encode(self, n): """Encode an ean8 barcode""" - self.label = self.space(n[:4], 3, n[4:]) - return self.enclose( self.encode_left(n[:4]), self.encode_right(n[4:]) ) + self.text = self.space(n[:4], 3, n[4:]) + return self.enclose(self.encode_left(n[:4]), self.encode_right(n[4:])) diff --git a/share/extensions/Barcode/Makefile.am b/share/extensions/Barcode/Makefile.am index 7a1e889c0..08e2f58b4 100644 --- a/share/extensions/Barcode/Makefile.am +++ b/share/extensions/Barcode/Makefile.am @@ -12,6 +12,7 @@ barcode_DATA = \ Ean13.py \ Ean8.py \ Ean5.py \ + Ean2.py \ __init__.py \ Rm4scc.py \ Upca.py \ diff --git a/share/extensions/Barcode/Rm4scc.py b/share/extensions/Barcode/Rm4scc.py index 0fb154280..7c36f26ee 100644 --- a/share/extensions/Barcode/Rm4scc.py +++ b/share/extensions/Barcode/Rm4scc.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # """ Python barcode renderer for RM4CC barcodes. Designed for use with Inkscape. @@ -66,10 +66,11 @@ check = ['ZUVWXY','501234','B6789A','HCDEFG','NIJKLM','TOPQRS'] (BAR_TRACK, BAR_DOWN, BAR_UP, BAR_FULL, BAR_NONE, WHITE_SPACE) = range(6) class Rm4scc(Barcode): + default_height = 18 + def encode(self, text): result = '' - self.height = 18 text = text.upper() text.replace('(', '') text.replace(')', '') @@ -80,10 +81,8 @@ class Rm4scc(Barcode): for char in text: if map.has_key(char): result = result + map[char] - i = i + 1 - self.inclabel = text return result; # given a string of data, return the check character @@ -117,7 +116,7 @@ class Rm4scc(Barcode): checkchar = check[total_upper][total_lower] return checkchar - def getStyle(self, index): + def get_style(self, index): """Royal Mail Barcodes use a completely different style""" result = { 'width' : 2, 'write' : True, 'top' : 0 } if index == BAR_TRACK: # Track Bar diff --git a/share/extensions/Barcode/Upca.py b/share/extensions/Barcode/Upca.py index d69ed11e6..32ecc0aa6 100644 --- a/share/extensions/Barcode/Upca.py +++ b/share/extensions/Barcode/Upca.py @@ -13,25 +13,23 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for UPCA barcodes. Designed for use with Inkscape. """ -from BaseEan import EanBarcode +from .BaseEan import EanBarcode class Upca(EanBarcode): """Provides a renderer for EAN12 aka UPC-A Barcodes""" - name = 'upca' - lengths = [ 11 ] - checks = [ 12 ] + name = 'upca' + font_size = 10 + lengths = [11] + checks = [12] def _encode(self, n): """Encode for a UPC-A Barcode""" - self.label = self.space(n[0:1], 3, n[1:6], 4, n[6:11], 3, n[11:]) + self.text = self.space(n[0:1], 3, n[1:6], 4, n[6:11], 3, n[11:]) return self.enclose(self.encode_left(n[0:6]), self.encode_right(n[6:12])) - def fontSize(self): - """We need a bigger barcode""" - return 10 diff --git a/share/extensions/Barcode/Upce.py b/share/extensions/Barcode/Upce.py index eee2a739c..193d605a0 100644 --- a/share/extensions/Barcode/Upce.py +++ b/share/extensions/Barcode/Upce.py @@ -13,79 +13,75 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA. # """ Python barcode renderer for UPCE barcodes. Designed for use with Inkscape. """ -from BaseEan import EanBarcode -import sys +from .BaseEan import EanBarcode # This is almost exactly the same as the standard FAMILIES # But flipped around and with the first 111000 instead of 000000. -FAMS = [ '111000', '110100', '110010', '110001', '101100', - '100110', '100011', '101010', '101001', '100101' ] +FAMS = ['111000', '110100', '110010', '110001', '101100', + '100110', '100011', '101010', '101001', '100101'] class Upce(EanBarcode): """Generate EAN6/UPC-E barcode generator""" - name = 'upce' - lengths = [ 6, 11 ] - checks = [ 7, 12 ] + name = 'upce' + font_size = 10 + lengths = [6, 11] + checks = [7, 12] + center_bar = '020' def _encode(self, n): """Generate a UPC-E Barcode""" - self.label = self.space(['0'], 2, n[:6], 2, n[-1]) + self.text = self.space(['0'], 2, n[:6], 2, n[-1]) code = self.encode_interleaved(n[-1], n[:6], FAMS) - # 202(guard) + code + 020(center) + 202(guard) - return self.enclose(code, center='020') + return self.enclose(code) - def appendChecksum(self, number): + def append_checksum(self, number): """Generate a UPCE Checksum""" if len(number) == 6: - number = self.ConvertEtoA(number) - result = self.getChecksum(number) - return self.ConvertAtoE(number) + result + number = self.convert_e2a(number) + result = self.get_checksum(number) + return self.convert_a2e(number) + result - def fontSize(self): - """We need a font size of 10""" - return 10 - - def ConvertAtoE(self, number): + def convert_a2e(self, number): """Converting UPC-A to UPC-E, may cause errors.""" # All UPC-E Numbers use number system 0 - if number[0] != '0' or len(number)!=11: + if number[0] != '0' or len(number) != 11: # If not then the code is invalid raise ValueError("Invalid UPC Number") # Most of the conversions deal # with the specific code parts - manufacturer = number[1:6] + maker = number[1:6] product = number[6:11] # There are 4 cases to convert: - if manufacturer[2:] == '000' or manufacturer[2:] == '100' or manufacturer[2:] == '200': + if maker[2:] == '000' or maker[2:] == '100' or maker[2:] == '200': # Maxium number product code digits can be encoded - if product[:2]=='00': - return manufacturer[:2] + product[2:] + manufacturer[2] - elif manufacturer[3:5] == '00': + if product[:2] == '00': + return maker[:2] + product[2:] + maker[2] + elif maker[3:5] == '00': # Now only 2 product code digits can be used - if product[:3]=='000': - return manufacturer[:3] + product[3:] + '3' - elif manufacturer[4] == '0': - # With even more manufacturer code we have less room for product code - if product[:4]=='0000': - return manufacturer[0:4] + product[4] + '4' - elif product[:4]=='0000' and int(product[4]) > 4: + if product[:3] == '000': + return maker[:3] + product[3:] + '3' + elif maker[4] == '0': + # With even more maker code we have less room for product code + if product[:4] == '0000': + return maker[0:4] + product[4] + '4' + elif product[:4] == '0000' and int(product[4]) > 4: # The last recorse is to try and squeeze it in the last 5 numbers # so long as the product is 00005-00009 so as not to conflict with # the 0-4 used above. - return manufacturer + product[4] + return maker + product[4] else: # Invalid UPC-A Numbe raise ValueError("Invalid UPC Number") - def ConvertEtoA(self, number): + def convert_e2a(self, number): """Convert UPC-E to UPC-A by padding with zeros""" # It's more likly to convert this without fault # But we still must be mindful of the 4 conversions diff --git a/share/extensions/Barcode/__init__.py b/share/extensions/Barcode/__init__.py index e4e328ae3..fcfcfdbad 100644 --- a/share/extensions/Barcode/__init__.py +++ b/share/extensions/Barcode/__init__.py @@ -13,7 +13,7 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # """ Renderer for barcodes, SVG extention for Inkscape. @@ -39,19 +39,23 @@ For supported barcodes see Barcode module directory. import sys -def getBarcode(code, **kwargs): +class NoBarcode(object): + """Simple class for no barcode""" + def generate(self): + return None + +def getBarcode(code, **kw): """Gets a barcode from a list of available barcode formats""" if not code: return sys.stderr.write("No barcode format given!\n") - code = str(code).replace('-', '').strip() + mod = 'Barcode' try: - barcode = getattr(__import__('Barcode.'+code, fromlist=['Barcode']), code) - return barcode(kwargs) + return getattr(__import__(mod+'.'+code, fromlist=[mod]), code)(kw) except ImportError: sys.stderr.write("Invalid type of barcode: %s\n" % code) except AttributeError: - raise - sys.stderr.write("Barcode module is missing the barcode class: %s\n" % code) + sys.stderr.write("Barcode module is missing barcode class: %s\n" % code) + return NoBarcode() |
