forked from madprogrammer/sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.py
37 lines (33 loc) · 1.04 KB
/
template.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
import os
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 output(filename, method='html', encoding='utf-8', **options):
def decorate(func):
def wrapper(*args, **kwargs):
cherrypy.thread_data.template = loader.load(filename)
opt = options.copy()
if method == 'html':
opt.setdefault('doctype', 'html')
serializer = get_serializer(method, **opt)
stream = func(*args, **kwargs)
if not isinstance(stream, Stream):
return stream
return encode(serializer(stream), method=serializer,encoding=encoding)
return wrapper
return decorate
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.push(kwargs)
return template.generate(ctxt)