B
    `5                 @   sj   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 G dd dZd	d
 ZdS )    )update_wrapper)Response)	Interface)action_method)IResourceURL	IResponse
ITraverser)takes_one_argc               @   sh   e Zd ZedddZdd Zdd Zeddd	Zed
d Zdd Z	edddZ
edddZdS )AdaptersConfiguratorMixinNc                s   j }||    dkr&tf t ttfs: f  fdd}dtdd<  d< jd|fd S )a  Add an event :term:`subscriber` for the event stream
        implied by the supplied ``iface`` interface.

        The ``subscriber`` argument represents a callable object (or a
        :term:`dotted Python name` which identifies a callable); it will be
        called with a single object ``event`` whenever :app:`Pyramid` emits
        an :term:`event` associated with the ``iface``, which may be an
        :term:`interface` or a class or a :term:`dotted Python name` to a
        global object representing an interface or a class.

        Using the default ``iface`` value, ``None`` will cause the subscriber
        to be registered for all event types. See :ref:`events_chapter` for
        more information about events and subscribers.

        Any number of predicate keyword arguments may be passed in
        ``**predicates``.  Each predicate named will narrow the set of
        circumstances in which the subscriber will be invoked.  Each named
        predicate must have been registered via
        :meth:`pyramid.config.Configurator.add_subscriber_predicate` before it
        can be used.  See :ref:`subscriber_predicates` for more information.

        .. versionadded:: 1.4
           The ``**predicates`` argument.
        Nc                 sd    d} | jf\}}}fdd|D }|}|||||d j|  d S )N
subscriberc                s   g | ]}  |qS  )_derive_predicate).0p)selfr   _/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/config/adapters.py
<listcomp>0   s    zNAdaptersConfiguratorMixin.add_subscriber.<locals>.register.<locals>.<listcomp>)phashorder
predicatesderived_predicatesderived_subscriber)Zget_predlistmake_derive_subscriberupdateregistryZregisterHandler)Zpredlistr   predsr   r   r   )ifaceintrr   r   r   r   r   register,   s    

z:AdaptersConfiguratorMixin.add_subscriber.<locals>.registerZsubscribersr   Z
interfaces)introspectables)	maybe_dottedr   
isinstancetuplelistintrospectableidobject_descriptionaction)r   r   r   r   Zdottedr   r   )r   r   r   r   r   r   add_subscriber   s     z(AdaptersConfiguratorMixin.add_subscriberc                s    }t  r fdd}|S )Nc                 s    | d S )Nr   r   )arg)	predicater   r   derived_predicateS   s    zFAdaptersConfiguratorMixin._derive_predicate.<locals>.derived_predicate)	eventonly)r   r+   r,   r   )r+   r   r   N   s    z+AdaptersConfiguratorMixin._derive_predicatec                sZ    t r,fdd tdr,t  s4 S  fdd}tdrVt| |S )Nc                 s    | d S )Nr   r   )r*   )r   r   r   r   `   s    zHAdaptersConfiguratorMixin._derive_subscriber.<locals>.derived_subscriber__name__c                 s"   t  fddD r  S d S )Nc             3   s   | ]}|  V  qd S )Nr   )r   r+   )r*   r   r   	<genexpr>|   s    z[AdaptersConfiguratorMixin._derive_subscriber.<locals>.subscriber_wrapper.<locals>.<genexpr>)all)r*   )r   r   )r*   r   subscriber_wrapperi   s    zHAdaptersConfiguratorMixin._derive_subscriber.<locals>.subscriber_wrapper)r-   hasattrr   )r   r   r   r1   r   )r   r   r   r   r   [   s    



z,AdaptersConfiguratorMixin._derive_subscriberc             C   s   | j d||||d dS )a  
        .. versionadded:: 1.4

        Adds a subscriber predicate factory.  The associated subscriber
        predicate can later be named as a keyword argument to
        :meth:`pyramid.config.Configurator.add_subscriber` in the
        ``**predicates`` anonymous keyword argument dictionary.

        ``name`` should be the name of the predicate.  It must be a valid
        Python identifier (it will be used as a ``**predicates`` keyword
        argument to :meth:`~pyramid.config.Configurator.add_subscriber`).

        ``factory`` should be a :term:`predicate factory` or :term:`dotted
        Python name` which refers to a predicate factory.

        See :ref:`subscriber_predicates` for more information.

        r   )weighs_more_thanweighs_less_thanN)Z_add_predicate)r   namefactoryr3   r4   r   r   r   add_subscriber_predicate   s    z2AdaptersConfiguratorMixin.add_subscriber_predicatec                sh        fdd}tf}d| d} |d< |d< j|||fd dS )	ae  When an object of type (or interface) ``type_or_iface`` is
        returned from a view callable, Pyramid will use the adapter
        ``adapter`` to convert it into an object which implements the
        :class:`pyramid.interfaces.IResponse` interface.  If ``adapter`` is
        None, an object returned of type (or interface) ``type_or_iface``
        will itself be used as a response object.

        ``adapter`` and ``type_or_interface`` may be Python objects or
        strings representing dotted names to importable Python global
        objects.

        See :ref:`using_iresponse` for more information.c                 s2   j }  d kr| ft n|  ft d S )N)r   ZregisterSelfAdapterr   registerAdapter)reg)adapterr   type_or_ifacer   r   r      s    z@AdaptersConfiguratorMixin.add_response_adapter.<locals>.registerzresponse adapterszresponse adapterr:   type)r    N)r!   r   r%   r'   r(   )r   r:   r;   r   discriminatorr   r   )r:   r   r;   r   add_response_adapter   s    

z.AdaptersConfiguratorMixin.add_response_adapterc             C   s   |  d t d S )N)r>   WebobResponse)r   r   r   r   add_default_response_adapters   s    z7AdaptersConfiguratorMixin.add_default_response_adaptersc                sh    |}   |f fdd	}d|f}d|d| d} |d< ||d< j|||fd d	S )
a%
  
        The superdefault :term:`traversal` algorithm that :app:`Pyramid` uses
        is explained in :ref:`traversal_algorithm`.  Though it is rarely
        necessary, this default algorithm can be swapped out selectively for
        a different traversal pattern via configuration.  The section
        entitled :ref:`changing_the_traverser` details how to create a
        traverser class.

        For example, to override the superdefault traverser used by Pyramid,
        you might do something like this:

        .. code-block:: python

           from myapp.traversal import MyCustomTraverser
           config.add_traverser(MyCustomTraverser)

        This would cause the Pyramid superdefault traverser to never be used;
        instead all traversal would be done using your ``MyCustomTraverser``
        class, no matter which object was returned by the :term:`root
        factory` of this application.  Note that we passed no arguments to
        the ``iface`` keyword parameter.  The default value of ``iface``,
        ``None`` represents that the registered traverser should be used when
        no other more specific traverser is available for the object returned
        by the root factory.

        However, more than one traversal algorithm can be active at the same
        time.  The traverser used can depend on the result of the :term:`root
        factory`.  For instance, if your root factory returns more than one
        type of object conditionally, you could claim that an alternate
        traverser adapter should be used against one particular class or
        interface returned by that root factory.  When the root factory
        returned an object that implemented that class or interface, a custom
        traverser would be used.  Otherwise, the default traverser would be
        used.  The ``iface`` argument represents the class of the object that
        the root factory might return or an :term:`interface` that the object
        might implement.

        To use a particular traverser only when the root factory returns a
        particular class:

        .. code-block:: python

           config.add_traverser(MyCustomTraverser, MyRootClass)

        When more than one traverser is active, the "most specific" traverser
        will be used (the one that matches the class or interface of the
        value returned by the root factory most closely).

        Note that either ``adapter`` or ``iface`` can be a :term:`dotted
        Python name` or a Python object.

        See :ref:`changing_the_traverser` for more information.
        c                s"   | d krt } j | ft d S )N)r   r   r8   r   )r   )r:   r   r   r   r     s    z9AdaptersConfiguratorMixin.add_traverser.<locals>.registerZ	traverserZ
traversersztraverser for %rr:   r   )r    N)r!   r%   r(   )r   r:   r   r   r=   r   r   )r:   r   r   add_traverser   s    7

z'AdaptersConfiguratorMixin.add_traverserc                sh       |}|f fdd	}d|f}d|d| d} |d< ||d< j|||fd d	S )
a  
        .. versionadded:: 1.3

        When you add a traverser as described in
        :ref:`changing_the_traverser`, it's convenient to continue to use the
        :meth:`pyramid.request.Request.resource_url` API.  However, since the
        way traversal is done may have been modified, the URLs that
        ``resource_url`` generates by default may be incorrect when resources
        are returned by a custom traverser.

        If you've added a traverser, you can change how
        :meth:`~pyramid.request.Request.resource_url` generates a URL for a
        specific type of resource by calling this method.

        The ``adapter`` argument represents a class that implements the
        :class:`~pyramid.interfaces.IResourceURL` interface.  The class
        constructor should accept two arguments in its constructor (the
        resource and the request) and the resulting instance should provide
        the attributes detailed in that interface (``virtual_path`` and
        ``physical_path``, in particular).

        The ``resource_iface`` argument represents a class or interface that
        the resource should possess for this url adapter to be used when
        :meth:`pyramid.request.Request.resource_url` looks up a resource url
        adapter.  If ``resource_iface`` is not passed, or it is passed as
        ``None``, the url adapter will be used for every type of resource.

        See :ref:`changing_resource_url` for more information.
        c                s$   | d krt } j | t ft d S )N)r   r   r8   r   )resource_iface)r:   r   r   r   r   5  s    zDAdaptersConfiguratorMixin.add_resource_url_adapter.<locals>.registerzresource url adapterzresource url adaptersz*resource url adapter for resource iface %rr:   rB   )r    N)r!   r%   r(   )r   r:   rB   r   r=   r   r   )r:   r   r   add_resource_url_adapter  s    

z2AdaptersConfiguratorMixin.add_resource_url_adapter)N)NN)N)N)r.   
__module____qualname__r   r)   r   r   r7   r>   r@   rA   rC   r   r   r   r   r
   
   s   B)#Ir
   c             C   s   t | ddS )Nevent)argname)r	   )Zcalleer   r   r   r-   H  s    r-   N)	functoolsr   Zwebobr   r?   Zzope.interfacer   Zpyramid.config.actionsr   Zpyramid.interfacesr   r   r   Zpyramid.utilr	   r
   r-   r   r   r   r   <module>   s     @