ó
;c]c           @` sÉ  d  Z  d d l m Z m Z m Z d d l Z d d l Z d d l m Z d d l	 Z	 d d l
 Z
 d d l Z d d l m Z m Z m Z m Z e j d f k  r« e Z n  y d d l m Z Wn e k
 rŲ e Z n Xd	 Z d Z d Z d
 Z d Z d Z d d d f Z d Z i d d 6d d 6d d 6d d 6d d 6d d 6d d 6d d  6d! d" 6d# d$ 6d% d& 6d' d( 6d) d* 6d+ d, 6d- d. 6d/ d0 6d1 d2 6d3 d4 6d5 d6 6Z  d7 e! f d8     YZ" d9   Z# d:   Z$ d S(;   sx   
Definition of the Request object, which acts as a kind of bridge between
what the user wants and what the plugins can.
i    (   t   absolute_importt   print_functiont   divisionN(   t   BytesIOi   (   t   string_typest   binary_typet   urlopent   get_remote_filei   (   t   Pathi   i   i   i   s   <videos   <screen>s   <clipboard>s   <bytes>s%   Image of the astronaut Eileen Collinss   astronaut.pngs)   Classic grayscale image of a photographers
   camera.pngs&   Black and white image of a chekerboards   checkerboard.pngs7   Photo of a clock with motion blur (Stefan van der Walt)s	   clock.pngs*   Image of a cup of coffee (Rachel Michetti)s
   coffee.pngs   Image of Stefan's cats   chelsea.pngs   Image of Almar's cats
   wikkie.pngs&   Image showing greek coins from Pompeiis	   coins.pngs8   Image showing the silhouette of a horse (Andreas Preuss)s	   horse.pngs+   Photograph taken by Hubble telescope (NASA)s   hubble_deep_field.pngs"   Immunohistochemical (IHC) stainings   immunohistochemistry.pngs2   Image showing a portion of the surface of the moons   moon.pngs   A scanned page of texts   page.pngs   A photograph of handdrawn texts   text.pngs*   The chelsea.png in a zipfile (for testing)s   chelsea.zips+   The chelsea.png in a BSDF file(for testing)s   chelsea.bsdfs!   Animated GIF of a newton's cradles   newtonscradle.gifs   Video file of a cockatoos   cockatoo.mp4s2   Volumetric image showing a stented abdominal aortas	   stent.npzt   Requestc           B` s   e  Z d  Z d   Z d   Z e d    Z e d    Z e d    Z e d    Z	 d   Z
 d   Z d	   Z d
   Z e d    Z d d  Z RS(   sR   Request(uri, mode, **kwargs)

    Represents a request for reading or saving an image resource. This
    object wraps information to that request and acts as an interface
    for the plugins to several resources; it allows the user to read
    from filenames, files, http, zipfiles, raw bytes, etc., but offer
    a simple interface to the plugins via ``get_file()`` and
    ``get_local_filename()``.

    For each read/write operation a single Request instance is used and passed
    to the can_read/can_write method of a format, and subsequently to
    the Reader/Writer class. This allows rudimentary passing of
    information between different formats and between a format and
    associated reader/writer.

    parameters
    ----------
    uri : {str, bytes, file}
        The resource to load the image from.
    mode : str
        The first character is "r" or "w", indicating a read or write
        request. The second character is used to indicate the kind of data:
        "i" for an image, "I" for multiple images, "v" for a volume,
        "V" for multiple volumes, "?" for don't care.
    c         K` sf  d  |  _ d  |  _ d  |  _ | |  _ d  |  _ d  |  _ d  |  _ d  |  _ d  |  _	 t
 |  _ d  |  _ d  |  _ | |  _ t | t  s t d   n  t |  d k s“ t d   n  | d d k rÓ t d   n  | d d k rņ t d	   n  |  j |  |  j d  k	 rb|  j } |  j j d  r?| j d  d } n  d | j d  d j   |  _ n  d  S(   Ns&   Request requires mode must be a stringi   s'   Request requires mode to have two charsi    t   rws)   Request requires mode[0] to be "r" or "w"i   s   iIvV?s)   Request requires mode[1] to be in "iIvV?"s   http://s   https://s   ftp://s   ftps://t   ?t   .i’’’’(   s   http://s   https://s   ftp://s   ftps://(   t   Nonet	   _uri_typet	   _filenamet
   _extensiont   _kwargst   _resultt   _filename_zipt   _bytest   _zipfilet   _filet   Falset   _file_is_localt   _filename_localt   _firstbytest   _modet
   isinstanceR   t
   ValueErrort   lent
   _parse_urit
   startswitht   splitt   lower(   t   selft   urit   modet   kwargst   ext(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   __init__[   s6    														c         C` s½  t  j d d k } |  j d d k } |  j d d k } t | t  rŹ| j d  r| rl t d   n  | j d d  d	 j   } | j	 d
  \ } } } | r³ | d 7} n  | t
 k rŅ t d |   n  t |  _ t d | d t |  _ | rĒ|  j d | 7_ qĒqö| j d  s1| j d  rFt |  _ | |  _ qö| j d  sd| j d  ryt |  _ | |  _ qö| j d  r”t |  _ | d |  _ qö| j t  rĖ| rĖt |  _ | |  _ qö| j t  rõ| rõt |  _ | |  _ qö| rt |  _ | |  _ qöy t j j |  } Wn t k
 r?t } n X| r[t |  _ | |  _ qöt |  d k  rt |  _ | |  _ qöt | t  rµ| rµt |  _ d |  _ | |  _ qöt |  _ | |  _ n,t | t  r| rt |  _ d |  _ | j   |  _ nó t | t  r6| r6t |  _ d |  _ | |  _ nĄ t  d( k	 rlt | t   rlt |  _ t" |  |  _ n | r±t# | d  röt# | d  röt$ |  _ d |  _ | |  _% qönE | röt# | d  röt# | d  röt$ |  _ d |  _ | |  _% qön  |  j t k r2|  j j d  r2t j j& |  j  |  _ n  |  j t k r¼xx d
 d g D]g }	 |  j j   j' |	  }
 |
 d k rN|
 d 7}
 t( |  _ |  j |
  |  j |
 j) d  f |  _* PqNqNWn  |  j d( k rt+ |  } t |  d  k rś| d!  d" } n  t, d# |   n  t t g } | r=|  j | k r=t, d$   n  | rµ|  j t t( g k rµ|  j } |  j* rz|  j* d } n  t j j- |  rµ| t
 k rµt, d% | | f   qµn  |  j t t( g k r|  j* r’t j j. |  j* d  |  j* d f |  _* qt j j. |  j  |  _ n  |  j t t( g k r¹|  j } |  j* rQ|  j* d } n  | rt j j- |  s¶t/ d& |   q¶q¹t j j0 |  } t j j- |  s¹t/ d' |   q¹n  d( S()   s.    Try to figure our what we were given
        i    i   t   rt   ws   imageio:s$   Cannot write to the standard images.t   :i   i’’’’s   .zip/s   .zips   Unknown standard image %r.s   images/t   autot   /s   http://s   https://s   ftp://s   ftps://s   file://i   i   s   <bytes>t   readt   closes   <file>t   writet   ~s   .zip\i   s   /\i<   i9   s   ...s    Cannot understand given URI: %s.s-   imageio does not support writing to http/ftp.s   No such file: %r. This file looks like one of the standard images, but from imageio 2.1, standard images have to be specified using "imageio:%s".s   No such file: '%s's   The directory %r does not existN(1   t   syst   version_infoR%   R   R   R    t   RuntimeErrorR!   R"   t	   partitiont   EXAMPLE_IMAGESR   t   URI_FILENAMER   R   t   TrueR   t   URI_HTTPt   URI_FTPt   SPECIAL_READ_URISt	   URI_BYTESt   RETURN_BYTESt   ost   patht   isfilet	   ExceptionR   R   R   R   t
   memoryviewt   tobytesR   R   t   strt   hasattrt   URI_FILER   t
   expandusert   findt
   URI_ZIPPEDt   lstripR   t   reprt   IOErrort   existst   abspatht   FileNotFoundErrort   dirname(   R#   R$   t   py3kt   is_read_requestt   is_write_requestt   fnt   _t   zip_partR@   t   needlet   zip_it   uri_rt	   noWritingt   dn(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyR      sä    							
														!
	
					c         C` s   |  j  S(   s   The uri for which reading/saving was requested. This
        can be a filename, an http address, or other resource
        identifier. Do not rely on the filename to obtain the data,
        but use ``get_file()`` or ``get_local_filename()`` instead.
        (   R   (   R#   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   filename  s    c         C` s   |  j  S(   s„    The (lowercase) extension of the requested filename.
        Suffixes in url's are stripped. Can be None if the request is
        not based on a filename.
        (   R   (   R#   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt	   extension&  s    c         C` s   |  j  S(   s/   The mode of the request. The first character is "r" or "w",
        indicating a read or write request. The second character is
        used to indicate the kind of data:
        "i" for an image, "I" for multiple images, "v" for a volume,
        "V" for multiple volumes, "?" for don't care.
        (   R   (   R#   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyR%   .  s    c         C` s   |  j  S(   s=    The dict of keyword arguments supplied by the user.
        (   R   (   R#   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyR&   8  s    c         C` s}  |  j  d d k } |  j d k	 r) |  j S|  j t k rk | rV t   |  _ t |  _ qvt |  j  |  _ n|  j t	 k r° | r t
 |  j d  |  _ qvt
 |  j d  |  _ nĘ |  j t k r)|  j \ } } | rģ t   |  _ t |  _ qvt j | d  |  _ |  j j
 | d  |  _ t |  j  nM |  j t p8t g k rv| sNt  t |  j d d |  _ t |  j  n  |  j S(	   s¹   get_file()
        Get a file object for the resource associated with this request.
        If this is a reading request, the file is in read mode,
        otherwise in write mode. This method is not thread safe. Plugins
        should not close the file when done.

        This is the preferred way to read/write the data. But if a
        format cannot handle file-like objects, they should use
        ``get_local_filename()``.
        i    R*   t   wbt   rbR)   t   timeouti   N(   R%   R   R   R   R<   R   R8   R   R   R7   t   openR\   RI   R   t   zipfilet   ZipFileR   t#   make_file_object_support_noop_seeksR9   R:   t   AssertionErrorR   (   R#   t   want_to_writeR\   t   name(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   get_file@  s2    c         C` sŌ   |  j  t k r |  j S|  j  t t f k rT t j j |  j j d  d  d } n t j j |  j  d } t	 j
 | d  |  _ |  j d d k rÉ t |  j d   } t j |  j   |  Wd QXn  |  j Sd S(   sō    get_local_filename()
        If the filename is an existing file on this filesystem, return
        that. Otherwise a temporary file is created on the local file
        system which can be used by the format to read from or write to.
        R   i    i   t   imageio_R)   R^   N(   R   R7   R   R9   R:   R>   R?   t   splitextR!   t   tempfilet   mktempR   R%   Ra   t   shutilt   copyfileobjRh   (   R#   R'   t   file(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   get_local_filenameu  s    )c         C` s  |  j  d d k rd } |  j rL t |  j d   } | j   } Wd QXn |  j rg |  j j   } n  | d k	 r|  j t	 k r | |  _
 q’ |  j t k r° |  j j |  q’ |  j t k r’ t j |  j d d  } | j |  j d |  | j   q’ qn  |  j r3|  j t k r3|  j j   d |  _ n  |  j rU|  j j   d |  _ n  |  j ry t j |  j  Wn t k
 rn Xd |  _ n  d |  _ d S(   s®    finish()
        For internal use (called when the context of the reader/writer
        exits). Finishes this request. Close open files and process
        results.
        i    R*   R_   Nt   ai   (   R%   R   R   Ra   R.   R   R   t   getvalueR   R<   R   RF   R0   RI   Rb   Rc   R   t   writestrR/   R   R>   t   removeRA   R   (   R#   t   bytesRo   t   zf(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   finish  s:    				c         C` s   d |  j |  _ } | S(   s    For internal use. In some situations a write action can have
        a result (bytes data). That is obtained with this function.
        N(   R   R   (   R#   t   res(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt
   get_result¼  s    c         C` s#   |  j  d k r |  j   n  |  j  S(   sv    The first 256 bytes of the file. These can be used to
        parse the header to determine the file-format.
        N(   R   R   t   _read_first_bytes(   R#   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt
   firstbytesĆ  s    i   c         C` s  |  j  d  k	 r" |  j  |  |  _ nļ y |  j   } Wn9 t k
 rm t j j |  j  rg t	   |  _ d  S  n Xy | j
   } Wn t k
 r d  } n Xt | |  |  _ y, | d  k rČ t d   n  | j |  Wn8 t k
 rd  |  _ |  j t k rt d   qn Xd  S(   Ns   cannot seek with Nones*   Cannot seek back after getting firstbytes!(   R   R   R   Rh   RL   R>   R?   t   isdirR\   R   t   tellRA   t   read_n_bytest   seekR   R   RF   (   R#   t   Nt   ft   i(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyRz   Ģ  s,    
	(   t   __name__t
   __module__t   __doc__R(   R   t   propertyR\   R]   R%   R&   Rh   Rp   Rw   Ry   R{   Rz   (    (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyR	   @   s   	,		
	5		1		c         C` sS   t    } xC t |  | k  rN |  j | t |   } | sA Pn  | | 7} q W| S(   s    read_n_bytes(file, n)

    Read n bytes from the given file, or less if the file has less
    bytes. Returns zero bytes if the file is closed.
    (   R   R   R.   (   R   R   t   bbt   extra_bytes(    (    s3   lib/python2.7/site-packages/imageio/core/request.pyR~   ė  s    	c         ` s   d g   d    f d  }   f d   } d    f d  } d d  } |  j  t |  d  ro |  j n |  | |  _ | |  _ | |  _ d S(   s“   This fixes up an HTTPResponse object so that it can tell(), and also
    seek() will work if its effectively a no-op. This allows tools like Pillow
    to use the file object.
    i    c         ` s&    |   }   d c t  |  7<| S(   Ni    (   R   (   t   nRx   (   t   countt   ori_read(    s3   lib/python2.7/site-packages/imageio/core/request.pyR.     s    c           ` s     d S(   Ni    (    (    (   R   (    s3   lib/python2.7/site-packages/imageio/core/request.pyR}     s    c         ` s0   | d k o |    d k s,  |  |  n  d  S(   Ni    (    (   R   R%   (   R   t   ori_seek(    s3   lib/python2.7/site-packages/imageio/core/request.pyR   	  s    c         S` s   t  d   d  S(   Ns   No seeking allowed!(   R4   (   R   R%   (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt	   fail_seek  s    R   N(   R   R.   RE   R   R}   (   R   R.   R}   R   R   (    (   R   R   R   s3   lib/python2.7/site-packages/imageio/core/request.pyRd   ś  s    				(%   R   t
   __future__R    R   R   R2   R>   t   ioR   Rb   Rk   Rm   t   coreR   R   R   R   R3   t   OSErrorRO   t   pathlibR   t   ImportErrorR   R<   RF   R7   RI   R9   R:   R;   R=   R6   t   objectR	   R~   Rd   (    (    (    s3   lib/python2.7/site-packages/imageio/core/request.pyt   <module>   s\   "	

’ ¬	