
    hQ)                    (   S SK Jr  S SKrS SKrS SKJr  S SKJrJr  S SK	J
r
  S SKJrJrJrJr  S SKJrJr  S SKJrJrJr  S S	KJr  \(       a  S S
KJrJr  S SKJr      SS jrSS jr  S     S S jjr \ S!SSSS.           S"S jjj5       r!\ S!SSSS.           S#S jjj5       r!\ S!SSSS.           S$S jjj5       r! S%SSSS.           S$S jjjr!SS.         S&S jjr"S'S jr#S%S(S jjr$S)S jr%S)S jr&g)*    )annotationsN)contextmanager)BytesIOStringIO)Path)IOTYPE_CHECKINGAnyoverload)_FSSPEC_AVAILABLEfsspec)is_int_sequenceis_str_sequencenormalize_filepath)NoDataError)IteratorSequence)AbstractContextManagerc                   U c  gSnSn[        U [        5      (       a  U /nX4$ [        U [        5      (       a  U /nX4$ [        U 5      (       a  [	        U 5        U nX4$ [        U 5      (       a  [	        U 5        U nX4$ Sn[        U5      e)a  
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       J/home/james-whalen/.local/lib/python3.13/site-packages/polars/io/_utils.pyparse_columns_argr       s     '+J)-L'3y ## 
GS	!	!Y
 ## 
	!	!"7+ ## 
	!	!"7+

 ## bn    c                h    [        U 5      [        [        U 5      5      :w  a  SU < 3n[        U5      eg )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                    U c  gX4$ )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*   B   s     11r!   .)use_pyarrowraise_if_emptystorage_optionsc                   g Nr'   fileencodingr+   r,   r-   s        r   prepare_file_argr3   Q        %(r!   c                   g r/   r'   r0   s        r   r3   r3   \   r4   r!   c                   g r/   r'   r0   s        r   r3   r3   g   s	     ADr!   FTc          
       ^ U(       a  UR                  5       O0 nU(       a  [        (       d  Sn[        U5      e[        SS j5       nU(       a  US;   OSnU(       a  UOSnUR	                  S5      (       a  USS	 S
4OUS4u  pU(       + n
[        U [        5      (       a9  U(       d  U R                  XS9R                  S5      n [        [        U 5      SUS9$ [        U [        5      (       a@  [        [        U R                  5       R                  S5      5      SU R                  5       US9$ [        U [        5      (       at  U(       dM  [        [        U R                  5       R                  XS9R                  S5      5      SU R                  5       US9$ U" [        U SU R                  5       US95      $ [        U [        5      (       aY  U(       dC  [        [        U R                  5       R                  XS9R                  S5      5      SU < S3US9$ U" [!        X
S95      $ [        U ["        5      (       a  [%        U 5      (       a  ['        X5      $ [        (       a  SSKJm  T" U 5      S   S:X  ak  U(       a  U" [!        X
S95      $ [        U 5      R-                  XS9 n[        [        UR                  5       R                  S5      5      U < US9sSSS5        $ XS'   XS'   [.        R,                  " U 40 UD6$ [        U [0        5      (       a  [3        U 5      (       a  [5        S U  5       5      (       aq  [        (       af  SSKJm  U(       a:  [5        U4S jU  5       5      (       a   U" U  Vs/ s H  n[!        XS9PM     sn5      $ XS'   XS'   [.        R6                  " U 40 UD6$ [        U ["        5      (       ae  [!        X
S9n U(       dU  [        U 5      R-                  XS9 n[        [        UR                  5       R                  S5      5      U < US9sSSS5        $ U" U 5      $ ! , (       d  f       GN]= fs  snf ! , (       d  f       N-= f)uX  
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   c              3      #     U v   g ! f = f7fr/   r'   r1   s    r   managed_file&prepare_file_arg.<locals>.managed_file   s     	JDs   	 >   utf8
utf8-lossyTr<   z-lossyNireplacestrict)errorsbytes)contextr,   r   )rB   read_positionr,   r   )brB   rC   r,   zPath ())check_not_directoryr   )infer_storage_optionsprotocol)r2   r@   r2   r@   c              3  B   #    U  H  n[        U[        5      v   M     g 7fr/   )r   r   ).0fs     r   	<genexpr>#prepare_file_arg.<locals>.<genexpr>   s     4VQUAZ35G5GQUs   c              3  @   >#    U  H  nT" U5      S    S:H  v   M     g7f)rH   r1   Nr'   )rJ   rK   rG   s     r   rL   rM      s!     Tt!,Q/
;vEts   )r1   r
   returnzIterator[Any])copyr   ImportErrorr   endswithr   rA   decodeencode_check_emptyr   r   readtellr   
read_bytesr   r   looks_like_urlprocess_file_urlfsspec.utilsrG   openr   listboolall
open_files)r1   r2   r+   r,   r-   r   r:   has_utf8_utf8_lossy_encodingencoding_strencoding_errorscheck_not_dirrK   rG   s               @r   r3   r3   r   s   > 1@o**,RO00C#   /7**D !  (8VL   ** 
cr	I&H% "L $OM$+;;|;DKKFSDDM7>
 	
 $!!DIIK&&v./))+)	
 	
 $  +IIKVLVAVF^
 ""iik-	 	 !"iik-	
 	
 $+OO%VLVAVF^
 !*-  .tWXX$ $#D77: %T*:6&@/'*4S  $Z__) % ' 78#'('5  +3J'(7H%;;t777$$t**4VQU4V1V1V:+TtTTT' &*%) /qT%)  +3J'(7H%$$T=_==$!$J+d,OST#AFFHOOF34#h#1 PO M & POs   #3P.3Q )3Q.
P=
Q)rC   c                   U(       aE  U R                  5       R                  S:X  a'  US;   a  U(       a  SU S3OSnSU U 3n[        U5      eU $ )Nr   )r   r   z (buffer position = z; try seek(0) before reading?) zempty data from )	getbuffernbytesr   )rD   rB   r,   rC   hintr   s         r   rU   rU     s`     !++-..!3 11m #=/1OP 	
 !	$0#Hr!   c                R    [         R                  " SU [         R                  5      S L$ )Nz^(ht|f)tps?://)rematch
IGNORECASE)paths    r   rY   rY   %  s    88$dBMM:$FFr!   c                    SSK Jn  U" U 5       nU(       a  US;   a"  [        UR                  5       5      sS S S 5        $ [        UR                  5       R	                  U5      R                  S5      5      sS S S 5        $ ! , (       d  f       g = f)Nr   )urlopen>   r<   r=   r<   )urllib.requestrp   r   rV   rS   rT   )rn   r2   rp   rK   s       r   rZ   rZ   )  s\    &	!8'==1668$ 
 1668??84;;FCD	 
s   &A?6A??
Bc                .   ^  [        U 4S jS 5       5      $ )Nc              3  ,   >#    U  H	  oT;   v   M     g 7fr/   r'   )rJ   charr1   s     r   rL   "is_glob_pattern.<locals>.<genexpr>4  s     8t|s   )*?[)anyr9   s   `r   is_glob_patternrz   3  s    8888r!   c                b     [        [        R                  " U SS95        g! [         a     gf = f)NT)	recursiveF)nextglobiglobStopIterationr9   s    r   is_local_filer   7  s3    TZZ-.   s   ! 
..)r   z0Sequence[str] | Sequence[int] | str | int | NonerO   z1tuple[Sequence[int] | None, Sequence[str] | None])r   zSequence[str] | Sequence[int]rO   None)Nr   )r(   
str | Noner)   r   rO   ztuple[str, int] | None).)r1   z*str | Path | list[str] | IO[bytes] | bytesr2   r   r+   r^   r,   r^   r-   dict[str, Any] | NonerO   ContextManager[str | BytesIO])r1   z(str | Path | IO[str] | IO[bytes] | bytesr2   r   r+   r^   r,   r^   r-   r   rO   r   )r1   z4str | Path | list[str] | IO[str] | IO[bytes] | bytesr2   r   r+   r^   r,   r^   r-   r   rO   z9ContextManager[str | list[str] | BytesIO | list[BytesIO]]r/   )
rD   r   rB   r   r,   r^   rC   z
int | NonerO   r   )rn   r   rO   r^   )rn   r   r2   r   rO   r   )r1   r   rO   r^   )'
__future__r   r~   rk   
contextlibr   ior   r   pathlibr   typingr   r	   r
   r   polars._dependenciesr   r   polars._utils.variousr   r   r   polars.exceptionsr   collections.abcr   r   r   ContextManagerr    r   r*   r3   rU   rY   rZ   rz   r   r'   r!   r   <module>r      sK   "  	 %    3 3 : 
 *2C"$="$6"$J "&222 2 
 ( -0(
4(( 	(
 ( +( #( 
( 
 ( -0(
2(( 	(
 ( +( #( 
( 
 D -0D
>DD 	D
 D +D ?D 
D  b -1b
>bb 	b
 b +b ?bL TX15FPGE9r!   