B
    `                 @   s   d dl mZ d dlmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZ ddd	Zdd
dZG dd deZdddZdS )    )global_registries)ConfigurationError)IRequestFactoryIRootFactory)Requestapply_request_extensions)RequestContext)DefaultRootFactoryNc                sL   | j }|dkrtd|}||_ t|     fdd}| |}||fS )a[  Return a tuple composed of ``(root, closer)`` when provided a
    :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.

    ``request`` is passed to the :app:`Pyramid` application root
    factory to compute the root. If ``request`` is None, a default
    will be constructed using the registry's :term:`Request Factory`
    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.
    N/c                  s       d S )N)end )ctxr   Y/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/scripting.pycloser   s    zget_root.<locals>.closer)registry_make_requestr   beginroot_factory)Zapprequestr   r   rootr   )r   r   get_root	   s    

r   c                s   |dkrt dtj}|dkr&tddkr8td||_t    t  fdd}|j	t
td}|}t dddkr|_t||||d	S )
a  This function pushes data onto the Pyramid threadlocal stack
    (request and registry), making those objects 'current'.  It
    returns a dictionary useful for bootstrapping a Pyramid
    application in a scripting environment.

    ``request`` is passed to the :app:`Pyramid` application root
    factory to compute the root. If ``request`` is None, a default
    will be constructed using the registry's :term:`Request Factory`
    via the :meth:`pyramid.interfaces.IRequestFactory.blank` method.

    If ``registry`` is not supplied, the last registry loaded from
    :attr:`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.

    The function returns a dictionary composed of ``root``,
    ``closer``, ``registry``, ``request`` and ``root_factory``.  The
    ``root`` returned is the application's root resource object.  The
    ``closer`` returned is a callable (accepting no arguments) that
    should be called when your scripting application is finished
    using the root.  ``registry`` is the resolved registry object.
    ``request`` is the request object passed or the constructed request
    if no request is passed.  ``root_factory`` is the root factory used
    to construct the root.

    This function may be used as a context manager to call the ``closer``
    automatically:

    .. code-block:: python

       registry = config.registry
       with prepare(registry) as env:
           request = env['request']
           # ...

    .. versionchanged:: 1.8

       Added the ability to use the return value as a context manager.

    .. versionchanged:: 2.0

       Request finished callbacks added via
       :meth:`pyramid.request.Request.add_finished_callback` will be invoked
       by the ``closer``.

    Nr   zjNo valid Pyramid applications could be found, make sure one has been created before trying to activate it.r
   c                  s   j r     d S )N)Zfinished_callbacksZ_process_finished_callbacksr   r   )r   r   r   r   r   f   s    zprepare.<locals>.closer)defaultcontext)r   r   r   r   r   )getattrr   lastr   r   r   r   r   r   queryUtilityr   r	   r   AppEnvironment)r   r   r   r   r   r   )r   r   r   prepare#   s.    1

r   c               @   s   e Zd Zdd Zdd ZdS )r   c             C   s   | S )Nr   )selfr   r   r   	__enter__{   s    zAppEnvironment.__enter__c             C   s   | d   d S )Nr   r   )r   typevalue	tracebackr   r   r   __exit__~   s    zAppEnvironment.__exit__N)__name__
__module____qualname__r   r#   r   r   r   r   r   z   s   r   c             C   s0   |dkrt j}|jttd}|| }||_|S )a  Return a :meth:`pyramid.request.Request` object anchored at a
    given path. 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`
    or :meth:`pyramid.scripting.prepare` 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
    :attr:`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.
    N)r   )r   r   r   r   r   Zblankr   )pathr   Zrequest_factoryr   r   r   r   r      s    
r   )N)NN)N)Zpyramid.configr   Zpyramid.exceptionsr   Zpyramid.interfacesr   r   Zpyramid.requestr   r   Zpyramid.threadlocalr   Zpyramid.traversalr	   r   r   dictr   r   r   r   r   r   <module>   s   

W