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
|
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
try:
return encode(serializer(stream), method = serializer, encoding = encoding)
except Exception, e:
return encode(serializer(exc(e)), method = serializer, encoding = encoding)
return wrapper
return decorate
def myurl(url):
return '/subdap/' + url
|