a
    ©ˆcr  ã                   @   s^   d Z ddlZddlZddlmZ ddlmZmZmZ ddl	m
Z
 e e¡ZG dd„ de
ƒZdS )	zG A plugin manager that finds plugins in packages on the 'plugin_path'. é    N)ÚFile)Ú	DirectoryÚListÚon_trait_changeé   )ÚPluginManagerc                   @   sT   e Zd ZdZdZeeƒZedƒdd„ ƒZ	dd„ Z
dd	„ Zd
d„ Zdd„ Zdd„ ZdS )ÚPackagePluginManagera¸   A plugin manager that finds plugins in packages on the 'plugin_path'.

    All items in 'plugin_path' are directory names and they are all added to
    'sys.path' (if not already present). Each directory is then searched for
    plugins as follows:-

    a) If the package contains a 'plugins.py' module, then we import it and
    look for a callable 'get_plugins' that takes no arguments and returns
    a list of plugins (i.e. instances that implement 'IPlugin'!).

    b) If the package contains any modules named in the form 'xxx_plugin.py'
    then the module is imported and if it contains a callable 'XXXPlugin' it is
    called with no arguments and it must return a single plugin.

    z
plugins.pyzplugin_path[]c                 C   s   |   ||¡ |  dg¡ d S )NZ_plugins)Ú_update_sys_dot_pathZreset_traits)ÚselfÚobjZ
trait_nameÚremovedÚadded© r   úb/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/envisage/package_plugin_manager.pyÚ_update_path_and_reset_plugins2   s    z3PackagePluginManager._update_path_and_reset_pluginsc                    s&   ‡ fdd„ˆ   ¡ D ƒ}t d|¡ |S )z Trait initializer. c                    s   g | ]}ˆ   |j¡r|‘qS r   )Z_include_pluginÚid)Ú.0Zplugin©r
   r   r   Ú
<listcomp><   s   þz:PackagePluginManager.__plugins_default.<locals>.<listcomp>z)package plugin manager found plugins <%s>)Ú_harvest_plugins_in_packagesÚloggerÚdebug)r
   Úpluginsr   r   r   Z__plugins_default9   s
    
þz&PackagePluginManager.__plugins_defaultc                 C   s2   zt |d dgd�}W n ty,   d}Y n0 |S )z§ Import 'plugins.py' from the package with the given name.

        If the package does not exist, or does not contain 'plugins.py' then
        return None.

        z.pluginsr   ©ÚfromlistN)Ú
__import__ÚImportError)r
   Úpackage_nameÚmoduler   r   r   Ú_get_plugins_moduleH   s    
ÿ

z(PackagePluginManager._get_plugins_modulec                 C   sÌ   |   |¡}|dur.t|ddƒ}|durÈ|ƒ }nšg }t d| ¡ t|ƒjpLg D ]x}|jdkrN|j d¡rNt	|d |j |jgd�}|j 
d¡}d	d
„ |D ƒ}	d |	¡}
t||
dƒ}|durN| |ƒ ¡ qN|S )z- Harvest plugins found in the given package. NZget_pluginszLooking for plugins in %sz.pyZ_pluginÚ.r   Ú_c                 S   s   g | ]}|  ¡ ‘qS r   )Ú
capitalize)r   Zatomr   r   r   r   t   ó    zDPackagePluginManager._harvest_plugins_in_package.<locals>.<listcomp>Ú )r   Úgetattrr   r   r   ÚchildrenÚextÚnameÚendswithr   ÚsplitÚjoinÚappend)r
   r   Zpackage_dirnameZplugins_moduleÚfactoryr   Úchildr   ZatomsZcapitalizedZfactory_namer   r   r   Ú_harvest_plugins_in_package[   s&    
ÿ
z0PackagePluginManager._harvest_plugins_in_packagec                 C   sD   g }| j D ]4}t|ƒjpg D ] }|jr| |  |j|j¡¡ qq
|S )z7 Harvest plugins found in packages on the plugin path. )Úplugin_pathr   r&   Ú
is_packageÚextendr/   r(   Úpath)r
   r   Údirnamer.   r   r   r   r   }   s    
ÿÿz1PackagePluginManager._harvest_plugins_in_packagesc                 C   sD   |D ]}|t jv rt j |¡ q|D ]}|t jvr$t j |¡ q$dS )z- Add/remove the given entries from sys.path. N)Úsysr3   Úremover,   )r
   r   r   r4   r   r   r   r	   Œ   s    

z)PackagePluginManager._update_sys_dot_pathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZPLUGIN_MANIFESTr   r   r0   r   r   Z&_PackagePluginManager__plugins_defaultr   r/   r   r	   r   r   r   r   r      s   
"r   )r:   Úloggingr5   Zapptools.ior   Z
traits.apir   r   r   Zplugin_managerr   Ú	getLoggerr7   r   r   r   r   r   r   Ú<module>
   s   
