a
    <b                     @  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   `/Users/vegardjervell/Documents/master/model/venv/lib/python3.9/site-packages/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  sx   | j r8t| jtsJ | j  | j  | j| j z| jD ]}|  q@W n t	t
fyf   Y n0 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	   flushdetachr<   removecloseOSError
ValueError)selfr:   r7   r7   r8   rF   g   s    


zIOHandles.closezIOHandles[AnyStr]c                 C  s   | S Nr7   rI   r7   r7   r8   	__enter__{   s    zIOHandles.__enter__r   )argsrA   c                 G  s   |    d S rJ   )rF   )rI   rM   r7   r7   r8   __exit__~   s    zIOHandles.__exit__N)r2   r3   r4   r5   r6   dataclassesfieldlistr<   r=   r>   rF   rL   rN   r7   r7   r7   r8   r9   Q   s   
r9   objectr0   )urlrA   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)rB   r,   	parse_urlscheme_VALID_URLSrS   r7   r7   r8   is_url   s    
rX   r,   )r+   rA   c                 C  s   d S rJ   r7   r+   r7   r7   r8   _expand_user   s    rZ   c                 C  s   d S rJ   r7   rY   r7   r7   r8   rZ      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
expanduserrY   r7   r7   r8   rZ      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    
r`   .r   )r+   convert_file_likerA   c                 C  s   d S rJ   r7   r+   ra   r7   r7   r8   stringify_path   s    rc   c                 C  s   d S rJ   r7   rb   r7   r7   r8   rc      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__rZ   rb   r7   r7   r8   rc      s
    
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)rM   kwargsurllibr7   r7   r8   rh      s    rh   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
startswithrW   r7   r7   r8   is_fsspec_url   s
    

rn   compression_optionsr+   )ro   utf-8rr   r   )r+   r-   r/   r.   storage_optionsrA   c              	   C  s  t | } t|\}}t| |}|rJt| drJd|vrJtjdtt d d}t||d}d|v r|dv r|d	v rt| d
| t	 |}d|vrd|vr|d7 }t
| tr<t| r<|pi }ddl}|jj| |d}t|:}	|	jdd}
|
dkrddi}t|	 }W d   n1 s 0    Y  t|||d|dS t| rbt
| tsVJ | drn| 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0 z$|j| fd|i|pi  }W n\ t |yN   |du rddi}nt|}d|d< |j| fd|i|p@i  }Y n0 t|||d|dS |rpt!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gziprv   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: )&rc   get_compression_methodinfer_compressionhasattrwarningswarnRuntimeWarningr#   dictUnicodeWarningrB   r,   rX   rf   rg   Requestrh   r{   getr   r   r)   rn   rm   replacer!   Zbotocore.exceptionsr~   r   PermissionErrorImportErroropentuplerH   bytesmmaprZ   type)r+   r-   r/   r.   rr   compression_methodZfsspec_moderj   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:)rf   r   r   )r\   r   r7   r7   r8   file_path_to_url  s    r   z.gzz.bz2.zipz.xzz.zst)r|   rx   zipry   zstdz"tuple[str | None, CompressionDict])r/   rA   c              
   C  sb   t | trRt| }z|d}W qZ tyN } ztd|W Y d}~qZd}~0 0 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
    rv   z.If mapping, compression must have key 'method'N)rB   r   r   popKeyErrorrH   )r/   compression_argsr   errr7   r7   r8   r     s    
"r   
str | None)r+   r/   rA   c                 C  s   |du rdS |dkrZt | dd} t| ts.dS t D ]\}}|  |r6|  S q6dS |tv 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)ra   Unrecognized compression type: z
Valid compression types are )	rc   rB   r,   _compression_to_extensionitemslowerendswithsortedrH   )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_dirrG   )r\   r   r7   r7   r8   check_parent_directory/  s    

r   )r-   r/   
memory_maperrorsrr   zLiteral[False]zIOHandles[bytes])	path_or_bufr.   r-   r/   r   is_textr   rr   rA   c                C  s   d S rJ   r7   r   r.   r-   r/   r   r   r   rr   r7   r7   r8   
get_handle>  s    r   )r-   r/   r   r   r   rr   zLiteral[True]zIOHandles[str]c                C  s   d S rJ   r7   r   r7   r7   r8   r   M  s    r   Tz!IOHandles[str] | IOHandles[bytes]c                C  s  |pd}t | |r"d|vr"|d7 }t| t|tr@t| t| ||||d}|j}	t|	||j	|j
||jd tv\}	}}
t|	t}t|j}|d}d|vr|rtt|	 |r|dkr|j
dd|_
n |dkrd|j
vr| j
d7  _
|d	krJ|r.t|	tsJ tjf |	|j
d
|}	ntjf |	|j
d|}	n<|dkrptj|	fd|j
i|}	n|dkrt|	|j
fi |}	|	j
dkr|
|	 |	 }t|dkr|	| }	n,t|dkrtd|  ntd| n|dkrt |	|j
}	nt|dkrttd}d|j
v rFd|jf i |i}nd|jf i |i}|j|	fd|j
i|}	nd| }t|t|	trJ |
|	 nNt|	tr|j	rd|j
vrt|	|j
|j	|dd}	nt|	|j
}	|
|	 d}|s"|j
dkr"t|	tr"t |	|j	d}	nP|rr|s<t |	|j
rrt!t"|	|j	|dd}	|
|	 t|jtpn|j# }d|j
v rt$|	dst%dt&|j d|
'  |j#rt|jtrJ |
|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
    rp   rt   )r-   r/   r.   rr   rv   rq   r   rz   r&   r|   )filenamer.   )fileobjr.   rx   r.   r      r   zZero files found in ZIP file z9Multiple files found in ZIP file. Only one file per ZIP: ry   	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   r|   GzipFilerx   BZ2File_BytesZipFileappendnamelistlenr   rH   r    r!   ZstdDecompressorZZstdCompressorr   _BytesIOWrapperr	   
_IOWrapperr1   r   r_   r   reverser9   )r   r.   r-   r/   r   r   r   rr   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                   sF   | dd}|| _d | _dtji}|| t j||fi | d S )Nrt   r&   r/   )r   r   multiple_write_bufferzipfileZIP_DEFLATEDupdatesuper__init__)rI   r   r.   r   ri   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[   rd   r,   r
   suffixwith_suffixname)rI   r   r7   r7   r8   infer_filename|  s    

z_BytesZipFile.infer_filenamec                 C  s2   | j d u r"t|trt nt | _ | j | d S rJ   )r   rB   r   r   r   rs   )rI   datar7   r7   r8   rs     s    
z_BytesZipFile.writer?   r@   c                   sf   | j d u s| j jrd S | jp&|  p&d}| j $ t || j   W d    n1 sX0    Y  d S )Nr   )r   closedr   r   r   writestrgetvalue)rI   r   r   r7   r8   rC     s
    z_BytesZipFile.flushc                   s   |    t   d S rJ   )rC   r   rF   rK   r   r7   r8   rF     s    z_BytesZipFile.closec                 C  s
   | j d u S rJ   )fprK   r7   r7   r8   r     s    z_BytesZipFile.closed)N)r2   r3   r4   r5   r   r   rs   rC   rF   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

    rp   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)rI   r   r-   r   r   	attributer7   r7   r8   r     s    
z_MMapWrapper.__init__r   c                   s$    j v r fddS tj S )Nc                     s
   j   S rJ   )r   r7   r   rI   r7   r8   <lambda>      z*_MMapWrapper.__getattr__.<locals>.<lambda>)r   r   r   rI   r   r7   r   r8   __getattr__  s    
z_MMapWrapper.__getattr__r@   c                 C  s   | S rJ   r7   rK   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 )Nrp   r   )final)r   r   r   r-   r   r   )rI   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)rI   Znewbytesr   r7   r7   r8   __next__  s
    
z_MMapWrapper.__next__N)rp   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 rJ   r   )rI   r   r7   r7   r8   r     s    z_IOWrapper.__init__r,   r   c                 C  s   t | j|S rJ   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   rK   r7   r7   r8   r     s    
z_IOWrapper.readablec                 C  s   t | jdr| j S dS )Nr   T)r   r   r   rK   r7   r7   r8   r     s    
z_IOWrapper.seekablec                 C  s   t | jdr| j S dS )NwritableT)r   r   r  rK   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   rp   zStringIO | TextIOBaser,   )r   r-   c                 C  s   || _ || _d| _d S )Nr   )r   r-   overflow)rI   r   r-   r7   r7   r8   r     s    z_BytesIOWrapper.__init__)attrc                 C  s   t | j|S rJ   r  )rI   r  r7   r7   r8   r     s    z_BytesIOWrapper.__getattr__r   z
int | Noner   )nrA   c                 C  st   | j d usJ | j || j}| j| }|d u sH|dk sH|t|krRd| _|S |d | }||d  | _|S d S )Nr   r   )r   r   encoder-   r  r   )rI   r  Z
bytestringZcombined_bytestringZ	to_returnr7   r7   r8   r     s    
z_BytesIOWrapper.readN)rp   )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|vrTt| |||dd} n
t| |} ||  z.ttt| |||}W t|D ]} | 	  qnt|D ]} | 	  q0 || |||fS )zTry to memory map file/buffer.r   rt   r&   r   )
r   rB   r,   r   r   r   r   r   reversedrF   )r:   r   r-   r.   r   r   r   wrappedr7   r7   r8   r   "  s(    	




r   c              	   C  sF   d}t | } t| ts|S ztj| }W n ttfy@   Y n0 |S )zTest whether file exists.F)rc   rB   r,   r[   r\   existsr_   rH   )r+   r	  r7   r7   r8   file_existsJ  s    
r
  )r:   r.   rA   c                 C  sV   d|v sd|v rd|v S t jt jt jf}tt| |r:dS t| t pTdt| d|v S )z+Whether the handle is opened in binary moderz   rt   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  s\   t tf} tddd}|durX| d}| t|f7 } W d   n1 sN0    Y  | S )z!IO classes that that expect bytesr   ignorer   Nr   )r   r   r!   r   stream_readerr   )Zbinary_classesr   r   r7   r7   r8   r  m  s    ,r  ).).)F)rp   Nrq   N)^r5   
__future__r   rx   r   collectionsr   rO   	functoolsr|   ior   r   r   r   r   r	   r   r[   pathlibr
   retypingr   r   r   r   r   r   r   r   r   urllib.parser   r   rT   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%   setrV   discardcompilerk   r'   Z	dataclassr)   r9   rX   rZ   r`   rc   rh   rn   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(