B
    \(                 @   s  d Z ddlZejdr&ddlmZ nddlmZ ddlZddlmZ ddl	m
Z
 dd	d
ddddgZdai Zi Zi ZdddgdgdgdZdd Ze  dd Zdd Zdd Zdd Zdd Ze  d(ddZe Zd d	 Zd)d!dZd"d# Zd$d% Zd&d
 Zd'd ZdS )*aA  Handle image reading, writing and plotting plugins.

To improve performance, plugins are only loaded as needed. As a result, there
can be multiple states for a given plugin:

    available: Defined in an *ini file located in `skimage.io._plugins`.
        See also `skimage.io.available_plugins`.
    partial definition: Specified in an *ini file, but not defined in the
        corresponding plugin module. This will raise an error when loaded.
    available but not on this system: Defined in `skimage.io._plugins`, but
        a dependent library (e.g. Qt, PIL) is not available on your system.
        This will raise an error when loaded.
    loaded: The real availability is determined when it's explicitly loaded,
        either because it's one of the default plugins, or because it's
        loaded explicitly by the user.

    N3)ConfigParser)glob   )imread_collection_wrapper
use_plugincall_pluginplugin_infoplugin_orderreset_pluginsfind_available_pluginsavailable_pluginsZpilZ
matplotlibZqt)allimshowimshow_collectionc               C   s   g g g g g g da dS )zNClear the plugin state to the default, i.e., where no plugins are loaded

    )imreadimsaver   imread_collectionr   	_app_showN)plugin_store r   r   8lib/python3.7/site-packages/skimage/io/manage_plugins.py_clear_plugins6   s    r   c              C   s\   dddddg} x| D ]}t |td  qW dd t D }x|D ]}t |t|  qBW d S )	Nr   r   r   r   r   r   c             s   s   | ]}|d kr|V  qdS )r   Nr   ).0pr   r   r   	<genexpr>M   s    z*_load_preferred_plugins.<locals>.<genexpr>)_set_pluginpreferred_pluginskeys)Zio_typesZp_typeZplugin_typesr   r   r   _load_preferred_pluginsF   s    

r   c             C   sJ   xD|D ]<}|t krqyt|| d P W q tttfk
r@   Y qX qW d S )N)kind)r   r   ImportErrorRuntimeErrorOSError)Zplugin_typeZplugin_listpluginr   r   r   r   R   s    
r   c               C   s   t   t  d S )N)r   r   r   r   r   r   r   ]   s    c             C   sL   t  }||  | d }i }x"||D ]}|||||< q,W ||fS )z>Return plugin name and meta-data dict from plugin config file.r   )r   readZsectionsZoptionsget)filenameparsername	meta_dataZoptr   r   r   _parse_config_fileb   s    
r+   c        	      C   s   t jt} tt j| dd}x|D ]}t|\}}|t|< dd |d dD }dd |D }x$|D ]}|t	krjt
d||f  qjW d	|kod
|k}|r|d	 |t|< t j|dd t|< q&W dS )z^Scan the plugins directory for .ini files and parse them
    to gather plugin meta-data.

    Z_pluginsz*.inic             S   s   g | ]}|  qS r   )strip)r   sr   r   r   
<listcomp>{   s    z!_scan_plugins.<locals>.<listcomp>provides,c             S   s   g | ]}|t kr|qS r   )r   )r   r   r   r   r   r.   |   s    z9Plugin `%s` wants to provide non-existent `%s`. Ignoring.r   r   N)ospathdirname__file__r   joinr+   plugin_meta_datasplitr   printappendplugin_providesbasenameplugin_module_name)	ZpdZconfig_filesr'   r)   r*   r/   Zvalid_providesr   Zneed_to_add_collectionr   r   r   _scan_pluginso   s"    


r>   Fc             C   sl   t  }x,t D ] }x|D ]\}}|| qW qW i }x.tD ]&}| rN||kr>dd t| D ||< q>W |S )a1  List available plugins.

    Parameters
    ----------
    loaded : bool
        If True, show only those plugins currently loaded.  By default,
        all plugins are shown.

    Returns
    -------
    p : dict
        Dictionary with plugin names as keys and exposed functions as
        values.

    c             S   s   g | ]}| d s|qS )_)
startswith)r   fr   r   r   r.      s    z*find_available_plugins.<locals>.<listcomp>)setr   valuesaddr;   )loadedZactive_pluginsZplugin_funcr$   funcdr   r   r   r      s    
c                s   | t krtd|  t |  }t|dkr8d}t||  |dd  dkrZ|d \}}nHt  y fdd|D d }W n$ tk
r   td | f Y nX |||S )	a  Find the appropriate plugin of 'kind' and execute it.

    Parameters
    ----------
    kind : {'imshow', 'imsave', 'imread', 'imread_collection'}
        Function to look up.
    plugin : str, optional
        Plugin to load.  Defaults to None, in which case the first
        matching plugin is used.
    *args, **kwargs : arguments and keyword arguments
        Passed to the plugin function.

    z Invalid function (%s) requested.r   zNo suitable plugin registered for %s.

You may load I/O plugins with the `skimage.io.use_plugin` command.  A list of all available plugins are shown in the `skimage.io` docstring.r$   Nc                s   g | ]\}}| kr|qS r   r   )r   r   rA   )r$   r   r   r.      s    zcall_plugin.<locals>.<listcomp>z&Could not find the plugin "%s" for %s.)r   
ValueErrorlenr"   pop_load
IndexError)r    argskwargsZplugin_funcsmsgr?   rF   r   )r$   r   r      s     c                s   |dkrt  }n4|t  kr.td |f |dkr@|dg}n|g}t  xT|D ]L}|t krltd| t | } fdd|D  fdd|D  }|t |< qTW dS )	aT  Set the default plugin for a specified operation.  The plugin
    will be loaded if it hasn't been already.

    Parameters
    ----------
    name : str
        Name of plugin.
    kind : {'imsave', 'imread', 'imshow', 'imread_collection', 'imshow_collection'}, optional
        Set the plugin for this function.  By default,
        the plugin is set for all functions.

    See Also
    --------
    available_plugins : List of available plugins

    Examples
    --------

    To use Matplotlib as the default image reader, you would write:

    >>> from skimage import io
    >>> io.use_plugin('matplotlib', 'imread')

    To see a list of available plugins run ``io.available_plugins``. Note that
    this lists plugins that are defined, but the full list may not be usable
    if your system does not have the required libraries installed.

    Nz Plugin %s does not support `%s`.r   r   z$'%s' is not a known plugin function.c                s    g | ]\}}| kr||fqS r   r   )r   nrA   )r)   r   r   r.     s    zuse_plugin.<locals>.<listcomp>c                s    g | ]\}}| kr||fqS r   r   )r   rP   rA   )r)   r   r   r.     s    )r   r   r;   r"   rK   )r)   r    kZfuncsr   )r)   r   r      s     


c             C   s6   t | ds2t | dr2t| d}t|}t| d| dS )z9Add `imread_collection` to module if not already present.r   r   N)hasattrgetattrr   setattr)moduler   rF   r   r   r   #_inject_imread_collection_if_needed  s    
rV   c             C   s   | t ddkrdS | tkr(td|  nt|  }td| |gd}t|  }xf|D ]^}|dkrft| nt||std| |f  qPt| }t	||}| |f|krP|
| |f qPW dS )	zLoad the given plugin.

    Parameters
    ----------
    plugin : str
        Name of plugin to load.

    See Also
    --------
    plugins : List of available plugins

    T)rE   NzPlugin %s not found.zskimage.io._plugins.)fromlistr   z7Plugin %s does not provide %s as advertised.  Ignoring.)r   r=   rH   
__import__r;   rV   rR   r9   r   rS   r:   )r$   modnameZplugin_moduler/   r   ZstorerF   r   r   r   rK     s&    




rK   c             C   s.   yt |  S  tk
r(   td|  Y nX dS )zReturn plugin meta-data.

    Parameters
    ----------
    plugin : str
        Name of plugin.

    Returns
    -------
    m : dict
        Meta data as specified in plugin ``.ini``.

    zNo information on plugin "%s"N)r7   KeyErrorrH   )r$   r   r   r   r	   ?  s    c              C   s,   i } x"t D ]}dd t | D | |< q
W | S )zReturn the currently preferred plugin order.

    Returns
    -------
    p : dict
        Dictionary of preferred plugin order, with function name as key and
        plugins (in order of preference) as value.

    c             S   s   g | ]\}}|qS r   r   )r   Zplugin_namerA   r   r   r   r.   _  s    z plugin_order.<locals>.<listcomp>)r   )r   rF   r   r   r   r
   S  s    

)F)N)__doc__sysversionr@   Zconfigparserr   os.pathr2   r   Z
collectionr   __all__r   r;   r=   r7   r   r   r   r   r   r+   r>   r   r   r   r   rV   rK   r	   r
   r   r   r   r   <module>   s@   


'
9%