3
5Ad]—'  ã               @   sÈ   d Z dddddgZddlZddlZddlZi Zx6dD ].Zeejkr4eje Ze	edƒr^ej
jZ[P q4W G d
d„ deƒZG dd„ dƒZ
e
ƒ Zdd„ Zejj e_ ddd„Zejj e_ dd„ Zejj e_ [dS )aü  
Implements a simple, robust, safe, Messenger class that allows one to
register callbacks for a signal/slot (or event/handler) kind of
messaging system.  One can basically register a callback
function/method to be called when an object sends a particular event.
The Messenger class is Borg.  So it is easy to instantiate and use.
This module is also reload-safe, so if the module is reloaded the
callback information is not lost.  Method callbacks do not have a
reference counting problem since weak references are used.

The main functionality of this module is provided by three functions,
`connect`, `disconnect` and `send`.

Here is example usage with VTK::

    >>> import messenger, vtk
    >>> def cb(obj, evt):
    ...  print obj.__class__.__name__, evt
    ...
    >>> o = vtk.vtkProperty()
    >>> o.AddObserver('ModifiedEvent', messenger.send)
    1
    >>> messenger.connect(o, 'ModifiedEvent', cb)
    >>>
    >>> o.SetRepresentation(1)
    vtkOpenGLProperty ModifiedEvent
    >>> messenger.connect(o, 'AnyEvent', cb)
    >>> o.SetRepresentation(2)
    vtkOpenGLProperty ModifiedEvent
    vtkOpenGLProperty ModifiedEvent
    >>>
    >>> messenger.send(o, 'foo')
    vtkOpenGLProperty foo
    >>> messenger.disconnect(o, 'AnyEvent')
    >>> messenger.send(o, 'foo')
    >>>

This approach is necessary if you don't want to be bitten by reference
cycles.  If you have a Python object holding a reference to a VTK
object and pass a method of the object to the AddObserver call, you
will get a reference cycle that cannot be collected by the garbage
collector.  Using this messenger module gets around the problem.

Also note that adding a connection for 'AnyEvent' will trigger a
callback no matter what event was generated.  The code above also
shows how disconnection works.

Ú	MessengerÚMessengerErrorÚconnectÚ
disconnectÚsendé    NÚ	messengerútvtk.messengerc               @   s   e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© r   r   úu/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/mayavi-4.7.1-py3.6-linux-x86_64.egg/tvtk/messenger.pyr   O   s   c               @   sN   e Zd ZdZeZdd„ Zdd„ Zddd	„Zd
d„ Z	dd„ Z
dd„ Zdd„ ZdS )r   a  Implements a messenger class which deals with something like
    signals and slots.  Basically, an object can register a signal
    that it plans to emit.  Any other object can decide to handle that
    signal (of that particular object) by registering itself with the
    messenger.  When a signal is emitted the messenger calls all
    handlers.  This makes it totally easy to deal with communication
    between objects.  The class is Borg.  Rather than use this class,
    please use the 'connect' and 'disconnect' functions.

    c             C   s&   | j | _t| dƒs"i | _ddg| _dS )z\Create the messenger.  This class is Borg.  So all
        instances are the same.

        Ú_signalsÚAnyEventÚallN)Ú_shared_dataÚ__dict__Úhasattrr   Ú
_catch_all)Úselfr   r   r   Ú__init__g   s    
zMessenger.__init__c       
      C   s¤   t |ƒ}t|ƒ}|| jkr$i | j|< | j| }||kr>i ||< || }t|ƒ}|tjkrfd|f||< n:|tjkr�tj|jƒ}|j	}	||	f||< nt
dt|ƒ ƒ‚dS )a�   Registers a slot given an object and its signal to slot
        into and also given a bound method in `callback` that should
        have two arguments.  `send` will call the callback
        with the object that emitted the signal and the actual
        event/signal as arguments.

        Parameters
        ----------

        - obj :  Python object

          Any Python object that will generate the particular event.

        - event : An event (can be anything, usually strings)

          The event `obj` will generate.  If this is in the list
          `self._catch_all`, then any event will call this callback.

        - callback : `function` or `method`

          This callback will be called when the object generates the
          particular event.  The object, event and any other arguments
          and keyword arguments given by the `obj` are passed along to
          the callback.

        Nz7Callback must be a function or method. You passed a %s.)ÚtypeÚhashr   ÚtypesÚFunctionTypeÚ
MethodTypeÚweakrefÚrefÚ__self__r	   r   Ústr)
r   ÚobjÚeventÚcallbackÚtypÚkeyÚsignalsÚslotsZcallback_keyÚnamer   r   r   r   x   s$    




zMessenger.connectNFc             C   s^   | j }|r|}nt|ƒ}||kr$dS |dkrH|dkr<||= qZ|| |= n|| | t|ƒ= dS )a/  Disconnects the object and its event handlers.

        Parameters
        ----------

        - obj : Object

          The object that generates events.

        - event : The event.  (defaults to None)

        - callback : `function` or `method`

          The event handler.

         If `event` and `callback` are None (the default) all the
         events and handlers for the object are removed.  If only
         `callback` is None, only this handler is removed.  If `obj`
         and 'event' alone are specified, all handlers for the event
         are removed.

        - obj_is_hash : `bool`

         Specifies if the object passed is a hash instead of the object itself.
         This is needed if the object is gc'd but only the hash exists and one
         wants to disconnect the object.

        N)r   r   )r   r    r!   r"   Úobj_is_hashr%   r$   r   r   r   r   ª   s    zMessenger.disconnectc             O   sÎ   y| j |ƒ}W n ttfk
r&   dS X | jdd… }||krH|j|ƒ x€|D ]x}||krN|| }xbt|jƒ ƒD ]R}	||	 \}
}|
r°|
ƒ }|r¨t||ƒ||f|ž|Ž qÂ||	= qp|||f|ž|Ž qpW qNW dS )a¯  To be called by the object `source` that desires to
        generate a particular event.  This function in turn invokes
        all the handlers for the event passing the `source` object,
        event and any additional arguments and keyword arguments.  If
        any connected callback is garbage collected without being
        disconnected, it is silently removed from the existing slots.

        Parameters
        ----------

        - source : Python object

          This is the object that generated the event.

        - event : The event.

          If there are handlers connected to events called 'AnyEvent'
          or 'all', then any event will invoke these.

        N)Ú_get_signalsr   ÚKeyErrorr   ÚappendÚlistÚkeysÚgetattr)r   Úsourcer!   ÚargsÚkw_argsÚsigsÚeventsZevtr&   r$   r    ÚmethÚinstr   r   r   r   Ö   s$    

zMessenger.sendc             C   s,   y| j |ƒ}W n tk
r"   dS X dS dS )zWReturns if the given object has registered itself with the
        messenger.

        r   é   N)r)   r   )r   r    r2   r   r   r   Úis_registered  s
    zMessenger.is_registeredc             C   s   t | j|ƒjƒ ƒS )zRReturns a list of signal names the object passed has
        registered.

        )r,   r)   r-   )r   r    r   r   r   Úget_signal_names  s    zMessenger.get_signal_namesc             C   s.   | j jt|ƒƒ}|dkr&td| ƒ‚n|S dS )zNGiven an object `obj` it returns the signals of that
        object.

        Nz=No such object: %s, has registered itself with the messenger.)r   Úgetr   r   )r   r    Úretr   r   r   r)     s    
zMessenger._get_signals)NNF)r	   r
   r   Ú__doc__Ú_savedr   r   r   r   r   r7   r8   r)   r   r   r   r   r   X   s   2
,+c             C   s   t j| ||ƒ d S )N)Ú
_messengerr   )r    r!   r"   r   r   r   r   -  s    Fc             C   s   t j| ||ƒ d S )N)r=   r   )r    r!   r"   r(   r   r   r   r   1  s    c             O   s   t j| |f|ž|Ž d S )N)r=   r   )r    r!   r0   r1   r   r   r   r   5  s    )r   r   )NNF)r;   Ú__all__r   Úsysr   r<   r'   ÚmodulesÚmodr   r   r   Ú	Exceptionr   r=   r   r   r   r   r   r   r   Ú<module>0   s0   




	 T



