diff options
| author | Michael Merickel <michael@merickel.org> | 2011-07-11 05:25:40 -0500 |
|---|---|---|
| committer | Michael Merickel <michael@merickel.org> | 2011-07-14 19:55:48 -0500 |
| commit | 31c20b20346d93c326159cabe02cff076eb4ce0f (patch) | |
| tree | d762b0957e4442ad99a84102a6c22bdde351427c | |
| parent | 91cd7e3c00e659391ffc64b19126c1016749bdd5 (diff) | |
| download | pyramid-31c20b20346d93c326159cabe02cff076eb4ce0f.tar.gz pyramid-31c20b20346d93c326159cabe02cff076eb4ce0f.tar.bz2 pyramid-31c20b20346d93c326159cabe02cff076eb4ce0f.zip | |
Added the ability to make a request object for use in scripts.
| -rw-r--r-- | pyramid/scripting.py | 47 |
1 files changed, 38 insertions, 9 deletions
diff --git a/pyramid/scripting.py b/pyramid/scripting.py index a3ec9bee5..d2495675e 100644 --- a/pyramid/scripting.py +++ b/pyramid/scripting.py @@ -1,3 +1,4 @@ +from pyramid.config import global_registries from pyramid.request import Request from pyramid.interfaces import IRequestFactory @@ -6,16 +7,18 @@ def get_root(app, request=None): :term:`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. If - ``request`` is not None, it is used as the request passed to the - :app:`Pyramid` application root factory. A request is - constructed and passed to the root factory if ``request`` is None.""" - registry = app.registry + your scripting application is finished using the root. + + If ``request`` is not None, it is used as the request passed to the + :app:`Pyramid` application root factory. A request is constructed + using :meth:`pyramid.scripting.make_request` and passed to the root + factory if ``request`` is None.""" + if hasattr(app, 'registry'): + registry = app.registry + else: + registry = global_registries.last if request is None: - request_factory = registry.queryUtility( - IRequestFactory, default=Request) - request = request_factory.blank('/') - request.registry = registry + request = make_request('/', registry) threadlocals = {'registry':registry, 'request':request} app.threadlocal_manager.push(threadlocals) def closer(request=request): # keep request alive via this function default @@ -23,3 +26,29 @@ def get_root(app, request=None): root = app.root_factory(request) return root, closer +def make_request(url, registry=None): + """ Return a :meth:`pyramid.request.Request` object anchored at a + given URL. The object returned will be generated from the supplied + registry's :term:`Request Factory` using the + :meth:`pyramid.interfaces.IRequestFactory.blank` method. + + This request object can be passed to + :meth:`pyramid.scripting.get_root` to initialize an application in + preparation for executing a script with a proper environment setup. + URLs can then be generated with the object, as well as rendering + templates. + + If ``registry`` is not supplied, the last registry loaded from + :meth:`pyramid.config.global_registries` will be used. If you have + loaded more than one :app:`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. + """ + if registry is None: + registry = global_registries.last + request_factory = registry.queryUtility(IRequestFactory, default=Request) + request = request_factory.blank(url) + request.registry = registry + return request + |
