Browse Source

Add preislister quickhack

Sebastian Pipping 10 years ago
parent
commit
e20a8cb48b
1 changed files with 118 additions and 0 deletions
  1. 118
    0
      preislister/main.py

+ 118
- 0
preislister/main.py View File

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

Loading…
Cancel
Save