3
6ßL]v  ã               @   sv   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZmZ ddlmZ ejeƒZG dd„ deƒZdS )	zC A plugin manager that finds plugins in eggs on the 'plugin_path'. é    N)ÚCallableÚ	DirectoryÚListÚon_trait_changeé   )Úadd_eggs_on_pathÚget_entry_points_in_egg_order)ÚPluginManagerc               @   sl   e Zd ZdZdZeZdd„ Ze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d„ ZdS )ÚEggBasketPluginManagera†   A plugin manager that finds plugins in eggs on the 'plugin_path'.

    To declare a plugin (or plugins) in your egg use an entry point in your
    'setup.py' file, e.g.

    [envisage.plugins]
    acme.foo = acme.foo.foo_plugin:FooPlugin
    acme.foo.fred = acme.foo.fred.fred_plugin:FredPlugin

    The left hand side of the entry point declaration MUST be the same as the
    'id' trait of the plugin (e.g. the 'FooPlugin' would have its 'id' trait
    set to 'acme.foo'). This allows the plugin manager to filter out plugins
    using the 'include' and 'exclude' lists (if specified) *without* having to
    import and instantiate them.

    zenvisage.pluginsc             C   s   dd„ }|S )Nc             S   s   |‚d S )N© )Úentry_pointÚexcr   r   úH/tmp/pip-install-5sebvq2j/envisage/envisage/egg_basket_plugin_manager.pyÚhandle_broken_plugin,   s    zNEggBasketPluginManager._on_broken_plugin_default.<locals>.handle_broken_pluginr   )Úselfr   r   r   r   Ú_on_broken_plugin_default+   s    z0EggBasketPluginManager._on_broken_plugin_defaultzplugin_path[]c             C   s   | j ||ƒ | jdgƒ d S )NZ_plugins)Ú_update_sys_dot_pathZreset_traits)r   ÚobjZ
trait_nameÚremovedÚaddedr   r   r   Ú_plugin_path_changed8   s    z+EggBasketPluginManager._plugin_path_changedc             C   s   | j | jƒ}tjd|ƒ |S )z Trait initializer. z,egg basket plugin manager found plugins <%s>)Ú_harvest_plugins_in_eggsÚapplicationÚloggerÚdebug)r   Úpluginsr   r   r   Z__plugins_default?   s    z(EggBasketPluginManager.__plugins_defaultc             C   s8   |j ƒ }||d�}|j|jkr4tjd|j|jf ƒ |S )z& Create a plugin from an entry point. )r   z>entry point name <%s> should be the same as the plugin id <%s>)ÚloadÚnameÚidr   Úwarn)r   Úepr   ÚklassÚpluginr   r   r   Ú_create_plugin_from_entry_pointJ   s    
z6EggBasketPluginManager._create_plugin_from_entry_pointc             C   s   t || jƒ}|S )z4 Return all plugin entry points in the working set. )r   ÚENVISAGE_PLUGINS_ENTRY_POINT)r   Úworking_setÚentry_pointsr   r   r   Ú_get_plugin_entry_pointsZ   s    
z/EggBasketPluginManager._get_plugin_entry_pointsc       	      C   sÆ   t j| jƒ}t|| j| jƒ tt j| j| jƒ g }xŽ| j|ƒD ]€}| j|jƒr>y| j	||ƒ}|j
|ƒ W q> tk
r¼ } z8tjƒ }d|j|jj|f }tj|ƒ | j||ƒ W Y dd}~X q>X q>W |S )z3 Harvest plugins found in eggs on the plugin path. z%Error loading plugin: %s (from %s)
%sN)Úpkg_resourcesÚ
WorkingSetÚplugin_pathr   Ú_handle_broken_distributionsr%   r'   Z_include_pluginr   r#   ÚappendÚ	ExceptionÚ	tracebackÚ
format_excÚdistÚlocationr   ÚerrorÚon_broken_plugin)	r   r   Zplugin_working_setr   r   r"   r   Úexc_tbÚmsgr   r   r   r   c   s&    

"z/EggBasketPluginManager._harvest_plugins_in_eggsc             C   sJ   t jd|ƒ | jd kr$td| ƒ‚n"x |jƒ D ]\}}| j||ƒ q.W d S )NzError loading distributions: %szCannot find eggs %s)r   r2   Úon_broken_distributionÚSystemErrorÚitems)r   Úerrorsr0   r   r   r   r   r+   ‚   s
    
z3EggBasketPluginManager._handle_broken_distributionsc             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)ÚsysÚpathÚremover,   )r   r   r   Údirnamer   r   r   r   Š   s    



z+EggBasketPluginManager._update_sys_dot_pathN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r   r3   r   r6   r   r   r*   r   r   Z(_EggBasketPluginManager__plugins_defaultr#   r'   r   r+   r   r   r   r   r   r
      s   	r
   )rA   Úloggingr(   r:   r.   Z
traits.apir   r   r   r   Z	egg_utilsr   r   Zplugin_managerr	   Ú	getLoggerr>   r   r
   r   r   r   r   Ú<module>   s   
