No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Copyright (C) 2012 Sebastian Pipping <sebastian@pipping.org>
  5. # Licensed under GPL v3 or later
  6. from __future__ import print_function
  7. import sys
  8. import simplejson as json
  9. import tempfile
  10. import os
  11. import subprocess
  12. import shutil
  13. sys.path.insert(0, '../client-barcode') # TODO
  14. from freitagslib.network import _request
  15. from freitagslib.item import Item
  16. _NAME_BLACKLIST = ('foo', )
  17. _BASENAME = 'preisliste'
  18. def write_utf8(f, text):
  19. f.write(unicode(text).encode('utf8'))
  20. def escape(text):
  21. return text.replace('&', '\\&')
  22. def german(phormat, value):
  23. return (phormat % value).replace('.', '{,}')
  24. if __name__ == '__main__':
  25. if len(sys.argv) != 2:
  26. print('USAGE:\n ./%s OUTPUT.pdf' % os.path.basename(sys.argv[0]))
  27. sys.exit(1)
  28. output_filename = sys.argv[1]
  29. content = _request('buyable/item/', None, method='GET')
  30. doc = json.loads(content)
  31. items = [Item(e) for e in doc]
  32. tempdir = tempfile.mkdtemp()
  33. preisliste_tex = os.path.join(tempdir, _BASENAME + '.tex')
  34. preisliste_pdf = os.path.join(tempdir, _BASENAME + '.pdf')
  35. f = open(preisliste_tex, 'w')
  36. f.write(r"""
  37. \documentclass[a4paper,10pt]{scrartcl}
  38. \usepackage[ngerman]{babel}
  39. \usepackage[utf8]{inputenc}
  40. \usepackage{fullpage}
  41. \usepackage{booktabs}
  42. \usepackage{eurosym}
  43. % Reduce vertical offset
  44. \addtolength{\textheight}{\voffset}
  45. \setlength{\voffset}{0cm}
  46. \setlength{\voffset}{-1cm}
  47. \addtolength{\textheight}{1cm}
  48. % No footskip
  49. \addtolength{\textheight}{\footskip}
  50. \setlength{\footskip}{0cm}
  51. % No footskip
  52. \addtolength{\textheight}{\footskip}
  53. \setlength{\footskip}{0cm}
  54. % No page number
  55. \pagestyle{empty}
  56. \begin{document}
  57. \begin{flushright}Stand \today\end{flushright}
  58. \vspace*{-4.6ex}
  59. %
  60. \begin{tabular}{l@{\quad}rr}
  61. \toprule
  62. Artikel & \multicolumn{1}{c}{Preis} & \multicolumn{1}{c}{Pfand} \\
  63. \midrule
  64. """)
  65. first = True
  66. for i in sorted(items, cmp=lambda x,y: cmp(x.name.lower(), y.name.lower())):
  67. if i.name in _NAME_BLACKLIST:
  68. continue
  69. latex_name = escape(i.name)
  70. latex_name = '\\textbf{%s}%s' % (latex_name[0], latex_name[1:])
  71. latex_price = german('\\textbf{%.2f} \\euro', i.price)
  72. latex_deposit = '~' if i.deposit == 0 else german('+ %.2f \\euro', i.deposit)
  73. write_utf8(f, '%s%s & %s & %s \\\\\n' % (
  74. '' if first else '\\midrule\n',
  75. latex_name, latex_price, latex_deposit))
  76. first = False
  77. f.write(r"""\bottomrule
  78. \end{tabular}
  79. \end{document}
  80. """)
  81. f.close()
  82. ret = subprocess.call(['pdflatex', _BASENAME + '.tex'], cwd=tempdir)
  83. if ret != 0:
  84. print("Call to LaTeX failed with code %d." % ret)
  85. sys.exit(ret)
  86. print('Moving output to "%s"...' % output_filename)
  87. shutil.move(preisliste_pdf, output_filename)
  88. print('Wiping directory "%s"...' % tempdir)
  89. shutil.rmtree(tempdir)