pyramid.scripting
¶
- get_root(app, request=None)[source]¶
Return a tuple composed of
(root, closer)
when provided a router instance as theapp
argument. Theroot
returned is the application root object. Thecloser
returned is a callable (accepting no arguments) that should be called when your scripting application is finished using the root.request
is passed to the Pyramid application root factory to compute the root. Ifrequest
is None, a default will be constructed using the registry's Request Factory via thepyramid.interfaces.IRequestFactory.blank()
method.
- prepare(request=None, registry=None)[source]¶
This function pushes data onto the Pyramid threadlocal stack (request and registry), making those objects 'current'. It returns a dictionary useful for bootstrapping a Pyramid application in a scripting environment.
request
is passed to the Pyramid application root factory to compute the root. Ifrequest
is None, a default will be constructed using the registry's Request Factory via thepyramid.interfaces.IRequestFactory.blank()
method.If
registry
is not supplied, the last registry loaded frompyramid.config.global_registries
will be used. If you have loaded more than one Pyramid application in the current process, you may not want to use the last registry loaded, thus you can search theglobal_registries
and supply the appropriate one based on your own criteria.The function returns a dictionary composed of
root
,closer
,registry
,request
androot_factory
. Theroot
returned is the application's root resource object. Thecloser
returned is a callable (accepting no arguments) that should be called when your scripting application is finished using the root.registry
is the resolved registry object.request
is the request object passed or the constructed request if no request is passed.root_factory
is the root factory used to construct the root.This function may be used as a context manager to call the
closer
automatically:registry = config.registry with prepare(registry) as env: request = env['request'] # ...
Changed in version 1.8: Added the ability to use the return value as a context manager.