Bowe Strickland
2018-10-27 323fa95deea50f49c119728fc2eeacb9e0c51241
commit | author | age
c515d7 1 from pyramid.scripting import prepare
14be69 2 from pyramid.scripts.common import get_config_loader
fe5e07 3
0c29cf 4
14be69 5 def setup_logging(config_uri, global_conf=None):
MM 6     """
7     Set up Python logging with the filename specified via ``config_uri``
8     (a string in the form ``filename#sectionname``).
9
10     Extra defaults can optionally be specified as a dict in ``global_conf``.
11     """
12     loader = get_config_loader(config_uri)
13     loader.setup_logging(global_conf)
0c29cf 14
14be69 15
MM 16 def get_app(config_uri, name=None, options=None):
82fefc 17     """ Return the WSGI application named ``name`` in the PasteDeploy
2e3a01 18     config file specified by ``config_uri``.
49fb77 19
8f4fcc 20     ``options``, if passed, should be a dictionary used as variable assignments
CM 21     like ``{'http_port': 8080}``.  This is useful if e.g. ``%(http_port)s`` is
22     used in the config file.
6b01ad 23
MM 24     If the ``name`` is None, this will attempt to parse the name from
2e3a01 25     the ``config_uri`` string expecting the format ``inifile#name``.
14be69 26     If no name is found, the name will default to "main".
49fb77 27
14be69 28     """
MM 29     loader = get_config_loader(config_uri)
30     return loader.get_wsgi_app(name, options)
49fb77 31
0c29cf 32
14be69 33 def get_appsettings(config_uri, name=None, options=None):
7f89e2 34     """ Return a dictionary representing the key/value pairs in an ``app``
38e4c7 35     section within the file represented by ``config_uri``.
e81e76 36
GD 37     ``options``, if passed, should be a dictionary used as variable assignments
38     like ``{'http_port': 8080}``.  This is useful if e.g. ``%(http_port)s`` is
39     used in the config file.
38e4c7 40
CM 41     If the ``name`` is None, this will attempt to parse the name from
42     the ``config_uri`` string expecting the format ``inifile#name``.
14be69 43     If no name is found, the name will default to "main".
596495 44
14be69 45     """
MM 46     loader = get_config_loader(config_uri)
47     return loader.get_wsgi_app_settings(name, options)
0c29cf 48
156375 49
e96817 50 def bootstrap(config_uri, request=None, options=None):
f422ad 51     """ Load a WSGI application from the PasteDeploy config file specified
71696b 52     by ``config_uri``. The environment will be configured as if it is
MM 53     currently serving ``request``, leaving a natural environment in place
54     to write scripts that can generate URLs and utilize renderers.
f422ad 55
bec6d1 56     This function returns a dictionary with ``app``, ``root``, ``closer``,
MM 57     ``request``, and ``registry`` keys.  ``app`` is the WSGI app loaded
58     (based on the ``config_uri``), ``root`` is the traversal root resource
59     of the Pyramid application, and ``closer`` is a parameterless callback
60     that may be called when your script is complete (it pops a threadlocal
61     stack).
153c2b 62
012b97 63     .. note::
f422ad 64
012b97 65        Most operations within :app:`Pyramid` expect to be invoked within the
M 66        context of a WSGI request, thus it's important when loading your
67        application to anchor it when executing scripts and other code that is
68        not normally invoked during active WSGI requests.
69
70     .. note::
71
72        For a complex config file containing multiple :app:`Pyramid`
73        applications, this function will setup the environment under the context
74        of the last-loaded :app:`Pyramid` application. You may load a specific
75        application yourself by using the lower-level functions
76        :meth:`pyramid.paster.get_app` and :meth:`pyramid.scripting.prepare` in
77        conjunction with :attr:`pyramid.config.global_registries`.
f422ad 78
MM 79     ``config_uri`` -- specifies the PasteDeploy config file to use for the
80     interactive shell. The format is ``inifile#name``. If the name is left
81     off, ``main`` will be assumed.
82
83     ``request`` -- specified to anchor the script to a given set of WSGI
84     parameters. For example, most people would want to specify the host,
85     scheme and port such that their script will generate URLs in relation
bec6d1 86     to those parameters. A request with default parameters is constructed
MM 87     for you if none is provided. You can mutate the request's ``environ``
88     later to setup a specific host/port/scheme/etc.
5fb458 89
a54bc1 90     ``options`` Is passed to get_app for use as variable assignments like
e96817 91     {'http_port': 8080} and then use %(http_port)s in the
JA 92     config file.
93
b36dd8 94     This function may be used as a context manager to call the ``closer``
MM 95     automatically:
96
97     .. code-block:: python
98
99        with bootstrap('development.ini') as env:
100            request = env['request']
101            # ...
102
5fb458 103     See :ref:`writing_a_script` for more information about how to use this
CM 104     function.
b36dd8 105
MM 106     .. versionchanged:: 1.8
107
108        Added the ability to use the return value as a context manager.
109
f422ad 110     """
49fb77 111     app = get_app(config_uri, options=options)
bec6d1 112     env = prepare(request)
MM 113     env['app'] = app
114     return env