pyramid.paster¶
- bootstrap(config_uri, request=None, options=None)[source]¶
Load a WSGI application from the PasteDeploy config file specified by
config_uri. The environment will be configured as if it is currently servingrequest, leaving a natural environment in place to write scripts that can generate URLs and utilize renderers.This function returns a dictionary with
app,root,closer,request, andregistrykeys.appis the WSGI app loaded (based on theconfig_uri),rootis the traversal root resource of the Pyramid application, andcloseris a parameterless callback that may be called when your script is complete (it pops a threadlocal stack).Note
Most operations within Pyramid expect to be invoked within the context of a WSGI request, thus it's important when loading your application to anchor it when executing scripts and other code that is not normally invoked during active WSGI requests.
Note
For a complex config file containing multiple Pyramid applications, this function will setup the environment under the context of the last-loaded Pyramid application. You may load a specific application yourself by using the lower-level functions
pyramid.paster.get_app()andpyramid.scripting.prepare()in conjunction withpyramid.config.global_registries.config_uri-- specifies the PasteDeploy config file to use for the interactive shell. The format isinifile#name. If the name is left off,mainwill be assumed.request-- specified to anchor the script to a given set of WSGI parameters. For example, most people would want to specify the host, scheme and port such that their script will generate URLs in relation to those parameters. A request with default parameters is constructed for you if none is provided. You can mutate the request'senvironlater to setup a specific host/port/scheme/etc.optionsIs passed to get_app for use as variable assignments like {'http_port': 8080} and then use %(http_port)s in the config file.This function may be used as a context manager to call the
closerautomatically:with bootstrap('development.ini') as env: request = env['request'] # ...
See Writing a Script for more information about how to use this function.
Changed in version 1.8: Added the ability to use the return value as a context manager.
Changed in version 2.0: Request finished callbacks added via
pyramid.request.Request.add_finished_callback()will be invoked by thecloser.
- get_app(config_uri, name=None, options=None)[source]¶
Return the WSGI application named
namein the PasteDeploy config file specified byconfig_uri.options, if passed, should be a dictionary used as variable assignments like{'http_port': 8080}. This is useful if e.g.%(http_port)sis used in the config file.If the
nameis None, this will attempt to parse the name from theconfig_uristring expecting the formatinifile#name. If no name is found, the name will default to "main".
- get_appsettings(config_uri, name=None, options=None)[source]¶
Return a dictionary representing the key/value pairs in an
appsection within the file represented byconfig_uri.options, if passed, should be a dictionary used as variable assignments like{'http_port': 8080}. This is useful if e.g.%(http_port)sis used in the config file.If the
nameis None, this will attempt to parse the name from theconfig_uristring expecting the formatinifile#name. If no name is found, the name will default to "main".