Advertisement
Hellerick_Ferlibay

generate_plate(plate_code)

Dec 2nd, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. def generate_plate(plate_code):
  2.    
  3.     print('Generating the plate', plate_code)
  4.     inner_id, zone = unpack(plate_code)
  5.     print('Inner ID:', inner_id)
  6.     print('Zone:', zone)
  7.     region = [regions[r]['code'] for r in regions if zone in regions[r]['zones']][0]
  8.     region_name = [r for r in regions if region==regions[r]['code']][0]
  9.     print('Region code:', region, '('+region_name+')')
  10.     colorscheme = get_colorscheme(region)
  11.     print('Color scheme: {}/{} {}/{}'.format(*colorscheme)) # RPFONT, RPBG, IPFONT, IPBG
  12.  
  13.     ElementTree.register_namespace('dc','http://purl.org/dc/elements/1.1/')
  14.     ElementTree.register_namespace('cc','http://creativecommons.org/ns#')
  15.     ElementTree.register_namespace('rdf','http://www.w3.org/1999/02/22-rdf-syntax-ns#')
  16.     ElementTree.register_namespace('svg','http://www.w3.org/2000/svg')
  17.     ElementTree.register_namespace('','http://www.w3.org/2000/svg')
  18.     ElementTree.register_namespace('xlink','http://www.w3.org/1999/xlink')
  19.     ElementTree.register_namespace('sodipodi','http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd')
  20.     ElementTree.register_namespace('inkscape','http://www.inkscape.org/namespaces/inkscape')
  21.     doc = ElementTree.parse(template_path)
  22.     RPFONT = doc.find('.//*[@id="RPFONT"]')
  23.     RPBG = doc.find('.//*[@id="RPBG"]')
  24.     IPFONT = doc.find('.//*[@id="IPFONT"]')
  25.     IPBG = doc.find('.//*[@id="IPBG"]')
  26.  
  27.     RPFONT.attrib['style']=re.sub(r'fill:#[0-9a-f]{6}', r'fill:'+colors[colorscheme[0]], RPFONT.attrib['style'])
  28.     RPBG.attrib['style']=re.sub(r'fill:#[0-9a-f]{6}', r'fill:'+colors[colorscheme[1]], RPBG.attrib['style'])
  29.     IPFONT.attrib['style']=re.sub(r'fill:#[0-9a-f]{6}', r'fill:'+colors[colorscheme[2]], IPFONT.attrib['style'])
  30.     IPBG.attrib['style']=re.sub(r'fill:#[0-9a-f]{6}', r'fill:'+colors[colorscheme[3]], IPBG.attrib['style'])
  31.  
  32.     RP1 = doc.find('.//*[@id="RP1"]')
  33.     RP2 = doc.find('.//*[@id="RP2"]')
  34.  
  35.     for n,e in enumerate([RP1, RP2]):
  36.         e.attrib['{http://www.w3.org/1999/xlink}href'] = '#smallDigit'+'{:02d}'.format(region)[n]
  37.  
  38.     IP1 = doc.find('.//*[@id="IP1"]')
  39.     IP2 = doc.find('.//*[@id="IP2"]')
  40.     IP3 = doc.find('.//*[@id="IP3"]')
  41.     IP4 = doc.find('.//*[@id="IP4"]')
  42.     IP5 = doc.find('.//*[@id="IP5"]')
  43.     IP6 = doc.find('.//*[@id="IP6"]')
  44.     IP7 = doc.find('.//*[@id="IP7"]')
  45.     IP8 = doc.find('.//*[@id="IP8"]')
  46.  
  47.     for n,e in enumerate([IP1, IP2]):
  48.         e.attrib['{http://www.w3.org/1999/xlink}href'] = '#letter'+plate_code[n]
  49.     for n,e in enumerate([IP3, IP4, IP5, IP6]):
  50.         e.attrib['{http://www.w3.org/1999/xlink}href'] = '#digit'+plate_code[n+2]
  51.     for n,e in enumerate([IP7, IP8]):
  52.         e.attrib['{http://www.w3.org/1999/xlink}href'] = '#letter'+plate_code[n+6]
  53.  
  54.     folder = os.path.dirname(template_path)
  55.     svg_path = os.path.join(folder, 'Plate ({:02d}) {}.svg'.format(region, plate_code))
  56.     png_path = os.path.join(folder, 'Plate ({:02d}) {}.png'.format(region, plate_code))
  57.     print('Saving as:', svg_path)
  58.     doc.write(svg_path, default_namespace=None)
  59.     print('Converting as:', png_path)
  60.     os.system('inkscape "{}" -e "{}"'.format(svg_path, png_path))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement