
    0-Phi              	          d 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ZddlZddl	m
Z
mZ ddlmZ ddlmZ ddlmZ d	ZdZd
ZdZdZdZ G d deej                  Z G d deej                  Zej         G d deej                              ZdZdZi ddddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?Z  G d@ dAe!          Z"dB Z# G dC dD          Z$ G dE dFe%          Z&dS )Gzx
Definition of the Request object, which acts as a kind of bridge between
what the user wants and what the plugins can.
    N)BytesIO   )urlopenget_remote_file)Path)urlparse)Optional               c                       e Zd ZdZdZdZdS )IOModeak  Available Image modes

    This is a helper enum for ``Request.Mode`` which is a composite of a
    ``Request.ImageMode`` and ``Request.IOMode``. The IOMode that tells the
    plugin if the resource should be read from or written to. Available values are

    - read ("r"): Read from the specified resource
    - write ("w"): Write to the specified resource

    rwN)__name__
__module____qualname____doc__readwrite     T/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/imageio/core/request.pyr   r       s$        	 	 DEEEr   r   c                   &    e Zd ZdZdZdZdZdZdZdS )	ImageModea2  Available Image modes

    This is a helper enum for ``Request.Mode`` which is a composite of a
    ``Request.ImageMode`` and ``Request.IOMode``. The image mode that tells the
    plugin the desired (and expected) image shape. Available values are

    - single_image ("i"): Return a single image extending in two spacial
      dimensions
    - multi_image ("I"): Return a list of images extending in two spacial
      dimensions
    - single_volume ("v"): Return an image extending into multiple dimensions.
      E.g. three spacial dimensions for image stacks, or two spatial and one
      time dimension for videos
    - multi_volume ("V"): Return a list of images extending into multiple
      dimensions.
    - any_mode ("?"): Return an image in any format (the plugin decides the
      appropriate action).

    iIvV?N)	r   r   r   r   single_imagemulti_imagesingle_volumemulti_volumeany_moder   r   r   r   r   0   s3         ( LKMLHHHr   r   c                       e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZed             Zedefd            Zedefd            Zd ZdS )Modea  The mode to use when interacting with the resource

    ``Request.Mode`` is a composite of ``Request.ImageMode`` and
    ``Request.IOMode``. The image mode that tells the plugin the desired (and
    expected) image shape and the ``Request.IOMode`` tells the plugin the way
    the resource should be interacted with. For a detailed description of the
    available modes, see the documentation for ``Request.ImageMode`` and
    ``Request.IOMode`` respectively.

    Available modes are all combinations of ``Request.IOMode`` and ``Request.ImageMode``:

    - read_single_image ("ri")
    - read_multi_image ("rI")
    - read_single_volume ("rv")
    - read_multi_volume ("rV")
    - read_any ("r?")
    - write_single_image ("wi")
    - write_multi_image ("wI")
    - write_single_volume ("wv")
    - write_multi_volume ("wV")
    - write_any ("w?")

    Examples
    --------
    >>> Request.Mode("rI")  # a list of simple images should be read from the resource
    >>> Request.Mode("wv")  # a single volume should be written to the resource

    rirIrvrVr?wiwIwvwVw?c                 j    |dk    r | d          S |dk    r | d          S t          | d          )a  Enable Mode("r") and Mode("w")

        The sunder method ``_missing_`` is called whenever the constructor fails
        to directly look up the corresponding enum value from the given input.
        In our case, we use it to convert the modes "r" and "w" (from the v3
        API) into their legacy versions "r?" and "w?".

        More info on _missing_:
        https://docs.python.org/3/library/enum.html#supported-sunder-names
        r   r.   r   r3   z is no valid Mode.)
ValueError)clsvalues     r   	_missing_zMode._missing_v   sI     C<<3t99c\\3t99999:::r   returnc                 6    t          | j        d                   S )Nr   )r   r7   selfs    r   io_modezMode.io_mode   s    djm$$$r   c                 6    t          | j        d                   S )Nr
   )r   r7   r;   s    r   
image_modezMode.image_mode   s    A'''r   c                 Z    |dk    r| j         S |dk    r| j        S t          d|           )z7For backwards compatibility with the old non-enum modesr   r
   zMode has no item )r=   r?   
IndexError)r<   keys     r   __getitem__zMode.__getitem__   s;    !88<AXX?"666777r   N)r   r   r   r   read_single_imageread_multi_imageread_single_volumeread_multi_volumeread_anywrite_single_imagewrite_multi_imagewrite_single_volumewrite_multi_volume	write_anyclassmethodr8   propertyr   r=   r   r?   rC   r   r   r   r)   r)   L   s         : HI; ; [;& % % % % X% (I ( ( ( X(8 8 8 8 8r   r)   )z<videoz<screen>z<clipboard><bytes>zastronaut.pngz%Image of the astronaut Eileen Collinsz
camera.pngz#A grayscale image of a photographerzcheckerboard.pngz&Black and white image of a chekerboardzwood.jpgz'A (repeatable) texture of wooden planksz
bricks.jpgz&A (repeatable) texture of stone bricksz	clock.pngz7Photo of a clock with motion blur (Stefan van der Walt)z
coffee.pngz*Image of a cup of coffee (Rachel Michetti)zchelsea.pngzImage of Stefan's catz
wikkie.pngzImage of Almar's catz	coins.pngz&Image showing greek coins from Pompeiiz	horse.pngz8Image showing the silhouette of a horse (Andreas Preuss)zhubble_deep_field.pngz+Photograph taken by Hubble telescope (NASA)zimmunohistochemistry.pngz"Immunohistochemical (IHC) stainingzmoon.pngz2Image showing a portion of the surface of the moonzpage.pngzA scanned page of textztext.pngzA photograph of handdrawn textzbacterial_colony.tifz+Multi-page TIFF image of a bacterial colonyz*The chelsea.png in a zipfile (for testing)z+The chelsea.png in a BSDF file(for testing)z!Animated GIF of a newton's cradlezVideo file of a cockatooz1Video file of a cockatoo with yuv420 pixel formatz2Volumetric image showing a stented abdominal aortaz5A cubemap image of a meadow, e.g. to render a skybox.)zchelsea.zipzchelsea.bsdfznewtonscradle.gifzcockatoo.mp4zcockatoo_yuv420.mp4z	stent.npzzmeadow_cube.jpgc                      e Zd ZdZddddefdZd Zed             Zedefd	            Z	ede
e         fd
            Zej        deddfd            Zed             Zed             Zd Zd ZddZd Zed             ZddZdS )RequestaU  ImageResource handling utility.

    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.

    N)	extensionformat_hintrT   c                   || _         d | _        d | _        d | _        d | _        || _        d | _        d | _        d | _        d | _	        d | _
        d| _        d | _        d | _        	 t          |          | _        n # t           $ r t!          d|           w xY w|                     |           |'|d         dk    rt!          d| d          || _        nr| j        k| j        t$          t&          fv r| j        }nt)          | j                  j        }t-          |          j                                        }|dk    r|nd | _        |t3          j        dt6                     ||d         dk    rt!          d	| d          || _        d S )
NFzInvalid Request.Mode: r   .zD`extension` should be a file extension starting with a `.`, but is `z`. zdThe usage of `format_hint` is deprecated and will be removed in ImageIO v3. Use `extension` instead.zF`format_hint` should be a file extension starting with a `.`, but is `)raw_uri	_uri_type	_filename
_extension_format_hint_kwargs_result_filename_zip_bytes_zipfile_file_file_is_local_filename_local_firstbytesr)   _moder5   
_parse_uriURI_FILENAME
URI_ZIPPEDr   pathr   suffixlowerwarningswarnDeprecationWarningrT   )r<   urimoderS   rT   kwargsrj   exts           r   __init__zRequest.__init__   s     " 
##	>dDJJ 	> 	> 	><d<<===	> 	  |s"" . ). . .   (DOO^'~,
!;;;~//4t**#))++C%(BYYccDDO"M:"   "{1~'<'<,', , ,  
 's   $A9 9Bc                 >   | j         j        t          j        u }| j         j        t          j        u }t          |t                    r|                    d          r|rt          d          |	                    dd          d         
                                }|                    d          \  }}}|r|dz  }|t          vrt          d|z            t          | _        t!          d	|z   d
          | _        |r| xj        d|z   z  c_        nL|                    d          s|                    d          rt$          | _        || _        n|                    d          s|                    d          rt&          | _        || _        n|                    d          rt          | _        |dd         | _        n|                    t(                    r|rt*          | _        || _        nk|                    t,                    r|rt*          | _        || _        n:t          | _        || _        n%t          |t.                    r/|r-t*          | _        d| _        |                                | _        nt          |t4                    r|rt*          | _        d| _        || _        nt          |t6                    r!t          | _        t          |          | _        ny|r;t9          |d          r*t9          |d          rt:          | _        d| _        || _        n<|r:t9          |d          r*t9          |d          rt:          | _        d| _        || _        | j        t          k    rC| j                            d          r)t>          j         !                    | j                  | _        | j        t          k    rdD ]}| j        
                                "                    |          }|dk    r|dz  }| j        d|         }	t>          j         #                    |	          rh|st>          j         $                    |	          r7tJ          | _        |	| j        |d         &                    d          f| _'         n| j        AtQ          |          }
tS          |
          dk    r|
dd         d z   }
tU          d!|
z            t$          t&          g}|r| j        |v rtU          d"          |rn| j        t          tJ          fv rY| j        }| j'        r| j'        d         }t>          j         +                    |          s|t          v rtU          d#|d$|d%          | j        t          tJ          fv rm| j'        r=t>          j         ,                    | j'        d                   | j'        d         f| _'        n)t>          j         ,                    | j                  | _        | j        t          tJ          fv r| j        }| j'        r| j'        d         }|r3t>          j         +                    |          st[          d&|z            dS t>          j         .                    |          }t>          j         +                    |          st[          d'|z            dS dS )(z$Try to figure our what we were givenzimageio:z$Cannot write to the standard images.:r
   .zip/z.zipzUnknown standard image %r.zimages/T)auto/zhttp://zhttps://zftp://zftps://zfile://   NrP   r   closez<file>r   ~)rx   z.zip\r   r   z/\<   9   z...z Cannot understand given URI: %s.z-imageio does not support writing to http/ftp.zNo such file: z}. This file looks like one of the standard images, but from imageio 2.1, standard images have to be specified using "imageio:z".zNo such file: '%s'zThe directory %r does not exist)/rq   r=   r   r   r   
isinstancestr
startswithRuntimeErrorsplitrl   	partitionEXAMPLE_IMAGESr5   rh   rY   r   rZ   URI_HTTPURI_FTPSPECIAL_READ_URIS	URI_BYTESRETURN_BYTES
memoryviewtobytesr`   bytesr   hasattrURI_FILErb   osrj   
expanduserfindisdirisfileri   lstripr_   reprlenIOErrorexistsabspathFileNotFoundErrordirname)r<   rp   is_read_requestis_write_requestfn_zip_partneedlezip_izip_pathuri_r	noWritingdns                r   rg   zRequest._parse_uri  sW   )+v{:9,<c3 7	!~~j)) %# O&'MNNNYYsA&&r*0022"$,,w"7"7Ax !&LB^++$%AB%FGGG!-!0Rd!K!K!K 5NNcHn4NN	** %cnnZ.H.H %!)!$)) %S^^I-F-F %!(!$	** %!-!$QRR 122 % %!*!$-- %2B %!*!$!-!$Z(( 	!_ 	!&DN&DN++--DKKU## 	! 	!&DN&DNDKKT"" 	!)DN XXDNN 		!sF## !W(=(= !!)!) 
 	!sG$$ !g)>)> !!)!) 
 >\))dn.G.G.L.L)W//??DN >\))-  ,,..33F;;199QJE#~fuf5Hw}}X.. ) RW^^H-E-E )3$ N566299%@@.*  >!IIE5zzBcrc
U*<uDEEE w'	 	K) ; ;IJJJ  
	t~,
1KKKB! +'*GNN2&& R>-A-Ag (*rr222/   >lJ777! AGOOD$6q$9::&q)&""
 "$!@!@ >lJ777B! +'* Tw~~b)) G+,@2,EFFFG G W__R((w~~b)) T+,MPR,RSSS 87T Tr   c                     | j         S )a(  Name of the ImageResource.


        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.
        )rZ   r;   s    r   filenamezRequest.filename  s     ~r   r9   c                     | j         S )zThe (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;   s    r   rS   zRequest.extension  s     r   c                     | j         S N)r\   r;   s    r   rT   zRequest.format_hint  s      r   formatc                 4    || _         | j        	|| _        d S d S r   )r\   r[   )r<   r   s     r   rT   zRequest.format_hint  s%    "?"$DOOO #"r   c                     | j         S )a.  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.
        )rf   r;   s    r   rq   zRequest.mode  s     zr   c                     | j         S )z3The dict of keyword arguments supplied by the user.)r]   r;   s    r   rr   zRequest.kwargs  s     |r   c                    | j         j        t          j        u }| j        | j        S | j        t          k    r9|rt                      | _        d| _        nwt          | j	                  | _        n\| j        t          k    r:|rt          | j        d          | _        n.t          | j        d          | _        n| j        t          k    r{| j        \  }}|rt                      | _        d| _        nt          j        |d          | _        | j                            |d          | _        t%          | j                  | _        n| j        t&          pt(          fv rq|rJ t+          j        d          }||                                sd}t1          | j        t3          |                    | _        t%          | j                  | _        | j        S )	a  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()``.
        NTwbrbr   IMAGEIO_REQUEST_TIMEOUTr   )timeout)rq   r=   r   r   rb   rY   r   r   rc   r`   rh   openr   ri   r_   zipfileZipFilera   SeekableFileObjectr   r   r   getenvisdigitr   float)r<   want_to_writer   namer   s        r   get_filezRequest.get_file  s    	)V\9
 :!:>Y&& 2$YY
&*##$T[11

^|++ 7!$-66

!$-66

^z))!/NHd <$YY
&*## !(# > >!]//c::
/
;;

^ 3G444$$$$i 9::Ggoo&7&7 gGGGDJ+DJ77DJzr   c                    | j         t          k    r| j        S | j        | j        }n*t          j                            | j                  d         }t          j        |d          \  }| _	        t	          j
        |           | j        j        t          j        k    rTt          | j	        d          5 }t!          j        |                                 |           ddd           n# 1 swxY w Y   | j	        S )zget_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.
        Nr
   imageio_r   )rY   rh   rZ   rS   r   rj   splitexttempfilemkstemprd   r|   rq   r=   r   r   r   shutilcopyfileobjr   )r<   rs   fdfiles       r   get_local_filenamezRequest.get_local_filename  s
    >\))>! ~)ng&&t~66q9'/'7Z'H'H$B$HRLLLy FK//$.55 >&t}}===> > > > > > > > > > > > > > >''s   1(C%%C),C)c                    | j         j        t          j        k    rd}| j        r't          | j                                                  }n'| j        r d| _        | j        	                                }|| j
        t          k    r|| _        n| j
        t          k    r| j                            |           ne| j
        t          k    rUt          j        | j        d         d          }|                    | j        d         |           |                                 | j        r0| j
        t          k    r | j                                         d| _        | j        r | j                                         d| _        | j        rO	 t+          j        | j                   n-# t.          $ r  t1          j        d| j         d           Y nw xY wd| _        d| _        dS )zWrap up this request.

        Finishes any pending reads or writes, closes any open files and frees
        any resources allocated by this request.
        NFr   ar
   z(Failed to delete the temporary file at `z`. Please report this issue.)rq   r=   r   r   rd   r   
read_bytesrc   rb   getvaluerY   r   r^   r   ri   r   r   r_   writestrr|   ra   r   remove	Exceptionrm   rn   r`   )r<   r   zfs      r   finishzRequest.finish  s    9,,E# .T122==??$ .&+#
++--  >Y..#(DLL^x//J$$U++++^z11 );A)>DDBKK 21 5u===HHJJJ
 : 	$.H44JDJ= 	!M!!! DM  	(	$.////   K,K K K    
 $(D  s   F 'GGc                 $    d| j         c| _         }|S )zFor internal use. In some situations a write action can have
        a result (bytes data). That is obtained with this function.
        N)r^   )r<   ress     r   
get_resultzRequest.get_resultQ  s    
 !$,c
r   c                 F    | j         |                                  | j         S )zuThe first 256 bytes of the file. These can be used to
        parse the header to determine the file-format.
        )re   _read_first_bytesr;   s    r   
firstbyteszRequest.firstbytesY  s(    
 #""$$$r      c                 4   | j         | j         d |         | _        d S 	 |                                 }nI# t          $ r< t          j                            | j                  rt                      | _        Y d S  w xY w	 |	                                }n# t          $ r d }Y nw xY wt          ||          | _        	 |t          d          |                    |           d S # t          $ r* d | _        | j        t          k    rt          d          Y d S w xY w)Nzcannot seek with Nonez*Cannot seek back after getting firstbytes!)r`   re   r   r   r   rj   r   r   r   tellr   read_n_bytesseekrb   rY   r   )r<   Nfr   s       r   r   zRequest._read_first_bytesb  sE   ;"#{2A2DMMOO   7==// ',wwD$FF	
FFHH     ,Aq11D	P9#$;<<<q					 P P P!
>X--!"NOOO .--	Ps4   4 AA:8A:>B B"!B";&C# #0DD)r9   N)r   )r   r   r   r   r   rt   rg   rO   r   rS   r	   rT   setterrq   rr   r   r   r   r   r   r   r   r   r   rR   rR      s        6 04 >' >' >'# >' >' >' >'@DT DT DTL 	 	 X	 3    X !Xc] ! ! ! X! %# %$ % % % %
   X   X6 6 6p( ( (.1 1 1 1f       X P P P P P Pr   rR   c                     t                      }t          |          |k     r@|                     |t          |          z
            }|sn||z  }t          |          |k     @|S )zread_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   bbextra_bytess       r   r   r     sd     
B
b''A++ffQR[)) 	
k	 b''A++
 Ir   c                   F    e Zd ZdZd ZddZd Zd ZddZd	 Z	d
 Z
d ZdS )r   zA readonly wrapper file object that add support for seeking, even if
    the wrapped file object does not. The allows us to stream from http and
    still use Pillow.
    c                 L    || _         d| _        d| _        d| _        d| _        d S )Nr   r   F)r   _i_buffer	_have_allclosed)r<   r   s     r   rt   zSeekableFileObject.__init__  s)    r   Nc                    |nt          |          }|dk     rd }| j        sd}|!| j                                        }d| _        n[| j        |z   }|t          | j                  z
  }|dk    r4| j                            |          }t          |          |k     rd| _        | xj        |z  c_        || j        | j        d          }n| j        | j        | j        |z            }| xj        t          |          z  c_        |S )Nr   r   T)intr   r   r   r   r   r   )r<   nmorewant_i	want_morer   s         r   r   zSeekableFileObject.read  s   9AA1uu ~ 	!Dyv{{}}!%1"S%6%66	q==6;;y11D4yy9,,)-LLD LL 9,twyy)CC,tw145C3s88
r   c              #   H   K   | j                                         E d {V  d S r   )rb   readliner;   s    r   r   zSeekableFileObject.readline  s2      :&&(((((((((((r   c                     | j         S r   )r   r;   s    r   r   zSeekableFileObject.tell  s	    wr   r   c                    t          |          }|dk    r(|dk     rt          dt          |          z             |}nx|dk    rt          d| j        |z             }nY|dk    rA| j        s|                                  t          dt          | j                  |z             }nt          d|z            |t          | j                  k    rn1| j        s*|| j        k    sJ |                     || j        z
             || _        | j        S )Nr   znegative seek value r
   r   z(invalid whence (%s, should be 0, 1 or 2))	r   r5   r   maxr   r   r   r   r   )r<   r   rq   real_is       r   r   zSeekableFileObject.seek  s    FF1991uu !7#a&&!@AAAFFQYYDGaK((FFQYY> 		C--122FFG!KLLL S&&&& 	(DG####IIftw&'''wr   c                 F    d| _         | j                                         d S NT)r   r   r|   r;   s    r   r|   zSeekableFileObject.close  s    r   c                     dS )NFr   r;   s    r   isattyzSeekableFileObject.isatty  s    ur   c                     dS r   r   r;   s    r   seekablezSeekableFileObject.seekable  s    tr   r   )r   )r   r   r   r   rt   r   r   r   r   r|   r   r   r   r   r   r   r     s         
     B) ) )     8        r   r   c                       e Zd ZdZdS )InitializationErrora  The plugin could not initialize from the given request.

    This is a _internal_ error that is raised by plugins that fail to handle
    a given request. We use this to differentiate incompatibility between
    a plugin and a request from an actual error/bug inside a plugin.

    N)r   r   r   r   r   r   r   r   r     s          	Dr   r   )'r   r   ior   r   r   r   enumrm   corer   r   pathlibr   urllib.parser   typingr	   r   r   rh   ri   r   r   r   Enumr   r   uniquer)   r   r   r   objectrR   r   r   r   r   r   r   r   <module>r     s-   
 
			            + + + + + + + +       ! ! ! ! ! !       	

    S$)        TY   8 L8 L8 L8 L8 L83	 L8 L8 L8^ 8  <7 @ 9	
 : J > * ( 9 K J  D D (  0!" I#$ @A<.NEN1  8@P @P @P @P @Pf @P @P @PF  X X X X X X X Xv		 		 		 		 		) 		 		 		 		 		r   