B
    Y0dz                 @  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Z5ddddZ6dddddZ7ddddZ8dQdddddd Z9d!d" Z:d#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ZAG dCdD dDe jBeZCG dEdF dFejDZEdGdd)d)d;ddHdIdJdKZFd#dddLdMZGd#d)ddNdOdPZHd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+   B/var/www/html/venv/lib/python3.7/site-packages/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  s~   | j r8t| jtst| j  | j  | j| j yx| jD ]}|	  qBW W n t
tfk
rl   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:   b   s    

zIOHandles.closec             C  s   | S )Nr+   )r=   r+   r+   r,   	__enter__v   s    zIOHandles.__enter__r
   )argsr4   c             G  s   |    d S )N)r:   )r=   r?   r+   r+   r,   __exit__y   s    zIOHandles.__exit__N)r&   r'   r(   r)   r*   dataclassesfieldlistr0   r1   r2   r:   r>   r@   r+   r+   r+   r,   r-   M   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)r5   r    	parse_urlscheme_VALID_URLS)urlr+   r+   r,   is_url}   s    
rH   zFileOrBuffer[AnyStr])r   r4   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    
rL   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)r5   r$   	TypeError)headerr+   r+   r,   validate_header_arg   s    
rO   FzFilePathOrBuffer[AnyStr])r   convert_file_liker4   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   r5   rI   PathLike
__fspath__rL   )r   rP   r+   r+   r,   stringify_path   s
    rS   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)r?   kwargsurllibr+   r+   r,   rV      s    rV   r   )rG   r4   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://)r5   r    
startswith)rG   r+   r+   r,   is_fsspec_url   s    
rZ   utf-8rr    r   r   )r   r!   r#   r"   storage_optionsr4   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 dQ R X t|||d|dS t| rht| tsXt| drp| dd} | dr| dd} td}g }y&td ddlm}m} ||tg}W n tk
r   Y nX y$|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gziprb   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: )(rS   get_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningdictreplacelowerUnicodeWarningr5   r    rH   rT   rU   RequestrV   ri   getr   readr   rZ   r6   rY   r   Zbotocore.exceptionsrl   rm   PermissionErrorImportErroropentupler<   bytesmmaprL   r   type)r   r!   r#   r"   r]   compression_methodZfsspec_moderX   Zreq_inforeqcontent_encodingreaderrk   Zerr_types_to_retry_with_anonrl   rm   Zfile_objmsgr+   r+   r,   _get_filepath_or_buffer   s    !




 

"
r   )rJ   r4   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:)rT   r   r   )rJ   r   r+   r+   r,   file_path_to_url  s    r   z.gzz.bz2z.zipz.xz)rj   rf   ziprg   z"tuple[str | None, CompressionDict])r#   r4   c          
   C  s`   t | trPt| }y|d}W qX tk
rL } ztd|W d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
    rb   z.If mapping, compression must have key 'method'N)r5   r   rt   popKeyErrorr<   )r#   compression_argsr   errr+   r+   r,   rn     s    
rn   z
str | None)r   r#   r4   c             C  s   |dkrdS |dkrZt | dd} t| ts.dS x&t D ]\}}|  |r8|S q8W d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)rP   zUnrecognized compression type: z
Valid compression types are )	rS   r5   r    _compression_to_extensionitemsrv   endswithsortedr<   )r   r#   	extensionZvalidr   r+   r+   r,   ro     s     
ro   T)	path_or_bufr"   r!   r#   
memory_mapis_texterrorsr]   r4   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
    zutf-8r_   )	Nstrictignoreru   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"   r]   rb   rh   r   rj   )filenamer"   )fileobjr"   rf   r"   r   r\      r   zZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: rg   zUnrecognized compression type: )r!   r   newlineF)r.   r0   r1   r2   r#   )_is_binary_moder5   r    rv   r<   r   r   _maybe_memory_mapr!   r"   r#   r   rt   r   ru   r6   rj   GzipFilerf   BZ2File_BytesZipFileappendnamelistlenr}   r   lzmar   r%   reverser-   )r   r"   r!   r#   r   r   r   r]   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    z
str | None)filer"   archive_namec               sB   | dd}|| _d | _dtji}|| t j||f| d S )Nr_   r   r#   )ru   r   multiple_write_bufferzipfileZIP_DEFLATEDupdatesuper__init__)r=   r   r"   r   rW   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 )N)r   r5   r   r   r   r^   )r=   datar+   r+   r,   r^     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 d Q R X d S )Nr   )r   closedr   r   r   writestrgetvalue)r=   r   )r   r+   r,   r7     s
    z_BytesZipFile.flushc               s   |    t   d S )N)r7   r   r:   )r=   )r   r+   r,   r:   '  s    z_BytesZipFile.closec             C  s
   | j d kS )N)fp)r=   r+   r+   r,   r   +  s    z_BytesZipFile.closed)N)r&   r'   r(   r)   r   r^   r7   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

    utf-8r   Tr	   r    r$   )fr!   r   decodec             C  sr   || _ || _t||d| _|| _i | _x*dD ]"}t||s@q0t|| | j|< q0W t	j	|
 dt	jd| _	d S )N)r   )seekablereadableZ	writeabler   )access)r!   r   codecsgetincrementaldecoderdecoderr   
attributesrp   getattrr   fileno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 )N)r   r+   )r   r=   r+   r,   <lambda>R      z*_MMapWrapper.__getattr__.<locals>.<lambda>)r   r   r   )r=   r   r+   )r   r=   r,   __getattr__P  s    
z_MMapWrapper.__getattr__)r4   c             C  s   | S )Nr+   )r=   r+   r+   r,   __iter__U  s    z_MMapWrapper.__iter__intzstr | bytes)sizer4   c             C  s:   | j |}| jr6|dkp$t||k }| jj||dS |S )Nr   )final)r   rz   r   r   r   )r=   r   contentr   r+   r+   r,   rz   X  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__b  s
    
z_MMapWrapper.__next__N)r   r   T)r   )	r&   r'   r(   r)   r   r   r   rz   r   r+   r+   r+   r,   r   0  s     
r   r   z'tuple[FileOrBuffer, bool, list[Buffer]])r.   r   r!   r"   r   r   r4   c             C  s   g }|t | dpt| tM }|s*| ||fS t| trh|rTd|krTt| |||dd} n
t| |} ||  y:ttjt| |||}|   |	|  || |} W n t
k
r   d}Y nX | ||fS )zTry to memory map file/buffer.r   r_   r   )r!   r   r   F)rp   r5   r    r}   r   r   r   r   r:   r9   	Exception)r.   r   r!   r"   r   r   r   wrappedr+   r+   r,   r   s  s(    	






r   c          	   C  sH   d}t | } t| ts|S ytj| }W n ttfk
rB   Y nX |S )zTest whether file exists.F)rS   r5   r    rI   rJ   existsrM   r<   )r   r   r+   r+   r,   file_exists  s    
r   )r.   r"   r4   c             C  s`   d|ksd|krd|kS t jt jt jtjf}tt| |r>dS tt	f}t
| |p^dt| d|kS )z+Whether the handle is opened in binary moderh   r_   Fr"   )r   StreamWriterStreamReaderStreamReaderWritertempfileSpooledTemporaryFile
issubclassr   r   r   r5   r   )r.   r"   Ztext_classesZbinary_classesr+   r+   r,   r     s    r   )F)r[   Nr\   N)NNFTNN)Ir)   
__future__r   rf   r   collectionsr   rA   rj   ior   r   r   r   r   r   rI   r   typingr	   r
   r   r   r   urllib.parser   r   rD   r   r   r   rq   r   Zpandas._typingr   r   r   r   r   r   Zpandas.compatr   r   Zpandas.compat._optionalr   Zpandas.core.dtypes.commonr   r   setrF   discard	dataclassr   r-   rH   rL   rO   rS   rV   rZ   r   r   r   rn   ro   r   ZipFiler   Iteratorr   r   r   r   r+   r+   r+   r,   <module>   sd    
0 
    0#>       f9C-