a
    ©ˆcß3  ã                   @   sî   d 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 "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(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   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/plugin.pyÚ_get_extension_registry^   s    zPlugin._get_extension_registryc                 C   s   | j S r   r   r   r   r   r   Ú_get_service_registryg   s    zPlugin._get_service_registryc                 C   s   dd„ | j dd� ¡ 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>s   s   ÿz/Plugin.get_extension_points.<locals>.<listcomp>T)Z__extension_point__)ÚtraitsÚvalues)r   Zextension_pointsr   r   r   Úget_extension_pointsp   s    þzPlugin.get_extension_pointsc                 C   sH   | j |d�}t|ƒdkrg }n&t|ƒdkr:|  |d ¡}n
|  |¡‚|S )z9 Return the provider's extensions to an extension point. )Úcontributes_tor   r
   )Útrait_namesÚlenÚ_get_extensions_from_traitÚ!_create_multiple_traits_exception)r   Úextension_point_idr'   Ú
extensionsr   r   r   Úget_extensionsz   s    	
zPlugin.get_extensionsc                 C   sB   t | jjdƒ}t|ƒs t |¡ t || jƒ}t|ƒs>t |¡ |S )ú 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   s4   dt | ƒjt | ƒjf }t d t | ¡|¡¡ |S )r.   z%s.%sz plugin {} has no Id - using <{}>)ÚtypeÚ
__module__Ú__name__ÚloggerÚwarningÚformatÚobjectÚ__repr__)r   r2   r   r   r   Ú_id_default¨   s    
ÿÿzPlugin._id_defaultc                 C   s*   t t| ƒjƒ}t d t | ¡|¡¡ |S )r.   z"plugin {} has no name - using <{}>)r	   r4   r6   r7   r8   r9   r:   r;   )r   Únamer   r   r   Ú_name_default³   s    
ÿÿzPlugin._name_defaultc                 C   s   dS )a2   Start the plugin.

        This method will *always* be empty so that you never have to call
        'super().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 )a/   Stop the plugin.

        This method will *always* be empty so that you never have to call
        'super().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  | ¡ 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_traitsr   r   r   r   rA   à   s    z%Plugin.connect_extension_point_traitsc                 C   s   t  | ¡ dS )z1 Disconnect all of the plugin's extension points.N)r   Ú!disconnect_extension_point_traitsr   r   r   r   rB   ê   s    z(Plugin.disconnect_extension_point_traitsc                 C   sH   | j dd� ¡ D ]2\}}t d| |f ¡ |  ||¡}| j |¡ q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#   Úitemsr7   r8   Ú_register_service_factoryÚ_service_idsÚappend)r   Ú
trait_namer!   Ú
service_idr   r   r   Úregister_servicesï   s    üÿ	zPlugin.register_servicesc              	   C   sZ   | j dd… }| ¡  |D ]4}z| j |¡ W n ty@   Y q0 | j |¡ qg | _ dS )z/ Unregister any service offered by the plugin. N)rE   Úreverser   Zget_service_from_idÚ
ValueErrorZunregister_service)r   Zservice_idsrH   r   r   r   Úunregister_services  s    zPlugin.unregister_servicesc           	      C   sl   |  d¡d }|  |¡}|jdurh| d¡r@|j}|j}|j}n|}|}tdt|ƒƒ}|  	|j|||¡ dS )z Static trait change handler. Z_itemsr   N)
Úsplitr!   r&   ÚendswithÚaddedÚremovedÚindexÚslicer(   Z_fire_extension_point_changed)	r   rG   ÚoldÚnewZbase_trait_namer!   rO   rP   rQ   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>)rK   r2   )r   r+   Ú	exceptionr   r   r   r*   8  s    ÿÿz(Plugin._create_multiple_traits_exceptionc                 C   s:   zt | |ƒ}W n& ty4   t d| |f ¡ ‚ Y n0 |S )z< Return the extensions contributed via the specified trait. z&getting extensions from %s, trait <%s>)ÚgetattrÚ	Exceptionr7   rV   )r   rG   r,   r   r   r   r)   B  s    
ÿz!Plugin._get_extensions_from_traitc                 C   s   |j du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_protocolP  s    
zPlugin._get_service_protocolc                    s&   ˆ   |¡}‡ ‡fdd„}ˆ j ||¡S )z5 Register a service factory for the specified trait. c                     s
   t ˆ ˆƒS )z A service factory. )rW   )Z
properties©r   rG   r   r   Úfactoryn  s    z1Plugin._register_service_factory.<locals>.factory)r[   r   Zregister_service)r   rG   r!   rZ   r]   r   r\   r   rD   d  s    
z Plugin._register_service_factoryc                 C   s   d  | j| j¡S )z* String representation of a Plugin object zPlugin(id={!r}, name={!r}))r9   r2   r=   r   r   r   r   r;   y  s    zPlugin.__repr__N)(r6   r5   Ú__qualname__Ú__doc__r   r   r   Z	activatorr   r   r   r/   r2   r=   r   r   Zextension_registryr   Zservice_registryr   rE   r   r   r%   r-   r3   r<   r>   r?   r@   rA   rB   rI   rL   rU   r*   r)   r[   rD   r;   r   r   r   r   r   %   s8   			


r   )&r_   Úloggingr0   Ú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   Ú	getLoggerr6   r7   r   r   r   r   r   Ú<module>
   s$   

