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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. from freitagslib.settings import settings, Settings
  17. _NAME_BLACKLIST = ('foo', )
  18. _BASENAME = 'preisliste'
  19. def write_utf8(f, text):
  20. f.write(unicode(text).encode('utf8'))
  21. def escape(text):
  22. return text.replace('&', '\\&')
  23. def german(phormat, value):
  24. return (phormat % value).replace('.', '{,}')
  25. if __name__ == '__main__':
  26. if len(sys.argv) != 2:
  27. print('USAGE:\n ./%s OUTPUT.pdf' % os.path.basename(sys.argv[0]))
  28. sys.exit(1)
  29. output_filename = sys.argv[1]
  30. if not settings.load('../client-barcode/freitagskasse.conf'):
  31. sys.exit(1)
  32. content = _request('buyable/item/', None, method='GET')
  33. doc = json.loads(content)
  34. items = [Item(e) for e in doc]
  35. tempdir = tempfile.mkdtemp()
  36. preisliste_tex = os.path.join(tempdir, _BASENAME + '.tex')
  37. preisliste_pdf = os.path.join(tempdir, _BASENAME + '.pdf')
  38. f = open(preisliste_tex, 'w')
  39. f.write(r"""
  40. \documentclass[a4paper,12pt]{scrartcl}
  41. \usepackage[ngerman]{babel}
  42. \usepackage[utf8]{inputenc}
  43. \usepackage{fullpage}
  44. \usepackage{eurosym}
  45. \usepackage[table]{xcolor}
  46. \usepackage[hmargin=1cm,vmargin=1cm]{geometry}
  47. % No footskip
  48. \addtolength{\textheight}{\footskip}
  49. \setlength{\footskip}{0cm}
  50. % No footskip
  51. \addtolength{\textheight}{\footskip}
  52. \setlength{\footskip}{0cm}
  53. % No page number
  54. \pagestyle{empty}
  55. \definecolor{mygray}{RGB}{224,224,224}
  56. \renewcommand{\arraystretch}{1.2}
  57. \begin{document}\sffamily
  58. \noindent
  59. Stand \today\\[\baselineskip]
  60. \rowcolors{2}{white}{mygray}
  61. \begin{tabular}{l@{\quad}rr}
  62. \textbf{Artikel} & \multicolumn{1}{c}{\textbf{Preis}} & \multicolumn{1}{c}{\textbf{Pfand}} \\
  63. """)
  64. first = True
  65. for i in sorted(items, cmp=lambda x,y: cmp(x.name.lower(), y.name.lower())):
  66. if i.name in _NAME_BLACKLIST:
  67. continue
  68. latex_name = escape(i.name)
  69. latex_name = '\\textbf{%s}%s' % (latex_name[0], latex_name[1:])
  70. latex_price = german('\\textbf{%.2f} \\euro', i.price)
  71. latex_deposit = '~' if i.deposit == 0 else german('+ %.2f \\euro', i.deposit)
  72. write_utf8(f, '%s & %s & %s \\\\\n' % (
  73. latex_name, latex_price, latex_deposit))
  74. first = False
  75. f.write(r"""
  76. \end{tabular}
  77. \end{document}
  78. """)
  79. f.close()
  80. ret = subprocess.call(['pdflatex', _BASENAME + '.tex'], cwd=tempdir)
  81. if ret != 0:
  82. print("Call to LaTeX failed with code %d." % ret)
  83. sys.exit(ret)
  84. print('Moving output to "%s"...' % output_filename)
  85. shutil.move(preisliste_pdf, output_filename)
  86. print('Wiping directory "%s"...' % tempdir)
  87. shutil.rmtree(tempdir)