
    AHj,                       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rd dlmZ d dl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	 	 	 	 	 	 	 	 	 	 	 d&d       Z%e	 d$dddd	 	 	 	 	 	 	 	 	 	 	 d'd       Z%	 d(dddd	 	 	 	 	 	 	 	 	 	 	 d'dZ%dd	 	 	 	 	 	 	 	 	 d)dZ&d*dZ'd(d+dZ(d,dZ)d,dZ*	 	 	 	 d-dZ+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)StorageOptionsDictc                    | yd}d}t        | t              r| g}||fS t        | t              r| g}||fS t        |       rt	        |        | }||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)columns
projectioncolumn_namesmsgs       H/root/tools/cai/cai_env/lib/python3.12/site-packages/polars/io/_utils.pyparse_columns_argr#      s     '+J)-L'3y |## 
GS	!Y
 |## 
	!"7+ |## 
	!"7+

 |## bn    c                d    t        |       t        t        |             k7  rd| }t        |      y )Nz2`columns` arg should only have unique values, got )lenset
ValueError)r   r!   s     r"   r   r   @   s2    
7|s3w<((B7+No )r$   c                    | y| |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 )row_index_namerow_index_offsets     r"   parse_row_index_argsr-   F   s      011r$   .)use_pyarrowraise_if_emptystorage_optionsc                    y Nr*   fileencodingr.   r/   r0   s        r"   prepare_file_argr6   U        %(r$   c                    y r2   r*   r3   s        r"   r6   r6   `   r7   r$   c                    y r2   r*   r3   s        r"   r6   r6   k   s	     ADr$   FTc          	        |r|j                         ni }|rt        sd}t        |      t        dd       }|r|dv nd}|r|nd}|j	                  d      r|dd	 d
fn|df\  }}	| }
t        | t              r;|s"| j                  ||	      j                  d      } t        t        |       d|      S t        | t              rCt        t        | j                         j                  d            d| j                         |      S t        | t              ry|sTt        t        | j                         j                  ||	      j                  d            d| j                         |      S  |t        | d| j                         |            S t        | t              r^|sIt        t        | j                         j                  ||	      j                  d            d| d|      S  |t!        | |
            S t        | t"              rt%        |       rt'        | |      S t        rddlm  |       d   dk(  rp|r |t!        | |
            S t        |       j-                  ||	      5 }t        t        |j                         j                  d            | |      cddd       S ||d<   |	|d<   t/        j,                  | fi |S t        | t0              rt3        |       rut5        d | D              rct        r]ddlm |r5t5        fd| D              r! || D cg c]  }t!        ||
       c}      S ||d<   |	|d<   t/        j6                  | fi |S t        | t"              rjt!        | |
      } |s[t        |       j-                  ||	      5 }t        t        |j                         j                  d            | |      cddd       S  ||       S # 1 sw Y   <xY wc c}w # 1 sw Y   #xY w)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` argumentr4   c              3      K   	 |  y # w xY wwr2   r*   r4   s    r"   managed_filez&prepare_file_arg.<locals>.managed_file   s     	JDs   	 >   utf8
utf8-lossyTr>   z-lossyNireplacestrict)errorsbytes)contextr/   r   )rD   read_positionr/   r   )brD   rE   r/   zPath ()check_not_directoryr   )infer_storage_optionsprotocol)r5   rB   r5   rB   c              3  <   K   | ]  }t        |t                y wr2   )r   r   ).0fs     r"   	<genexpr>z#prepare_file_arg.<locals>.<genexpr>   s     4VAZ35G4Vs   c              3  :   K   | ]  } |      d    dk(    yw)rK   r4   Nr*   )rM   rN   rJ   s     r"   rO   z#prepare_file_arg.<locals>.<genexpr>  s!     T!,Q/
;vETs   )r4   r   returnzIterator[Any])copyr   ImportErrorr   endswithr   rC   decodeencode_check_emptyr   r   readtellr   
read_bytesr   r   looks_like_urlprocess_file_urlfsspec.utilsrJ   openr   listboolall
open_files)r4   r5   r.   r/   r0   r!   r=   has_utf8_utf8_lossy_encodingencoding_strencoding_errorscheck_not_dirrN   rJ   s               @r"   r6   r6   v   s   > 1@o**,RO0C#   /7**D !  (8VL   * 
cr	I&H% "L/ $OM$+;;|O;DKKFSDDM7>
 	
 $!DIIK&&v./))+)	
 	
 $ +IIKVLVAVF^
 ""iik-	 	 !"iik-	
 	
 $+OO%VLVAVF^
 !*-  .tWXX$ $#D,77: %T*:6&@/'*4]S  $Z__)/ %  ' 78#'('5  +3OJ'(7OH%;;t777$$t*4VQU4V1V:+TtTT' &* ! /qmT  +3OJ'(7OH%$$T=_==$!$MJ+d,O ST#AFFHOOF34#h#1  M & s   /5O #O-5O2 O*2O;)rE   c                   |r=| j                         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   )rF   rD   r/   rE   hintr!   s         r"   rW   rW     s`     !++-..!3 11m #=/1OP 	
 !	$0#Hr$   c                P    t        j                  d| t         j                        d uS )Nz^(ht|f)tps?://)rematch
IGNORECASE)paths    r"   r[   r[   )  s    88%tR]];4GGr$   c                   ddl m}  ||       5 }|r|dv r"t        |j                               cd d d        S t        |j                         j	                  |      j                  d            cd d d        S # 1 sw Y   y xY w)Nr   )urlopen>   r>   r?   r>   )urllib.requestrr   r   rX   rU   rV   )rp   r5   rr   rN   s       r"   r\   r\   -  sp    &	 E!8'==1668$E E 1668??84;;FCD	E E Es   A86A88Bc                ,     t         fddD              S )Nc              3  &   K   | ]  }|v  
 y wr2   r*   )rM   charr4   s     r"   rO   z"is_glob_pattern.<locals>.<genexpr>8  s     8tt|8s   )*?[)anyr<   s   `r"   is_glob_patternr{   7  s    8888r$   c                d    	 t        t        j                  | d             y# t        $ r Y yw xY w)NT)	recursiveF)nextglobiglobStopIterationr<   s    r"   is_local_filer   ;  s3    TZZ-.   s    # 	//c                   t        | t        t        f      rt        | d      } n&t	        |       r| D  cg c]  } t        | d       c} } t        | t
              rt        | t        t        f      r| g}t        d|      S | S c c} w )NFrH   z9list[bytes] | list[str] | list[IO[bytes]] | list[IO[str]])r   r   r   r   r   r   rC   r   )sourceouts     r"   get_sourcesr   D  s|     &3+&#FF	 	(PV
FLv5A
 fh':fsEl+K8>xOQTUUM
s   B)r   z0Sequence[str] | Sequence[int] | str | int | NonerQ   z1tuple[Sequence[int] | None, Sequence[str] | None])r   zSequence[str] | Sequence[int]rQ   None)Nr   )r+   
str | Noner,   r   rQ   ztuple[str, int] | None).)r4   z*str | Path | list[str] | IO[bytes] | bytesr5   r   r.   r`   r/   r`   r0   StorageOptionsDict | NonerQ   ContextManager[str | BytesIO])r4   z(str | Path | IO[str] | IO[bytes] | bytesr5   r   r.   r`   r/   r`   r0   r   rQ   r   )r4   z4str | Path | list[str] | IO[str] | IO[bytes] | bytesr5   r   r.   r`   r/   r`   r0   r   rQ   z9ContextManager[str | list[str] | BytesIO | list[BytesIO]]r2   )
rF   r   rD   r   r/   r`   rE   z
int | NonerQ   r   )rp   r   rQ   r`   )rp   r   r5   r   rQ   r   )r4   r   rQ   r`   )r   zqstr | Path | IO[bytes] | IO[str] | bytes | list[str] | list[Path] | list[IO[bytes]] | list[IO[str]] | list[bytes]rQ   zFlist[str] | list[Path] | list[IO[str]] | list[IO[bytes]] | list[bytes]),
__future__r   r   rm   collections.abcr   
contextlibr   ior   r   pathlibr   typingr	   r
   r   r   r   polars._dependenciesr   r   polars._utils.variousr   r   r   r   polars.exceptionsr   r   r   ContextManagerpolars._typingr   r#   r   r-   r6   rW   r[   r\   r{   r   r   r*   r$   r"   <module>r      sk   "  	 $ %    9 9 :  *(C1"$="$6"$J "&222 2 
 ( 14(
4(( 	(
 ( /( #( 
( 
 ( 14(
2(( 	(
 ( /( #( 
( 
 D 14D
>DD 	D
 D /D ?D 
D  b 15b
>bb 	b
 b /b ?bL TX15FPHE9	 Lr$   