
    h                     r    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 reneZ G d d       Zy)PluginManager    N)Plugin c                   @    e Zd ZdZd Zd Zed        Zd Zd Z	d	dZ
y)
r   zFind and load plugins.

    Plugins are stored in files named '*_plugin.py' in the list of directories
    given to the constructor.
    c                     || _         t        |t              r#t        dj	                  t        |                  t        |      | _        d | _        y )NzExpected sequence, got {})	_app
isinstance	STR_TYPES	TypeErrorformattypelist_plugin_dirs_plugins)selfappplugin_dirss      </usr/lib/python3/dist-packages/janitor/plugincore/manager.py__init__zPluginManager.__init__)   sK    	 k9-+2243DE  !-    c           	   #   $  K   | j                   D ]  }	 t        j                  |      D cg c]  }|j                  d      r| }}t        j                  dj                  |t        j                  |                   t        |      D ]$  }t        j                  j                  ||       &  yc c}w # t        $ rL}|j
                  t
        j                  k7  r t        j                  dj                  |             Y d}~d}~ww xY ww)z4Return all filenames in which plugins may be stored.z
_plugin.pyzNo such plugin directory: {}NzPlugin modules in {}: {})r   oslistdirendswithOSErrorerrnoENOENTloggingdebugr   SPACEjoinsortedpath)r   dirnamefilename	basenameserrors        r   get_plugin_fileszPluginManager.get_plugin_files4   s      (( 	6G
 %'JJw$7 ((6 	  MM*11UZZ	2 #9- 6ggll7H556%	6
  ;;%,,.<CCGLM	sA   DB8B3B8A-D3B88	DADDDDc              #   >   K   | j                         D ]  }|  y wN)r(   )r   r%   s     r   plugin_fileszPluginManager.plugin_filesL   s#     --/ 	HN	s   c           	   #     K   d }t        j                  ||      D cg c]  \  }}|	 }}}t        j                  dj	                  |t
        j                  d |D                           |D ]  } |         yc c}}w w)z-Find and instantiate all plugins in a module.c                 d    t        j                  |       xr t        | t              xr | t        uS r*   )inspectisclass
issubclassr   )targets    r   	is_pluginz.PluginManager._find_plugins.<locals>.is_pluginT   s1     ' )vv.)&(r   zPlugins in {}: {}c              3   2   K   | ]  }t        |        y wr*   )str).0plugins     r   	<genexpr>z.PluginManager._find_plugins.<locals>.<genexpr>a   s     "L63v;"Ls   N)r.   
getmembersr   r   r   r    r!   )r   moduler2   namememberplugin_classesplugin_classs          r   _find_pluginszPluginManager._find_pluginsQ   s     	 (/'9'9&)'L
#tVF
 
 	&&

"L^"LL	

 + 	!L. 	!
s   BA>ABc           	      4   t        j                  dj                  |             t        j                  j                  t        j                  j                  |            \  }}|t        j                  v rt        j                  |   S t        |d      5 }	 t        j                  |||ddt        j                  f      }|cddd       S # t        $ r7}t        j                  dj                  ||             Y d}~ddd       yd}~ww xY w# 1 sw Y   yxY w)zLoad a module from a filename.zLoading module from file {}rz.pyNzFailed to load plugin '{}' ({}))r   r   r   r   r#   splitextbasenamesysmodulesopenimpload_module	PY_SOURCE	Exceptionwarning)r   r%   module_nameignorefpr9   r'   s          r   _load_modulezPluginManager._load_moduleg   s    3::8DE& !gg..rww/?/?/IJV#++%;;{++(C  	BXsCMM/J 	 	
  5<<#U
 	 	
	 	s6   D)C D	D%D9DDDDNc                 6   |g }| j                   g | _         t        | j                        }t        |      }t	        |      D ]n  \  }}|
 ||||       | j                  |      }| j                  |      D ]8  }|j                  | j                         | j                   j                  |       : p | j                   D cg c]&  }|j                  |k(  s||j                  v s|dk(  r|( }	}t        j                  dj                  ||	             |	S c c}w )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.
        *z#plugins for condition '{}' are '{}')r   r   r+   len	enumeraterN   r>   set_applicationr   append	conditionr   r   r   )
r   rU   callback	filenamestotalir%   r9   r6   pluginss
             r   get_pluginszPluginManager.get_plugins   s$   6 I == DMT../I	NE(3 18'Xq%0**84"008 1F**4995MM((01	1 --
  I- 0 00# 
 
 	188GL	
 
s   +D)NN)__name__
__module____qualname____doc__r   r(   propertyr+   r>   rN   r[    r   r   r   r   "   s5    	60  !,%N:r   )r   __metaclass____all__r   rF   rC   r   r.   r   janitor.plugincore.pluginr   r    r4   bytesr
   r   ra   r   r   <module>rf      sK     
 
 
 
    , 	%<CS	f fr   