diff options
author | equinox <equinox@diac24.net> | 2011-04-01 05:42:06 +0200 |
---|---|---|
committer | equinox <equinox@taifun.local.sublab.org> | 2011-04-01 05:42:28 +0200 |
commit | 49856f36361a71aba71c43c77cebb22340010181 (patch) | |
tree | 9d7d86422eac39c779a3bd01432586a0d01b7693 /tmpl.py |
initial checkin
Diffstat (limited to 'tmpl.py')
-rw-r--r-- | tmpl.py | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -0,0 +1,50 @@ +import os, traceback + +import cherrypy +from genshi.core import Stream +from genshi.output import encode, get_serializer +from genshi.template import Context, TemplateLoader + +loader = TemplateLoader( + os.path.join(os.path.dirname(__file__), 'templates'), + auto_reload=True +) + +def render(*args, **kwargs): + if args: + assert len(args) == 1, 'Expected exactly one argument, but got %r' % (args,) + template = loader.load(args[0]) + else: + template = cherrypy.thread_data.template + #ctxt = Context(url = cherrypy.url) + ctxt = Context(url = myurl) + ctxt.push(kwargs) + return template.generate(ctxt) + +def exc(exc): + return render('error.html', details = traceback.format_exc()) + +def expose(filename, method='html', encoding='utf-8', **options): + def decorate(func): + @cherrypy.expose + def wrapper(*args, **kwargs): + cherrypy.thread_data.template = loader.load(filename) + cherrypy.thread_data.func = func + opt = options.copy() + if method == 'html': + opt.setdefault('doctype', 'html') + serializer = get_serializer(method, **opt) + try: + stream = func(*args, **kwargs) + except Exception, e: + stream = exc(e) + if not isinstance(stream, Stream): + return stream + return encode(serializer(stream), method = serializer, encoding = encoding) + return wrapper + return decorate + +def myurl(url): + return '/subdap/' + url + + |