pestotools.genshi

Introduction

This package contains utility classes and functions for rendering Genshi templates from pesto applications.

For more examples of usage, please see the source code and tests.

API documention

Decorators and utility functions to integrate pesto handlers with the Genshi templating system.

class pestotools.genshi.GenshiRender(loader=<genshi.template.loader.TemplateLoader object at 0x35271d0c>, default_vars=None, template_cls=<class 'genshi.template.markup.MarkupTemplate'>, serializer='xhtml')

A GenshiRender instance provides decorators and functions for rendering Genshi templates to pesto Response objects or to strings.

There are methods to render using both Genshi’s MarkupTemplate or TextTemplate, and to return the result as a string, a genshi stream, or a response object.

This example shows the class being used as a decorator. Calling myfunc will return a pesto.response.Response object to be returned with the contents of ‘page.html’ rendered with the variables provided in the return value of myfunc:

>>> render = GenshiRender(TemplateLoader(['path/to/templates/']))
>>> @render('page.html')
... def myfunc():
...     return {'x': 'foo', 'y': 'bar'}
...

Instead of rendering a response object directly it’s also possible to simply return the rendered value as string:

>>> @render.as_string('page.html')
... def myfunc():
...     return {'x': 'foo', 'y': 'bar'}
...

For rapid prototyping, it’s also possible to render function docstrings as Genshi templates:

>>> @render.docstring()
... def myfunc():
...    '''   
...    <html>
...        $foo
...    </html>
...    '''
...    return {'foo': 'bar'}

Each of the rendering methods can also be called as a regular (non-decorator) method, for example:

>>> render.as_string('page.html', {'foo': 'bar'})
as_response(template=None, data=None, serializer=None, filters=[], template_cls=None, _in_decorator=False, **response_kwargs)

Return a response object for the rendered template:

>>> render = GenshiRender(TemplateLoader(['.']))
>>> response = render.as_response('my_template.html', {'foo': 'bar'})

Can also be used as a decorator. The decorated function will merge the original function’s return value (a dict) with the specified template:

>>> from pestotools.genshi import GenshiRender
>>> from pesto import currentrequest
>>> render = GenshiRender(TemplateLoader(['.']))
>>> 
>>> @render.as_response('my_template.html')
... def handler(request):
...     return {'foo': 'bar'}
...
as_stream(template=None, data=None, filters=[], template_cls=None, _in_decorator=False)

Return a Genshi stream for the rendered template

as_string(template=None, data=None, serializer=None, filters=[], template_cls=None, _in_decorator=False)

Return the string output of the rendered template. Can also work as a function decorator

docstring(serializer=None, filters=[], template_cls=None, **response_kwargs)

Render the function’s docstring as a genshi template stream:

>>> render = GenshiRender()
>>> @render.docstring()
... def myhandler(request):
...     '''
...     <html>
...         $foo
...     </html>
...     '''
...     return {'foo': 'bar'}
docstring_as_string(serializer=None, filters=[], template_cls=None, **response_kwargs)

Render the function’s docstring as a genshi template stream:

>>> render = GenshiRender()
>>> @render.docstring_as_string()
... def myfunc():
...     '''
...     <html>
...         $foo
...     </html>
...     '''
...     return {'foo': 'bar'}
text_as_response(*args, **kwargs)

Same as as_string but use a Genshi TextTemplate and text serializer

text_as_stream(*args, **kwargs)

Same as as_stream but use a Genshi TextTemplate

text_as_string(*args, **kwargs)

Same as as_string but use a Genshi TextTemplate and text serializer

pestotools.genshi.select(path)

Take a function emitting a genshi stream and apply an XPATH select to it to filter out all but the selected elements, eg:

>>> render = GenshiRender()
>>> @select("//p[@id='content']")
... @render.docstring()
... def myfunc():
...     '''
...     <html>
...         <h1>Welcome to $foo!</h1>
...         <p id="content">$foo<p>
...     </html>
...     '''
...     return {'foo': 'bar'}
...
>>> myfunc().content
['<p id="content">bar</p>']
pestotools.genshi.formfilled(defaults=None, form_id=None, form_name=None, source='form', **kwdefaults)

Apply the genshi HTMLFormFiller filter to a genshi stream, populating form fields from the request object or other specified defaults.

defaults:
dictionary of default form values
form_id
HTML id attribute of the form
form_name
HTML name attribute of the form
source
Name of the data source on the request object, ie 'form' (for request.form) or 'query' (for request.query).
**kwdefaults
Additional defaults passed as keyword arguments
class pestotools.genshi.GenshiApp(renderer)

Return a WSGI application serving genshi templated HTML pages. Example:

>>> from wsgiref.simple_server import make_server
>>> render = GenshiRender(TemplateLoader(['path/to/document/root']))
>>> app = GenshiApp(render)
>>> print "Serving on port 8000..."
>>> make_server('', 8000, app).serve_forever()
pestotools.genshi.genshi_app_factory(config, document_root, serializer='xhtml', auto_reload=True)

Return a WSGI app serving Genshi templated pages from a filesystem directory.

Indices and tables

Table Of Contents

This Page