U
    f/ez                     @  sx  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	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 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& dd	l'm(Z(m)Z) dd
l*m+Z+ ddl,m-Z- e) Z.e/ee e Z0e01d ej2G dd dZ3ej2G dd dZ4ddddZ5dddddZ6ddddZ7dQdddddd Z8d!d" Z9d#dd$d%d&Z:dRd#d)d*d)d+dd,d-d.Z;d)d)d/d0d1Z<d2d3d4d5d6Z=d*d7d8d9d:Z>d#d;d;d<d=d>Z?dSd#d)d;d*ddd;d+dd@	dAdBZ@G dCdD dDejAeZBG dEdF dFejCZDdGdd)d)d;ddHdIdJdKZEd#dddLdMZFd#d)ddNdOdPZGdS )TzCommon IO api utilities    )annotationsN)abc)BufferedIOBaseBytesIO	RawIOBaseStringIOTextIOWrapper)IOAnyAnyStrMappingcast)urljoinurlparseuses_netlocuses_paramsuses_relative)BufferCompressionDictCompressionOptionsFileOrBufferFilePathOrBufferStorageOptions)get_lzma_fileimport_lzma)import_optional_dependency)is_file_like c                   @  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 )IOArgsa)  
    Return value of io/common.py:_get_filepath_or_buffer.

    Note (copy&past from io/parsers):
    filepath_or_buffer can be Union[FilePathOrBuffer, s3fs.S3File, gcsfs.GCSFile]
    though mypy handling of conditional imports is difficult.
    See https://github.com/python/mypy/issues/1297
    r   filepath_or_bufferstrencodingmoder   compressionFboolshould_closeN)__name__
__module____qualname____doc____annotations__r%    r+   r+   4/tmp/pip-unpacked-wheel-tiezk1ph/pandas/io/common.pyr   :   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.
    r   handler   r#   )default_factoryzlist[Buffer]created_handlesFr$   
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)r1   
isinstancer.   r   AssertionErrorflushdetachr0   removecloseOSError
ValueError)selfr.   r+   r+   r,   r;   a   s    


zIOHandles.closec                 C  s   | S Nr+   r>   r+   r+   r,   	__enter__u   s    zIOHandles.__enter__r
   )argsr5   c                 G  s   |    d S r?   )r;   )r>   rB   r+   r+   r,   __exit__x   s    zIOHandles.__exit__N)r&   r'   r(   r)   r*   dataclassesfieldlistr0   r1   r2   r;   rA   rC   r+   r+   r+   r,   r-   L   s   
r-   r$   r4   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)r6   r    	parse_urlscheme_VALID_URLSurlr+   r+   r,   is_url|   s    
rL   zFileOrBuffer[AnyStr])r   r5   c                 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
    )r6   r    ospath
expanduser)r   r+   r+   r,   _expand_user   s    
rP   r3   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)r6   r$   	TypeError)headerr+   r+   r,   validate_header_arg   s    
rS   FzFilePathOrBuffer[AnyStr])r   convert_file_liker5   c                 C  s6   |st | rt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   r   r6   rM   PathLike
__fspath__rP   )r   rT   r+   r+   r,   stringify_path   s
    rW   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)rB   kwargsurllibr+   r+   r,   rZ      s    rZ   r   )rK   r5   c                 C  s   t | tod| ko| d S )zR
    Returns true if the given URL looks like
    something fsspec can handle
    z://)zhttp://zhttps://)r6   r    
startswithrJ   r+   r+   r,   is_fsspec_url   s
    

r^   utf-8rr    r   r   )r   r!   r#   r"   storage_optionsr5   c              	   C  s  t | } t|\}}t| |}|rHt| drHd|krHtjdtdd d}t||d}|dk	rl|d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|2}	|	jdd}
|
dkrddi}t|	 }W 5 Q R X t|||d|dS t| rht| tsXt| drp| dd} | dr| 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
rT   |dkr ddi}nt|}d|d< |j | fd|i|pFi   }Y nX t|||d|dS |rvt"dt| tt#t$j$frtt%| ||d|dS t&| 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 : {{'gzip', 'bz2', 'zip', 'xz', None}}, optional
    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gziprf   T)r   r!   r#   r%   r"   zs3a://zs3://zs3n://fsspecZbotocore)ClientErrorNoCredentialsErrorr"   Zanonz?storage_options passed with file object or non-fsspec file pathFz)Invalid file path or buffer object type: )(rW   get_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningdictreplacelowerUnicodeWarningr6   r    rL   rX   rY   RequestrZ   rm   getr   readr   r^   r7   r]   r   Zbotocore.exceptionsrp   rq   PermissionErrorImportErroropentupler=   bytesmmaprP   r   type)r   r!   r#   r"   ra   compression_methodZfsspec_moder\   Zreq_inforeqcontent_encodingreaderro   Zerr_types_to_retry_with_anonrp   rq   Zfile_objmsgr+   r+   r,   _get_filepath_or_buffer   s    !



 



r   )rN   r5   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:)rX   r   r   )rN   r   r+   r+   r,   file_path_to_url  s    r   z.gzz.bz2z.zipz.xz)rn   rj   ziprk   z"tuple[str | None, CompressionDict])r#   r5   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
    rf   z.If mapping, compression must have key 'method'N)r6   r   rx   popKeyErrorr=   )r#   compression_argsr   errr+   r+   r,   rr     s    
rr   
str | None)r   r#   r5   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 : {'infer', 'gzip', 'bz2', 'zip', 'xz', None}
        If 'infer' and `filepath_or_buffer` is path-like, then detect
        compression from the following extensions: '.gz', '.bz2', '.zip',
        or '.xz' (otherwise no compression).

    Returns
    -------
    string or None

    Raises
    ------
    ValueError on invalid compression specified.
    NZinferT)rT   Unrecognized compression type: z
Valid compression types are )	rW   r6   r    _compression_to_extensionitemsrz   endswithsortedr=   )r   r#   	extensionZvalidr   r+   r+   r,   rs     s$    

rs   T)	path_or_bufr"   r!   r#   
memory_mapis_texterrorsra   r5   c                 C  s  |pd}t | |r"d|kr"|d7 }t|tr4| }|dkrTtd| dd d t| ||||d}|j}	t|	||j|j	||j
d	 tk\}	}}
t|	t}t|j
}|d	}|r|j	d
d|_	|dkr|rt|	tsttjf |	|j	d|}	ntjf |	|j	d|}	n|dkr:tj|	fd|j	i|}	n|dkrt|	|j	f|}	|	j	dkr|
|	 |	 }t|dkr|	| }	n,t|dkrtd|  ntd| n.|dkrtt|	|j	}	nd| }t|t|	trt|
|	 nNt|	trV|jr@d|j	kr@t|	|j	|j|dd}	nt|	|j	}	|
|	 d}|r|stt |	|j	rt|	|j|dd}	|
|	 t|jtp|j }|
  |jrt|jtrt|
|j t|	trtt|	|
|||j
dS )a  
    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 : str or dict, default None
        If string, specifies compression mode. If dict, value at key 'method'
        specifies compression mode. Compression mode must be one of {'infer',
        'gzip', 'bz2', 'zip', 'xz', None}. If compression mode is 'infer'
        and `filepath_or_buffer` is path-like, then detect compression from
        the following extensions: '.gz', '.bz2', '.zip', or '.xz' (otherwise
        no compression). If dict and compression mode is one of
        {'zip', 'gzip', 'bz2'}, or inferred as one of the above,
        other entries passed as additional 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' and 'bz2' as well as 'zip'.

    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
    r_   rc   )	Nstrictignorery   xmlcharrefreplacebackslashreplacenamereplacesurrogateescapesurrogatepassz%Invalid value for `encoding_errors` (z). Please see z=https://docs.python.org/3/library/codecs.html#error-handlers zfor valid values.)r!   r#   r"   ra   rf   rl   r   rn   )filenamer"   )fileobjr"   rj   r"   r   r`      r   zZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rk   r   r!   r   newlineF)r.   r0   r1   r2   r#   )_is_binary_moder6   r    rz   r=   r   r   _maybe_memory_mapr!   r"   r#   r   rx   r   ry   r7   rn   GzipFilerj   BZ2File_BytesZipFileappendnamelistlenr   r   lzmar   r%   reverser-   )r   r"   r!   r#   r   r   r   ra   Zioargsr.   handlesZis_pathr   Z	zip_namesr   r1   r+   r+   r,   
get_handle	  s    <


	









	
	
r   c                      s^   e Zd ZdZddddd f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.
    Nr   r    r   )filer"   archive_namec                   sB   | dd}|| _d | _dtji}|| t j||f| d S )Nrc   r   r#   )ry   r   multiple_write_bufferzipfileZIP_DEFLATEDupdatesuper__init__)r>   r   r"   r   r[   Z
kwargs_zip	__class__r+   r,   r      s    

z_BytesZipFile.__init__c                 C  s2   | j d kr"t|trt nt | _ | j | d S r?   )r   r6   r   r   r   rb   )r>   datar+   r+   r,   rb     s    
z_BytesZipFile.writer3   r4   c              	     sP   | j d ks| j jrd S | jp$| jp$d}| j  t || j   W 5 Q R X d S )Nr   )r   closedr   r   r   writestrgetvalue)r>   r   r   r+   r,   r8     s
    z_BytesZipFile.flushc                   s   |    t   d S r?   )r8   r   r;   r@   r   r+   r,   r;   &  s    z_BytesZipFile.closec                 C  s
   | j d kS r?   )fpr@   r+   r+   r,   r   *  s    z_BytesZipFile.closed)N)r&   r'   r(   r)   r   rb   r8   r;   propertyr   __classcell__r+   r+   r   r,   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

    r_   r   Tr	   r    r$   )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readableZ	writeabler   )access)r!   r   codecsgetincrementaldecoderdecoderr   
attributesrt   getattrr   filenoZACCESS_READ)r>   r   r!   r   r   	attributer+   r+   r,   r   <  s    
z_MMapWrapper.__init__)namec                   s$    j kr fddS tj S )Nc                     s
   j   S r?   )r   r+   r   r>   r+   r,   <lambda>Q      z*_MMapWrapper.__getattr__.<locals>.<lambda>)r   r   r   )r>   r   r+   r   r,   __getattr__O  s    
z_MMapWrapper.__getattr__r4   c                 C  s   | S r?   r+   r@   r+   r+   r,   __iter__T  s    z_MMapWrapper.__iter__intzstr | bytes)sizer5   c                 C  s(   | j |}| jr$|j| j| jdS |S )Nr   )r   r~   r   r!   r   )r>   r   contentr+   r+   r,   r~   W  s    z_MMapWrapper.readc                 C  s,   | j  }| j|}|dkr"t|dS )Nr   
)r   readliner   r   StopIterationlstrip)r>   Znewbytesr   r+   r+   r,   __next__`  s
    
z_MMapWrapper.__next__N)r_   r   T)r   )	r&   r'   r(   r)   r   r   r   r~   r   r+   r+   r+   r,   r   /  s      	r   r   z'tuple[FileOrBuffer, bool, list[Buffer]])r.   r   r!   r"   r   r   r5   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jt| |||}|   |	|  || |} W n t
k
r   d}Y nX | ||fS )zTry to memory map file/buffer.r   rc   r   r   F)rt   r6   r    r   r   r   r   r   r;   r:   	Exception)r.   r   r!   r"   r   r   r   wrappedr+   r+   r,   r   q  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)rW   r6   r    rM   rN   existsrQ   r=   )r   r   r+   r+   r,   file_exists  s    
r   )r.   r"   r5   c                 C  s\   d|ksd|krd|kS t jt jt jf}tt| |r:dS ttf}t| |pZdt	| d|kS )z+Whether the handle is opened in binary moderl   rc   Fr"   )
r   StreamWriterStreamReaderStreamReaderWriter
issubclassr   r   r   r6   r   )r.   r"   Ztext_classesZbinary_classesr+   r+   r,   r     s    r   )F)r_   Nr`   N)NNFTNN)Hr)   
__future__r   rj   r   collectionsr   rD   rn   ior   r   r   r   r   r   rM   typingr	   r
   r   r   r   urllib.parser   r   rG   r   r   r   ru   r   Zpandas._typingr   r   r   r   r   r   Zpandas.compatr   r   Zpandas.compat._optionalr   Zpandas.core.dtypes.commonr   r   setrI   discardZ	dataclassr   r-   rL   rP   rS   rW   rZ   r^   r   r   r   rr   rs   r   ZipFiler   Iteratorr   r   r   r   r+   r+   r+   r,   <module>   sl    
/ "
     5#>        n9B-