a
    ©ˆcÄ  ã                   @   s’   d Z ddlmZ ddlZddlmZmZ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 e e¡Ze	eƒG d
d„ deƒƒZdS )z) A simple plugin manager implementation. é    )ÚfnmatchN)ÚEventÚ	HasTraitsÚInstanceÚListÚobserveÚprovidesÚStré   )ÚIApplication)ÚIPlugin)ÚIPluginManager)ÚPluginEventc                       sæ   e Zd ZdZeeƒZeeƒZee	ƒZ
edƒdd„ ƒZeeƒZeeƒZd&‡ fdd„	Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zd'dd„Zdd„ Zd(dd„ZeeƒZedƒdd„ ƒZedƒdd„ ƒZdd„ Zd d!„ Zd"d#„ Z d$d%„ Z!‡  Z"S ))ÚPluginManageraE   A simple plugin manager implementation.

    This implementation manages an explicit collection of plugin instances,
    e.g::

        plugin_manager = PluginManager(plugins=[MyPlugin(), YourPlugin()])

    Plugins can be added and removed after construction time via the methods
    'add_plugin' and 'remove_plugin'.

    Úapplicationc                 C   s   |   g | j¡ dS ©z Static trait change handler. N)Ú_update_application_on_pluginsÚ_plugins©ÚselfÚevent© r   úZ/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/plugin_manager.pyÚ#_set_new_application_on_all_plugins6   s    z1PluginManager._set_new_application_on_all_pluginsNc                    s$   t ƒ jf i |¤Ž |dur || _dS )a|   Constructor.

        We allow the caller to specify an initial list of plugins, but the
        list itself is not part of the public API. To add and remove plugins
        after construction, use the 'add_plugin' and 'remove_plugin' methods
        respectively. The manager is also iterable, so to iterate over the
        plugins use 'for plugin in plugin_manager'.

        N)ÚsuperÚ__init__r   )r   ÚpluginsZtraits©Ú	__class__r   r   r   K   s    zPluginManager.__init__c                    s   ‡ fdd„ˆ j D ƒ}t|ƒS )z0 Return an iterator over the manager's plugins. c                    s   g | ]}ˆ   |j¡r|‘qS r   )Ú_include_pluginÚid)Ú.0Úplugin©r   r   r   Ú
<listcomp>^   s   þz*PluginManager.__iter__.<locals>.<listcomp>)r   Úiter)r   r   r   r#   r   Ú__iter__[   s    
þzPluginManager.__iter__c                 C   s   | j  |¡ t|d�| _dS )z Add a plugin to the manager. ©r"   N)r   Úappendr   Úplugin_added©r   r"   r   r   r   Ú
add_pluginh   s    zPluginManager.add_pluginc                 C   s2   | j D ]"}||jkr|  |j¡s$d} q.qd}|S )z* Return the plugin with the specified Id. N)r   r    r   )r   Ú	plugin_idr"   r   r   r   Ú
get_pluginn   s    

zPluginManager.get_pluginc                 C   s   | j  |¡ t|d�| _dS )z# Remove a plugin from the manager. r'   N)r   Úremover   Úplugin_removedr*   r   r   r   Úremove_plugin}   s    zPluginManager.remove_pluginc                 C   s   | j D ]}|  |¡ qdS )z Start the plugin manager. N)r   Ústart_pluginr*   r   r   r   Ústartƒ   s    
zPluginManager.startc                 C   sP   |p|   |¡}|dur@t d|j¡ |j |¡ t d|j¡ ntd| ƒ‚dS )z Start the specified plugin. Nzplugin %s startingzplugin %s startedúno such plugin %s)r-   ÚloggerÚdebugr    Ú	activatorr1   ÚSystemError©r   r"   r,   r   r   r   r1   ‰   s    zPluginManager.start_pluginc                 C   s.   | j dd… }| ¡  |D ]}|  |¡ qdS )z Stop the plugin manager. N)r   ÚreverseÚstop_plugin)r   Z
stop_orderr"   r   r   r   Ústop•   s    zPluginManager.stopc                 C   sP   |p|   |¡}|dur@t d|j¡ |j |¡ t d|j¡ ntd| ƒ‚dS )z Stop the specified plugin. Nzplugin %s stoppingzplugin %s stoppedr3   )r-   r4   r5   r    r6   r:   r7   r8   r   r   r   r:   Ÿ   s    zPluginManager.stop_pluginr   c                 C   s   |j |j }}|  ||¡ dS r   )ÚoldÚnewr   )r   r   r<   r=   r   r   r   Ú"_update_application_on_all_plugins°   s    z0PluginManager._update_application_on_all_pluginsz_plugins:itemsc                 C   s   |   |j|j¡ dS r   )r   ÚremovedÚaddedr   r   r   r   Ú&_update_application_on_changed_plugins¶   s    z4PluginManager._update_application_on_changed_pluginsc                 C   s   |   |¡o|  |¡ S )z½ Return True if the plugin should be included.

        This is just shorthand for:-

        if self._is_included(plugin_id) and not self._is_excluded(plugin_id):
            ...

        )Ú_is_includedÚ_is_excluded)r   r,   r   r   r   r   ¼   s    
ÿzPluginManager._include_pluginc                 C   s2   t | jƒdkrdS | jD ]}t||ƒr dS qdS )zŸ Return True if the plugin Id is excluded.

        If no 'exclude' patterns are specified then this method returns False
        for all plugin Ids.

        r   FT)ÚlenÚexcluder   ©r   r,   Úpatternr   r   r   rC   Ì   s    

zPluginManager._is_excludedc                 C   s2   t | jƒdkrdS | jD ]}t||ƒr dS qdS )zž Return True if the plugin Id is included.

        If no 'include' patterns are specified then this method returns True
        for all plugin Ids.

        r   TF)rD   Úincluder   rF   r   r   r   rB   Ý   s    

zPluginManager._is_includedc                 C   s&   |D ]
}d|_ q|D ]}| j |_ qdS )z: Update the 'application' trait of plugins added/removed. N)r   )r   r?   r@   r"   r   r   r   r   î   s    z,PluginManager._update_application_on_plugins)N)NN)NN)#Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r)   r/   r   r   r   r   r   r   r	   rE   rH   r   r&   r+   r-   r0   r2   r1   r;   r:   r   r   r>   rA   r   rC   rB   r   Ú__classcell__r   r   r   r   r      s4   
	




r   )rL   r   ÚloggingZ
traits.apir   r   r   r   r   r   r	   Zi_applicationr   Zi_pluginr   Zi_plugin_managerr   Zplugin_eventr   Ú	getLoggerrI   r4   r   r   r   r   r   Ú<module>
   s   $
