Pyramid Configuration Introspection¶
New in version 1.3.
When Pyramid starts up, each call to a configuration directive causes one or more introspectable objects to be registered with an introspector. The introspector can be queried by application code to obtain information about the configuration of the running application. This feature is useful for debug toolbars, command-line scripts which show some aspect of configuration, and for runtime reporting of startup-time configuration settings.
Using the Introspector¶
Here's an example of using Pyramid's introspector from within a view callable:
1from pyramid.view import view_config
2from pyramid.response import Response
3
4@view_config(route_name='bar')
5def show_current_route_pattern(request):
6    introspector = request.registry.introspector
7    route_name = request.matched_route.name
8    route_intr = introspector.get('routes', route_name)
9    return Response(str(route_intr['pattern']))
This view will return a response that contains the "pattern" argument provided
to the add_route method of the route which matched when the view was
called.  It uses the pyramid.interfaces.IIntrospector.get() method to
return an introspectable in the category routes with a
discriminator equal to the matched route name.  It then uses the
returned introspectable to obtain a "pattern" value.
The introspectable returned by the query methods of the introspector has
methods and attributes described by
pyramid.interfaces.IIntrospectable.  In particular, the
get(),
get_category(),
categories(),
categorized(), and
related() methods of an introspector
can be used to query for introspectables.
Introspectable Objects¶
Introspectable objects are returned from query methods of an introspector. Each
introspectable object implements the attributes and methods documented at
pyramid.interfaces.IIntrospectable.
The important attributes shared by all introspectables are the following:
title
A human-readable text title describing the introspectable
category_name
A text category name describing the introspection category to which this introspectable belongs. It is often a plural if there are expected to be more than one introspectable registered within the category.
discriminator
A hashable object representing the unique value of this introspectable within its category.
discriminator_hash
The integer hash of the discriminator (useful in HTML links).
type_name
The text name of a subtype within this introspectable's category. If there is only one type name in this introspectable's category, this value will often be a singular version of the category name but it can be an arbitrary value.
action_info
An object describing the directive call site which caused this introspectable to be registered. It contains attributes described in
pyramid.interfaces.IActionInfo.
Besides having the attributes described above, an introspectable is a
dictionary-like object.  An introspectable can be queried for data values via
its __getitem__, get, keys, values, or items methods.
For example:
1route_intr = introspector.get('routes', 'edit_user')
2pattern = route_intr['pattern']
Pyramid Introspection Categories¶
The list of concrete introspection categories provided by built-in Pyramid configuration directives follows. Add-on packages may supply other introspectables in categories not described here.
subscribers
Each introspectable in the
subscriberscategory represents a call topyramid.config.Configurator.add_subscriber()(or the decorator equivalent). Each will have the following data.
subscriberThe subscriber callable object (the resolution of the
subscriberargument passed toadd_subscriber).
interfacesA sequence of interfaces (or classes) that are subscribed to (the resolution of the
ifacesargument passed toadd_subscriber).
derived_subscriberA wrapper around the subscriber used internally by the system so it can call it with more than one argument if your original subscriber accepts only one.
predicatesThe predicate objects created as the result of passing predicate arguments to
add_subscriber.
derived_predicatesWrappers around the predicate objects created as the result of passing predicate arguments to
add_subscriber(to be used when predicates take only one value but must be passed more than one).
response adapters
Each introspectable in the
response adapterscategory represents a call topyramid.config.Configurator.add_response_adapter()(or a decorator equivalent). Each will have the following data.
adapterThe adapter object (the resolved
adapterargument toadd_response_adapter).
typeThe resolved
type_or_ifaceargument passed toadd_response_adapter.
root factories
Each introspectable in the
root factoriescategory represents a call topyramid.config.Configurator.set_root_factory()(or the Configurator constructor equivalent) or afactoryargument passed topyramid.config.Configurator.add_route(). Each will have the following data.
factoryThe factory object (the resolved
factoryargument toset_root_factory).
route_nameThe name of the route which will use this factory. If this is the default root factory (if it's registered during a call to
set_root_factory), this value will beNone.
session factory
Only one introspectable will exist in the
session factorycategory. It represents a call topyramid.config.Configurator.set_session_factory()(or the Configurator constructor equivalent). It will have the following data.
factoryThe factory object (the resolved
factoryargument toset_session_factory).
request factory
Only one introspectable will exist in the
request factorycategory. It represents a call topyramid.config.Configurator.set_request_factory()(or the Configurator constructor equivalent). It will have the following data.
factoryThe factory object (the resolved
factoryargument toset_request_factory).
locale negotiator
Only one introspectable will exist in the
locale negotiatorcategory. It represents a call topyramid.config.Configurator.set_locale_negotiator()(or the Configurator constructor equivalent). It will have the following data.
negotiatorThe factory object (the resolved
negotiatorargument toset_locale_negotiator).
renderer factories
Each introspectable in the
renderer factoriescategory represents a call topyramid.config.Configurator.add_renderer()(or the Configurator constructor equivalent). Each will have the following data.
nameThe name of the renderer (the value of the
nameargument toadd_renderer).
factoryThe factory object (the resolved
factoryargument toadd_renderer).
routes
Each introspectable in the
routescategory represents a call topyramid.config.Configurator.add_route(). Each will have the following data.
nameThe
nameargument passed toadd_route.
patternThe
patternargument passed toadd_route.
factoryThe (resolved)
factoryargument passed toadd_route.
xhrThe
xhrargument passed toadd_route.
request_methodThe
request_methodargument passed toadd_route.
request_methodsA sequence of request method names implied by the
request_methodargument passed toadd_routeor the valueNoneif arequest_methodargument was not supplied.
path_infoThe
path_infoargument passed toadd_route.
request_paramThe
request_paramargument passed toadd_route.
headerThe
headerargument passed toadd_route.
acceptThe
acceptargument passed toadd_route.
traverseThe
traverseargument passed toadd_route.
custom_predicatesThe
custom_predicatesargument passed toadd_route.
pregeneratorThe
pregeneratorargument passed toadd_route.
staticThe
staticargument passed toadd_route.
use_global_viewsThe
use_global_viewsargument passed toadd_route.
objectThe
pyramid.interfaces.IRouteobject that is used to perform matching and generation for this route.
security policy
There will be one and only one introspectable in the
security policycategory. It represents a call to thepyramid.config.Configurator.set_security_policy()method (or its Configurator constructor equivalent). It will have the following data:
policyThe policy object (the resolved
policyargument toset_security_policy).
authentication policy
There will be one and only one introspectable in the
authentication policycategory. It represents a call to thepyramid.config.Configurator.set_authentication_policy()method (or its Configurator constructor equivalent). It will have the following data.
policyThe policy object (the resolved
policyargument toset_authentication_policy).
authorization policy
There will be one and only one introspectable in the
authorization policycategory. It represents a call to thepyramid.config.Configurator.set_authorization_policy()method (or its Configurator constructor equivalent). It will have the following data.
policyThe policy object (the resolved
policyargument toset_authorization_policy).
default permission
There will be one and only one introspectable in the
default permissioncategory. It represents a call to thepyramid.config.Configurator.set_default_permission()method (or its Configurator constructor equivalent). It will have the following data.
valueThe permission name passed to
set_default_permission.
default csrf options
There will be one and only one introspectable in the
default csrf optionscategory. It represents a call to thepyramid.config.Configurator.set_default_csrf_options()method. It will have the following data.
require_csrfThe default value for
require_csrfif left unspecified on calls topyramid.config.Configurator.add_view().
tokenThe name of the token searched in
request.POSTto find a valid CSRF token.
headerThe name of the request header searched to find a valid CSRF token.
safe_methodsThe list of HTTP methods considered safe and exempt from CSRF checks.
views
Each introspectable in the
viewscategory represents a call topyramid.config.Configurator.add_view(). Each will have the following data.
nameThe
nameargument passed toadd_view.
contextThe (resolved)
contextargument passed toadd_view.
containmentThe (resolved)
containmentargument passed toadd_view.
request_paramThe
request_paramargument passed toadd_view.
request_methodsA sequence of request method names implied by the
request_methodargument passed toadd_viewor the valueNoneif arequest_methodargument was not supplied.
route_nameThe
route_nameargument passed toadd_view.
attrThe
attrargument passed toadd_view.
xhrThe
xhrargument passed toadd_view.
acceptThe
acceptargument passed toadd_view.
headerThe
headerargument passed toadd_view.
path_infoThe
path_infoargument passed toadd_view.
match_paramThe
match_paramargument passed toadd_view.
csrf_tokenThe
csrf_tokenargument passed toadd_view.
callableThe (resolved)
viewargument passed toadd_view. Represents the "raw" view callable.
derived_callableThe view callable derived from the
viewargument passed toadd_view. Represents the view callable which Pyramid itself calls (wrapped in security and other wrappers).
mapperThe (resolved)
mapperargument passed toadd_view.
decoratorThe (resolved)
decoratorargument passed toadd_view.
permissions
Each introspectable in the
permissionscategory represents a call topyramid.config.Configurator.add_view()that has an explicitpermissionargument or a call topyramid.config.Configurator.set_default_permission(). Each will have the following data.
valueThe permission name passed to
add_vieworset_default_permission.
templates
Each introspectable in the
templatescategory represents a call topyramid.config.Configurator.add_view()that has arendererargument which points to a template. Each will have the following data.
nameThe renderer's name (a string).
typeThe renderer's type (a string).
rendererThe
pyramid.interfaces.IRendererInfoobject which represents this template's renderer.
view mappers
Each introspectable in the
view mapperscategory represents a call topyramid.config.Configurator.add_view()that has an explicitmapperargument or a call topyramid.config.Configurator.set_view_mapper(). Each will have the following data.
mapperThe (resolved)
mapperargument passed toadd_vieworset_view_mapper.
asset overrides
Each introspectable in the
asset overridescategory represents a call topyramid.config.Configurator.override_asset(). Each will have the following data.
to_overrideThe
to_overrideargument (an asset spec) passed tooverride_asset.
override_withThe
override_withargument (an asset spec) passed tooverride_asset.
translation directories
Each introspectable in the
translation directoriescategory represents an individual element in aspecsargument passed topyramid.config.Configurator.add_translation_dirs(). Each will have the following data.
directoryThe absolute path of the translation directory.
specThe asset specification passed to
add_translation_dirs.
tweens
Each introspectable in the
tweenscategory represents a call topyramid.config.Configurator.add_tween(). Each will have the following data.
nameThe dotted name to the tween factory as a string (passed as the
tween_factoryargument toadd_tween).
factoryThe (resolved) tween factory object.
type
implicitorexplicitas a string.
underThe
underargument passed toadd_tween(a string).
overThe
overargument passed toadd_tween(a string).
static views
Each introspectable in the
static viewscategory represents a call topyramid.config.Configurator.add_static_view(). Each will have the following data.
nameThe
nameargument provided toadd_static_view.
specA normalized version of the
specargument provided toadd_static_view.
traversers
Each introspectable in the
traverserscategory represents a call topyramid.config.Configurator.add_traverser(). Each will have the following data.
ifaceThe (resolved) interface or class object that represents the return value of a root factory for which this traverser will be used.
adapterThe (resolved) traverser class.
resource url adapters
Each introspectable in the
resource url adapterscategory represents a call topyramid.config.Configurator.add_resource_url_adapter(). Each will have the following data.
adapterThe (resolved) resource URL adapter class.
resource_ifaceThe (resolved) interface or class object that represents the resource interface for which this URL adapter is registered.
request_ifaceThe (resolved) interface or class object that represents the request interface for which this URL adapter is registered.
Introspection in the Toolbar¶
The Pyramid debug toolbar (part of the pyramid_debugtoolbar package)
provides a canned view of all registered introspectables and their
relationships.  It is currently under the "Global" tab in the main navigation,
and it looks something like this:
Disabling Introspection¶
You can disable Pyramid introspection by passing the flag
introspection=False to the Configurator constructor in your
application setup:
from pyramid.config import Configurator
config = Configurator(..., introspection=False)
When introspection is False, all introspectables generated by
configuration directives are thrown away.