a
    ©ˆc  ã                   @   sX   d Z ddlZddlZddlmZmZmZmZ ddlm	Z	 dZ
ee	ƒG dd„ deƒƒZdS )	z; A trait type used to declare and access extension points. é    N)ÚListÚ	TraitTypeÚ	UndefinedÚprovidesé   )ÚIExtensionPointzVextension points must be "List"s e.g. List, List(Int) but a value of %s was specified.c                       sz   e Zd ZdZedd„ ƒZedd„ ƒZedd„ ƒZedf‡ fd	d
„	Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚExtensionPointzÁ A trait type used to declare and access extension points.

    Note that this is a trait *type* and hence does *NOT* have traits itself
    (i.e. it does *not* inherit from 'HasTraits').

    c                 C   s   ddl m} || ||ƒS )z) Create a binding to an extension point. r   )Úbind_extension_point)Zextension_point_bindingr	   )ÚobjÚ
trait_nameZextension_point_idr	   © r   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/extension_point.pyÚbind.   s    zExtensionPoint.bindc                 C   s,   | j dd� ¡ D ]\}}|j | |¡ qdS )z: Connect all of the 'ExtensionPoint' traits on an object. T©Ú__extension_point__N)ÚtraitsÚitemsÚ
trait_typeÚconnect©r
   r   Ztraitr   r   r   Úconnect_extension_point_traits6   s    z-ExtensionPoint.connect_extension_point_traitsc                 C   s,   | j dd� ¡ D ]\}}|j | |¡ qdS )z= Disconnect all of the 'ExtensionPoint' traits on an object. Tr   N)r   r   r   Ú
disconnectr   r   r   r   Ú!disconnect_extension_point_traits=   s    z0ExtensionPoint.disconnect_extension_point_traitsNc                    sj   d|d< t ƒ jf i |¤Ž t |¡r*|ƒ }t|tƒs@tt| ƒ‚|| _|du rVt	dƒ‚|| _
t ¡ | _dS )z Constructor. Tr   Nz"an extension point must have an Id)ÚsuperÚ__init__ÚinspectÚisclassÚ
isinstancer   Ú	TypeErrorÚINVALID_TRAIT_TYPEr   Ú
ValueErrorÚidÚweakrefÚWeakKeyDictionaryÚ_obj_to_listeners_map)Úselfr   r!   Úmetadata©Ú	__class__r   r   r   H   s    

zExtensionPoint.__init__c                 C   s   d  | j¡S )z3 String representation of an ExtensionPoint object zExtensionPoint(id={!r}))Úformatr!   )r%   r   r   r   Ú__repr__l   s    zExtensionPoint.__repr__c                 C   s&   |   |¡}| | j¡}| j |||¡S )z Trait type getter. )Ú_get_extension_registryZget_extensionsr!   r   Úvalidate)r%   r
   r   Úextension_registryÚ
extensionsr   r   r   Úgett   s    
zExtensionPoint.getc                 C   s   |   |¡}| | j|¡ dS )z Trait type setter. N)r+   Zset_extensionsr!   )r%   r
   ÚnameÚvaluer-   r   r   r   Úset   s    
zExtensionPoint.setc                    s@   ‡ ‡fdd„}|   ˆ ¡}| || j¡ | j ˆ i ¡}||ˆ< dS )až   Connect the extension point to a trait on an object.

        This allows the object to react when contributions are added or
        removed from the extension point.

        fixme: It would be nice to be able to make the connection automatically
        but we would need a slight tweak to traits to allow the trait type to
        be notified when a new instance that uses the trait type is created.

        c                    s>   |j durˆd }t}|}nˆ}|j}|j}ˆ  |||¡ dS )z5 Listener called when an extension point is changed. NZ_items)Úindexr   ÚremovedÚaddedZtrait_property_changed)r-   Úeventr0   ÚoldÚnew©r
   r   r   r   Úlistener™   s    
z(ExtensionPoint.connect.<locals>.listenerN)r+   Zadd_extension_point_listenerr!   r$   Ú
setdefault)r%   r
   r   r:   r-   Z	listenersr   r9   r   r   �   s
    
zExtensionPoint.connectc                 C   s@   |   |¡}| j|  |¡}|dur<| || j¡ | j| |= dS )z; Disconnect the extension point from a trait on an object. N)r+   r$   r/   Zremove_extension_point_listenerr!   )r%   r
   r   r-   r:   r   r   r   r   ´   s    
ÿzExtensionPoint.disconnectc                 C   s&   t |ddƒ}|du r"td| j ƒ‚|S )z8 Return the extension registry in effect for an object. r-   Nz¨The "ExtensionPoint" trait type can only be used in objects that have a reference to an extension registry via their "extension_registry" trait. Extension point Id <%s>)Úgetattrr    r!   )r%   r
   r-   r   r   r   r+   Ç   s    ýÿz&ExtensionPoint._get_extension_registry)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r   r*   r/   r2   r   r   r+   Ú__classcell__r   r   r'   r   r   !   s   



$'r   )r@   r   r"   Z
traits.apir   r   r   r   Zi_extension_pointr   r   r   r   r   r   r   Ú<module>
   s   ÿ