22
33from __future__ import unicode_literals
44
5- """barcode.ean
5+ """Module: barcode.ean
66
7+ :Provided barcodes: EAN-13, EAN-8, JAN
78"""
89
910from barcode .base import Barcode
11+ from barcode .charsets import ean as _ean
1012from barcode .errors import *
1113
1214# Python 3
1921# EAN13 Specs (all sizes in mm)
2022SIZES = dict (SC0 = 0.27 , SC1 = 0.297 , SC2 = 0.33 , SC3 = 0.363 , SC4 = 0.396 , SC5 = 0.445 ,
2123 SC6 = 0.495 , SC7 = 0.544 , SC8 = 0.61 , SC9 = 0.66 )
22- EDGE = '101'
23- MIDDLE = '01010'
24- CODES = {
25- 'A' : ('0001101' , '0011001' , '0010011' , '0111101' , '0100011' ,
26- '0110001' , '0101111' , '0111011' , '0110111' , '0001011' ),
27- 'B' : ('0100111' , '0110011' , '0011011' , '0100001' , '0011101' ,
28- '0111001' , '0000101' , '0010001' , '0001001' , '0010111' ),
29- 'C' : ('1110010' , '1100110' , '1101100' , '1000010' , '1011100' ,
30- '1001110' , '1010000' , '1000100' , '1001000' , '1110100' ),
31- }
32- LEFT_PATTERN = ('AAAAAA' , 'AABABB' , 'AABBAB' , 'AABBBA' , 'ABAABB' ,
33- 'ABBAAB' , 'ABBBAA' , 'ABABAB' , 'ABABBA' , 'ABBABA' )
3424
3525
3626class EuropeanArticleNumber13 (Barcode ):
@@ -50,7 +40,7 @@ class EuropeanArticleNumber13(Barcode):
5040 def __init__ (self , ean , writer = None ):
5141 ean = ean [:self .digits ]
5242 if not ean .isdigit ():
53- raise IllegalCharacterError ('Code can only contain numbers.' )
43+ raise IllegalCharacterError ('EAN code can only contain numbers.' )
5444 self .ean = ean
5545 self .ean = '{0}{1}' .format (ean , self .calculate_checksum ())
5646 self .writer = writer or Barcode .default_writer ()
@@ -80,14 +70,14 @@ def build(self):
8070 :returns: The pattern as string
8171 :rtype: String
8272 """
83- code = EDGE [:]
84- pattern = LEFT_PATTERN [int (self .ean [0 ])]
73+ code = _ean . EDGE [:]
74+ pattern = _ean . LEFT_PATTERN [int (self .ean [0 ])]
8575 for i , number in enumerate (self .ean [1 :7 ]):
86- code += CODES [pattern [i ]][int (number )]
87- code += MIDDLE
76+ code += _ean . CODES [pattern [i ]][int (number )]
77+ code += _ean . MIDDLE
8878 for number in self .ean [7 :]:
89- code += CODES ['C' ][int (number )]
90- code += EDGE
79+ code += _ean . CODES ['C' ][int (number )]
80+ code += _ean . EDGE
9181 return [code ]
9282
9383 def to_ascii (self ):
@@ -122,8 +112,8 @@ class JapanArticleNumber(EuropeanArticleNumber13):
122112
123113 def __init__ (self , jan , writer = None ):
124114 if int (jan [:3 ]) not in JapanArticleNumber .valid_country_codes :
125- raise WrongCountryCodeError ("Country code isn't between 450-460 or "
126- "490-500." )
115+ raise WrongCountryCodeError ("Country code isn't between 450-460 "
116+ "or 490-500." )
127117 EuropeanArticleNumber13 .__init__ (self , jan , writer )
128118
129119
@@ -161,13 +151,13 @@ def build(self):
161151 :returns: The pattern as string
162152 :rtype: String
163153 """
164- code = EDGE [:]
154+ code = _ean . EDGE [:]
165155 for number in self .ean [:4 ]:
166- code += CODES ['A' ][int (number )]
167- code += MIDDLE
156+ code += _ean . CODES ['A' ][int (number )]
157+ code += _ean . MIDDLE
168158 for number in self .ean [4 :]:
169- code += CODES ['C' ][int (number )]
170- code += EDGE
159+ code += _ean . CODES ['C' ][int (number )]
160+ code += _ean . EDGE
171161 return [code ]
172162
173163
0 commit comments