ó
 ‰\c           @   sf  d  Z  d d l Z e j j d ƒ r7 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 i d d d g d 6d g d 6d g d 6Z d „  Z e ƒ  d „  Z d „  Z d „  Z d „  Z d „  Z e ƒ  e d „ Z e ƒ  Z d „  Z d d „ Z d „  Z d „  Z d  „  Z d! „  Z  d S("   sA  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.

iÿÿÿÿNt   3(   t   ConfigParser(   t   globi   (   t   imread_collection_wrappert
   use_plugint   call_plugint   plugin_infot   plugin_ordert   reset_pluginst   find_available_pluginst   available_pluginst   pilt
   matplotlibt   qtt   allt   imshowt   imshow_collectionc           C   s4   i g  d 6g  d 6g  d 6g  d 6g  d 6g  d 6a  d S(   sN   Clear the plugin state to the default, i.e., where no plugins are loaded

    t   imreadt   imsaveR   t   imread_collectionR   t	   _app_showN(   t   plugin_store(    (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt   _clear_plugins6   s    
c          C   ss   d d d d d g }  x |  D] } t  | t d ƒ q Wd „  t j ƒ  Dƒ } x | D] } t  | t | ƒ qT Wd  S(   NR   R   R   R   R   R   c         s   s!   |  ] } | d  k r | Vq d S(   R   N(    (   t   .0t   p(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pys	   <genexpr>M   s    (   t   _set_plugint   preferred_pluginst   keys(   t   io_typest   p_typet   plugin_types(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt   _load_preferred_pluginsF   s    	c         C   sY   xR | D]J } | t  k r q n  y t | d |  ƒPWq t t t f k
 rP q Xq Wd  S(   Nt   kind(   R
   R   t   ImportErrort   RuntimeErrort   OSError(   t   plugin_typet   plugin_listt   plugin(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   R   s    c           C   s   t  ƒ  t ƒ  d  S(   N(   R   R   (    (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   ]   s    c         C   sf   t  ƒ  } | j |  ƒ | j ƒ  d } i  } x- | j | ƒ D] } | j | | ƒ | | <q< W| | f S(   s>   Return plugin name and meta-data dict from plugin config file.i    (   R   t   readt   sectionst   optionst   get(   t   filenamet   parsert   namet	   meta_datat   opt(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt   _parse_config_fileb   s    	c    
      C   s3  t  j j t ƒ }  t t  j j |  d d ƒ ƒ } xü | D]ô } t | ƒ \ } } | t | <g  | d j d ƒ D] } | j	 ƒ  ^ qm } g  | D] } | t
 k rŒ | ^ qŒ } x, | D]$ } | t
 k r± d | | f GHq± q± Wd | k oî d | k }	 |	 r| j d ƒ n  | t | <t  j j | ƒ d  t | <q7 Wd	 S(
   s^   Scan the plugins directory for .ini files and parse them
    to gather plugin meta-data.

    t   _pluginss   *.init   providest   ,s9   Plugin `%s` wants to provide non-existent `%s`. Ignoring.R   R   iüÿÿÿN(   t   ost   patht   dirnamet   __file__R   t   joinR0   t   plugin_meta_datat   splitt   stripR   t   appendt   plugin_providest   basenamet   plugin_module_name(
   t   pdt   config_filesR+   R-   R.   t   sR2   R   t   valid_providest   need_to_add_collection(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt   _scan_pluginso   s"    
,%
c         C   s¥   t  ƒ  } x8 t j ƒ  D]* } x! | D] \ } } | j | ƒ q# Wq Wi  } xT t D]L } |  sj | | k rQ g  t | D] } | j d ƒ su | ^ qu | | <qQ qQ W| S(   s1  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.

    t   _(   t   setR   t   valuest   addR=   t
   startswith(   t   loadedt   active_pluginst   plugin_funcR&   t   funct   dt   f(    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR	   ‘   s    	&c   
      O   sù   |  t  k r t d |  ƒ ‚ n  t  |  } t | ƒ d k rT d } t | |  ƒ ‚ n  | j d d ƒ } | d k r… | d \ } } ng t | ƒ y3 g  | D] \ } }	 | | k r™ |	 ^ q™ d } Wn' t k
 rë t d | |  f ƒ ‚ n X| | | Ž  S(   s’  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.

    s    Invalid function (%s) requested.i    s³   No 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&   s&   Could not find the plugin "%s" for %s.N(   R   t
   ValueErrort   lenR"   t   popt   Nonet   _loadt
   IndexError(
   R    t   argst   kwargst   plugin_funcst   msgR&   RF   RN   R   RP   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   ²   s     

3c         C   s  | d k r t j ƒ  } nM | t |  k rD t d |  | f ƒ ‚ n  | d k r_ | d g } n	 | g } t |  ƒ x¡ | D]™ } | t k rž t d | ƒ ‚ n  t | } g  | D]$ \ } } | |  k r¯ | | f ^ q¯ g  | D]$ \ } } | |  k rÝ | | f ^ qÝ } | t | <qy Wd S(   sT  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.

    s    Plugin %s does not support `%s`.R   R   s$   '%s' is not a known plugin function.N(   RT   R   R   R=   R"   RU   (   R-   R    t   kt   funcst   nRP   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   Ù   s     	

.2c         C   sQ   t  |  d ƒ rM t  |  d ƒ rM t |  d ƒ } t | ƒ } t |  d | ƒ n  d S(   s9   Add `imread_collection` to module if not already present.R   R   N(   t   hasattrt   getattrR   t   setattr(   t   moduleR   RN   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt#   _inject_imread_collection_if_needed  s    c         C   sø   |  t  d t ƒ k r d S|  t k r8 t d |  ƒ ‚ n# t |  } t d | d | g ƒ} t |  } xŒ | D]„ } | d k r‹ t | ƒ n$ t | | ƒ s¯ d |  | f GHql n  t | } t	 | | ƒ } |  | f | k rl | j
 |  | f ƒ ql ql Wd S(   s¯   Load the given plugin.

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

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

    RK   Ns   Plugin %s not found.s   skimage.io._plugins.t   fromlistR   s7   Plugin %s does not provide %s as advertised.  Ignoring.(   R	   t   TrueR?   RQ   t
   __import__R=   Rb   R^   R   R_   R<   (   R&   t   modnamet   plugin_moduleR2   R   t   storeRN   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyRU     s&    


c         C   s4   y t  |  SWn! t k
 r/ t d |  ƒ ‚ n Xd S(   s¿   Return plugin meta-data.

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

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

    s   No information on plugin "%s"N(   R9   t   KeyErrorRQ   (   R&   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   ?  s    c          C   sB   i  }  x5 t  D]- } g  t  | D] \ } } | ^ q |  | <q W|  S(   s×   Return 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.

    (   R   (   R   RN   t   plugin_nameRP   (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyR   S  s    
+(!   t   __doc__t   syst   versionRJ   t   configparserR   t   os.pathR4   R   t
   collectionR   t   __all__RT   R   R=   R?   R9   R   R   R   R   R   R0   RE   t   FalseR	   R
   R   R   Rb   RU   R   R   (    (    (    s8   lib/python2.7/site-packages/skimage/io/manage_plugins.pyt   <module>   sB   
								'9		%	