pyramid.scripting

get_root(app, request=None)[ソース]

Return a tuple composed of (root, closer) when provided a router instance as the app argument. The root returned is the application root object. The closer 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. If request is None, a default will be constructed using the registry's Request Factory via the pyramid.interfaces.IRequestFactory.blank() method.

prepare(request=None, registry=None)[ソース]

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. If request is None, a default will be constructed using the registry's Request Factory via the pyramid.interfaces.IRequestFactory.blank() method.

If registry is not supplied, the last registry loaded from pyramid.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 the global_registries and supply the appropriate one based on your own criteria.

The function returns a dictionary composed of root, closer, registry, request and root_factory. The root returned is the application's root resource object. The closer 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']
    # ...

バージョン 1.8 で変更: Added the ability to use the return value as a context manager.

バージョン 2.0 で変更: Request finished callbacks added via pyramid.request.Request.add_finished_callback() will be invoked by the closer.