ó
N`¾Tc           @   sé   d  Z  d d l m Z d d l m Z d d l m Z m Z m Z m	 Z	 m
 Z
 m Z m Z e d ƒ Z d e _  d Z d e f d	 „  ƒ  YZ e d
 ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  j Z d S(   s+  Signals and events.

A small implementation of signals, inspired by a snippet of Django signal
API client code seen in a blog post.  Signals are first-class objects and
each manages its own receivers and message emission.

The :func:`signal` function provides singleton behavior for named signals.

iÿÿÿÿ(   t   warn(   t   WeakValueDictionary(   t	   WeakTypest   contextmanagert   defaultdictt   hashable_identityt   lazy_propertyt	   referencet   symbolt   ANYs   Token for "any sender".i    t   Signalc           B   sÂ   e  Z d  Z e Z e d „  ƒ Z e d „  ƒ Z d d „ Z e e	 d „ Z
 e d „ Z e e d „ ƒ Z e d „ Z d „  Z d	 „  Z d
 „  Z e d „ Z d „  Z d „  Z d „  Z d „  Z RS(   s   A notification emitter.c         C   s   t  d d ƒ S(   sß   Emitted after each :meth:`connect`.

        The signal sender is the signal instance, and the :meth:`connect`
        arguments are passed through: *receiver*, *sender*, and *weak*.

        .. versionadded:: 1.2

        t   docs"   Emitted after a receiver connects.(   R
   (   t   self(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   receiver_connected%   s    
c         C   s   t  d d ƒ S(   s  Emitted after :meth:`disconnect`.

        The sender is the signal instance, and the :meth:`disconnect` arguments
        are passed through: *receiver* and *sender*.

        Note, this signal is emitted **only** when :meth:`disconnect` is
        called explicitly.

        The disconnect signal can not be emitted by an automatic disconnect
        (due to a weakly referenced receiver or sender going out of scope),
        as the receiver and/or sender instances are no longer available for
        use at the time this signal would be emitted.

        An alternative approach is available by subscribing to
        :attr:`receiver_connected` and setting up a custom weakref cleanup
        callback on weak receivers and senders.

        .. versionadded:: 1.2

        R   s%   Emitted after a receiver disconnects.(   R
   (   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   receiver_disconnected1   s    c         C   sF   | r | |  _  n  i  |  _ t t ƒ |  _ t t ƒ |  _ i  |  _ d S(   st   
        :param doc: optional.  If provided, will be assigned to the signal's
          __doc__ attribute.

        N(   t   __doc__t	   receiversR   t   sett   _by_receivert
   _by_sendert   _weak_senders(   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   __init__I   s    	c         C   s¹  t  | ƒ } | r0 t | |  j ƒ } | | _ n | } | t k rK t } n t  | ƒ } |  j j | | ƒ |  j | j	 | ƒ |  j
 | j	 | ƒ ~ | t k	 rü | |  j k rü y t | |  j ƒ } | | _ Wn t k
 râ qü X|  j j | | ƒ ~ n  d |  j k r]|  j j r]y& |  j j |  d | d | d | ƒWq]|  j | | ƒ ‚  q]Xn  t j rµ|  t k	 rµy# t j |  d | d | d | ƒWqµ|  j | | ƒ ‚  qµXn  | S(   sa  Connect *receiver* to signal events sent by *sender*.

        :param receiver: A callable.  Will be invoked by :meth:`send` with
          `sender=` as a single positional argument and any \*\*kwargs that
          were provided to a call to :meth:`send`.

        :param sender: Any object or :obj:`ANY`, defaults to ``ANY``.
          Restricts notifications delivered to *receiver* to only those
          :meth:`send` emissions sent by *sender*.  If ``ANY``, the receiver
          will always be notified.  A *receiver* may be connected to
          multiple *sender* values on the same Signal through multiple calls
          to :meth:`connect`.

        :param weak: If true, the Signal will hold a weakref to *receiver*
          and automatically disconnect when *receiver* goes out of scope or
          is garbage collected.  Defaults to True.

        R   t   receivert   sendert   weakt   receiver_argt
   sender_argt   weak_arg(   R   R   t   _cleanup_receivert   receiver_idR	   t   ANY_IDR   t
   setdefaultR   t   addR   R   t   _cleanup_sendert	   sender_idt	   TypeErrort   __dict__R   t   sendt
   disconnect(   R   R   R   R   R   t   receiver_refR"   t
   sender_ref(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   connect\   sP    	

c            s   ‡  ‡ ‡ f d †  } | S(   sK  Connect the decorated function as a receiver for *sender*.

        :param sender: Any object or :obj:`ANY`.  The decorated function
          will only receive :meth:`send` emissions sent by *sender*.  If
          ``ANY``, the receiver will always be notified.  A function may be
          decorated multiple times with differing *sender* values.

        :param weak: If true, the Signal will hold a weakref to the
          decorated function and automatically disconnect when *receiver*
          goes out of scope or is garbage collected.  Unlike
          :meth:`connect`, this defaults to False.

        The decorated function will be invoked by :meth:`send` with
          `sender=` as a single positional argument and any \*\*kwargs that
          were provided to the call to :meth:`send`.


        .. versionadded:: 1.1

        c            s   ˆ  j  |  ˆ ˆ ƒ |  S(   N(   R)   (   t   fn(   R   R   R   (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt	   decoratorµ   s    (    (   R   R   R   R+   (    (   R   R   R   sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   connect_via    s    c         c   sM   |  j  | d | d t ƒy	 d VWn |  j | ƒ ‚  n X|  j | ƒ d S(   s  Execute a block with the signal temporarily connected to *receiver*.

        :param receiver: a receiver callable
        :param sender: optional, a sender to filter on

        This is a context manager for use in the ``with`` statement.  It can
        be useful in unit tests.  *receiver* is connected to the signal for
        the duration of the ``with`` block, and will be disconnected
        automatically when exiting the block:

        .. testsetup::

          from __future__ import with_statement
          from blinker import Signal
          on_ready = Signal()
          receiver = lambda sender: None

        .. testcode::

          with on_ready.connected_to(receiver):
             # do stuff
             on_ready.send(123)

        .. versionadded:: 1.1

        R   R   N(   R)   t   Falset   NoneR&   (   R   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   connected_toº   s    	c         C   s   t  d t ƒ |  j | | ƒ S(   s_  An alias for :meth:`connected_to`.

        :param receiver: a receiver callable
        :param sender: optional, a sender to filter on

        .. versionadded:: 0.9

        .. versionchanged:: 1.1
          Renamed to :meth:`connected_to`.  ``temporarily_connected_to``
          was deprecated in 1.2 and removed in a subsequent version.

        sA   temporarily_connected_to is deprecated; use connected_to instead.(   R    t   DeprecationWarningR/   (   R   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   temporarily_connected_toß   s    c         O   s   t  | ƒ d k r d } n5 t  | ƒ d k rF t d t  | ƒ ƒ ‚ n
 | d } |  j s] g  Sg  |  j | ƒ D] } | | | |  f ^ qm Sd S(   s˜  Emit this signal on behalf of *sender*, passing on \*\*kwargs.

        Returns a list of 2-tuples, pairing receivers with their return
        value. The ordering of receiver notification is undefined.

        :param \*sender: Any object or ``None``.  If omitted, synonymous
          with ``None``.  Only accepts one positional argument.

        :param \*\*kwargs: Data to be sent to receivers.

        i    i   s5   send() accepts only one positional argument, %s givenN(   t   lenR.   R#   R   t   receivers_for(   R   R   t   kwargsR   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR%   ñ   s    	
	c         C   sA   |  j  s t S|  j t r t S| t k r. t St | ƒ |  j k S(   sô   True if there is probably a receiver for *sender*.

        Performs an optimistic check only.  Does not guarantee that all
        weakly referenced receivers are still alive.  See
        :meth:`receivers_for` for a stronger search.

        (   R   R-   R   R   t   TrueR	   R   (   R   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   has_receivers_for  s    	c         c   sÖ   |  j  rÒ t | ƒ } | |  j k r? |  j t |  j | B} n |  j t j ƒ  } x} | D]r } |  j  j | ƒ } | d k rƒ qY n  t | t ƒ rÆ | ƒ  } | d k r½ |  j	 | t ƒ qY n  | } n  | VqY Wn  d S(   s2   Iterate all live receivers listening for *sender*.N(
   R   R   R   R   t   copyt   getR.   t
   isinstanceR   t   _disconnect(   R   R   R"   t   idsR   R   t   strong(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR3     s"    	
		c         C   s{   | t  k r t } n t | ƒ } t | ƒ } |  j | | ƒ d |  j k rw |  j j rw |  j j |  d | d | ƒn  d S(   s  Disconnect *receiver* from this signal's events.

        :param receiver: a previously :meth:`connected<connect>` callable

        :param sender: a specific sender to disconnect from, or :obj:`ANY`
          to disconnect from all senders.  Defaults to ``ANY``.

        R   R   R   N(   R	   R   R   R:   R$   R   R   R%   (   R   R   R   R"   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR&   3  s    		c         C   sy   | t  k ra |  j j | t ƒ rK x' |  j j ƒ  D] } | j | ƒ q1 Wn  |  j j | d  ƒ n |  j | j | ƒ d  S(   N(	   R   R   t   popR-   R   t   valuest   discardR   R.   (   R   R   R"   t   bucket(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR:   I  s    c         C   s   |  j  | j t ƒ d S(   s'   Disconnect a receiver from all senders.N(   R:   R   R   (   R   R'   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR   R  s    c         C   sf   | j  } | t k s t ‚ |  j j | d ƒ x1 |  j j | d ƒ D] } |  j | j | ƒ qD Wd S(   s'   Disconnect all receivers from a sender.N(    (	   R"   R   t   AssertionErrorR   R=   R.   R   R   R?   (   R   R(   R"   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR!   V  s
    	c         C   s8   |  j  j ƒ  |  j j ƒ  |  j j ƒ  |  j j ƒ  d S(   s4   Throw away all signal state.  Useful for unit tests.N(   R   t   clearR   R   R   (   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   _clear_state^  s    N(   t   __name__t
   __module__R   R	   R   R   R   R.   R   R5   R)   R-   R,   R   R/   R1   R%   R6   R3   R&   R:   R   R!   RC   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR
      s$   D$							s  Sent by a :class:`Signal` after a receiver connects.

:argument: the Signal that was connected to
:keyword receiver_arg: the connected receiver
:keyword sender_arg: the sender to connect to
:keyword weak_arg: true if the connection to receiver_arg is a weak reference

.. deprecated:: 1.2

As of 1.2, individual signals have their own private
:attr:`~Signal.receiver_connected` and
:attr:`~Signal.receiver_disconnected` signals with a slightly simplified
call signature.  This global signal is planned to be removed in 1.6.

t   NamedSignalc           B   s#   e  Z d  Z d d „ Z d „  Z RS(   s%   A named generic notification emitter.c         C   s   t  j |  | ƒ | |  _ d  S(   N(   R
   R   t   name(   R   RG   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyR   {  s    c         C   s$   t  j |  ƒ } d | d  |  j f S(   Ns   %s; %r>iÿÿÿÿ(   R
   t   __repr__RG   (   R   t   base(    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyRH     s    N(   RD   RE   R   R.   R   RH   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyRF   x  s   t	   Namespacec           B   s   e  Z d  Z d d „ Z RS(   s%   A mapping of signal names to signals.c         C   s:   y |  | SWn' t  k
 r5 |  j | t | | ƒ ƒ SXd S(   s—   Return the :class:`NamedSignal` *name*, creating it if required.

        Repeated calls to this function will return the same signal object.

        N(   t   KeyErrorR   RF   (   R   RG   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   signal‰  s    N(   RD   RE   R   R.   RL   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyRJ   †  s   t   WeakNamespacec           B   s   e  Z d  Z d d „ Z RS(   s  A weak mapping of signal names to signals.

    Automatically cleans up unused Signals when the last reference goes out
    of scope.  This namespace implementation exists for a measure of legacy
    compatibility with Blinker <= 1.2, and may be dropped in the future.

    c         C   s:   y |  | SWn' t  k
 r5 |  j | t | | ƒ ƒ SXd S(   s—   Return the :class:`NamedSignal` *name*, creating it if required.

        Repeated calls to this function will return the same signal object.

        N(   RK   R   RF   (   R   RG   R   (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyRL   ž  s    N(   RD   RE   R   R.   RL   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyRM   •  s   N(   R   t   warningsR    t   weakrefR   t   blinker._utilitiesR   R   R   R   R   R   R   R	   R   t   objectR
   R   RF   t   dictRJ   RM   RL   (    (    (    sR   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/blinker/base.pyt   <module>
   s   4	ÿ I	