29. USING HOOKS
12if __name__ == ’__main__’:
13config = Configurator()
14config.include(pyramid_handlers)
15config.add_handler(’one’, ’/{id}’, MyController, action=’index’)
16config.add_handler(’two’, ’/{action}/{id}’, MyController)
17server.make_server(’0.0.0.0’, 8080, config.make_wsgi_app())
18server.serve_forever()
The pyramid.config.Configurator.set_view_mapper() method can be used to set a default view mapper (overriding the superdefault view mapper used by Pyramid itself).
A single view registration can use a view mapper by passing the mapper as the mapper argument to add_view().
29.12 Registering Configuration Decorators
Decorators such as view_config don’t change the behavior of the functions or classes they’re decorating. Instead, when a scan is performed, a modified version of the function or class is registered with Pyramid.
You may wish to have your own decorators that offer such behaviour. This is possible by using the Venusian package in the same way that it is used by Pyramid.
By way of example, let’s suppose you want to write a decorator that registers the function it wraps with a Zope Component Architecture “utility” within the application registry provided by Pyramid. The application registry and the utility inside the registry is likely only to be available once your application’s configuration is at least partially completed. A normal decorator would fail as it would be executed before the configuration had even begun.
However, using Venusian, the decorator could be written as follows:
import venusian
from mypackage.interfaces import IMyUtility
class registerFunction(object):
def __init__(self, path): self.path = path
def register(self, scanner, name, wrapped): registry = scanner.config.registry