escher_pattern.py 3.1 KB
Newer Older
1 2 3 4 5
#!/usr/bin/env python3

'''
/**
 * @file escher_pattern.py
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
6
 * @brief wrapper for web
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
'''

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
23
from urllib.parse import urlparse, parse_qs
24

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
25
import time
26

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
27 28 29 30 31
# this is needed for importing?
import os, sys
os.chdir(os.path.dirname(__file__))
sys.path.append(os.path.dirname(__file__))
# also add:
32
'''
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
33 34 35 36 37 38 39 40 41
/etc/apache2/conf-enabled/serve-cgi-bin.conf:
                <Directory /var/www/html/pytest>
                        Options +ExecCGI
                        AddHandler wsgi-script .py
                        WSGIProcessGroup %{GLOBAL}
                        WSGIApplicationGroup %{GLOBAL}
                        Order deny,allow
                        Allow from all
                </Directory>
42 43 44

'''

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
from escher_pattern_class import Escher_Pattern

# def application is a hardcoded keyword for WSGI ?!
def application(environ,start_response):

    params = parse_qs(environ["QUERY_STRING"])

    status = '200 OK'

    lpm = 50
    page_width  = 270
    page_height = 210
    rotate = 5
    escher = 2.0
    tmp_dir = 'tmp'

    try:
      lpm = float(params['LPM'][0])
    except KeyError:
      pass

    try:
      page_width = float(params['PAGE_WIDTH'][0])
    except KeyError:
      pass

    try:
      page_height = float(params['PAGE_HEIGHT'][0])
    except KeyError:
      pass

    try:
      rotate = float(params['ROTATE'][0])
    except KeyError:
      pass

    try:
      escher = float(params['ESCHER'][0])
    except KeyError:
      pass

    basename  = 'escher-pattern'
    basename += '-ESCHER'+str(escher)
    basename += '-LPM'+str(lpm)
    basename += '-ROT'+str(rotate)
    basename += '-PAGE_WIDTH'+str(page_width)
    basename += '-PAGE_HEIGHT'+str(page_height)
    pdf_name  = basename+".pdf"

    ep = Escher_Pattern(width= page_width, height= page_height, escher=escher, lpm=lpm, rotate=rotate)
    ep.generate()
    ep.save()

    with open(pdf_name,'rb') as f:
      contents = f.read()

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
101 102
    os.remove(pdf_name)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    filesize = str(len(contents))
    filesize = filesize.encode('utf-8')

    response_header = [
      ('Content-Type','application/pdf'),
      ('Cache-Control', 'public, must-revalidate, max-age=0'),
      ('Pragma', 'public'),
      ('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT'),
      ('Last-Modified', time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime())),
      ('Content-Length', str(len(contents))),
      ('Content-Disposition', 'inline; filename='+pdf_name+';')
    ]

    start_response(status,response_header)

    return [contents]