3
6ßL]v9  ã               @   sö   d Z ddlZddlZddlZddlmZmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ej#e$ƒZ%eeee ƒG dd„ deƒƒZ&dS )z8 The default implementation of the 'IPlugin' interface. é    N)ÚexistsÚjoin)ÚInstanceÚListÚPropertyÚStrÚprovides)Úcamel_case_to_wordsé   )ÚExtensionPoint)ÚExtensionProvider)ÚIApplication)ÚIExtensionPointUser)ÚIExtensionRegistry)ÚIPlugin)ÚIPluginActivator)ÚIServiceRegistry)ÚIServiceUser)ÚPluginActivatorc               @   sì   e Zd ZdZeeeƒ ƒZeeƒZ	e
Ze
Ze
ZeeeƒƒZeeeƒƒZeZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dd„ Z#d d!„ Z$d"d#„ Z%d$d%„ Z&d&d'„ Z'd(d)„ Z(d*S )+ÚPluginz‹ The default implementation of the 'IPlugin' interface.

    This class is intended to be subclassed for each plugin that you create.

    c             C   s   | j S )z Trait property getter. )Úapplication)Úself© r   ú5/tmp/pip-install-5sebvq2j/envisage/envisage/plugin.pyÚ_get_extension_registryT   s    zPlugin._get_extension_registryc             C   s   | j S )z Trait property getter. )r   )r   r   r   r   Ú_get_service_registry]   s    zPlugin._get_service_registryc             C   s   dd„ | j dd�jƒ D ƒ}|S )z6 Return the extension points offered by the provider. c             S   s   g | ]
}|j ‘qS r   )Ú
trait_type)Ú.0Útraitr   r   r   ú
<listcomp>j   s   z/Plugin.get_extension_points.<locals>.<listcomp>T)Ú__extension_point__)ÚtraitsÚvalues)r   Zextension_pointsr   r   r   Úget_extension_pointsf   s    zPlugin.get_extension_pointsc             C   s„   | j |d�}t|ƒdkr,d| }| j |d�}t|ƒdkrZ| j|ƒ}|s€d| }| j|ƒ}n&t|ƒdkrv| j|d ƒ}n
| j|ƒ‚|S )z9 Return the provider's extensions to an extension point. )Úcontributes_tor   z
enthought.r
   )Útrait_namesÚlenÚ_harvest_methodsÚ_get_extensions_from_traitÚ!_create_multiple_traits_exception)r   Úextension_point_idr%   Zold_idÚ
extensionsr   r   r   Úget_extensionsq   s    	

zPlugin.get_extensionsc             C   sB   t | jjdƒ}t|ƒs tj|ƒ t || jƒ}t|ƒs>tj|ƒ |S )z Trait initializer. Zplugins)r   r   Úhomer   ÚosÚmkdirÚid)r   Zplugins_dirZhome_dirr   r   r   Ú_home_defaultŸ   s    

zPlugin._home_defaultc             C   s.   dt | ƒjt | ƒjf }tjd| |f ƒ |S )z Trait initializer. z%s.%sz plugin %s has no Id - using <%s>)ÚtypeÚ
__module__Ú__name__ÚloggerÚwarn)r   r0   r   r   r   Ú_id_default±   s    zPlugin._id_defaultc             C   s$   t t| ƒjƒ}tjd| |f ƒ |S )z Trait initializer. z"plugin %s has no name - using <%s>)r	   r2   r4   r5   r6   )r   Únamer   r   r   Ú_name_default¹   s    zPlugin._name_defaultc             C   s   dS )a;   Start the plugin.

        This method will *always* be empty so that you never have to call
        'super(xxx, self).start()' if you provide an implementation in a
        derived class.

        The framework does what it needs to do when it starts a plugin by means
        of the plugin's activator.

        Nr   )r   r   r   r   ÚstartÃ   s    zPlugin.startc             C   s   dS )a8   Stop the plugin.

        This method will *always* be empty so that you never have to call
        'super(xxx, self).stop()' if you provide an implementation in a
        derived class.

        The framework does what it needs to do when it stops a plugin by means
        of the plugin's activator.

        Nr   )r   r   r   r   ÚstopÑ   s    zPlugin.stopc             C   s   t j| ƒ dS )z£ Connect all of the plugin's extension points.

        This means that the plugin will be notified if and when contributions
        are add or removed.

        N)r   Úconnect_extension_point_traits)r   r   r   r   r<   ã   s    
z%Plugin.connect_extension_point_traitsc             C   s   t j| ƒ dS )z1 Disconnect all of the plugin's extension points.N)r   Ú!disconnect_extension_point_traits)r   r   r   r   r=   ï   s    
z(Plugin.disconnect_extension_point_traitsc             C   sL   xF| j dd�jƒ D ]2\}}tjd| |f ƒ | j||ƒ}| jj|ƒ qW dS )z. Register the services offered by the plugin. T)ZservicezÇDEPRECATED: Do not use the "service=True" metadata anymore. Services should now be offered using the service offer extension point (envisage.service_offers) from the core plugin. Plugin %s trait <%s>N)r!   Úitemsr5   r6   Ú_register_service_factoryÚ_service_idsÚappend)r   Ú
trait_namer   Ú
service_idr   r   r   Úregister_servicesö   s    zPlugin.register_servicesc             C   s:   | j dd… }|jƒ  x|D ]}| jj|ƒ qW g | _ dS )z/ Unregister any service offered by the plugin. N)r@   Úreverser   Zunregister_service)r   Zservice_idsrC   r   r   r   Úunregister_services  s    
zPlugin.unregister_servicesc       	      C   sv   |j dƒd }| j|ƒ}|jdk	rr|jdƒr@|j}|j}|j}n |}|}tdtt	|ƒt	|ƒƒƒ}| j
|j|||ƒ dS )z Static trait change handler. Z_itemsr   N)Úsplitr   r$   ÚendswithÚaddedÚremovedÚindexÚsliceÚmaxr&   Z_fire_extension_point_changed)	r   rB   ÚoldÚnewZbase_trait_namer   rI   rJ   rK   r   r   r   Ú_anytrait_changed!  s    


zPlugin._anytrait_changedc             C   s   t d|| jf ƒ}|S )z= Create the exception raised when multiple traits are found. z7multiple traits for extension point <%s> in plugin <%s>)Ú
ValueErrorr0   )r   r*   Ú	exceptionr   r   r   r)   ?  s    z(Plugin._create_multiple_traits_exceptionc          
   C   s4   yt | |ƒ}W n    tjd| |f ƒ ‚ Y nX |S )z< Return the extensions contributed via the specified trait. z&getting extensions from %s, trait <%s>)Úgetattrr5   rR   )r   rB   r+   r   r   r   r(   J  s    z!Plugin._get_extensions_from_traitc             C   s   |j dk	r|j }n|jj}|S )z: Determine the protocol to register a service trait with. N)Zservice_protocolr   Úklass)r   r   Úprotocolr   r   r   Ú_get_service_protocolX  s    
zPlugin._get_service_protocolc             C   sR   g }xH| j | ƒD ]:}t| |ƒ}| j||ƒr|ƒ }t|tƒs@|g}|j|ƒ qW |S )z) Harvest all method-based contributions. )Z_each_trait_methodrS   Ú_is_extension_methodÚ
isinstanceÚlistÚextend)r   r*   r+   r8   ÚvalueÚresultr   r   r   r'   l  s    

zPlugin._harvest_methodsc             C   s   t j|ƒo|t|ddƒk}|S )aè   Return True if the value is an extension method.

        i.e. If the method is one that makes a contribution to the extension
        point. Currently there is exactly one way to make a method make a
        contribution, and that is to mark it using the 'contributes_to'
        decorator, e.g::

          @contributes_to('acme.motd.messages')
          def get_messages(self):
              ...
              messages = [...]
              ...
              return messages

        r    N)ÚinspectÚismethodrS   )r   r[   r*   Zis_extension_methodr   r   r   rW   ~  s    
zPlugin._is_extension_methodc                s&   ˆ j |ƒ}‡ ‡fdd„}ˆ jj||ƒS )z5 Register a service factory for the specified trait. c                 s
   t ˆ ˆƒS )z A service factory. )rS   )Z
properties)r   rB   r   r   Úfactoryž  s    z1Plugin._register_service_factory.<locals>.factory)rV   r   Zregister_service)r   rB   r   rU   r_   r   )r   rB   r   r?   ”  s    
z Plugin._register_service_factoryN))r4   r3   Ú__qualname__Ú__doc__r   r   r   Z	activatorr   r   r   r-   r0   r8   r   r   Zextension_registryr   Zservice_registryr   r@   r   r   r#   r,   r1   r7   r9   r:   r;   r<   r=   rD   rF   rP   r)   r(   rV   r'   rW   r?   r   r   r   r   r      s:   			.
r   )'ra   r]   Úloggingr.   Úos.pathr   r   Z
traits.apir   r   r   r   r   Ztraits.util.camel_caser	   Zextension_pointr   Zextension_providerr   Zi_applicationr   Zi_extension_point_userr   Zi_extension_registryr   Zi_pluginr   Zi_plugin_activatorr   Zi_service_registryr   Zi_service_userr   Zplugin_activatorr   Ú	getLoggerr4   r5   r   r   r   r   r   Ú<module>   s"   
