3
6ßL]û  ã               @   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j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   | j ||ƒ | jdgƒ d S )NZ_plugins)Ú_update_sys_dot_pathZreset_traits)ÚselfÚobjZ
trait_nameÚremovedÚadded© r   úE/tmp/pip-install-5sebvq2j/envisage/envisage/package_plugin_manager.pyÚ_plugin_path_changed(   s    z)PackagePluginManager._plugin_path_changedc                s&   ‡ fdd„ˆ j ƒ D ƒ}tjd|ƒ |S )z Trait initializer. c                s   g | ]}ˆ j |jƒr|‘qS r   )Z_include_pluginÚid)Ú.0Zplugin)r
   r   r   ú
<listcomp>3   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_default/   s    z&PackagePluginManager.__plugins_defaultc             C   s4   yt |d dgd�}W n tk
r.   d}Y nX |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_module>   s
    
z(PackagePluginManager._get_plugins_modulec             C   sÐ   | j |ƒ}|dk	r.t|ddƒ}|dk	rÌ|ƒ }nžg }tjd| ƒ xŠt|ƒjpNg D ]x}|jdkrP|jjdƒrPt	|d |j |jgd�}|jj
dƒ}d	d
„ |D ƒ}	dj|	ƒ}
t||
dƒ}|dk	rP|j|ƒ ƒ qPW |S )z- Harvest plugins found in the given package. NZget_pluginszLooking for plugins in %sz.pyZ_pluginÚ.)r   Ú_c             S   s   g | ]}|j ƒ ‘qS r   )Ú
capitalize)r   Zatomr   r   r   r   h   s    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_packageO   s$    

z0PackagePluginManager._harvest_plugins_in_packagec             C   sL   g }xB| j D ]8}x2t|ƒjpg D ] }|jr |j| j|j|jƒƒ q W qW |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   q   s    z1PackagePluginManager._harvest_plugins_in_packagesc             C   sL   x"|D ]}|t jkrt jj|ƒ qW x"|D ]}|t jkr*t jj|ƒ q*W dS )z- Add/remove the given entries from sys.path. N)Úsysr0   Úremover)   )r
   r   r   r1   r   r   r   r	   €   s    



z)PackagePluginManager._update_sys_dot_pathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZPLUGIN_MANIFESTr   r   r-   r   r   Z&_PackagePluginManager__plugins_defaultr   r,   r   r	   r   r   r   r   r      s   "r   )r7   Úloggingr2   Zapptools.ior   Z
traits.apir   r   r   Zplugin_managerr   Ú	getLoggerr4   r   r   r   r   r   r   Ú<module>   s   
