33. EXTENDING PYRAMID CONFIGURATION
config.add_jammyjam(’first’)
What happens now? When the add_jammyjam method is called, an action is appended to the pending actions list. When the pending configuration actions are processed during commit(), and no conflicts occur, the callable provided as the second argument to the action() method within add_jammyjam is called with no arguments. The callable in add_jammyjam is the register closure function. It simply sets the value config.registry.jammyjam to whatever the user passed in as the jammyjam argument to the add_jammyjam function. Therefore, the result of the user’s call to our directive will set the jammyjam attribute of the registry to the string first. A callable is used by a directive to defer the result of a user’s call to the directive until conflict detection has had a chance to do its job.
Other arguments exist to the action() method, including args, kw, order, and introspectables.
args and kw exist as values, which, if passed, will be used as arguments to the callable function when it is called back. For example our directive might use them like so:
1 def add_jammyjam(config, jammyjam):
2def register(*arg, **kw):
3 config.registry.jammyjam_args = arg
4config.registry.jammyjam_kw = kw
5config.registry.jammyjam = jammyjam
6config.action(’jammyjam’, register, args=(’one’,), kw={’two’:’two’})
In the above example, when this directive is used to generate an action, and that action is committed, config.registry.jammyjam_args will be set to (’one’,) and config.registry.jammyjam_kw will be set to {’two’:’two’}. args and kw are honestly not very useful when your callable is a closure function, because you already usually have access to every local in the directive without needing them to be passed back. They can be useful, however, if you don’t use a closure as a callable.
order is a crude order control mechanism. |
order defaults to |
the |
integer |
0; it |
can |
be |
set |
to any |
other integer. |
All |
actions |
that share |
an order will be called |
before |
other |
actions |
that |
share a |
higher order. |
This |
makes |
it possible |
to write a directive |
with |
callable logic that |
relies |
on the execution of the callable of another directive being done first. For example, Pyramid’s pyramid.config.Configurator.add_view() directive registers an action with a higher order than the pyramid.config.Configurator.add_route() method. Due to this, the add_view method’s callable can assume that, if a route_name was passed to it, that a route by this name was already registered by add_route, and if such a route has not already been registered, it’s a configuration error (a view that names a nonexistent route via its route_name parameter will never be called).
introspectables is a sequence of introspectable objects. You can pass a sequence of introspectables to the action() method, which allows you to augment Pyramid’s configuration introspection system.