B
    `U                 @   s   d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZmZmZ G dd dZeeG d	d
 d
ZdS )    )implementer)action_method)ConfigurationError)ITweens)EXCVIEWINGRESSMAIN)TopologicalSorteris_nonstr_iteris_string_or_iterablec               @   s,   e Zd Zd	ddZdd Zed
ddZdS )TweensConfiguratorMixinNc             C   s   | j |||ddS )a  
        .. versionadded:: 1.2

        Add a 'tween factory'.  A :term:`tween` (a contraction of 'between')
        is a bit of code that sits between the Pyramid router's main request
        handling function and the upstream WSGI component that uses
        :app:`Pyramid` as its 'app'.  Tweens are a feature that may be used
        by Pyramid framework extensions, to provide, for example,
        Pyramid-specific view timing support, bookkeeping code that examines
        exceptions before they are returned to the upstream WSGI application,
        or a variety of other features.  Tweens behave a bit like
        :term:`WSGI` 'middleware' but they have the benefit of running in a
        context in which they have access to the Pyramid :term:`application
        registry` as well as the Pyramid rendering machinery.

        .. note:: You can view the tween ordering configured into a given
                  Pyramid application by using the ``ptweens``
                  command.  See :ref:`displaying_tweens`.

        The ``tween_factory`` argument must be a :term:`dotted Python name`
        to a global object representing the tween factory.

        The ``under`` and ``over`` arguments allow the caller of
        ``add_tween`` to provide a hint about where in the tween chain this
        tween factory should be placed when an implicit tween chain is used.
        These hints are only used when an explicit tween chain is not used
        (when the ``pyramid.tweens`` configuration value is not set).
        Allowable values for ``under`` or ``over`` (or both) are:

        - ``None`` (the default).

        - A :term:`dotted Python name` to a tween factory: a string
          representing the dotted name of a tween factory added in a call to
          ``add_tween`` in the same configuration session.

        - One of the constants :attr:`pyramid.tweens.MAIN`,
          :attr:`pyramid.tweens.INGRESS`, or :attr:`pyramid.tweens.EXCVIEW`.

        - An iterable of any combination of the above. This allows the user
          to specify fallbacks if the desired tween is not included, as well
          as compatibility with multiple other tweens.

        ``under`` means 'closer to the main Pyramid application than',
        ``over`` means 'closer to the request ingress than'.

        For example, calling ``add_tween('myapp.tfactory',
        over=pyramid.tweens.MAIN)`` will attempt to place the tween factory
        represented by the dotted name ``myapp.tfactory`` directly 'above'
        (in ``ptweens`` order) the main Pyramid request handler.
        Likewise, calling ``add_tween('myapp.tfactory',
        over=pyramid.tweens.MAIN, under='mypkg.someothertween')`` will
        attempt to place this tween factory 'above' the main handler but
        'below' (a fictional) 'mypkg.someothertween' tween factory.

        If all options for ``under`` (or ``over``) cannot be found in the
        current configuration, it is an error. If some options are specified
        purely for compatibility with other tweens, just add a fallback of
        MAIN or INGRESS. For example, ``under=('mypkg.someothertween',
        'mypkg.someothertween2', INGRESS)``.  This constraint will require
        the tween to be located under both the 'mypkg.someothertween' tween,
        the 'mypkg.someothertween2' tween, and INGRESS. If any of these is
        not in the current configuration, this constraint will only organize
        itself based on the tweens that are present.

        Specifying neither ``over`` nor ``under`` is equivalent to specifying
        ``under=INGRESS``.

        Implicit tween ordering is obviously only best-effort.  Pyramid will
        attempt to present an implicit order of tweens as best it can, but
        the only surefire way to get any particular ordering is to use an
        explicit tween order.  A user may always override the implicit tween
        ordering by using an explicit ``pyramid.tweens`` configuration value
        setting.

        ``under``, and ``over`` arguments are ignored when an explicit tween
        chain is specified using the ``pyramid.tweens`` configuration value.

        For more information, see :ref:`registering_tweens`.

        F)underoverexplicit)
_add_tween)selftween_factoryr   r    r   ]/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/config/tweens.py	add_tween   s    Qz!TweensConfiguratorMixin.add_tweenc             C   s   |  t d S )N)r   r   )r   r   r   r   add_default_tweensd   s    z*TweensConfiguratorMixin.add_default_tweensFc                sz  t tstd ttfkr2td | x<dfdfgD ](\}}|d k	rNt|sNtd||f qNW tkstrtkrtd tkstrtkrtd | j}g }|	t
d krt |t
  fdd	}	d
 f}
 rdpd}| d|
d| }|d< |d< ||d< |d< |d< || | j|
|	|d d S )NzgThe "tween_factory" argument to add_tween must be a dotted name to a globally importable object, not %rz%s is a reserved tween namer   r   z)"%s" must be a string or iterable, not %sz%s cannot be over INGRESSz%s cannot be under MAINc                  s(    r  njd d S )N)r   r   )add_explicitadd_implicitr   )r   namer   r   tweensr   r   r   register   s    z4TweensConfiguratorMixin._add_tween.<locals>.registerZtweenr   implicitr   z%s tweenr   factorytype)introspectables)
isinstancestrr   r   r   Zmaybe_dottedr   r
   registryZqueryUtilityr   TweensZregisterUtilityZintrospectableappendaction)r   r   r   r   r   tpr"   r   r   ZdiscriminatorZ
tween_typeZintrr   )r   r   r   r   r   r   r   r   g   sF    




z"TweensConfiguratorMixin._add_tween)NN)NNF)__name__
__module____qualname__r   r   r   r   r   r   r   r   r      s   
Ur   c               @   s6   e Zd Zdd Zdd ZdddZdd	 Zd
d ZdS )r#   c             C   s   t d tttd| _g | _d S )N)Zdefault_beforeZdefault_afterfirstlast)r	   r   r   sorterr   )r   r   r   r   __init__   s    
zTweens.__init__c             C   s   | j ||f d S )N)r   r$   )r   r   r   r   r   r   r      s    zTweens.add_explicitNc             C   s   | j j||||d d S )N)afterbefore)r-   add)r   r   r   r   r   r   r   r   r      s    zTweens.add_implicitc             C   s
   | j  S )N)r-   sorted)r   r   r   r   r      s    zTweens.implicitc             C   s@   | j r| j }n|  }x$|d d d D ]\}}|||}q&W |S )N)r   r   )r   handlerr"   Zuser   r   r   r   r   __call__   s    zTweens.__call__)NN)r(   r)   r*   r.   r   r   r   r5   r   r   r   r   r#      s
   	
r#   N)Zzope.interfacer   Zpyramid.config.actionsr   Zpyramid.exceptionsr   Zpyramid.interfacesr   Zpyramid.tweensr   r   r   Zpyramid.utilr	   r
   r   r   r#   r   r   r   r   <module>   s    