<filename>chals/web/docs/app/__init__.py<gh_stars>10-100
#linux x64
#python 2.7 dependencies:
#certifi==2019.6.16
#chardet==3.0.4
#Click==7.0
#Flask==1.1.1
#gunicorn==19.9.0
#idna==2.8
#itsdangerous==1.1.0
#Jinja2==2.10.1
#MarkupSafe==1.1.1
#requests==2.22.0
#urllib3==1.25.3
#uuid==1.30
#Werkzeug==0.15.5
#setuptools==41.0.1

from flask import Flask, Response, request, make_response, render_template
import requests
import uuid
from subprocess import Popen, PIPE
from itsdangerous import Signer, BadSignature

app = Flask(__name__)


@app.route('/document.pdf', methods=['POST'])
def format():
    md = request.form.get(
        "text", "# hello\nyou didnt put anything so this is default doc")
    filename = '/tmp/' + str(uuid.uuid4())
    dbg_option = request.form.get("debug", "")
    template = request.form.get(
        "template", """
    \\documentclass[12pt]{article}
    \\begin{document}
    $body$
    
    Generated by DocuFMT
    \\end{document}
  """)
    if dbg_option != "":
        # this makes us super safe
        with open("/key", "r") as key_file:
            skey = key_file.read().strip()
        s = Signer(skey)
        try:
            dbg_option = s.unsign(dbg_option).decode('utf-8')
        except BadSignature:
            return "ERROR: debug option is not signed correctly"

    with open(filename + ".md", "w") as text_file:
        text_file.write(md)
    with open(filename + "_template.tex", "w") as text_file:
        text_file.write(template)
    try:
        p = Popen(('timeout', '5', 'pandoc',
                   '--template=' + filename + "_template.tex",
                   '--from=markdown', '-s', '-o', filename + '.pdf',
                   '--latex-engine-opt', dbg_option, filename + '.md'),
                  stdout=PIPE,
                  stderr=PIPE)
        out, err = p.communicate()
        exitcode = p.returncode
        if exitcode != 0:
            rsp = make_response(
                "%s\n%s\nexited with error code %d" % (out, err, exitcode))
            rsp.headers['Content-type'] = 'text/plain'
            return rsp
        try:
            with open(filename + '.pdf', mode='rb') as pdf_file:
                rsp = make_response(pdf_file.read())
                rsp.headers['Content-type'] = "application/pdf"
            return rsp
        except FileNotFoundError:
            rsp = make_response(
                "%s\n%s\nNo response PDF generated" % (out, err))
            rsp.headers['Content-type'] = 'text/plain'
            return rsp
    except Exception as e:
        return "ERROR: " + str(e)


@app.route('/source')
def viewsource():
    fp = __file__
    if fp.endswith(".pyc"):
        fp = fp[:-1]
    with open(fp) as f:
        rsp = make_response(f.read())
        rsp.headers['Content-type'] = 'text/plain'
    return rsp


@app.route('/')
def index():
    return render_template("index.html")


if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=80)

<filename>chals/web/docs/app/__init__.py<gh_stars>10-100
#linux x64
#python 2.7 dependencies:
#certifi==2019.6.16
#chardet==3.0.4
#Click==7.0
#Flask==1.1.1
#gunicorn==19.9.0
#idna==2.8
#itsdangerous==1.1.0
#Jinja2==2.10.1
#MarkupSafe==1.1.1
#requests==2.22.0
#urllib3==1.25.3
#uuid==1.30
#Werkzeug==0.15.5
#setuptools==41.0.1

from flask import Flask, Response, request, make_response, render_template
import requests
import uuid
from subprocess import Popen, PIPE
from itsdangerous import Signer, BadSignature

app = Flask(__name__)


@app.route('/document.pdf', methods=['POST'])
def format():
    md = request.form.get(
        "text", "# hello\nyou didnt put anything so this is default doc")
    filename = '/tmp/' + str(uuid.uuid4())
    dbg_option = request.form.get("debug", "")
    template = request.form.get(
        "template", """
    \\documentclass[12pt]{article}
    \\begin{document}
    $body$
    
    Generated by DocuFMT
    \\end{document}
  """)
    if dbg_option != "":
        # this makes us super safe
        with open("/key", "r") as key_file:
            skey = key_file.read().strip()
        s = Signer(skey)
        try:
            dbg_option = s.unsign(dbg_option).decode('utf-8')
        except BadSignature:
            return "ERROR: debug option is not signed correctly"

    with open(filename + ".md", "w") as text_file:
        text_file.write(md)
    with open(filename + "_template.tex", "w") as text_file:
        text_file.write(template)
    try:
        p = Popen(('timeout', '5', 'pandoc',
                   '--template=' + filename + "_template.tex",
                   '--from=markdown', '-s', '-o', filename + '.pdf',
                   '--latex-engine-opt', dbg_option, filename + '.md'),
                  stdout=PIPE,
                  stderr=PIPE)
        out, err = p.communicate()
        exitcode = p.returncode
        if exitcode != 0:
            rsp = make_response(
                "%s\n%s\nexited with error code %d" % (out, err, exitcode))
            rsp.headers['Content-type'] = 'text/plain'
            return rsp
        try:
            with open(filename + '.pdf', mode='rb') as pdf_file:
                rsp = make_response(pdf_file.read())
                rsp.headers['Content-type'] = "application/pdf"
            return rsp
        except FileNotFoundError:
            rsp = make_response(
                "%s\n%s\nNo response PDF generated" % (out, err))
            rsp.headers['Content-type'] = 'text/plain'
            return rsp
    except Exception as e:
        return "ERROR: " + str(e)


@app.route('/source')
def viewsource():
    fp = __file__
    if fp.endswith(".pyc"):
        fp = fp[:-1]
    with open(fp) as f:
        rsp = make_response(f.read())
        rsp.headers['Content-type'] = 'text/plain'
    return rsp


@app.route('/')
def index():
    return render_template("index.html")


if __name__ == '__main__':
    app.run(debug=False, host='0.0.0.0', port=80)

#linux x64 #python 2.7 dependencies: #certifi==2019.6.16 #chardet==3.0.4 #Click==7.0 #Flask==1.1.1 #gunicorn==19.9.0 #idna==2.8 #itsdangerous==1.1.0 #Jinja2==2.10.1 #MarkupSafe==1.1.1 #requests==2.22.0 #urllib3==1.25.3 #uuid==1.30 #Werkzeug==0.15.5 #setuptools==41.0.1 \\documentclass[12pt]{article}     \\begin{document}     $body$          Generated by DocuFMT     \\end{document} # this makes us super safe