
    ˀhP)                       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	 	 	 	 	 	 	 	 	 	 	 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&y)*    )annotationsN)contextmanager)BytesIOStringIO)Path)IOTYPE_CHECKINGAnyoverload)is_int_sequenceis_str_sequencenormalize_filepath)_FSSPEC_AVAILABLEfsspec)NoDataError)IteratorSequence)AbstractContextManagerc                    | 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       l/var/www/html/wine-match-dev/backend/winematch-backend/venv/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*   B   s      011r!   .)use_pyarrowraise_if_emptystorage_optionsc                    y Nr'   fileencodingr+   r,   r-   s        r   prepare_file_argr3   Q        %(r!   c                    y r/   r'   r0   s        r   r3   r3   \   r4   r!   c                    y r/   r'   r0   s        r   r3   r3   g   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` argumentr1   c              3      K   	 |  y # w xY wwr/   r'   r1   s    r   managed_filez&prepare_file_arg.<locals>.managed_file   s     	JDs   	 >   utf8
utf8-lossyTr;   z-lossyNireplacestrict)errorsbytes)contextr,   r   )rA   read_positionr,   r   )brA   rB   r,   zPath ())check_not_directoryr   )infer_storage_optionsprotocol)r2   r?   r2   r?   c              3  <   K   | ]  }t        |t                y wr/   )r   r   ).0fs     r   	<genexpr>z#prepare_file_arg.<locals>.<genexpr>   s     4VAZ35G4Vs   c              3  :   K   | ]  } |      d    dk(    yw)rG   r1   Nr'   )rI   rJ   rF   s     r   rK   z#prepare_file_arg.<locals>.<genexpr>   s!     T!,Q/
;vETs   )r1   r
   returnzIterator[Any])copyr   ImportErrorr   endswithr   r@   decodeencode_check_emptyr   r   readtellr   
read_bytesr   r   looks_like_urlprocess_file_urlfsspec.utilsrF   openr   listboolall
open_files)r1   r2   r+   r,   r-   r   r:   has_utf8_utf8_lossy_encodingencoding_strencoding_errorscheck_not_dirrJ   rF   s               @r   r3   r3   r   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;)rB   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   )rC   rA   r,   rB   hintr   s         r   rS   rS     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   rW   rW   %  s    88$dBMM:$FFr!   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.requestrn   r   rT   rQ   rR   )rl   r2   rn   rJ   s       r   rX   rX   )  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r/   r'   )rI   charr1   s     r   rK   z"is_glob_pattern.<locals>.<genexpr>4  s     8tt|8s   )*?[)anyr9   s   `r   is_glob_patternrw   3  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StopIterationr9   s    r   is_local_filer~   7  s3    TZZ-.   s    # 	//)r   z0Sequence[str] | Sequence[int] | str | int | NonerM   z1tuple[Sequence[int] | None, Sequence[str] | None])r   zSequence[str] | Sequence[int]rM   None)Nr   )r(   
str | Noner)   r   rM   ztuple[str, int] | None).)r1   z*str | Path | list[str] | IO[bytes] | bytesr2   r   r+   r\   r,   r\   r-   dict[str, Any] | NonerM   ContextManager[str | BytesIO])r1   z(str | Path | IO[str] | IO[bytes] | bytesr2   r   r+   r\   r,   r\   r-   r   rM   r   )r1   z4str | Path | list[str] | IO[str] | IO[bytes] | bytesr2   r   r+   r\   r,   r\   r-   r   rM   z9ContextManager[str | list[str] | BytesIO | list[BytesIO]]r/   )
rC   r   rA   r   r,   r\   rB   z
int | NonerM   r   )rl   r   rM   r\   )rl   r   r2   r   rM   r   )r1   r   rM   r\   )'
__future__r   r{   ri   
contextlibr   ior   r   pathlibr   typingr   r	   r
   r   polars._utils.variousr   r   r   polars.dependenciesr   r   polars.exceptionsr   collections.abcr   r   r   ContextManagerr    r   r*   r3   rS   rW   rX   rw   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!   