From The Compiler, 8 Months ago, written in Python.
Embed
  1. import jinja2
  2. import csv
  3.  
  4. template = r"""
  5. \documentclass[paper=a4]{scrlttr2}
  6. \usepackage[utf8]{inputenc}
  7.  
  8. \makeatletter
  9. \@setplength{toaddrvpos}{3.5cm}
  10. \@setplength{toaddrhpos}{-2cm}
  11. \makeatother
  12.  
  13. \setkomavar{fromname}{Florian Bruhin}
  14. \setkomavar{fromaddress}{Ruchwiesenstrasse 36 \\ 8404 Winterthur \\ Switzerland}
  15. \setkomavar{signature}{Florian Bruhin / The Compiler}
  16. \setkomavar{date}{6th July 2016}
  17. \setkomavar{fromemail}{me@the-compiler.org}
  18.  
  19. \setkomavar{subject}{
  20. {{what}} crowdfunding stickers}
  21.  
  22. \setlength{\parindent}{0em}
  23. \setlength{\parskip}{1em}
  24.  
  25. \begin{document}
  26. {% for addr in addrs %}
  27. \begin{letter}{ {{addr}} }
  28. \opening {Hi!}
  29.  
  30. \vspace{1em}
  31.  
  32. As promised some months ago, here are the {{what}} stickers you selected with
  33. your pledge on Indiegogo. According to my records, your pledge does not include
  34. a t-shirt.
  35.  
  36. If you think this is wrong, please contact me ASAP on me@the-compiler.org. If
  37. you need more stickers (like for a local hackerspace), don't hesitate to
  38. contact me either!
  39.  
  40. \vspace{1em}
  41.  
  42. Thanks again for your support!
  43.  
  44. \closing{Greetings from Switzerland,}
  45. \end{letter}
  46. {% endfor %}
  47. \end{document}
  48. """
  49.  
  50. pytest_addr = []
  51. qute_addr = []
  52.  
  53.  
  54. def process(line, what):
  55.     if line['Name'] != line['Shipping Name']:
  56.         print('{}: name {:35} != shipping {:35}'.format(
  57.             what, line['Name'], line['Shipping Name']))
  58.  
  59.     line['zip_and_city'] = '{} {}'.format(line['Shipping Zip/Postal Code'],
  60.                                           line['Shipping City'])
  61.  
  62.     line['state_and_zip'] = '{} {}'.format(line['Shipping State/Province'],
  63.                                            line['Shipping Zip/Postal Code'])
  64.                                          
  65.  
  66.     country_mapping = {
  67.         'UK': 'United Kingdom',
  68.         'US': 'United States',
  69.         'USA': 'United States',
  70.     }
  71.  
  72.     try:
  73.         line['Shipping Country'] = country_mapping[line['Shipping Country']]
  74.     except KeyError:
  75.         pass
  76.  
  77.     if line['Shipping Country'] == 'United States':
  78.         addr_parts = [
  79.             'Shipping Name',
  80.             'Shipping Address',
  81.             'Shipping Address 2',
  82.             'Shipping City',
  83.             'state_and_zip',
  84.             'Shipping Country',
  85.         ]
  86.     elif line['Shipping Country'] == 'United Kingdom':
  87.         addr_parts = [
  88.             'Shipping Name',
  89.             'Shipping Address',
  90.             'Shipping Address 2',
  91.             'Shipping City',
  92.             'Shipping State/Province',
  93.             'Shipping Zip/Postal Code',
  94.             'Shipping Country',
  95.         ]
  96.     else:
  97.         addr_parts = [
  98.             'Shipping Name',
  99.             'Shipping Address',
  100.             'Shipping Address 2',
  101.             'zip_and_city',
  102.             'Shipping State/Province',
  103.             'Shipping Country',
  104.         ]
  105.  
  106.     addr = r' \\ '.join(line[e].replace('#', '\\#') for e in addr_parts if line[e])
  107.     if what == 'pytest':
  108.         pytest_addr.append(addr)
  109.     elif what == 'qutebrowser':
  110.         qute_addr.append(addr)
  111.     else:
  112.         assert False, what
  113.  
  114.  
  115. with open('pytest.csv') as ptf, open('qutebrowser.csv') as qf:
  116.     ptreader = csv.DictReader(ptf)
  117.     qreader = csv.DictReader(qf)
  118.  
  119.     for line in ptreader:
  120.         if line['shirt color'] not in ['no', 'N/A']:
  121.             continue
  122.         assert line['shirt size'] == line['shirt color']
  123.         process(line, 'pytest')
  124.  
  125.     for line in qreader:
  126.         if line['T-Shirt'] not in ['no', 'N/A']:
  127.             continue
  128.         if line['Stickers']  == 'no':
  129.             continue
  130.         process(line, 'qutebrowser')
  131.  
  132. print()
  133.  
  134. pytest_out = jinja2.Template(template).render(addrs=pytest_addr, what='pytest')
  135. qute_out = jinja2.Template(template).render(addrs=qute_addr,
  136.                                             what='qutebrowser')
  137.  
  138. with open('qute.tex', 'w', encoding='utf-8') as f:
  139.     f.write(qute_out)
  140.  
  141.  
  142. with open('pytest.tex', 'w', encoding='utf-8') as f:
  143.     f.write(pytest_out)
  144.