summaryrefslogtreecommitdiff
path: root/index.py
blob: eda595c8874841050c63e9768c211c59745ec78b (plain)
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
#!/usr/bin/python

from __future__ import with_statement

import os
import web
import mako
from mako.lookup import TemplateLookup

from functools import partial

import helper

APPDIR = os.path.dirname(os.path.abspath(__file__))

app = web.auto_application()

def appdir (*args):
    return os.path.join(APPDIR, *args)

class Renderer:
    def __init__ (self):
        self.lookup = TemplateLookup(directories=[appdir('templates')],
        module_directory = "/tmp/portato/",
        input_encoding='utf-8',
        output_encoding='utf-8',
        format_exceptions = True)

    def render (self, tpl, level = "pages" , **kwargs):
        try:
            t = self.get_tpl(tpl, level)
        except mako.exceptions.TopLevelLookupException, e:
            raise app.notfound(tpl)
       
        return t.render(h = helper, url = helper.url, w = web, **kwargs)

    __call__ = render

    def get_tpl (self, tpl, level):
        return self.lookup.get_template(self.get_tpl_name(tpl, level))
    
    def get_tpl_name (self, tpl, level):
        if not tpl.endswith(".mako"):
            tpl = tpl+".mako"

        return os.path.join(level, tpl)

web.config.debug = True

render = Renderer()

def FourOhFour(page):
    return web.notfound(render("404", level = "", page = page))

app.notfound = FourOhFour

class Handler (app.page):
    path = "/(.*)"
    def GET(self, name = '/'):
        if not name or name == '/': name = 'index'
        return render(name)

if __name__ == "__main__":
    app.run()