o
    ïØ{fU  ã                   @   s€   d dl mZmZmZ eZdgZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlmZ dZeeu r5eneZG dd„ dƒZdS )é    )Úabsolute_importÚprint_functionÚunicode_literalsÚPluginManagerN)ÚPluginú c                   @   sF   e Zd ZdZdd„ Zdd„ Zedd„ ƒZdd	„ Zd
d„ Z	ddd„Z
dS )r   z‰Find and load plugins.

    Plugins are stored in files named '*_plugin.py' in the list of directories
    given to the constructor.
    c                 C   s6   || _ t|tƒrtd t|ƒ¡ƒ‚t|ƒ| _d | _d S )NzExpected sequence, got {})	Ú_appÚ
isinstanceÚ	STR_TYPESÚ	TypeErrorÚformatÚtypeÚlistÚ_plugin_dirsÚ_plugins)ÚselfÚappÚplugin_dirs© r   ú</usr/lib/python3/dist-packages/janitor/plugincore/manager.pyÚ__init__.   s   
ÿ

zPluginManager.__init__c                 c   s¤   � | j D ]K}zdd„ t |¡D ƒ}W n! ty3 } z|jtjkr!‚ t d |¡¡ W Y d}~qd}~ww t d |t	 
|¡¡¡ t|ƒD ]
}tj 
||¡V  qDqdS )z4Return all filenames in which plugins may be stored.c                 S   s   g | ]	}|  d ¡r|‘qS )z
_plugin.py)Úendswith)Ú.0Úfilenamer   r   r   Ú
<listcomp>=   s    
ÿz2PluginManager.get_plugin_files.<locals>.<listcomp>zNo such plugin directory: {}NzPlugin modules in {}: {})r   ÚosÚlistdirÚOSErrorÚerrnoÚENOENTÚloggingÚdebugr   ÚSPACEÚjoinÚsortedÚpath)r   ÚdirnameÚ	basenamesÚerrorr   r   r   r   Úget_plugin_files8   s(   €
€ü
ÿÿÿózPluginManager.get_plugin_filesc                 c   s   � |   ¡ D ]}|V  qd S ©N)r)   )r   r   r   r   r   Úplugin_filesK   s   €ÿzPluginManager.plugin_filesc              	   c   sX   � dd„ }dd„ t  ||¡D ƒ}t d |t dd„ |D ƒ¡¡¡ |D ]}|ƒ V  q#dS )	z-Find and instantiate all plugins in a module.c                 S   s   t  | ¡ot| tƒo| tuS r*   )ÚinspectÚisclassÚ
issubclassr   )Útargetr   r   r   Ú	is_pluginR   s
   
ÿþz.PluginManager._find_plugins.<locals>.is_pluginc                 S   s   g | ]\}}|‘qS r   r   )r   ÚnameÚmemberr   r   r   r   W   s    ÿÿz/PluginManager._find_plugins.<locals>.<listcomp>zPlugins in {}: {}c                 s   s   � | ]}t |ƒV  qd S r*   )Ústr©r   Úpluginr   r   r   Ú	<genexpr>\   s   € z.PluginManager._find_plugins.<locals>.<genexpr>N)r,   Ú
getmembersr    r!   r   r"   r#   )r   Úmoduler0   Úplugin_classesÚplugin_classr   r   r   Ú_find_pluginsP   s   €
þÿ
ÿzPluginManager._find_pluginsc                 C   sÐ   t  d |¡¡ tj tj |¡¡\}}|tjv rtj| S t	|dƒ�;}zt
 |||ddt
jf¡}W n" tyT } zt  d ||¡¡ W Y d}~W d  ƒ dS d}~ww |W  d  ƒ S 1 saw   Y  dS )zLoad a module from a filename.zLoading module from file {}Úrz.pyzFailed to load plugin '{}' ({})N)r    r!   r   r   r%   ÚsplitextÚbasenameÚsysÚmodulesÚopenÚimpÚload_moduleÚ	PY_SOURCEÚ	ExceptionÚwarning)r   r   Úmodule_nameÚignoreÚfpr8   r(   r   r   r   Ú_load_module`   s(   


þÿ
ø€$özPluginManager._load_moduleNc           
         s°   ˆ du rg ‰ | j du rCg | _ t| jƒ}t|ƒ}t|ƒD ]'\}}|dur)||||ƒ |  |¡}|  |¡D ]}| | j¡ | j  	|¡ q3q‡ fdd„| j D ƒ}	t
 d ˆ |	¡¡ |	S )a�  Return all plugins that have been found.

        Loaded plugins are cached, so they will only be loaded once.

        `condition` is matched against each plugin to determine whether it
        will be returned or not.  A `condition` of the string '*' matches all
        plugins.  The default condition matches all default plugins, since by
        default, plugins have a condition of the empty list.

        If `condition` matches the plugin's condition exactly, the plugin is
        returned.  The plugin's condition can also be a sequence, and if
        `condition` is in that sequence, the plugin is returned.

        Note that even though loaded plugins are cached, calling
        `get_plugin()` with different a `condition` can return a different set
        of plugins.

        If `callback` is specified, it is called after each plugin has
        been found, with the following arguments: filename, index of
        filename in list of files to be examined (starting with 0), and
        total number of files to be examined. The purpose of this is to
        allow the callback to inform the user in case things take a long
        time.
        Nc                    s,   g | ]}|j ˆ ksˆ |j v sˆ d kr|‘qS )Ú*©Ú	conditionr4   rL   r   r   r   ²   s    

ýz-PluginManager.get_plugins.<locals>.<listcomp>z#plugins for condition '{}' are '{}')r   r   r+   ÚlenÚ	enumeraterJ   r;   Úset_applicationr   Úappendr    r!   r   )
r   rM   ÚcallbackÚ	filenamesÚtotalÚir   r8   r5   Úpluginsr   rL   r   Úget_plugins„   s*   


þ
ÿÿzPluginManager.get_plugins)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   Úpropertyr+   r;   rJ   rW   r   r   r   r   r   '   s    

$)Ú
__future__r   r   r   r   Ú__metaclass__Ú__all__r   rB   r?   r   r,   r    Újanitor.plugincore.pluginr   r"   r3   Úbytesr
   r   r   r   r   r   Ú<module>   s   ÿ