schilder.py 13.4 KB
Newer Older
Robin Sonnabend's avatar
Robin Sonnabend committed
1
#!/usr/bin/env python3
Dave Kliczbor's avatar
Dave Kliczbor committed
2
3
4
# -*- encoding: utf8 -*-

from flask import Flask, flash, session, redirect, url_for, escape, request, Response, Markup
5
6
7
8
import sys
import os
import os.path
import glob
9
10
11
import werkzeug
# genshi expects old location, fix
werkzeug.cached_property = werkzeug.utils.cached_property
Dave Kliczbor's avatar
Dave Kliczbor committed
12
13
14
15
16
from genshi.template import TemplateLoader
from genshi.template.text import NewTextTemplate
from flaskext.genshi import Genshi, render_response
from werkzeug.utils import secure_filename
from collections import defaultdict
17
from docutils.core import publish_parts
Dave Kliczbor's avatar
Dave Kliczbor committed
18
19
20
21
22
23
24
25
26
27
import warnings
import shutil
import subprocess
from subprocess import CalledProcessError, STDOUT
import PythonMagick
import json
import tempfile
import config

app = Flask(__name__)
Dave Kliczbor's avatar
Dave Kliczbor committed
28
29
30
app.config.update(
    UPLOAD_FOLDER = config.uploaddir,
    PROPAGATE_EXCEPTIONS = True,
Robin Sonnabend's avatar
Robin Sonnabend committed
31
    MAX_CONTENT_LENGTH = 8388608
Dave Kliczbor's avatar
Dave Kliczbor committed
32
)
Dave Kliczbor's avatar
Dave Kliczbor committed
33
34
35
36
app.secret_key = config.app_secret
genshi = Genshi(app)
genshi.extensions['html'] = 'html5'

37

Dave Kliczbor's avatar
Dave Kliczbor committed
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
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):
73
74
    return '.' in filename and filename.rsplit('.', 1)[1] in config.allowed_extensions

75
def load_data(filename):
76
77
    with open(os.path.join(config.datadir, filename), 'r') as infile:
        formdata = defaultdict(str, json.load(infile))
78
        formdata['filename'] = filename
79
80
        if len(formdata['markup']) < 1:
            formdata['markup'] = 'latex'
81
82
83
84
85
86
        return formdata
        
def save_data(formdata, outfilename):
    with open(os.path.join(config.datadir, outfilename), 'w') as outfile:
        json.dump(formdata, outfile)
    
87
def run_pdflatex(context, outputfilename, overwrite=True):
Robin Sonnabend's avatar
Robin Sonnabend committed
88
    if 'textemplate' not in context:
89
90
91
92
93
94
95
96
        context['textemplate'] = "text-image-quer.tex"
    genshitex = TemplateLoader([config.textemplatedir])
    template = genshitex.load(
        context['textemplate'], cls=NewTextTemplate, encoding='utf8')
    if not overwrite and os.path.isfile(outputfilename) and os.path.getmtime(template.filepath) < os.path.getmtime(outputfilename):
        return
    if context['markup'] == 'rst':
        context['text'] = publish_parts(context['text'], writer_name='latex')['body']
97
        #context['headline'] = publish_parts(context['headline'], writer_name='latex')['body']
98
    tmpdir = tempfile.mkdtemp(dir=config.tmpdir)
Robin Sonnabend's avatar
Robin Sonnabend committed
99
    if 'img' in context and context['img'] and context['img'] != '__none':
100
        try:
Dave Kliczbor's avatar
Dave Kliczbor committed
101
102
            shutil.copy(os.path.join(config.imagedir, context['img']), 
                        os.path.join(tmpdir, context['img']))
103
104
105
106
107
108
109
110
111
112
113
114
        except:
            raise IOError("COULD NOT COPY")
    else:
        # print "MEH No image"
        pass
    tmptexfile = os.path.join(tmpdir, 'output.tex')
    tmppdffile = os.path.join(tmpdir, 'output.pdf')
    with open(tmptexfile, 'w') as texfile:
        texfile.write(template.generate(form=context).render(encoding='utf8'))
    cwd = os.getcwd()
    os.chdir(tmpdir)
    os.symlink(config.texsupportdir, os.path.join(tmpdir, 'support'))
Dave Kliczbor's avatar
Dave Kliczbor committed
115
    try:
116
117
118
119
        texlog = check_output(
            ['pdflatex', '--halt-on-error', tmptexfile], stderr=STDOUT)
    except CalledProcessError as e:
        if overwrite:
120
121
122
            try:
                flash(Markup("<p>PDFLaTeX Output:</p><pre>%s</pre>" % e.output), 'log')
            except:
Robin Sonnabend's avatar
Robin Sonnabend committed
123
                print((e.output))
124
125
126
        raise SyntaxWarning("PDFLaTeX bailed out")
    finally:
        os.chdir(cwd)
127
    if overwrite:
128
129
130
131
        try:
            flash(Markup("<p>PDFLaTeX Output:</p><pre>%s</pre>" % texlog), 'log')
        except:
            print(texlog)
132
133
134
    shutil.copy(tmppdffile, outputfilename)
    shutil.rmtree(tmpdir)

Dave Kliczbor's avatar
Dave Kliczbor committed
135
def save_and_convert_image_upload(inputname):
136
137
138
    imgfile = request.files[inputname]
    if imgfile:
        if not allowed_file(imgfile.filename):
139
140
141
            raise UserWarning(
                "Uploaded image is not in the list of allowed file types.")
        filename = os.path.join(
142
143
            config.uploaddir, secure_filename(imgfile.filename))
        imgfile.save(filename)
144
        img = PythonMagick.Image(filename)
145
        imgname = os.path.splitext(secure_filename(imgfile.filename))[
146
147
148
149
150
151
152
            0].replace('.', '_') + '.png'
        savedfilename = os.path.join(config.imagedir, imgname)
        img.write(savedfilename)
        os.remove(filename)
        return imgname
    return None

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
def make_thumb(filename, maxgeometry):
    thumbpath = filename + '.' + str(maxgeometry)
    if not os.path.exists(thumbpath) or os.path.getmtime(filename) > os.path.getmtime(thumbpath):
        img = PythonMagick.Image(str(filename))
        img.transform("%sx%s" % (maxgeometry, maxgeometry))
        img.quality(90)
        img.write(str("png:%s" % thumbpath))
    return thumbpath



@app.route('/')
def index(**kwargs):
    data = defaultdict(str)
    data.update(**kwargs)
    filelist = glob.glob(config.datadir + '/*.schild')
Robin Sonnabend's avatar
Robin Sonnabend committed
169
    data['files'] = [str(os.path.basename(f)) for f in sorted(filelist)]
170
171
172
173
174
175
176
    return render_response('index.html', data)


@app.route('/edit')
def edit(**kwargs):
    data = defaultdict(str)
    data.update(**kwargs)
Dave Kliczbor's avatar
Dave Kliczbor committed
177
    imagelist = sorted(glob.glob(config.imagedir + '/*.png'))
178
179
    data['images'] = [os.path.basename(f) for f in imagelist]
    templatelist = glob.glob(config.textemplatedir + '/*.tex')
Robin Sonnabend's avatar
Robin Sonnabend committed
180
    data['templates'] = [str(os.path.basename(f))
181
                         for f in sorted(templatelist)]
Dave Kliczbor's avatar
Dave Kliczbor committed
182
    data['imageextensions'] = config.allowed_extensions
183
184
185
186
187
188
189
    return render_response('edit.html', data)


@app.route('/edit/<filename>')
def edit_one(filename):
    return edit(form=load_data(filename))

Dave Kliczbor's avatar
Dave Kliczbor committed
190
191
192

@app.route('/create', methods=['POST'])
def create():
193
194
195
    if request.method == 'POST':
        formdata = defaultdict(str, request.form.to_dict(flat=True))
        for a in ('headline', 'text'):
Robin Sonnabend's avatar
Robin Sonnabend committed
196
            formdata[a] = str(formdata[a])
197
198
199
200
201
202
        try:
            imgpath = save_and_convert_image_upload('imgupload')
            if imgpath is not None:
                formdata['img'] = imgpath
            outfilename = secure_filename(formdata['headline'][:16]) + str(hash(formdata['headline'] + formdata[
                'text'] + os.path.splitext(formdata['textemplate'])[0] + os.path.splitext(formdata['img'])[0])) + '.schild'
203
204
            if formdata['reusefilename']:
                outfilename = secure_filename(formdata['filename'])
205
            outpdfname = outfilename + '.pdf'
206
            formdata['filename'] = outfilename
207
            formdata['pdfname'] = outpdfname
208
            save_data(formdata, outfilename)
209
            run_pdflatex(formdata, os.path.join(config.pdfdir, outpdfname))
210
            try:
Robin Sonnabend's avatar
Robin Sonnabend committed
211
                flash(Markup("""PDF created and data saved. You might create another one. Here's a preview. Click to print.<br/>
212
                                <a href="%s"><img src="%s"/></a>""" %
213
214
215
                         (url_for('schild', filename=outfilename), url_for(
                             'pdfthumbnail', pdfname=outpdfname, maxgeometry=200))
                         ))
216
            except:
Robin Sonnabend's avatar
Robin Sonnabend committed
217
                print(("%s created" % outpdfname))
218
        except Exception as e:
219
            try:
Robin Sonnabend's avatar
Robin Sonnabend committed
220
                flash("Could not create pdf or save data: %s" % str(e), 'error')
221
            except:
Robin Sonnabend's avatar
Robin Sonnabend committed
222
                print(("Could not create pdf or save data: %s" % str(e)))
223
224
225
226
227
228

        data = {'form': formdata}
        imagelist = glob.glob(config.imagedir + '/*.png')
        data['images'] = [os.path.basename(f) for f in imagelist]
        templatelist = glob.glob(config.textemplatedir + '/*.tex')
        data['templates'] = [os.path.basename(f) for f in sorted(templatelist)]
229
230
231
232
233
234
235
236
237
        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
Dave Kliczbor's avatar
Dave Kliczbor committed
238
239
240
241


@app.route('/schild/<filename>')
def schild(filename):
Robin Sonnabend's avatar
Robin Sonnabend committed
242
    return render_response('schild.html', {'filename': filename, 'printer': [str(f) for f in sorted(config.printers.keys())]})
243

Dave Kliczbor's avatar
Dave Kliczbor committed
244
245
246

@app.route('/printout', methods=['POST'])
def printout():
247
248
249
250
251
252
253
254
    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)
Robin Sonnabend's avatar
Robin Sonnabend committed
255
            flash('Schild wurde zum Drucker geschickt!')
256
257
258
        except CalledProcessError as e:
            flash(Markup("<p>Could not print:</p><pre>%s</pre>" % e.output), 'error')
    else:
Robin Sonnabend's avatar
Robin Sonnabend committed
259
        flash('Ungültige Anzahl Kopien!')
260
261
    return redirect(url_for('index'))

Dave Kliczbor's avatar
Dave Kliczbor committed
262
def delete_file(filename):
263
264
265
266
    try:
        os.unlink(os.path.join(config.datadir, filename))
        for f in glob.glob(os.path.join(config.pdfdir, filename + '.pdf*')):
            os.unlink(f)
Robin Sonnabend's avatar
Robin Sonnabend committed
267
        flash("Schild %s wurde gelöscht" % filename)
268
269
        return redirect(url_for('index'))
    except:
Robin Sonnabend's avatar
Robin Sonnabend committed
270
        flash("Schild %s konnte nicht gelöscht werden." % filename, 'error')
271
272
        return redirect(url_for('schild', filename=filename))

Dave Kliczbor's avatar
Dave Kliczbor committed
273

Dave Kliczbor's avatar
Dave Kliczbor committed
274
275
276
277
278
279
280
281
282
283
@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'))

Dave Kliczbor's avatar
Dave Kliczbor committed
284
285
@app.route('/image/<imgname>')
def image(imgname):
286
287
288
289
290
291
292
293
    imgpath = os.path.join(config.imagedir, secure_filename(imgname))
    # print(imgpath)
    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'))

Dave Kliczbor's avatar
Dave Kliczbor committed
294
295
296

@app.route('/thumbnail/<imgname>/<int:maxgeometry>')
def thumbnail(imgname, maxgeometry):
297
298
299
300
301
    imgpath = os.path.join(config.imagedir, secure_filename(imgname))
    thumbpath = make_thumb(imgpath, maxgeometry)
    with open(thumbpath, 'r') as imgfile:
        return Response(imgfile.read(), mimetype="image/png")

Dave Kliczbor's avatar
Dave Kliczbor committed
302
303
304

@app.route('/pdfthumb/<pdfname>/<int:maxgeometry>')
def pdfthumbnail(pdfname, maxgeometry):
305
306
307
308
309
    pdfpath = os.path.join(config.pdfdir, secure_filename(pdfname))
    thumbpath = make_thumb(pdfpath, maxgeometry)
    with open(thumbpath, 'r') as imgfile:
        return Response(imgfile.read(), mimetype="image/png")

Dave Kliczbor's avatar
Dave Kliczbor committed
310

311
312
@app.route('/tplthumb/<tplname>/<int:maxgeometry>')
def tplthumbnail(tplname, maxgeometry):
313
314
315
316
317
    pdfpath = os.path.join(config.cachedir, secure_filename(tplname) + '.pdf')
    try:
        run_pdflatex(
            {'textemplate': secure_filename(tplname),
             'img': 'pictograms-nps-misc-camera.png',
Robin Sonnabend's avatar
Robin Sonnabend committed
318
319
             'headline': 'Überschrift',
             'text': 'Dies ist der Text, der in der UI als Text bezeichnet ist.',
320
             'markup': 'latex',
321
322
323
324
325
326
327
328
329
             }, pdfpath, overwrite=False
        )
    except Exception as e:
        return str(e)
    else:
        thumbpath = make_thumb(pdfpath, maxgeometry)
        with open(thumbpath, 'r') as imgfile:
            return Response(imgfile.read(), mimetype="image/png")

330

Dave Kliczbor's avatar
Dave Kliczbor committed
331
332
@app.route('/pdfdownload/<pdfname>')
def pdfdownload(pdfname):
333
334
335
    pdfpath = os.path.join(config.pdfdir, secure_filename(pdfname))
    with open(pdfpath, 'r') as pdffile:
        return Response(pdffile.read(), mimetype="application/pdf")
Dave Kliczbor's avatar
Dave Kliczbor committed
336

337

338
339
340
341
342
343
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)
Robin Sonnabend's avatar
Robin Sonnabend committed
344
            print(("Deleted %s" % filename))
345
        except Exception as e:
Robin Sonnabend's avatar
Robin Sonnabend committed
346
            print(("Could not delete %s: %s" % (filename, str(e))))
347
348
349
    for filename in glob.glob(os.path.join(config.datadir, '*.schild')):
        data = load_data(filename)
        pdfname = os.path.join(config.pdfdir, data['pdfname'])
Robin Sonnabend's avatar
Robin Sonnabend committed
350
        print(("Recreating %s" % pdfname))
351
        run_pdflatex(data, pdfname)
352

Dave Kliczbor's avatar
Dave Kliczbor committed
353
if __name__ == '__main__':
354
355
356
357
358
    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)