2008-09-02 07:04:54 +00:00
|
|
|
from werkzeug import BaseRequest, BaseResponse, ETagResponseMixin, escape, run_simple, SharedDataMiddleware
|
2008-01-31 20:38:37 +00:00
|
|
|
from werkzeug.exceptions import HTTPException
|
|
|
|
from werkzeug.routing import RequestRedirect
|
2008-12-23 11:34:18 +00:00
|
|
|
from jinja import Environment, FileSystemLoader
|
2008-01-31 20:38:37 +00:00
|
|
|
from jinja.exceptions import TemplateNotFound
|
|
|
|
import os
|
2008-02-01 19:31:47 +00:00
|
|
|
import sha
|
|
|
|
from time import time
|
2008-01-31 20:38:37 +00:00
|
|
|
from random import randint
|
|
|
|
|
|
|
|
class Request(BaseRequest):
|
|
|
|
"""Useful subclass of the default request that knows how to build urls."""
|
|
|
|
|
|
|
|
def __init__(self, environ):
|
|
|
|
BaseRequest.__init__(self, environ)
|
|
|
|
|
|
|
|
|
2008-02-01 19:31:47 +00:00
|
|
|
class Response(BaseResponse, ETagResponseMixin):
|
2008-01-31 20:38:37 +00:00
|
|
|
"""Subclass of base response that has a default mimetype of text/html."""
|
|
|
|
default_mimetype = 'text/html'
|
|
|
|
|
|
|
|
|
|
|
|
# setup jinja
|
2008-12-23 11:34:18 +00:00
|
|
|
env = Environment(loader=FileSystemLoader('pages', use_memcache=False))
|
2008-01-31 20:38:37 +00:00
|
|
|
|
|
|
|
def app(environ, start_response):
|
|
|
|
"""The WSGI application that connects all together."""
|
|
|
|
req = Request(environ)
|
|
|
|
path = req.path[1:].lower()
|
2009-06-04 09:37:51 +00:00
|
|
|
# do theme handling
|
|
|
|
theme = 'light'
|
|
|
|
if 'style' in req.cookies:
|
|
|
|
theme = req.cookies['style']
|
|
|
|
if 'theme' in req.args.keys():
|
|
|
|
theme = req.args['theme']
|
2009-06-04 09:43:41 +00:00
|
|
|
if not os.path.isfile('static/styles/%s.css' % theme):
|
2009-06-04 09:37:51 +00:00
|
|
|
theme = 'light'
|
2008-01-31 20:38:37 +00:00
|
|
|
if path == '':
|
|
|
|
path = 'index'
|
2008-02-01 19:31:47 +00:00
|
|
|
mime_type='text/html'
|
2008-01-31 20:38:37 +00:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
path.index('.')
|
2008-03-09 21:14:21 +00:00
|
|
|
if path.split('.')[-1].isdigit() and not path.split('.')[-1].isalpha():
|
|
|
|
raise ValueError()
|
2008-01-31 20:38:37 +00:00
|
|
|
tmpl = env.get_template(path)
|
2008-02-01 19:31:47 +00:00
|
|
|
if path[-3:] == 'txt':
|
|
|
|
mime_type='text/plain'
|
2008-02-01 22:53:38 +00:00
|
|
|
if path[-3:] == 'xml':
|
|
|
|
mime_type='text/xml'
|
2008-01-31 20:38:37 +00:00
|
|
|
except ValueError:
|
|
|
|
tmpl = env.get_template(path + '.html')
|
|
|
|
except TemplateNotFound:
|
|
|
|
tmpl = env.get_template('not_found.html')
|
2009-06-04 09:37:51 +00:00
|
|
|
resp = Response(tmpl.render(static=False, theme=theme), mimetype=mime_type)
|
|
|
|
# more theme handling
|
|
|
|
if theme == 'light' and 'style' in req.cookies:
|
|
|
|
resp.delete_cookie('style')
|
|
|
|
elif theme != 'light':
|
|
|
|
resp.set_cookie('style', theme)
|
2008-02-01 19:31:47 +00:00
|
|
|
resp.add_etag()
|
2008-06-14 12:52:44 +00:00
|
|
|
resp.make_conditional(req)
|
2008-01-31 20:38:37 +00:00
|
|
|
return resp(environ, start_response)
|
|
|
|
|
2008-09-02 07:02:12 +00:00
|
|
|
app = SharedDataMiddleware(app, {
|
|
|
|
'/_static': os.path.join(os.path.dirname(__file__), 'static')
|
|
|
|
})
|
|
|
|
|
2008-01-31 20:38:37 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
run_simple('localhost', 5009, app)
|