a
    ©ˆcÇ  ã                   @   s‚   d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ e e¡Zdd	„ ZeeƒG d
d„ deƒƒZdS )ú5 A base class for extension registry implementation. é    N)ÚDictÚ	HasTraitsÚprovidesé   ©ÚExtensionPointChangedEvent)ÚIExtensionRegistry)ÚUnknownExtensionPointc                 C   s$   t | tjƒrt | ¡S t | ¡S dS )aM  
    Weak reference for a (possibly bound method) listener.

    Returns a weakref.WeakMethod reference for bound methods,
    and a regular weakref.ref otherwise.

    This means that for example ``_saferef(myobj.mymethod)``
    returns a reference whose lifetime is connected to the
    lifetime of the object ``myobj``, rather than the lifetime
    of the temporary method ``myobj.mymethod``.

    Parameters
    ----------
    listener : callable
        Listener to return a weak reference for. This can be
        either a plain function, a bound method, or some other
        form of callable.

    Returns
    -------
    weakref.ref
        A weak reference to the listener. This will be a ``weakref.WeakMethod``
        object if the listener is an instance of ``types.MethodType``, and a
        plain ``weakref.ref`` otherwise.

    N)Ú
isinstanceÚtypesÚ
MethodTypeÚweakrefÚ
WeakMethodÚref)Úlistener© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/extension_registry.pyÚ_saferef   s    
r   c                   @   s€   e Zd ZdZeZeZeZddd„Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚExtensionRegistryr   Nc                 C   s    | j  |g ¡}| t|ƒ¡ dS )z7 Add a listener for extensions being added or removed. N)Ú
_listenersÚ
setdefaultÚappendr   ©Úselfr   Úextension_point_idZ	listenersr   r   r   Úadd_extension_point_listener_   s    z.ExtensionRegistry.add_extension_point_listenerc                 C   s   || j |j< t d|j¡ dS )z Add an extension point. zextension point <%s> addedN)Ú_extension_pointsÚidÚloggerÚdebug)r   Zextension_pointr   r   r   Úadd_extension_pointe   s    z%ExtensionRegistry.add_extension_pointc                 C   s   |   |¡dd… S )z: Return the extensions contributed to an extension point. N)Ú_get_extensions©r   r   r   r   r   Úget_extensionsk   s    z ExtensionRegistry.get_extensionsc                 C   s   | j  |¡S )z3 Return the extension point with the specified Id. )r   Úgetr#   r   r   r   Úget_extension_pointp   s    z%ExtensionRegistry.get_extension_pointc                 C   s   t | j ¡ ƒS )z Return all extension points. )Úlistr   Úvalues)r   r   r   r   Úget_extension_pointsu   s    z&ExtensionRegistry.get_extension_pointsc                 C   s    | j  |g ¡}| t|ƒ¡ dS )z: Remove a listener for extensions being added or removed. N)r   r   Úremover   r   r   r   r   Úremove_extension_point_listenerz   s    z1ExtensionRegistry.remove_extension_point_listenerc                 C   s`   |   |¡ | j|= || jv r0| j| }| j|= ng }|  |¡}|  ||g |d¡ t d|¡ dS )z Remove an extension point. r   zextension point <%s> removedN)Ú_check_extension_pointr   Ú_extensionsÚ_get_listener_refsÚ_call_listenersr   r    )r   r   ÚoldÚrefsr   r   r   Úremove_extension_point‚   s    




z(ExtensionRegistry.remove_extension_pointc                 C   s>   |   |¡ |  |¡}|| j|< |  |¡}|  ||||d¡ dS )z7 Set the extensions contributed to an extension point. N)r,   r"   r-   r.   r/   )r   r   Ú
extensionsr0   r1   r   r   r   Úset_extensions—   s
    



z ExtensionRegistry.set_extensionsc           	      C   s6   t ||||d�}|D ]}|ƒ }|dur|| |ƒ qdS )z: Call listeners that are listening to an extension point. )r   ÚaddedÚremovedÚindexNr   )	r   r1   r   r5   r6   r7   Úeventr   r   r   r   r   r/   ¦   s    üz!ExtensionRegistry._call_listenersc                 C   s   || j vrt|ƒ‚dS )zp Check to see if the extension point exists.

        Raise an 'UnknownExtensionPoint' if it does not.

        N)r   r
   r#   r   r   r   r,   µ   s    
z(ExtensionRegistry._check_extension_pointc                 C   s   | j  |g ¡S )z6 Return the extensions for the given extension point. )r-   r   r#   r   r   r   r"   ¿   s    z!ExtensionRegistry._get_extensionsc                 C   s0   g }|  | j |g ¡¡ |  | j dg ¡¡ |S )a   Get weak references to all listeners to an extension point.

        Returns a list containing the weak references to those listeners that
        are listening to this extension point specifically first, followed by
        those that are listening to any extension point.

        N)Úextendr   r%   )r   r   r1   r   r   r   r.   Ä   s    	z$ExtensionRegistry._get_listener_refs)N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r-   r   r   r   r!   r$   r&   r)   r+   r2   r4   r/   r,   r"   r.   r   r   r   r   r   @   s"   
 ÿ

r   )r=   Úloggingr   r   Z
traits.apir   r   r   Zextension_point_changed_eventr   Zi_extension_registryr	   Zunknown_extension_pointr
   Ú	getLoggerr:   r   r   r   r   r   r   r   Ú<module>
   s   
!