forked from kif-ev/schildergenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schilder.py
executable file
·540 lines (450 loc) · 18.6 KB
/
schilder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#!/usr/bin/env python3
# -*- encoding: utf8 -*-
from flask import Flask, flash, session, redirect, url_for, escape, request, Response, Markup, render_template, send_file
import sys
import os
import os.path
import glob
import jinja2
from jinja2 import Template
from werkzeug.utils import secure_filename
from collections import defaultdict
from docutils.core import publish_parts
import warnings
import shutil
import subprocess
from subprocess import CalledProcessError, STDOUT
import wand
from wand.image import Image
import json
import tempfile
import config
app = Flask(__name__)
app.config.update(UPLOAD_FOLDER=config.uploaddir,
PROPAGATE_EXCEPTIONS=True,
MAX_CONTENT_LENGTH=8388608)
app.jinja_env.lstrip_blocks = True
app.jinja_env.trim_blocks = True
app.secret_key = config.app_secret
def check_output(*popenargs, **kwargs):
# Copied from py2.7s subprocess module
r"""Run command with arguments and return its output as a byte string.
If the exit code was non-zero it raises a CalledProcessError. The
CalledProcessError object will have the return code in the returncode
attribute and output in the output attribute.
The arguments are the same as for the Popen constructor. Example:
>>> check_output(["ls", "-l", "/dev/null"])
'crw-rw-rw- 1 root root 1, 3 Oct 18 2007 /dev/null\n'
The stdout argument is not allowed as it is used internally.
To capture standard error in the result, use stderr=STDOUT.
>>> check_output(["/bin/sh", "-c",
... "ls -l non_existent_file ; exit 0"],
... stderr=STDOUT)
'ls: non_existent_file: No such file or directory\n'
"""
if 'stdout' in kwargs:
raise ValueError('stdout argument not allowed, it will be overridden.')
process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs)
output, unused_err = process.communicate()
retcode = process.poll()
if retcode != 0:
cmd = kwargs.get("args")
if cmd is None:
cmd = popenargs[0]
raise CalledProcessError(retcode, cmd, output=output)
#raise Exception(output)
return output
def allowed_file(filename):
return '.' in filename and filename.rsplit(
'.', 1)[1] in config.allowed_extensions
def load_data(filename):
with open(os.path.join(config.datadir, filename), 'r') as infile:
formdata = defaultdict(str, json.load(infile))
formdata['filename'] = filename
if len(formdata['markup']) < 1:
formdata['markup'] = 'latex'
return formdata
def save_data(formdata, outfilename):
with open(os.path.join(config.datadir, outfilename), 'w') as outfile:
json.dump(formdata, outfile)
def load_tex_template(name):
latex_jinja_env = jinja2.Environment(
#block_start_string = '\BLOCK{',
#block_end_string = '}',
variable_start_string='${',
variable_end_string='}',
#comment_start_string = '\#{',
#comment_end_string = '}',
#line_statement_prefix = '%%:',
#line_comment_prefix = '%%',
trim_blocks=True,
autoescape=False,
loader=jinja2.FileSystemLoader(config.textemplatedir))
template = latex_jinja_env.get_template(name)
return template
#template = template.render(data=data,load=load_template)
def run_pdflatex(context, outputfilename, overwrite=True):
if not 'textemplate' in context.keys(
) or context['textemplate'] == '': #context.has_key('textemplate'):
context['textemplate'] = "image-left_bothtext-right.tex"
template = load_tex_template(context['textemplate'])
if not overwrite and os.path.isfile(outputfilename) and os.path.getmtime(
os.path.join(
config.textemplatedir,
context['textemplate'])) < os.path.getmtime(outputfilename):
return
if context['markup'] == 'rst':
context['text'] = publish_parts(context['text'],
writer_name='latex')['body']
#context['headline'] = publish_parts(context['headline'], writer_name='latex')['body']
tmpdir = tempfile.mkdtemp(dir=config.tmpdir)
#wenn die vorlage ein bild enthält: kopiere bild nach temp
if 'img' in context.keys(
) and context['img'] and context['img'] != '__none':
try:
source = os.path.join(config.imagedir, context['img'])
filename = os.path.split(context['img'])[1]
context['img'] = filename
#create destinationfolder if not exist
dest = os.path.join(tmpdir, filename)
shutil.copy(source, dest)
except:
raise IOError("COULD NOT COPY IMAGE")
else:
# print( "MEH No image")
pass
#wenn vorlage ein logo enthält: kopiere logo nach temp
if 'logo' in context.keys(
) and context['logo'] and context['logo'] != '__none':
try:
source = os.path.join(config.logodir, context['logo'])
filename = os.path.split(context['logo'])[1]
context['logo'] = filename
#create destinationfolder if not exist
dest = os.path.join(tmpdir, filename)
shutil.copy(source, dest)
except:
raise IOError("COULD NOT COPY LOGO")
else:
# print( "MEH No logo")
pass
tmptexfile = os.path.join(tmpdir, 'output.tex')
tmppdffile = os.path.join(tmpdir, 'output.pdf')
with open(tmptexfile, 'w', encoding='utf-8') as texfile:
texfile.write(template.render(form=context))
cwd = os.getcwd()
os.chdir(tmpdir)
os.symlink(config.texsupportdir, os.path.join(tmpdir, 'support'))
try:
texlog = check_output(['pdflatex', '--halt-on-error', tmptexfile],
stderr=STDOUT)
except CalledProcessError as e:
if overwrite:
try:
flash(
Markup("<p>PDFLaTeX Output:</p><pre>%s</pre>" % e.output),
'log')
except:
print(e.output)
raise SyntaxWarning("PDFLaTeX bailed out")
finally:
os.chdir(cwd)
if overwrite:
try:
flash(Markup("<p>PDFLaTeX Output:</p><pre>%s</pre>" % texlog),
'log')
except:
print(texlog)
shutil.copy(tmppdffile, outputfilename)
shutil.rmtree(tmpdir)
def save_and_convert_image_upload(inputname, folder):
imgfile = request.files[inputname]
if imgfile:
if not allowed_file(imgfile.filename):
raise UserWarning(
"Uploaded image is not in the list of allowed file types.")
filename = os.path.join(config.uploaddir,
secure_filename(imgfile.filename))
imgfile.save(filename)
img = Image(filename=str(filename))
imgname = os.path.splitext(secure_filename(
imgfile.filename))[0].replace('.', '_') + '.png'
savedfilename = os.path.join(folder, imgname)
img.save(filename=str(savedfilename))
os.remove(filename)
return imgname
return None
def make_thumb(filename, maxgeometry):
thumbpath = filename + '.' + str(maxgeometry)
if not os.path.exists(thumbpath) or os.path.getmtime(
filename) > os.path.getmtime(thumbpath):
try:
img = Image(filename=str(filename))
except Exception as e:
print(e)
raise (e)
img.format = 'png'
img.resize(maxgeometry, maxgeometry)
img.compression_quality = 90
img.save(filename=str(thumbpath))
return thumbpath
@app.route('/')
def index(**kwargs):
data = defaultdict(str)
data.update(**kwargs)
filelist = glob.glob(config.datadir + '/*.schild')
data['files'] = [os.path.basename(f) for f in sorted(filelist)]
return render_template('index.html', data=data)
@app.route('/edit')
def edit(**kwargs):
data = defaultdict(str)
data.update(**kwargs)
#imagelist = sorted(glob.glob(config.imagedir + '/*.png')) #TODO
#data['images'] = [os.path.basename(f) for f in imagelist] #TODO
data['images'] = generateImagelist()
data['logos'] = generateImagelist(config.logodir)
data['standard_logo'] = config.standartLogo
data['standard_footer'] = config.standartFooter
templatelist = glob.glob(config.textemplatedir + '/*.tex')
data['templates'] = [os.path.basename(f) for f in sorted(templatelist)]
data['imageextensions'] = config.allowed_extensions
return render_template('edit.html', data=data)
@app.route('/edit/<filename>')
def edit_one(filename):
return edit(form=load_data(filename))
@app.route('/create', methods=['POST'])
def create():
if request.method == 'POST':
formdata = defaultdict(str, request.form.to_dict(flat=True))
try:
#Bild upload
imagedir = config.imagedir
category = formdata['img--cat']
if not category:
category = 'none'
#benuterdefinierte /neue kategorie
if (category == "__user"):
category = formdata['usercat']
if not category:
category = 'none'
#kategorie/ordner festlegen
if (category != 'none'):
category = category.replace(' ', '_').replace('/', '_')
imagedir = os.path.join(imagedir, category)
if not os.path.exists(imagedir):
os.makedirs(imagedir)
#prüfe ob bild hochgeladen wurde und speichere es
imgpath = None
if formdata['img'] == '__upload':
#if formdata['imgupload']:
imgpath = save_and_convert_image_upload('imgupload', imagedir)
if imgpath is not None:
if (category != 'none'):
formdata['img'] = os.path.join(category, imgpath)
else:
formdata['img'] = imgpath
#logo upload
logopath = None
if formdata['logo'] == '__upload':
logopath = save_and_convert_image_upload(
'logoupload', config.logodir)
if logopath is not None:
formdata['logo'] = logopath
outfilename = secure_filename(formdata['headline'][:16]) + str(
hash(formdata['headline'] + formdata['text'] +
os.path.splitext(formdata['textemplate'])[0] +
os.path.splitext(formdata['img'])[0] +
formdata['footer'])) + '.schild'
if formdata['reusefilename']:
outfilename = secure_filename(formdata['filename'])
outpdfname = outfilename + '.pdf'
formdata['filename'] = outfilename
formdata['pdfname'] = outpdfname
save_data(formdata, outfilename)
run_pdflatex(formdata, os.path.join(config.pdfdir, outpdfname))
try:
flash(
Markup(
u"""PDF created and data saved. You might create another one. Here's a preview. Click to print.<br/>
<a href="%s"><img src="%s"/></a>""" %
(url_for('schild', filename=outfilename),
url_for('pdfthumbnail',
pdfname=outpdfname,
maxgeometry=200))))
except:
print("%s created" % outpdfname)
except Exception as e:
try:
flash(u"Could not create pdf or save data: %s" % str(e),
'error')
except:
print("Could not create pdf or save data: %s" % str(e))
data = {'form': formdata}
# imagelist = glob.glob(config.imagedir + '/*.png') #TODO unterordner hinzufügen
# data['images'] = [os.path.basename(f) for f in imagelist] #TODO nach unterordnern kategorieisieren
data['images'] = generateImagelist()
templatelist = glob.glob(config.textemplatedir + '/*.tex')
data['templates'] = [os.path.basename(f) for f in sorted(templatelist)]
try:
return redirect(url_for('edit_one', filename=outfilename))
except:
pass
try:
flash("No POST data. You've been redirected to the edit page.",
'warning')
return redirect(url_for('edit'))
except:
pass
@app.route('/schild/<filename>')
def schild(filename):
return render_template('schild.html',
data={
'filename':
filename,
'printer':
[f for f in sorted(config.printers.keys())]
})
@app.route('/printout', methods=['POST'])
def printout():
filename = os.path.join(config.pdfdir,
secure_filename(request.form['filename']))
printer = config.printers[request.form['printer']]
copies = int(request.form['copies']) or 0
if copies > 0 and copies <= 6:
try:
lprout = check_output([
'lpr', '-H',
str(config.printserver), '-P',
str(printer), '-#',
str(copies)
] + config.lproptions + [filename],
stderr=STDOUT)
flash(u'Schild wurde zum Drucker geschickt!')
except CalledProcessError as e:
flash(Markup("<p>Could not print:</p><pre>%s</pre>" % e.output),
'error')
else:
flash(u'Ungültige Anzahl Kopien!')
return redirect(url_for('index'))
def delete_file(filename):
try:
os.unlink(os.path.join(config.datadir, filename))
for f in glob.glob(os.path.join(config.pdfdir, filename + '.pdf*')):
os.unlink(f)
flash(u"Schild %s wurde gelöscht" % filename)
return redirect(url_for('index'))
except:
flash(u"Schild %s konnte nicht gelöscht werden." % filename, 'error')
return redirect(url_for('schild', filename=filename))
@app.route('/delete', methods=['POST'])
def delete():
return delete_file(secure_filename(request.form['filename']))
@app.route('/deletelist', methods=['POST'])
def deletelist():
for filename in request.form.getlist('filenames'):
delete_file(secure_filename(filename))
return redirect(url_for('index'))
@app.route('/image/<imgname>')
def image(imgname):
imgpath = os.path.join(config.imagedir, secure_filename(imgname))
if os.path.exists(imgpath):
with open(imgpath, 'r') as imgfile:
return Response(imgfile.read(), mimetype="image/png")
else:
return "Meh" # redirect(url_for('index'))
@app.route('/thumbnail/<category>/<imgname>/<int:maxgeometry>')
def thumbnail(imgname, category, maxgeometry):
if category == 'none':
imgpath = os.path.join(config.imagedir, secure_filename(imgname))
else:
imgpath = os.path.join(config.imagedir, category,
secure_filename(imgname))
thumbpath = make_thumb(imgpath, maxgeometry)
with open(thumbpath, 'rb') as imgfile:
return Response(imgfile.read(), mimetype="image/png")
@app.route('/logothumbnail/<category>/<imgname>/<int:maxgeometry>')
def logothumbnail(imgname, category, maxgeometry):
if category == 'none':
imgpath = os.path.join(config.logodir, secure_filename(imgname))
else:
imgpath = os.path.join(config.logodir,
category + '/' + secure_filename(imgname))
thumbpath = make_thumb(imgpath, maxgeometry)
with open(thumbpath, 'rb') as imgfile:
return Response(imgfile.read(), mimetype="image/png")
@app.route('/pdfthumb/<pdfname>/<int:maxgeometry>')
def pdfthumbnail(pdfname, maxgeometry):
pdfpath = os.path.join(config.pdfdir, secure_filename(pdfname))
thumbpath = make_thumb(pdfpath, maxgeometry)
with open(thumbpath, 'rb') as imgfile:
return Response(imgfile.read(), mimetype="image/png")
@app.route('/tplthumb/<tplname>/<int:maxgeometry>')
def tplthumbnail(tplname, maxgeometry):
pdfpath = os.path.join(config.cachedir, secure_filename(tplname) + '.pdf')
try:
run_pdflatex(
{
'textemplate': secure_filename(tplname),
'img': 'pictograms-nps-misc-camera.png',
'headline': u'Überschrift',
'text':
u'Dies ist der Text, der in der UI als Text bezeichnet ist.',
'markup': 'latex',
'footer': u'Das hier ist der Footer',
'logo': config.standartLogo,
},
pdfpath,
overwrite=False)
except Exception as e:
print(str(e))
return str(e)
else:
thumbpath = make_thumb(pdfpath, maxgeometry)
return send_file(thumbpath, mimetype="image/png")
# with open(thumbpath, 'rb') as imgfile:
# return Response(imgfile.read(), mimetype="image/png")
@app.route('/pdfdownload/<pdfname>')
def pdfdownload(pdfname):
pdfpath = os.path.join(config.pdfdir, secure_filename(pdfname))
with open(pdfpath, 'rb') as pdffile:
return Response(pdffile.read(), mimetype="application/pdf")
def generateImagelist(path=None):
#imagelist = sorted(glob.glob(config.imagedir + '/*.png'))
#standart bilder pfad
if (path == None):
path = config.imagedir + '/'
imagelist = {}
imagelist['none'] = []
files = os.walk(path)
for root, dirs, files in os.walk(path):
for f in files:
if f.endswith('.png'):
filename = os.path.basename(f)
category = root.replace(path, '')
if (category == ""):
imagelist['none'].append(filename)
else:
if category not in imagelist.keys():
imagelist[category] = []
imagelist[category].append(filename)
return imagelist
def recreate_cache():
for filename in (glob.glob(os.path.join(config.pdfdir, '*.pdf*')) +
glob.glob(os.path.join(config.cachedir, '*.pdf*')) +
glob.glob(os.path.join(config.imagedir, '*.png.*'))):
try:
os.unlink(filename)
print("Deleted %s" % filename)
except Exception as e:
print("Could not delete %s: %s" % (filename, str(e)))
for filename in glob.glob(os.path.join(config.datadir, '*.schild')):
data = load_data(filename)
pdfname = os.path.join(config.pdfdir, data['pdfname'])
print("Recreating %s" % pdfname)
run_pdflatex(data, pdfname)
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == '--recreate-cache':
recreate_cache()
else:
app.debug = True
app.run(host=config.listen, port=config.port)