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