
    q-PhP)                       d dl mZ d dlZd dl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 d dlmZmZmZ d dlmZmZ d d	lmZ erd d
lmZmZ d dlmZ d;dZd<dZ	 	 d=d>dZ e	 d?ddddd@d%            Z!e	 d?dddddAd'            Z!e	 d?dddddBd*            Z!	 dCd+d,dddBd-Z!dd.dDd5Z"dEd7Z#dCdFd8Z$dGd9Z%dGd:Z&dS )H    )annotationsN)contextmanager)BytesIOStringIO)Path)IOTYPE_CHECKINGAnyoverload)is_int_sequenceis_str_sequencenormalize_filepath)_FSSPEC_AVAILABLEfsspec)NoDataError)IteratorSequence)AbstractContextManagercolumns0Sequence[str] | Sequence[int] | str | int | Nonereturn1tuple[Sequence[int] | None, Sequence[str] | None]c                $   | dS d}d}t          | t                    r| g}nlt          | t                    r| g}nSt          |           rt	          |            | }n2t          |           rt	          |            | }nd}t          |          ||fS )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   
projectioncolumn_namesmsgs       P/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/io/_utils.pyparse_columns_argr#      s     z'+J)-L'3 y	GS	!	! 
Y

		!	! "7+++		!	! "7+++

ann|##    Sequence[str] | Sequence[int]Nonec                    t          |           t          t          |                     k    rd| }t          |          d S )Nz2`columns` arg should only have unique values, got )lenset
ValueError)r   r!   s     r"   r   r   <   sB    
7||s3w<<((((N7NNoo )(r$   row_index_name
str | Nonerow_index_offsetr   tuple[str, int] | Nonec                    | 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.
    N )r+   r-   s     r"   parse_row_index_argsr1   B   s     t 011r$   .)use_pyarrowraise_if_emptystorage_optionsfile*str | Path | list[str] | IO[bytes] | bytesencodingr2   boolr3   r4   dict[str, Any] | NoneContextManager[str | BytesIO]c                   d S Nr0   r5   r7   r2   r3   r4   s        r"   prepare_file_argr>   Q   	     %(Cr$   (str | Path | IO[str] | IO[bytes] | bytesc                   d S r<   r0   r=   s        r"   r>   r>   \   r?   r$   4str | Path | list[str] | IO[str] | IO[bytes] | bytes9ContextManager[str | list[str] | BytesIO | list[BytesIO]]c                   d S r<   r0   r=   s        r"   r>   r>   g   s     ADr$   FTc               	   |r|                                 ni }|rt          sd}t          |          t          d"d            }|r|dv nd}|r|nd	}|                    d
          r|dd         dfn|df\  }}	| t          | t                    rK|s*|                     ||	                              d	          } t          t          |           d|          S t          | t                    rWt          t          |                                                     d	                    d|                                 |          S t          | t                    r|slt          t          |                                                     ||	                              d	                    d|                                 |          S  |t          | d|                                 |                    S t          | t                    ry|s]t          t          |                                                     ||	                              d	                    d| d|          S  |t!          |                     S t          | t"                    rt%          |           rt'          | |          S t          rddlm  |           d         dk    r|r |t!          |                     S t          |                               ||	          5 }
t          t          |
                                                    d	                    | |          cddd           S # 1 swxY w Y   ||d<   |	|d<   t/          j        | fi |S t          | t0                    rt3          |           rvt5          d | D                       r]t          rVddlm |r2t5          fd | D                       r |fd!| D                       S ||d<   |	|d<   t/          j        | fi |S t          | t"                    rt!          |           } |st          |                               ||	          5 }
t          t          |
                                                    d	                    | |          cddd           S # 1 swxY w 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` argumentr5   r
   r   Iterator[Any]c              3     K   	 | V  d S # w xY wr<   r0   r5   s    r"   managed_filez&prepare_file_arg.<locals>.managed_file   s"      	JJJDDDDDs   
 >   utf8
utf8-lossyTrJ   z-lossyNireplacestrict)errorsbytes)contextr3   r   )rP   read_positionr3   r   )brP   rQ   r3   zPath ()check_not_directoryr   )infer_storage_optionsprotocol)r7   rN   r7   rN   c              3  @   K   | ]}t          |t                    V  d S r<   )r   r   ).0fs     r"   	<genexpr>z#prepare_file_arg.<locals>.<genexpr>   s,      4V4VAZ35G5G4V4V4V4V4V4Vr$   c              3  B   K   | ]} |          d          dk    V  dS )rW   r5   Nr0   )rY   rZ   rV   s     r"   r[   z#prepare_file_arg.<locals>.<genexpr>   s9      TT!,,Q//
;vETTTTTTr$   c                2    g | ]}t          |           S )rT   )r   )rY   rZ   check_not_dirs     r"   
<listcomp>z$prepare_file_arg.<locals>.<listcomp>  s6        ! /qmTTT  r$   )r5   r
   r   rF   )copyr   ImportErrorr   endswithr   rO   decodeencode_check_emptyr   r   readtellr   
read_bytesr   r   looks_like_urlprocess_file_urlfsspec.utilsrV   openr   listr8   all
open_files)r5   r7   r2   r3   r4   r!   rI   has_utf8_utf8_lossy_encodingencoding_strencoding_errorsrZ   r^   rV   s              @@r"   r>   r>   r   s;   > 1@Go**,,,RO 0 C#    ^ /7@***D !  (388VL   **	&crc	I&&H% "L/ $OM$ 
+ 	T;;|O;DDKKFSSDDMM7>
 
 
 	
 $!! 
DIIKK&&v..//))++)	
 
 
 	
 $   
+ 
	IIKKVLVAAVF^^ 
 ""iikk-	 	 	 	 |!"iikk-	  
 
 	
 $ Y+ 		OO%%VLVAAVF^^ 
 +***-    |.tWWWXXX$ 8 $ 	8#D,777 	8:::::: %$T**:6&@@/ '<*4]SSS   $ZZ__)/ %   ' 7 788#''5                  +3OJ'(7OH%;t77777$ >$t** >4V4VQU4V4V4V1V1V > 	>::::::+ TTTTtTTTTT '<   %)     +3OJ'(7OH%$T==_===$ !$MJJJ+ 	d,OO ST#AFFHHOOF3344#K#1                  <s&    AM22M69M6?ASSS)rQ   rR   r   rP   r   rQ   
int | Nonec                   |rA|                                  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   )rR   rP   r3   rQ   hintr!   s         r"   re   re     sw      !++--.!33 111m1 Q=PPPP 	
 10$00#Hr$   pathc                F    t          j        d| t           j                  d uS )Nz^(ht|f)tps?://)rematch
IGNORECASE)ry   s    r"   ri   ri   %  s    8$dBM::$FFr$   c                L   ddl m}  ||           5 }|r|dv r-t          |                                          cd d d            S t          |                                                    |                              d                    cd d d            S # 1 swxY w Y   d S )Nr   )urlopen>   rJ   rK   rJ   )urllib.requestr   r   rf   rc   rd   )ry   r7   r   rZ   s       r"   rj   rj   )  s   &&&&&&	 E! 	E8'===16688$$E E E E E E E E 16688??844;;FCCDD	E E E E E E E E E E E E E E E E E Es   'BABB Bc                :     t           fddD                       S )Nc              3      K   | ]}|v V  	d S r<   r0   )rY   charr5   s     r"   r[   z"is_glob_pattern.<locals>.<genexpr>4  s'      88tt|888888r$   )*?[)anyrH   s   `r"   is_glob_patternr   3  s&    8888888888r$   c                p    	 t          t          j        | d                     dS # t          $ r Y dS w xY w)NT)	recursiveF)nextglobiglobStopIterationrH   s    r"   is_local_filer   7  sO    TZ---... t    uus   #' 
55)r   r   r   r   )r   r%   r   r&   )Nr   )r+   r,   r-   r   r   r.   ).)r5   r6   r7   r,   r2   r8   r3   r8   r4   r9   r   r:   )r5   r@   r7   r,   r2   r8   r3   r8   r4   r9   r   r:   )r5   rB   r7   r,   r2   r8   r3   r8   r4   r9   r   rC   r<   )
rR   r   rP   r   r3   r8   rQ   rs   r   r   )ry   r   r   r8   )ry   r   r7   r,   r   r   )r5   r   r   r8   )'
__future__r   r   r{   
contextlibr   ior   r   pathlibr   typingr   r	   r
   r   polars._utils.variousr   r   r   polars.dependenciesr   r   polars.exceptionsr   collections.abcr   r   r   ContextManagerr#   r   r1   r>   re   ri   rj   r   r   r0   r$   r"   <module>r      s   " " " " " "  				 % % % % % %                       3 3 3 3 3 3 3 3 3 3 3 3         
 : 9 9 9 9 9 9 9 ) ) ) ) ) ) D22222222CCCCCC"$ "$ "$ "$J    "&2 2 2 2 2 
 ( -0( ( ( ( ( 
( 
 ( -0( ( ( ( ( 
( 
 D -0D D D D D 
D  b -1b b b b b bL TX     G G G GE E E E E9 9 9 9     r$   