o
    gz                     @  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	m
Z
mZmZmZ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 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/e0ee e Z1e12d ej3G dd dZ4ej3G dd dZ5dUddZ6dVddZ7dWddZ8	dXdYdd Z9d!d" Z:dZd%d&Z;	'		(	d[d\d0d1Z<d]d3d4Z=d5d6d7d8d9Z>d^d;d<Z?d_d>d?Z@				@		d`dadEdFZAG dGdH dHe jBeZCG dIdJ dJejDZEdbdOdPZFdcdQdRZGdddSdTZHdS )ez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+   N/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pandas/io/common.pyr   ;   s   
 	r   c                   @  sl   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ZdddZ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returnNonec              	   C  sx   | j rt| jtsJ | j  | j  | j| j z| jD ]}|  q W n t	t
fy3   Y nw 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   flushdetachr0   removecloseOSError
ValueError)selfr.   r+   r+   r,   r9   b   s   




zIOHandles.closec                 C     | S Nr+   r<   r+   r+   r,   	__enter__v      zIOHandles.__enter__argsr
   c                 G  s   |    d S r>   )r9   )r<   rB   r+   r+   r,   __exit__y   s   zIOHandles.__exit__Nr3   r4   )r3   r-   )rB   r
   r3   r4   )r&   r'   r(   r)   r*   dataclassesfieldlistr0   r1   r2   r9   r@   rC   r+   r+   r+   r,   r-   M   s   
 

r-   r3   r$   c                 C  s   t | tsdS t| jtv 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)r5   r    	parse_urlscheme_VALID_URLSurlr+   r+   r,   is_url}   s   
rM   r   FileOrBuffer[AnyStr]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
    )r5   r    ospath
expanduser)r   r+   r+   r,   _expand_user   s   
rR   r4   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)r5   r$   	TypeError)headerr+   r+   r,   validate_header_arg   s
   
rU   FFilePathOrBuffer[AnyStr]convert_file_likec                 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   r5   rO   PathLike
__fspath__rR   )r   rW   r+   r+   r,   stringify_path   s
   rZ   c                  O  s   ddl }|jj| i |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,   r]      s   r]   rL   r   c                 C  s   t | tod| v o| d S )zR
    Returns true if the given URL looks like
    something fsspec can handle
    z://)zhttp://zhttps://)r5   r    
startswithrK   r+   r+   r,   is_fsspec_url   s
   

ra   utf-8rr!   r    r#   r   r"   storage_optionsr   c              	   C  s  t | } t|\}}t| |}|r$t| dr$d|vr$tjdtdd d}t||d}|dur6|dd		 }d
|v rM|dv rM|dv rMt| d| t
 |}d|vr[d|vr[|d7 }t| trt| r|pgi }ddl}|jj| |d}t|}	|	jdd}
|
dkrddi}t|	 }W d   n1 sw   Y  t|||d|dS t| r4t| tsJ | dr| dd} | dr| dd} td}g }ztd ddlm}m} ||tg}W n	 ty   Y nw z|j| fd|i|pi  }W n. t |y*   |du rddi}nt|}d|d< |j| fd|i|p#i  }Y nw t|||d|dS |r;t!dt| tt"t#j#frPtt$| ||d|dS t%| s`d 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gzipri   T)r   r!   r#   r%   r"   zs3a://zs3://zs3n://fsspecbotocore)ClientErrorNoCredentialsErrorr"   anonz?storage_options passed with file object or non-fsspec file pathFz)Invalid file path or buffer object type: )'rZ   get_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningdictreplacelowerUnicodeWarningr5   r    rM   r[   r\   Requestr]   rp   getr   readr   ra   r`   r   botocore.exceptionsrt   ru   PermissionErrorImportErroropentupler;   bytesmmaprR   r   type)r   r!   r#   r"   rd   compression_methodfsspec_moder_   req_inforeqcontent_encodingreaderrr   err_types_to_retry_with_anonrt   ru   file_objmsgr+   r+   r,   _get_filepath_or_buffer   s   !







r   rP   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:)r[   r   r   )rP   r   r+   r+   r,   file_path_to_url  s   r   z.gzz.bz2z.zipz.xz)rq   rm   ziprn   "tuple[str | None, CompressionDict]c              
   C  sZ   t | tr%t| }z
|d}W ||fS  ty$ } ztd|d}~ww 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
    ri   z.If mapping, compression must have key 'method'N)r5   r   r}   popKeyErrorr;   )r#   compression_argsr   errr+   r+   r,   rw     s   

rw   
str | Nonec                 C  s   |du rdS |dkr-t | dd} t| tsdS t D ]\}}|  |r*|  S qdS |tv r3|S ddgtt }d| d| }t|)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.
    NinferT)rW   Unrecognized compression type: z
Valid compression types are )	rZ   r5   r    _compression_to_extensionitemsr   endswithsortedr;   )r   r#   	extensionvalidr   r+   r+   r,   rx     s*   
rx   Tpath_or_buf
memory_mapis_texterrorsc                 C  s  |pd}t | |rd|vr|d7 }t|tr| }|dvr*td| dd d t| ||||d}|j}	t|	||j|j	||j
d	 tv\}	}}
t|	t}t|j
}|d	}|r|j	d
d|_	|dkr|r|t|	tsoJ tjd|	|j	d|}	nstjd|	|j	d|}	nf|dkrtj|	fd|j	i|}	nU|dkrt|	|j	fi |}	|	j	dkr|
|	 |	 }t|dkr|	| }	n+t|dkrtd|  td| n|dkrtt|	|j	}	n	d| }t|t|	trJ |
|	 n't|	tr#|jrd|j	vrt|	|j	|j|dd}	nt|	|j	}	|
|	 d}|rK|s2t |	|j	rKt|	|j|dd}	|
|	 t|jtpI|j }|
  |jrbt|jtr\J |
|j t|	trjJ 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
    rb   rf   )	Nstrictignorer~   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"   rd   ri   ro   r   rq   )filenamer"   )fileobjr"   rm   r"   r   rc      r   zZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rn   r   r!   r   newlineF)r.   r0   r1   r2   r#   Nr+   )_is_binary_moder5   r    r   r;   r   r   _maybe_memory_mapr!   r"   r#   r   r}   r   r~   rq   GzipFilerm   BZ2File_BytesZipFileappendnamelistlenr   r   lzmar   r%   reverser-   )r   r"   r!   r#   r   r   r   rd   ioargsr.   handlesis_pathr   	zip_namesr   r1   r+   r+   r,   
get_handle
  s   <



	





	

	r   c                      sT   e Zd ZdZ	dd fd	d
Zdd Z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.
    Nfiler   r"   r    archive_namer   c                   sF   | dd}|| _d | _dtji}|| t j||fi | d S )Nrf   r   r#   )r~   r   multiple_write_bufferzipfileZIP_DEFLATEDupdatesuper__init__)r<   r   r"   r   r^   
kwargs_zip	__class__r+   r,   r     s   

z_BytesZipFile.__init__c                 C  s2   | j d u rt|trt nt | _ | j | d S r>   )r   r5   r   r   r   re   )r<   datar+   r+   r,   re     s   
z_BytesZipFile.writer3   r4   c                   sf   | j d u s	| j jrd S | jp| jpd}| j  t || j   W d    d S 1 s,w   Y  d S )Nr   )r   closedr   r   r   writestrgetvalue)r<   r   r   r+   r,   r6     s   "z_BytesZipFile.flushc                   s   |    t   d S r>   )r6   r   r9   r?   r   r+   r,   r9   '  s   z_BytesZipFile.closec                 C  s
   | j d u S r>   )fpr?   r+   r+   r,   r   +  s   
z_BytesZipFile.closedr>   )r   r   r"   r    r   r   rD   )r&   r'   r(   r)   r   re   r6   r9   propertyr   __classcell__r+   r+   r   r,   r     s    
r   c                   @  sL   e Zd ZdZ			ddddZdddZd ddZd!d"ddZ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

    rb   r   Tfr	   r!   r    r   decoder$   c                 C  sn   || _ || _t||d| _|| _i | _dD ]}t||sqt|| | j|< qt	j	|
 dt	jd| _	d S )N)r   )seekablereadable	writeabler   )access)r!   r   codecsgetincrementaldecoderdecoderr   
attributesry   getattrr   filenoACCESS_READ)r<   r   r!   r   r   	attributer+   r+   r,   r   =  s   
z_MMapWrapper.__init__namec                   s$    j v r fddS tj S )Nc                     s
   j   S r>   )r   r+   r   r<   r+   r,   <lambda>R  s   
 z*_MMapWrapper.__getattr__.<locals>.<lambda>)r   r   r   )r<   r   r+   r   r,   __getattr__P  s   
z_MMapWrapper.__getattr__r3   c                 C  r=   r>   r+   r?   r+   r+   r,   __iter__U  rA   z_MMapWrapper.__iter__sizeintstr | bytesc                 C  s:   | j |}| jr|dkpt||k }| jj||dS |S )Nr   )final)r   r   r   r   r   )r<   r   contentr   r+   r+   r,   r   X  s
   z_MMapWrapper.readc                 C  s,   | j  }| j|}|dkrt|dS )Nr   
)r   readliner   r   StopIterationlstrip)r<   newbytesr   r+   r+   r,   __next__b  s
   

z_MMapWrapper.__next__N)rb   r   T)r   r	   r!   r    r   r    r   r$   )r   r    )r3   r   )r   )r   r   r3   r   )r3   r    )	r&   r'   r(   r)   r   r   r   r   r   r+   r+   r+   r,   r   0  s    


r   r.   r   r   'tuple[FileOrBuffer, bool, list[Buffer]]c                 C  s   g }|t | dpt| tM }|s| ||fS t| tr4|r*d|vr*t| |||dd} nt| |} ||  zttjt| |||}|   |	|  || |} W n t
y\   d}Y nw | ||fS )zTry to memory map file/buffer.r   rf   r   r   F)ry   r5   r    r   r   r   r   r   r9   r8   	Exception)r.   r   r!   r"   r   r   r   wrappedr+   r+   r,   r   s  s,   	






r   c              	   C  sF   d}t | } t| ts|S z	tj| }W |S  ttfy"   Y |S w )zTest whether file exists.F)rZ   r5   r    rO   rP   existsrS   r;   )r   r   r+   r+   r,   file_exists  s   
r   c                 C  s`   d|v sd|v rd|v S t jt jt jtjf}tt| |rdS tt	f}t
| |p/dt| d|v S )z+Whether the handle is opened in binary modero   rf   Fr"   )r   StreamWriterStreamReaderStreamReaderWritertempfileSpooledTemporaryFile
issubclassr   r   r   r5   r   )r.   r"   text_classesbinary_classesr+   r+   r,   r     s   r   )r3   r$   )r   rN   r3   rN   rD   )F)r   rV   rW   r$   r3   rN   )rL   r   r3   r$   )rb   Nrc   N)r   r   r!   r    r#   r   r"   r    rd   r   r3   r   )rP   r    r3   r    )r#   r   r3   r   )r   r   r#   r   r3   r   )NNFTNN)r   r   r"   r    r!   r   r#   r   r   r$   r   r$   r   r   rd   r   r3   r-   )r.   r   r   r$   r!   r    r"   r    r   r   r   r$   r3   r   )r   r   r3   r$   )r.   r   r"   r    r3   r$   )Ir)   
__future__r   rm   r   collectionsr   rE   rq   ior   r   r   r   r   r   rO   r   typingr	   r
   r   r   r   urllib.parser   r   rH   r   r   r   rz   r   pandas._typingr   r   r   r   r   r   pandas.compatr   r   pandas.compat._optionalr   pandas.core.dtypes.commonr   r   setrJ   discard	dataclassr   r-   rM   rR   rU   rZ   r]   ra   r   r   r   rw   rx   r   ZipFiler   Iteratorr   r   r   r   r+   r+   r+   r,   <module>   sp     

/

"

 
5

#> n9
C
-