annotate examples/tutorial/geddit/lib/template.py @ 611:16b1be35c265

Add current code for GenshiTutorial to the `examples` directory.
author cmlenz
date Wed, 29 Aug 2007 17:51:45 +0000
parents
children 0dc152d128f5
rev   line source
611
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
1 import os
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
2
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
3 import cherrypy
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
4 from genshi.core import Stream
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
5 from genshi.output import encode, get_serializer
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
6 from genshi.template import TemplateLoader
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
7
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
8 loader = TemplateLoader(
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
9 os.path.join(os.path.dirname(__file__), '..', 'templates'),
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
10 auto_reload=True
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
11 )
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
12
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
13 def output(filename, method=None, encoding='utf-8', **options):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
14 """Decorator for exposed methods to specify what template the should use
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
15 for rendering, and which serialization method and options should be
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
16 applied.
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
17 """
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
18 def decorate(func):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
19 def wrapper(*args, **kwargs):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
20 cherrypy.thread_data.template = loader.load(filename)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
21 serializer = get_serializer(method, **options)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
22 stream = func(*args, **kwargs)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
23 if not isinstance(stream, Stream):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
24 return stream
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
25 return encode(serializer(stream), method=serializer,
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
26 encoding=encoding)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
27 return wrapper
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
28 return decorate
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
29
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
30 def render(*args, **kwargs):
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
31 """Function to render the given data to the template specified via the
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
32 ``@output`` decorator.
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
33 """
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
34 if args:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
35 assert len(args) == 1, \
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
36 'Expected exactly one argument, but got %r' % (args,)
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
37 template = loader.load(args[0])
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
38 else:
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
39 template = cherrypy.thread_data.template
16b1be35c265 Add current code for GenshiTutorial to the `examples` directory.
cmlenz
parents:
diff changeset
40 return template.generate(**kwargs)
Copyright (C) 2012-2017 Edgewall Software