σ
P'^c           @@  s   d  Z  d d l m Z d d l Z d d l m Z d d l m Z d d l m	 Z	 m
 Z
 d g Z e j e j f Z d	   Z d e f d
     YZ d S(   s   Signal class.i    (   t   absolute_importNi   (   t   saferef(   t   range(   t   PromiseProxyt   Proxyt   Signalc         C@  sS   t  |  t  r |  j   }  n  t |  d  rI t |  j  t |  j  f St |   S(   Nt   __func__(   t
   isinstanceR   t   _get_current_objectt   hasattrt   idt   __self__R   (   t   target(    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   _make_id   s
    c           B@  st   e  Z d  Z d
 d  Z d   Z d   Z d
 d
 e d
 d  Z d   Z	 d   Z
 d   Z d   Z d	   Z e Z RS(   s«   Base class for all signals


    .. attribute:: receivers
        Internal attribute, holds a dictionary of
        `{receiverkey (id): weakref(receiver)}` mappings.

    c         C@  s1   g  |  _  | d k r g  } n  t |  |  _ d S(   s   Create a new signal.

        :param providing_args: A list of the arguments this signal can pass
            along in a :meth:`send` call.

        N(   t	   receiverst   Nonet   sett   providing_args(   t   selfR   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   __init__"   s    		c         C@  s%   |  j  | d | j   d | d | S(   Nt   sendert   weakt   dispatch_uid(   t   connectR   (   R   t   funR   R   R   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   _connect_proxy.   s    c         @  sV   d t d   f d  } | rI t | d  rI | | d |   | d  S| | |   S(   sχ  Connect receiver to sender for signal.

        :param receiver: A function or an instance method which is to
            receive signals. Receivers must be hashable objects.

            if weak is :const:`True`, then receiver must be weak-referencable
            (more precisely :func:`saferef.safe_ref()` must be able to create a
            reference to the receiver).

            Receivers must be able to accept keyword arguments.

            If receivers have a `dispatch_uid` attribute, the receiver will
            not be added if another receiver already exists with that
            `dispatch_uid`.

        :keyword sender: The sender to which the receiver should respond.
            Must either be of type :class:`Signal`, or :const:`None` to receive
            events from any sender.

        :keyword weak: Whether to use weak references to the receiver.
            By default, the module will attempt to use weak references to the
            receiver objects. If this parameter is false, then strong
            references will be used.

        :keyword dispatch_uid: An identifier used to uniquely identify a
            particular instance of a receiver. This will usually be a
            string, though it may be anything hashable.

        c         @  s        f d   } | S(   Nc         @  sΝ   |  } t   t  r5  j  j |       |  S  rP   t   f } n t |  t   f }  r t j | d  j } n  x=  j D] \ } } | | k r Pq q W j j	 | | f  |  S(   Nt	   on_delete(
   R   R   t   __then__R   R   R   t   safe_reft   _remove_receiverR   t   append(   R   t   receivert
   lookup_keyt   r_keyt   _(   R   R   R   R   (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   _connect_signalT   s     (    (   R   R   R   R#   (   R   (   R   R   R   s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   _handle_optionsR   s    i    i   N(   R   t   Truet   callable(   R   t   argst   kwargsR$   (    (   R   s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyR   4   s    c   	      C@  s   | r | t  |  f } n t  |  t  |  f } xJ t t |  j   D]3 } |  j | \ } } | | k rI |  j | =PqI qI Wd S(   s  Disconnect receiver from sender for signal.

        If weak references are used, disconnect need not be called. The
        receiver will be removed from dispatch automatically.

        :keyword receiver: The registered receiver to disconnect. May be
            none if `dispatch_uid` is specified.

        :keyword sender: The registered sender to disconnect.

        :keyword weak: The weakref state to disconnect.

        :keyword dispatch_uid: the unique identifier of the receiver
            to disconnect

        N(   R   R   t   lenR   (	   R   R   R   R   R   R    t   indexR!   R"   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt
   disconnectu   s    
c         K@  sb   g  } |  j  s | SxH |  j t |   D]1 } | d |  d | |  } | j | | f  q) W| S(   s  Send signal from sender to all connected receivers.

        If any receiver raises an error, the error propagates back through
        send, terminating the dispatch loop, so it is quite possible to not
        have all receivers called if a raises an error.

        :param sender: The sender of the signal. Either a specific
            object or :const:`None`.

        :keyword \*\*named: Named arguments which will be passed to receivers.

        :returns: a list of tuple pairs: `[(receiver, response), β¦ ]`.

        t   signalR   (   R   t   _live_receiversR   R   (   R   R   t   namedt	   responsesR   t   response(    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   send   s    	c         K@  s   g  } |  j  s | Sxu |  j t |   D]^ } y | d |  d | |  } Wn& t k
 rs } | j | | f  q) X| j | | f  q) W| S(   sέ  Send signal from sender to all connected receivers catching errors.

        :param sender: The sender of the signal. Can be any python object
            (normally one registered with a connect if you actually want
            something to occur).

        :keyword \*\*named: Named arguments which will be passed to receivers.
            These arguments must be a subset of the argument names defined in
            :attr:`providing_args`.

        :returns: a list of tuple pairs: `[(receiver, response), β¦ ]`.

        :raises DispatcherKeyError:

        if any receiver raises an error (specifically any subclass of
        :exc:`Exception`), the error instance is returned as the result
        for that receiver.

        R,   R   (   R   R-   R   t	   ExceptionR   (   R   R   R.   R/   R   R0   t   err(    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   send_robustͺ   s    	c         C@  s   t  d  } g  } x| |  j D]q \ \ } } } | | k sF | | k r t | t  r} |   } | d k	 r | j |  q q | j |  q q W| S(   s―   Filter sequence of receivers to get resolved, live receivers.

        This checks for weak references and resolves them, then returning only
        live receivers.

        N(   R   R   R   R   t   WEAKREF_TYPESR   (   R   t	   senderkeyt   none_senderkeyR   t   receiverkeyt   r_senderkeyR   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyR-   Ν   s    	c         C@  s   g  } x3 |  j  D]( \ } } | | k r | j |  q q WxM | D]E } x< t |  j   D]+ \ } \ } } | | k rY |  j  | =qY qY WqC Wd S(   s'   Remove dead receivers from connections.N(   R   R   t	   enumerate(   R   R   t	   to_removet   keyt   connected_receivert   idxR!   R"   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyR   β   s    "c         C@  s   d j  t |   j  S(   Ns   <Signal: {0}>(   t   formatt   typet   __name__(   R   (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   __repr__ξ   s    N(   RA   t
   __module__t   __doc__R   R   R   R   R%   R+   R1   R4   R-   R   RB   t   __str__(    (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyR      s   		A			#			(   RD   t
   __future__R    t   weakreft    R   t   celery.fiveR   t   celery.localR   R   t   __all__t   ReferenceTypet   BoundMethodWeakrefR5   R   t   objectR   (    (    (    s>   /tmp/pip-unpacked-wheel-gV1wwp/celery/utils/dispatch/signal.pyt   <module>   s   		