o
    i,                     @  s  d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
 d dlmZ d dlmZmZmZmZmZ d dlmZmZ d d	lmZmZmZmZ d d
lmZ erdd dlmZ d dlmZ d dl m!Z! dKddZ"dLddZ#		 dMdNddZ$e	dOdddd dPd*d+Z%e	dOdddd dQd-d+Z%e	dOdddd dRd0d+Z%	dSd1d2dd dRd3d+Z%dd4dTd;d<Z&dUd>d?Z'dSdVd@dAZ(dWdBdCZ)dWdDdEZ*dXdIdJZ+dS )Y    )annotationsN)Sequence)contextmanager)BytesIOStringIO)Path)IOTYPE_CHECKINGAnycastoverload)_FSSPEC_AVAILABLEfsspec)is_int_sequenceis_path_or_str_sequenceis_str_sequencenormalize_filepath)NoDataError)Iterator)AbstractContextManager)StorageOptionsDictcolumns0Sequence[str] | Sequence[int] | str | int | Nonereturn1tuple[Sequence[int] | None, Sequence[str] | None]c                 C  s   | du rdS d}d}t | tr| g}||fS t | tr"| g}||fS t| r0t|  | }||fS t| r>t|  | }||fS d}t|)a0  
    Parse the `columns` argument of an I/O function.

    Disambiguates between column names and column indices input.

    Returns
    -------
    tuple
        A tuple containing the columns as a projection and a list of column names.
        Only one will be specified, the other will be `None`.
    N)NNzQthe `columns` argument should contain a list of all integers or all string values)
isinstancestrintr   _ensure_columns_are_uniquer   	TypeError)r   Z
projectionZcolumn_namesmsg r!   G/home/app/Keep/.python/lib/python3.10/site-packages/polars/io/_utils.pyparse_columns_arg   s(   

r#   Sequence[str] | Sequence[int]Nonec                 C  s*   t | t t| krd| }t|d S )Nz2`columns` arg should only have unique values, got )lenset
ValueError)r   r    r!   r!   r"   r   @   s   
r   row_index_name
str | Nonerow_index_offsetr   tuple[str, int] | Nonec                 C  s   | du rdS | |fS )z
    Parse the `row_index_name` and `row_index_offset` arguments of an I/O function.

    The Rust functions take a single tuple rather than two separate arguments.
    Nr!   )r)   r+   r!   r!   r"   parse_row_index_argsF   s   	r-   .)use_pyarrowraise_if_emptystorage_optionsfile*str | Path | list[str] | IO[bytes] | bytesencodingr.   boolr/   r0   StorageOptionsDict | NoneContextManager[str | BytesIO]c                C     d S Nr!   r1   r3   r.   r/   r0   r!   r!   r"   prepare_file_argU      r:   (str | Path | IO[str] | IO[bytes] | bytesc                C  r7   r8   r!   r9   r!   r!   r"   r:   `   r;   4str | Path | list[str] | IO[str] | IO[bytes] | bytes9ContextManager[str | list[str] | BytesIO | list[BytesIO]]c                C  r7   r8   r!   r9   r!   r!   r"   r:   k   r;   FTc                  sL  |r|  ni }|rtsd}t|td%dd}|r|dv nd	}|r%|nd
}|dr4|dd dfn|df\}}	|  t| trW|sN| j||	dd
} t	t
| d|dS t| trmt	t
|  d
d|  |dS t| t
r|st	t
|  j||	dd
d|  |dS |t	| d|  |dS t| tr|st	t
|  j||	dd
d| d|dS |t|  dS t| tr!t| rt| |S tr!ddlm | d dkr|r|t|  dS t| j||	d}
t	t
|
 d
| |dW  d   S 1 sw   Y  ||d< |	|d< tj| fi |S t| trjt| rjtd d! | D rjtrjddlm |rYtfd"d!| D rY| fd#d$| D S ||d< |	|d< tj| fi |S t| trt|  d} |st| j||	d}
t	t
|
 d
| |dW  d   S 1 sw   Y  || S )&u  
    Prepare file argument.

    Utility for read_[csv, parquet]. (not to be used by scan_[csv, parquet]).
    Returned value is always usable as a context.

    A `StringIO`, `BytesIO` file is returned as a `BytesIO`.
    A local path is returned as a string.
    An http URL is read into a buffer and returned as a `BytesIO`.

    When `encoding` is not `utf8` or `utf8-lossy`, the whole file is
    first read in Python and decoded using the specified encoding and
    returned as a `BytesIO` (for usage with `read_csv`). If encoding
    ends with "-lossy", characters that can't be decoded are replaced
    with `�`.

    A `bytes` file is returned as a `BytesIO` if `use_pyarrow=True`.

    When fsspec is installed, remote file(s) is (are) opened with
    `fsspec.open(file, **kwargs)` or `fsspec.open_files(file, **kwargs)`.
    If encoding is not `utf8` or `utf8-lossy`, decoding is handled by
    fsspec too.
    z3`fsspec` is required for `storage_options` argumentr1   r
   r   Iterator[Any]c                 s  s    z| V  W d S w r8   r!   r1   r!   r!   r"   managed_file   s   z&prepare_file_arg.<locals>.managed_file>   utf8
utf8-lossyTrB   z-lossyNireplacestrict)errorsbytes)contextr/   r   )rH   read_positionr/   r   )brH   rI   r/   zPath ()Zcheck_not_directoryr   infer_storage_optionsprotocol)r3   rF   r3   rF   c                 s  s    | ]}t |tV  qd S r8   )r   r   .0fr!   r!   r"   	<genexpr>   s    z#prepare_file_arg.<locals>.<genexpr>c                 3  s     | ]} |d  dkV  qdS )rO   r1   Nr!   rP   rM   r!   r"   rS     s    c                   s   g | ]}t | d qS )rL   r   rP   )check_not_dirr!   r"   
<listcomp>  s    
z$prepare_file_arg.<locals>.<listcomp>)r1   r
   r   r?   )copyr   ImportErrorr   endswithr   rG   decodeencode_check_emptyr   r   readtellr   
read_bytesr   r   looks_like_urlprocess_file_urlZfsspec.utilsrN   openr   listr4   allZ
open_files)r1   r3   r.   r/   r0   r    rA   Zhas_utf8_utf8_lossy_encodingZencoding_strZencoding_errorsrR   r!   )rU   rN   r"   r:   v   s   





	
	

"*
")rI   rJ   r   rH   r   rI   
int | Nonec                C  sH   |r"|   jdkr"|dv r|rd| dnd}d| | }t|| S )Nr   )r   r   z (buffer position = z; try seek(0) before reading?) zempty data from )	getbuffernbytesr   )rJ   rH   r/   rI   hintr    r!   r!   r"   r\     s   r\   pathc                 C  s   t d| t jd uS )Nz^(ht|f)tps?://)rematch
IGNORECASE)rj   r!   r!   r"   r`   )  s   r`   c                 C  sv   ddl m} || '}|r|dv rt| W  d    S t| |dW  d    S 1 s4w   Y  d S )Nr   )urlopen>   rB   rC   rB   )urllib.requestrn   r   r]   rZ   r[   )rj   r3   rn   rR   r!   r!   r"   ra   -  s   

$ra   c                   s   t  fdddD S )Nc                 3  s    | ]}| v V  qd S r8   r!   )rQ   charr@   r!   r"   rS   8  s    z"is_glob_pattern.<locals>.<genexpr>)*?[)anyr@   r!   r@   r"   is_glob_pattern7  s   ru   c                 C  s.   zt tj| dd W dS  ty   Y dS w )NT)	recursiveF)nextglobiglobStopIterationr@   r!   r!   r"   is_local_file;  s   r{   sourceqstr | Path | IO[bytes] | IO[str] | bytes | list[str] | list[Path] | list[IO[bytes]] | list[IO[str]] | list[bytes]Flist[str] | list[Path] | list[IO[str]] | list[IO[bytes]] | list[bytes]c                 C  s^   t | ttfrt| dd} nt| rdd | D } t | tr%t | ttfr-| g}td|S | S )NFrL   c                 S  s   g | ]}t |d dqS )FrL   rT   )rQ   r|   r!   r!   r"   rV   S  s    zget_sources.<locals>.<listcomp>z9list[bytes] | list[str] | list[IO[bytes]] | list[IO[str]])r   r   r   r   r   r   rG   r   )r|   outr!   r!   r"   get_sourcesD  s   
r   )r   r   r   r   )r   r$   r   r%   )Nr   )r)   r*   r+   r   r   r,   ).)r1   r2   r3   r*   r.   r4   r/   r4   r0   r5   r   r6   )r1   r<   r3   r*   r.   r4   r/   r4   r0   r5   r   r6   )r1   r=   r3   r*   r.   r4   r/   r4   r0   r5   r   r>   r8   )
rJ   r   rH   r   r/   r4   rI   re   r   r   )rj   r   r   r4   )rj   r   r3   r*   r   r   )r1   r   r   r4   )r|   r}   r   r~   ),
__future__r   rx   rk   collections.abcr   
contextlibr   ior   r   pathlibr   typingr   r	   r
   r   r   Zpolars._dependenciesr   r   Zpolars._utils.variousr   r   r   r   Zpolars.exceptionsr   r   r   ContextManagerZpolars._typingr   r#   r   r-   r:   r\   r`   ra   ru   r{   r   r!   r!   r!   r"   <module>   sn    

%

 '



	