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