3
d\                 @   s   d d Z G dd dZdS )c             C   s   t | |||||d}|  dS )a   Add/Remove notifiers on objects following the description on an
    ObserverGraph.

    All nodes in ``ObserverGraph`` are required to be instances of
    ``IObserver``. The interface of ``IObserver`` supports this function.

    Parameters
    ----------
    object : object
        An object to be observed.
    graph : ObserverGraph
        A graph describing what and how extended traits are being observed.
        All nodes must be ``IObserver``.
    handler : callable(event)
        User-defined callable to handle change events.
        ``event`` is an object representing the change.
        Its type and content depends on the change.
    target : Any
        An object for defining the context of the user's handler notifier.
        This is typically an instance of HasTraits seen by the user as the
        "owner" of the observer.
    dispatcher : callable(callable, event)
        Callable for dispatching the user-defined handler, e.g. dispatching
        callback on a different thread.
    remove : boolean
        If true, notifiers are being removed.

    Raises
    ------
    NotiferNotFound
        Raised when notifier cannot be found for removal.
    )objectgraphhandlertarget
dispatcherremoveN)_AddOrRemoveNotifier)r   r   r   r   r   r   Z	callable_ r   =/tmp/pip-build-7vycvbft/traits/traits/observation/_observe.pyadd_or_remove_notifiers   s    "r
   c               @   s@   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dS )r   zo Callable for adding or removing notifiers.

    See ``add_or_remove_notifiers`` for the input parameters.
    c            C   s.   || _ || _|| _|| _|| _|| _g | _d S )N)r   r   r   r   r   r   
_processed)selfr   r   r   r   r   r   r   r   r	   __init__?   s    z_AddOrRemoveNotifier.__init__c             C   s   | j | j| j| jg}| jr(|ddd }yx|D ]
}|  q0W W nL tk
r   x4| jr| jj \}}| jrx|j| qR|j	| qRW  Y nX | jj
  dS )z6 Main function for adding/removing notifiers.
        N   )_add_or_remove_notifiers_add_or_remove_maintainers!_add_or_remove_children_notifiers_add_or_remove_extra_graphsr   	Exceptionr   popadd_toremove_fromclear)r   Zstepsstepnotifier
observabler   r   r	   __call__J   s"    
z_AddOrRemoveNotifier.__call__c          	   C   s<   x6| j jj| j D ]"}t| j|| j| j| j| jd qW dS )z~ Add or remove additional ObserverGraph contributed by the root
        observer. e.g. for handing trait_added event.
        )r   r   r   r   r   r   N)	r   nodeZiter_extra_graphsr
   r   r   r   r   r   )r   Zextra_graphr   r   r	   r   k   s    z0_AddOrRemoveNotifier._add_or_remove_extra_graphsc          
   C   sL   xF| j jD ]:}x4| j jj| jD ] }t||| j| j| j| j	d q W q
W dS )zM Recursively add or remove notifiers for the children ObserverGraph.
        )r   r   r   r   r   r   N)
r   childrenr   Ziter_objectsr   r
   r   r   r   r   )r   child_graphZnext_objectr   r   r	   r   y   s    z6_AddOrRemoveNotifier._add_or_remove_children_notifiersc             C   sx   xr| j jj| jD ]^}xX| j jD ]L}| j jj|| j| j| jd}| j	rR|j
| n
|j| | jj||f q W qW dS )z Add or remove notifiers for maintaining children notifiers when
        the objects being observed by the root observer change.
        )r   r   r   r   N)r   r   iter_observablesr   r   get_maintainerr   r   r   r   r   r   r   append)r   r   r   Zchange_notifierr   r   r	   r      s    

z/_AddOrRemoveNotifier._add_or_remove_maintainersc             C   sr   | j jjsdS x^| j jj| jD ]J}| j jj| j| j| jd}| j	rP|j
| n
|j| | jj||f q W dS )z] Add or remove user notifiers for the objects observed by the root
        observer.
        N)r   r   r   )r   r   notifyr    r   Zget_notifierr   r   r   r   r   r   r   r"   )r   r   r   r   r   r	   r      s    


z-_AddOrRemoveNotifier._add_or_remove_notifiersN)
__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r	   r   9   s   !r   N)r
   r   r   r   r   r	   <module>   s   -