3
6ßL]$  ã               @   s`   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d„ 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é   )ÚIExtensionPointc                s   ‡ fdd„}|S )aV   A factory for extension point decorators!

    As an alternative to making contributions via traits, you can use this
    decorator to mark any method on a 'Plugin' as contributing to an extension
    point (note this is *only* used on 'Plugin' instances!).

    e.g. Using a trait you might have something like::

        class MyPlugin(Plugin):
            messages = List(contributes_to='acme.messages')

            def _messages_default(self):
                return ['Hello', 'Hola']

    whereas, using the decorator, it would be::

        class MyPlugin(Plugin):
            @contributes_to('acme.messages')
            def _get_messages(self):
                return ['Hello', 'Hola']

    There is not much in it really, but the decorator version looks a little
    less like 'magic' since it doesn't require the developer to know about
    Traits default initializers. However, if you know that you will want to
    dynamically change your contributions then use the trait version  because
    all you have to do is change the value of the trait and the framework will
    react accordingly.

    c                s
   ˆ | _ | S )z< A decorator for marking methods as extension contributors. )Ú__extension_point__)Úfn)Úid© ú>/tmp/pip-install-5sebvq2j/envisage/envisage/extension_point.pyÚ	decorator.   s    z!contributes_to.<locals>.decoratorr   )r
   r   r   )r
   r   Úcontributes_to   s    r   zVextension points must be "List"s e.g. List, List(Int) but a value of %s was specified.c                   sr   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‡  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   r   r   ÚbindL   s    zExtensionPoint.bindc             C   s0   x*| j dd�jƒ D ]\}}|jj| |ƒ qW dS )z: Connect all of the 'ExtensionPoint' traits on an object. T)r   N)ÚtraitsÚitemsÚ
trait_typeÚconnect)r   r   Útraitr   r   r   Úconnect_extension_point_traitsT   s    z-ExtensionPoint.connect_extension_point_traitsc             C   s0   x*| j dd�jƒ D ]\}}|jj| |ƒ qW dS )z= Disconnect all of the 'ExtensionPoint' traits on an object. T)r   N)r   r   r   Ú
disconnect)r   r   r   r   r   r   Ú!disconnect_extension_point_traits]   s    z0ExtensionPoint.disconnect_extension_point_traitsNc                sj   d|d< t t| ƒjf |Ž tj|ƒr*|ƒ }t|tƒs@tt| ƒ‚|| _	|dkrVt
dƒ‚|| _tjƒ | _dS )z Constructor. Tr   Nz"an extension point must have an Id)Úsuperr   Ú__init__ÚinspectÚisclassÚ
isinstancer   Ú	TypeErrorÚINVALID_TRAIT_TYPEr   Ú
ValueErrorr
   ÚweakrefÚWeakKeyDictionaryÚ_obj_to_listeners_map)Úselfr   r
   Úmetadata)Ú	__class__r   r   r   j   s    


zExtensionPoint.__init__c             C   s&   | j |ƒ}|j| jƒ}| jj|||ƒS )z Trait type getter. )Ú_get_extension_registryZget_extensionsr
   r   Úvalidate)r'   r   r   Úextension_registryÚ
extensionsr   r   r   Úget”   s    
zExtensionPoint.getc             C   s   | j |ƒ}|j| 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|| jƒ | 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k	rˆd }t}|}nˆ}|j}|j}ˆ j|||ƒ dS )z5 Listener called when an extension point is changed. NZ_items)Úindexr   ÚremovedÚaddedZtrait_property_changed)r,   Úeventr/   ÚoldÚnew)r   r   r   r   Úlistener»   s    
z(ExtensionPoint.connect.<locals>.listenerN)r*   Zadd_extension_point_listenerr
   r&   Ú
setdefault)r'   r   r   r8   r,   Z	listenersr   )r   r   r   r   ¯   s    
zExtensionPoint.connectc             C   s@   | j |ƒ}| j| j|ƒ}|dk	r<|j|| 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,   r8   r   r   r   r   Ú   s    

zExtensionPoint.disconnectc          	   C   sL   t |ddƒ}|dkrHyddlm} |dƒ W n   Y nX td| j ƒ‚|S )z8 Return the extension registry in effect for an object. r,   Nr   )Úlog_called_fromé
   z¨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>)ÚgetattrZetsdevtools.debug.apir:   r#   r
   )r'   r   r,   r:   r   r   r   r*   ï   s    
z&ExtensionPoint._get_extension_registry)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r   r   r   r   r.   r1   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   r   Ú<module>   s   *