/
render.py
executable file
·80 lines (63 loc) · 2.08 KB
/
render.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
"""
@summary: Has all the necessary functions needed to render a page to HTML.
@author: Abhishek Shrivastava <i.abhi27 [at] gmail [dot] com>
"""
import re
import sys
from config import PAGE_NAME_2_TITLE
from yaptu import copier
def render(page_name, value_dict):
"""
Pass the name of the controller's template file and the dict of values to substitute.
Returns ready-to-be-pushed HTML.
"""
page_tpl = open('templates/'+page_name+'.tpl','r').read()
render_tpl = open('templates/index.tpl','r').read()
page_html = templatify(page_tpl, value_dict)
return templatify(render_tpl, {'page_html' : page_html,
'page_title' : PAGE_NAME_2_TITLE[page_name] })
def push_html(html):
"""
Pushes the HTML to browser and quits.
"""
print "Content-Type: text/html"
print
print html
sys.exit(1)
def push_zip(filename, length, data):
"""
Pushes the ZIP file to browser and quits. User gets to see a 'Save As' option.
"""
print "Content-type: application/zip"
print "Content-Disposition: attachment; filename=%s" % filename
print "Content-Title: %s" % filename
print "Content-Length: %i" % length
print
print data
sys.exit(1)
def error(error_msg):
"""
Throws the error message in a rendered HTML way and quits.
"""
error_html = render('error',{'error' : error_msg})
push_html(error_html)
def templatify(tpl, values):
"""
Custom wrapper around the YAPTU template engine. Does the usual job.
"""
class DecoyFile:
def __init__(self):
self.data = []
def write(self, line):
self.data.append(line)
def read(self):
return "".join(self.data)
rex=re.compile('%([^@]+)s')
rbe=re.compile('\+')
ren=re.compile('-')
rco=re.compile('= ')
decoy = DecoyFile()
cop = copier(rex, values, rbe, ren, rco, ouf=decoy)
lines_block = [line+'\n' for line in tpl.split('\n')]
cop.copy(lines_block)
return decoy.read()