| #!/usr/bin/env python
|
| # vim: noet nowrap number ts=4
|
|
|
| import barcode
|
| #import bytes_as_braille as bab
|
|
|
| from bs4 import BeautifulSoup
|
|
|
| def enc(string):
|
| return string.encode('utf-8')
|
|
|
|
|
| class MakeAnInt(Exception): pass
|
|
|
| barcode.Code128.default_writer_options = {
|
| "module_width": 0.2,
|
| "module_height": 10.0,
|
| "quiet_zone": 6.5,
|
| "font_size": 10,
|
| "text_distance": 3.0,
|
| "background": None,
|
| "foreground": "black",
|
| "write_text": True,
|
| "text": "",
|
| }
|
|
|
| def mkBar( byte_content, ASCII = True, fname = '/tmp/barcode.svg' ):
|
| try:
|
| if not ASCII:
|
| raise MakeAnInt
|
| else:
|
| try:
|
| int(byte_content)
|
| raise MakeAnInt
|
| except ValueError:
|
| string_content = byte_content.decode('ascii')
|
| except (MakeAnInt, UnicodeDecodeError):
|
| string_content = str(int.from_bytes(byte_content,'big'))
|
| #print(f"Encoded: {string_content}")
|
| b = barcode.Code128( string_content )
|
| b.write(open(fname,'wb'), text = byte_content.decode('utf-8'))
|
| return fname
|
|
|
|
|
| if __name__ == '__main__':
|
| #while True:
|
| # #mkBar( bab.input() )
|
| # mkBar( input('> ').encode('utf-8') )
|
|
|
| ## all good
|
| #mkBar(b'gdgdggdgd')
|
| #mkBar('ddddddé'.encode('utf-8'))
|
| ## why is "converting to int" not being displayed?!?!" that except clause is not being used!
|
| #mkBar(b'12345689')
|
|
|
| from sys import argv
|
| year = int(argv[1])
|
|
|
| svg_templates = ("00_admin.svg",)
|
|
|
| for t in svg_templates:
|
| svg = BeautifulSoup( open(t,'r'), features = 'xml' )
|
| layer = svg.find("svg:g", {'id':'layer1'})
|
| codes = layer.find_all("svg:g")
|
| for code in codes:
|
| if code.get('id')[:14] == 'barcode_group':
|
| transform = code.get('transform') # this allows us to place the new barcode at the same position as the template
|
| text = code.get('transform')
|
| newcontent = enc(str(year)+code.find('text').text)
|
| print(newcontent, transform)
|
| # content of the new barcode, as bs4 object
|
| bar = BeautifulSoup( open(mkBar(newcontent),'rb').read(), features='xml' ).find('g',{'id': 'barcode_group'})
|
| # we could basically write this for each barcode to a blank SVG, but all other features of the SVG
|
| # (typically groups that have id != "barcode_group"*) will be lost
|