î
_ó¤WÄ  ã               @   sc   d  Z  d d l Z d d l Z d d l Z d d l m Z e j d ƒ Z Gd d „  d e ƒ Z	 d S)z.
utils.plugins - Plugin loading functionality
é    N)ÚSourceFileLoaderz\* depends: (.+)c               @   s=   e  Z d  Z d Z d d „  Z d d d „ Z d d „  Z d S)	ÚPluginLoaderz€
    The plugin loader
    Generates a dependency graph and returns a list of loaded modules
    when load_all() is called.
    c             C   sÍ   d |  _  i  |  _ t j t j d ƒ d ƒ } x™ | D]‘ } x] t t j j |  j  | ƒ ƒ D]= } t	 j
 | ƒ } | rY | j d ƒ j d ƒ |  j | <qY qY W| |  j k r4 | d k r4 g  |  j | <q4 q4 Wd S)z? Initializes the PluginLoader by generating a dependency graph z	./pluginsz*.pyé   z, z__init__.pyN)Ú	directoryÚgraphÚfnmatchÚfilterÚosÚlistdirÚopenÚpathÚjoinÚ
DEPENDS_REÚsearchÚgroupÚsplit)ÚselfÚfilesÚfÚlineÚmatch© r   ú/opt/Infobot/utils/plugins.pyÚ__init__   s    		%&zPluginLoader.__init__Nc             C   sN   | d  k r# d | d  d … } n  t  | t j j |  j | ƒ ƒ j ƒ  } | S)Nzplugins.é   éýÿÿÿ)r   r	   r   r   r   Úload_module)r   Zplugin_filenameÚnameÚpluginr   r   r   Úload_plugin!   s    'zPluginLoader.load_pluginc             C   sÞ   g  } |  j  d d d ƒx¾ |  j rÙ t t d d „  |  j j ƒ  ƒ ƒ } xŠ | j ƒ  D]| \ } } | j |  j  | ƒ ƒ |  j | =xM |  j j ƒ  D]< \ } } y | j | d  d … ƒ Wq’ t k
 rÍ Yq’ Xq’ WqV Wq W| S)Nz__init__.pyr   Úpluginsc             S   s	   |  d S)Nr   r   )Úxr   r   r   Ú<lambda>+   s    z'PluginLoader.load_all.<locals>.<lambda>r   r   )r   r   Údictr   ÚitemsÚappendÚremoveÚ
ValueError)r   r    Zsatisfied_pluginsZsatisfied_pluginÚ_r   Zdepsr   r   r   Úload_all'   s    $
zPluginLoader.load_all)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r)   r   r   r   r   r      s   r   )
r-   Úrer   r	   Úimportlib.machineryr   Úcompiler   Úobjectr   r   r   r   r   Ú<module>   s   