U
    Mf                     @  s&  d Z ddl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mZmZ ddlZddlZddlmZ ddlZddlmZmZmZmZmZmZmZmZmZ ddl m!Z!m"Z#m$Z$m%Z%m&Z& ddl'Z'ddl(Z(dd	l)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0 dd
l1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z: ddl;m<Z< e=e&e$ e% Z>e>?d e@dZAede*dZBejCG dd dZDejCG dd dee ZEdddddZFeddddd ZGedddd!d ZGd"d"dd#d ZGdd$d%d&d'ZHedxd)ddd*d+d,ZIedydddd*d-d,ZIdzd/dd"d*d0d,ZId1d2 ZJd3ddd4d5ZKe6e<d6 d7 d8d{d3dd;dd<dd=d>d?ZLddd@dAdBZMdCdDdEdFdGdHZNd;dIdJdKdLZOe6e<d6 d7 d8d3dMdMdNdOdPZPdQd$d@dRdSZQed(d(d(d(d(dTd3ddMd;ddUdMd<dVdW	dXdYZRed(d(d(d(d(d(dZd3ddMd;dd[dMd<d\dW	d]dYZRe6e<d6 d^ d8ddd.d_dddZd3ddMd;dddMd<d`dW	dadYZRG dbdc dce(jSeZTG ddde deejUZVG dfdg dgZWG dhdi diZXdjddddMddkdldmdnZYd3dddodpZZd3dddqdrdsZ[ej\dtdudvdwZ]dS )|zCommon IO api utilities    )annotationsN)abc)BufferedIOBaseBytesIO	RawIOBaseStringIO
TextIOBaseTextIOWrapper)Path)	IOAnyAnyStrGenericLiteralMappingTypeVarcastoverload)urljoinurlparseuses_netlocuses_paramsuses_relative)
BaseBufferCompressionDictCompressionOptionsFilePath
ReadBufferStorageOptionsWriteBuffer)get_lzma_file)import_optional_dependency)doc)find_stack_level)is_file_like)_shared_docs z^[A-Za-z][A-Za-z0-9+\-+.]*://BaseBufferT)boundc                   @  s>   e Zd ZU dZded< ded< ded< ded< d	Zd
ed< dS )IOArgsz?
    Return value of io/common.py:_get_filepath_or_buffer.
    str | BaseBufferfilepath_or_bufferstrencodingmoder   compressionFboolshould_closeN)__name__
__module____qualname____doc____annotations__r1    r7   r7   4/tmp/pip-unpacked-wheel-eb6vo0j3/pandas/io/common.pyr)   D   s   
r)   c                   @  sz   e Zd ZU dZded< ded< ejedZded< d	Z	d
ed< d	Z
d
ed< ddddZddddZdddddZdS )	IOHandlesau  
    Return value of io/common.py:get_handle

    Can be used as a context manager.

    This is used to easily close created buffers and to handle corner cases when
    TextIOWrapper is inserted.

    handle: The file handle to be used.
    created_handles: All file handles that are created by get_handle
    is_wrapped: Whether a TextIOWrapper needs to be detached.
    z
IO[AnyStr]handler   r/   )default_factoryzlist[IO[bytes] | IO[str]]created_handlesFr0   
is_wrappedis_mmapNonereturnc              	   C  sz   | j r8t| jtst| j  | j  | j| j z| jD ]}|	  q@W n t
tfk
rh   Y nX g | _d| _ dS )z
        Close all created buffers.

        Note: If a TextIOWrapper was inserted, it is flushed and detached to
        avoid closing the potentially user-created buffer.
        FN)r=   
isinstancer:   r	   AssertionErrorflushdetachr<   removecloseOSError
ValueError)selfr:   r7   r7   r8   rG   g   s    


zIOHandles.closezIOHandles[AnyStr]c                 C  s   | S Nr7   rJ   r7   r7   r8   	__enter__{   s    zIOHandles.__enter__r   )argsrA   c                 G  s   |    d S rK   )rG   )rJ   rN   r7   r7   r8   __exit__~   s    zIOHandles.__exit__N)r2   r3   r4   r5   r6   dataclassesfieldlistr<   r=   r>   rG   rM   rO   r7   r7   r7   r8   r9   Q   s   
r9   objectr0   )urlrA   c                 C  s   t | tsdS t| jtkS )z
    Check to see if a URL has a valid protocol.

    Parameters
    ----------
    url : str or unicode

    Returns
    -------
    isurl : bool
        If `url` has a valid protocol return True otherwise False.
    F)rB   r,   	parse_urlscheme_VALID_URLSrT   r7   r7   r8   is_url   s    
rY   r,   )r+   rA   c                 C  s   d S rK   r7   r+   r7   r7   r8   _expand_user   s    r[   c                 C  s   d S rK   r7   rZ   r7   r7   r8   r[      s    zstr | BaseBufferTc                 C  s   t | trtj| S | S )a]  
    Return the argument with an initial component of ~ or ~user
    replaced by that user's home directory.

    Parameters
    ----------
    filepath_or_buffer : object to be converted if possible

    Returns
    -------
    expanded_filepath_or_buffer : an expanded filepath or the
                                  input if not expandable
    )rB   r,   ospath
expanduserrZ   r7   r7   r8   r[      s    
r?   )headerrA   c                 C  s   t | trtdd S )NzPassing a bool to header is invalid. Use header=None for no header or header=int or list-like of ints to specify the row(s) making up the column names)rB   r0   	TypeError)r_   r7   r7   r8   validate_header_arg   s    
ra   .r   )r+   convert_file_likerA   c                 C  s   d S rK   r7   r+   rb   r7   r7   r8   stringify_path   s    rd   c                 C  s   d S rK   r7   rc   r7   r7   r8   rd      s    FzFilePath | BaseBufferTc                 C  s2   |st | rtt| S t| tjr*|  } t| S )a  
    Attempt to convert a path-like object to a string.

    Parameters
    ----------
    filepath_or_buffer : object to be converted

    Returns
    -------
    str_filepath_or_buffer : maybe a string version of the object

    Notes
    -----
    Objects supporting the fspath protocol (python 3.6+) are coerced
    according to its __fspath__ method.

    Any other object is passed through unchanged, which includes bytes,
    strings, buffers, or anything else that's not even path-like.
    )r$   r   r'   rB   r\   PathLike
__fspath__r[   rc   r7   r7   r8   rd      s
    
c                  O  s   ddl }|jj| |S )z`
    Lazy-import wrapper for stdlib urlopen, as that imports a big chunk of
    the stdlib.
    r   N)urllib.requestrequesturlopen)rN   kwargsurllibr7   r7   r8   ri      s    ri   zFilePath | BaseBufferc                 C  s$   t | to"tt| o"| d S )zR
    Returns true if the given URL looks like
    something fsspec can handle
    )zhttp://zhttps://)rB   r,   r0   _RFC_3986_PATTERNmatch
startswithrX   r7   r7   r8   is_fsspec_url   s
    

ro   compression_optionsr+   )rp   utf-8rr   r   )r+   r-   r/   r.   storage_optionsrA   c              	   C  s  t | } t|\}}t| |}|rJt| drJd|krJtjdtt d d}t||d}d|kr|dkr|d	krt| d
| t	 |}d|krd|kr|d7 }t
| tr&t| r&|pi }ddl}|jj| |d}t|0}	|	jdd}
|
dkrddi}t|	 }W 5 Q R X t|||d|dS t| rPt
| ts@t| drX| dd} | drp| dd} td}g }z&td ddlm}m} ||tg}W n tk
r   Y nX z$|j | fd|i|pi   }W n^ t!|k
r<   |dkrddi}nt|}d|d< |j | fd|i|p.i   }Y nX t|||d|dS |r^t"dt
| tt#t$j$frtt%| ||d|dS t| dst| dsdt&|  }t"|t| ||d|dS )a  
    If the filepath_or_buffer is a url, translate and return the buffer.
    Otherwise passthrough.

    Parameters
    ----------
    filepath_or_buffer : a url, filepath (str, py.path.local or pathlib.Path),
                         or buffer
    {compression_options}

        .. versionchanged:: 1.4.0 Zstandard support.

    encoding : the encoding to use to decode bytes, default is 'utf-8'
    mode : str, optional

    storage_options : dict, optional
        Extra options that make sense for a particular storage connection, e.g.
        host, port, username, password, etc., if using a URL that will
        be parsed by ``fsspec``, e.g., starting "s3://", "gcs://". An error
        will be raised if providing this argument with a local path or
        a file-like buffer. See the fsspec and backend storage implementation
        docs for the set of allowed keys and values

        .. versionadded:: 1.2.0

    ..versionchange:: 1.2.0

      Returns the dataclass IOArgs.
    writebzDcompression has no effect when passing a non-binary object as input.)
stacklevelN)methodw)bz2xz)zutf-16zutf-32z( will not write the byte order mark for tr   )headerszContent-Encodinggziprw   T)r+   r-   r/   r1   r.   zs3a://zs3://zs3n://fsspecZbotocore)ClientErrorNoCredentialsErrorr.   Zanonz?storage_options passed with file object or non-fsspec file pathFreadz)Invalid file path or buffer object type: )'rd   get_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningr#   dictUnicodeWarningrB   r,   rY   rg   rh   Requestri   r|   getr   r   r)   ro   rC   rn   replacer!   Zbotocore.exceptionsr   r   PermissionErrorImportErroropentuplerI   bytesmmapr[   type)r+   r-   r/   r.   rs   compression_methodZfsspec_moderk   Zreq_inforeqcontent_encodingreaderr~   Zerr_types_to_retry_with_anonr   r   Zfile_objmsgr7   r7   r8   _get_filepath_or_buffer   s    %


 


r   )r]   rA   c                 C  s   ddl m} td|| S )z
    converts an absolute native path to a FILE URL.

    Parameters
    ----------
    path : a path in native format

    Returns
    -------
    a valid FILE URL
    r   )pathname2urlzfile:)rg   r   r   )r]   r   r7   r7   r8   file_path_to_url  s    r   z.gzz.bz2.zipz.xzz.zst)r}   ry   ziprz   zstdz"tuple[str | None, CompressionDict])r/   rA   c              
   C  s`   t | trPt| }z|d}W qX tk
rL } ztd|W 5 d}~X Y qXX ni }| }||fS )a  
    Simplifies a compression argument to a compression method string and
    a mapping containing additional arguments.

    Parameters
    ----------
    compression : str or mapping
        If string, specifies the compression method. If mapping, value at key
        'method' specifies compression method.

    Returns
    -------
    tuple of ({compression method}, Optional[str]
              {compression arguments}, Dict[str, Any])

    Raises
    ------
    ValueError on mapping missing 'method' key
    rw   z.If mapping, compression must have key 'method'N)rB   r   r   popKeyErrorrI   )r/   compression_argsr   errr7   r7   r8   r     s    
r   
str | None)r+   r/   rA   c                 C  s   |dkrdS |dkrZt | dd} t| ts.dS t D ]\}}|  |r6|  S q6dS |tkrf|S ddgtt }d| d| }t|dS )a/  
    Get the compression method for filepath_or_buffer. If compression='infer',
    the inferred compression method is returned. Otherwise, the input
    compression method is returned unchanged, unless it's invalid, in which
    case an error is raised.

    Parameters
    ----------
    filepath_or_buffer : str or file handle
        File path or object.
    {compression_options}

        .. versionchanged:: 1.4.0 Zstandard support.

    Returns
    -------
    string or None

    Raises
    ------
    ValueError on invalid compression specified.
    NZinferT)rb   Unrecognized compression type: z
Valid compression types are )	rd   rB   r,   _compression_to_extensionitemslowerendswithsortedrI   )r+   r/   	extensionZvalidr   r7   r7   r8   r     s$    

r   z
Path | strc                 C  s&   t | j}| s"td| ddS )z
    Check if parent directory of a file exists, raise OSError if it does not

    Parameters
    ----------
    path: Path or str
        Path to check parent directory of

    z1Cannot save file into a non-existent directory: ''N)r
   parentis_dirrH   )r]   r   r7   r7   r8   check_parent_directory/  s    

r   )r-   r/   
memory_maperrorsrs   zLiteral[False]zIOHandles[bytes])	path_or_bufr.   r-   r/   r   is_textr   rs   rA   c                C  s   d S rK   r7   r   r.   r-   r/   r   r   r   rs   r7   r7   r8   
get_handle>  s    r   )r-   r/   r   r   r   rs   zLiteral[True]zIOHandles[str]c                C  s   d S rK   r7   r   r7   r7   r8   r   M  s    r   Tz!IOHandles[str] | IOHandles[bytes]c                C  s  |pd}t | |r"d|kr"|d7 }t| t|tr@t| t| ||||d}|j}	t|	||j	|j
||jd tk\}	}}
t|	t}t|j}|d}d|kr|rtt|	 |r|dkr|j
dd|_
n |dkrd|j
kr| j
d7  _
|d	krJ|r.t|	tsttjf |	|j
d
|}	ntjf |	|j
d|}	n0|dkrptj|	fd|j
i|}	n
|dkrt|	|j
f|}	|	j
dkrz|
|	 |	 }t|dkr|	| }	n,t|dkrtd|  ntd| n|dkrt |	|j
}	nl|dkrhtd}d|j
kr>d|jf |i}nd|jf |i}|j|	fd|j
i|}	nd| }t|t|	trt|
|	 nNt|	tr|j	rd|j
krt|	|j
|j	|dd}	nt|	|j
}	|
|	 d}|s|j
dkrt|	t rt!|	|j	d}	nP|rf|s0t |	|j
rft"t#|	|j	|dd}	|
|	 t|jtpb|j$ }d|j
krt%|	dst&dt'|j d|
(  |j$rt|jtrt|
|j t)|	|
|||jdS ) aw  
    Get file handle for given path/buffer and mode.

    Parameters
    ----------
    path_or_buf : str or file handle
        File path or object.
    mode : str
        Mode to open path_or_buf with.
    encoding : str or None
        Encoding to use.
    {compression_options}

        .. versionchanged:: 1.0.0
           May now be a dict with key 'method' as compression mode
           and other keys as compression options if compression
           mode is 'zip'.

        .. versionchanged:: 1.1.0
           Passing compression options as keys in dict is now
           supported for compression modes 'gzip', 'bz2', 'zstd' and 'zip'.

        .. versionchanged:: 1.4.0 Zstandard support.

    memory_map : bool, default False
        See parsers._parser_params for more information.
    is_text : bool, default True
        Whether the type of the content passed to the file/buffer is string or
        bytes. This is not the same as `"b" not in mode`. If a string content is
        passed to a binary file/buffer, a wrapper is inserted.
    errors : str, default 'strict'
        Specifies how encoding and decoding errors are to be handled.
        See the errors argument for :func:`open` for a full list
        of options.
    storage_options: StorageOptions = None
        Passed to _get_filepath_or_buffer

    .. versionchanged:: 1.2.0

    Returns the dataclass IOHandles
    rq   ru   )r-   r/   r.   rs   rw   rr   r   r{   r&   r}   )filenamer.   )fileobjr.   ry   r.   r      r   zZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rz   	zstandardZdctxZcctxr   r-   r   newlineFrb)r-   r   z1Expected file path name or file-like object, got z type)r:   r<   r=   r>   r/   )*_is_binary_modecodecslookuprB   r,   lookup_errorr   r+   _maybe_memory_mapr-   r.   r/   r   r   r   r   r   rC   r}   GzipFilery   BZ2File_BytesZipFileappendnamelistlenr   rI   r    r!   ZstdDecompressorZZstdCompressorr   _BytesIOWrapperr	   
_IOWrapperr1   r   r`   r   reverser9   )r   r.   r-   r/   r   r   r   rs   Zioargsr:   handlesZis_pathr   Z	zip_namesr   Z	open_argsr   r=   r7   r7   r8   r   \  s   6



	




	


 



	
	
c                      sf   e Zd ZdZddddd fddZd	d
 Zdd Zdd fddZ fddZe	dd Z
  ZS )r   a  
    Wrapper for standard library class ZipFile and allow the returned file-like
    handle to accept byte strings via `write` method.

    BytesIO provides attributes of file-like object and ZipFile.writestr writes
    bytes strings into a member of the archive.
    Nz1FilePath | ReadBuffer[bytes] | WriteBuffer[bytes]r,   r   )filer.   archive_namec                   sB   | dd}|| _d | _dtji}|| t j||f| d S )Nru   r&   r/   )r   r   multiple_write_bufferzipfileZIP_DEFLATEDupdatesuper__init__)rJ   r   r.   r   rj   Z
kwargs_zip	__class__r7   r8   r   h  s    

z_BytesZipFile.__init__c                 C  s<   t | jtjtfr8t| j}|jdkr2|djS |jS dS )z
        If an explicit archive_name is not given, we still want the file inside the zip
        file not to be named something.zip, because that causes confusion (GH39465).
        r   r&   N)	rB   r   r\   re   r,   r
   suffixwith_suffixname)rJ   r   r7   r7   r8   infer_filename|  s    

z_BytesZipFile.infer_filenamec                 C  s2   | j d kr"t|trt nt | _ | j | d S rK   )r   rB   r   r   r   rt   )rJ   datar7   r7   r8   rt     s    
z_BytesZipFile.writer?   r@   c              	     sR   | j d ks| j jrd S | jp&|  p&d}| j  t || j   W 5 Q R X d S )Nr   )r   closedr   r   r   writestrgetvalue)rJ   r   r   r7   r8   rD     s
    z_BytesZipFile.flushc                   s   |    t   d S rK   )rD   r   rG   rL   r   r7   r8   rG     s    z_BytesZipFile.closec                 C  s
   | j d kS rK   )fprL   r7   r7   r8   r     s    z_BytesZipFile.closed)N)r2   r3   r4   r5   r   r   rt   rD   rG   propertyr   __classcell__r7   r7   r   r8   r   ^  s    
r   c                   @  sb   e Zd ZdZddddddd	d
ZddddZd dddZddddddZddddZdS )_MMapWrappera  
    Wrapper for the Python's mmap class so that it can be properly read in
    by Python's csv.reader class.

    Parameters
    ----------
    f : file object
        File object to be mapped onto memory. Must support the 'fileno'
        method or have an equivalent attribute

    rq   strictTr   r,   r0   )fr-   r   decodec                 C  sn   || _ || _t||d| _|| _i | _dD ]"}t||s>q.t|| | j|< q.t	j	|
 dt	jd| _	d S )Nr   )seekablereadabler   )access)r-   r   r   getincrementaldecoderdecoderr   
attributesr   getattrr   filenoZACCESS_READ)rJ   r   r-   r   r   	attributer7   r7   r8   r     s    
z_MMapWrapper.__init__r   c                   s$    j kr fddS tj S )Nc                     s
   j   S rK   )r   r7   r   rJ   r7   r8   <lambda>      z*_MMapWrapper.__getattr__.<locals>.<lambda>)r   r   r   rJ   r   r7   r   r8   __getattr__  s    
z_MMapWrapper.__getattr__r@   c                 C  s   | S rK   r7   rL   r7   r7   r8   __iter__  s    z_MMapWrapper.__iter__intzstr | bytes)sizerA   c                 C  sD   | j |}| jr@| jdkr@|dkp.t||k }| jj||dS |S )Nrq   r   )final)r   r   r   r-   r   r   )rJ   r   contentr   r7   r7   r8   r     s
    z_MMapWrapper.readc                 C  s,   | j  }| j|}|dkr"t|dS )Nr&   
)r   readliner   r   StopIterationlstrip)rJ   Znewbytesr   r7   r7   r8   __next__  s
    
z_MMapWrapper.__next__N)rq   r   T)r   )	r2   r3   r4   r5   r   r   r   r   r   r7   r7   r7   r8   r     s      
r   c                   @  sR   e Zd ZddddZddddZd	d
ddZd	d
ddZd	d
ddZdS )r   r   bufferc                 C  s
   || _ d S rK   r   )rJ   r  r7   r7   r8   r     s    z_IOWrapper.__init__r,   r   c                 C  s   t | j|S rK   r   r  r   r7   r7   r8   r     s    z_IOWrapper.__getattr__r0   r@   c                 C  s   t | jdr| j S dS )Nr   T)r   r  r   rL   r7   r7   r8   r     s    
z_IOWrapper.readablec                 C  s   t | jdr| j S dS )Nr   T)r   r  r   rL   r7   r7   r8   r     s    
z_IOWrapper.seekablec                 C  s   t | jdr| j S dS )NwritableT)r   r  r  rL   r7   r7   r8   r    s    
z_IOWrapper.writableN)r2   r3   r4   r   r   r   r   r  r7   r7   r7   r8   r     s
   r   c                   @  s>   e Zd ZddddddZdddd	ZddddddZdS )r   rq   zStringIO | TextIOBaser,   )r  r-   c                 C  s   || _ || _d| _d S )Nr   )r  r-   overflow)rJ   r  r-   r7   r7   r8   r     s    z_BytesIOWrapper.__init__)attrc                 C  s   t | j|S rK   r  )rJ   r  r7   r7   r8   r     s    z_BytesIOWrapper.__getattr__r   z
int | Noner   )nrA   c                 C  st   | j d k	st| j || j}| j| }|d ksH|dk sH|t|krRd| _|S |d | }||d  | _|S d S )Nr   r   )r  rC   r   encoder-   r  r   )rJ   r  Z
bytestringZcombined_bytestringZ	to_returnr7   r7   r8   r     s    
z_BytesIOWrapper.readN)rq   )r   )r2   r3   r4   r   r   r   r7   r7   r7   r8   r     s   	r   r*   z/tuple[str | BaseBuffer, bool, list[BaseBuffer]])r:   r   r-   r.   r   r   rA   c              	   C  s   g }|t | dpt| tM }|s*| ||fS t| trh|rTd|krTt| |||dd} n
t| |} ||  zttt	| |||}W 5 t|D ]} |   qX || |||fS )zTry to memory map file/buffer.r   ru   r&   r   )
r   rB   r,   r   r   reversedrG   r   r   r   )r:   r   r-   r.   r   r   r   wrappedr7   r7   r8   r   "  s$    	




r   c              	   C  sH   d}t | } t| ts|S ztj| }W n ttfk
rB   Y nX |S )zTest whether file exists.F)rd   rB   r,   r\   r]   existsr`   rI   )r+   r
  r7   r7   r8   file_existsJ  s    
r  )r:   r.   rA   c                 C  sV   d|ksd|krd|kS t jt jt jf}tt| |r:dS t| t pTdt| d|kS )z+Whether the handle is opened in binary moder{   ru   Fr.   )	r   StreamWriterStreamReaderStreamReaderWriter
issubclassr   rB   _get_binary_io_classesr   )r:   r.   Ztext_classesr7   r7   r8   r   X  s      r   ztuple[type, ...]r@   c               	   C  sH   t tf} tddd}|dk	rD| d}| t|f7 } W 5 Q R X | S )z!IO classes that that expect bytesr   ignorer   Nr   )r   r   r!   r   Zstream_readerr   )Zbinary_classesr   r   r7   r7   r8   r  m  s    r  ).).)F)rq   Nrr   N)^r5   
__future__r   ry   r   collectionsr   rP   	functoolsr}   ior   r   r   r   r   r	   r   r\   pathlibr
   retypingr   r   r   r   r   r   r   r   r   urllib.parser   r   rU   r   r   r   r   r   Zpandas._typingr   r   r   r   r   r   r   Zpandas.compatr    Zpandas.compat._optionalr!   Zpandas.util._decoratorsr"   Zpandas.util._exceptionsr#   Zpandas.core.dtypes.commonr$   Zpandas.core.shared_docsr%   setrW   discardcompilerl   r'   Z	dataclassr)   r9   rY   r[   ra   rd   ri   ro   r   r   r   r   r   r   r   ZipFiler   Iteratorr   r   r   r   r  r   	lru_cacher  r7   r7   r7   r8   <module>   s    ,$	

0	  "
     8	#:$$$  EC(