o
    +&]i)=                     @  s  d Z ddlm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 ddlmZ dd	lmZ dd
lmZmZmZ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#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ej*dkrzddlm+Z+ nddl,m+Z+ ej*dkrddlm-Z- nddl,m-Z- erddl.m/Z/m0Z0m1Z1m2Z2 ddl3m4Z4 ddl5m6Z6 ddl7Z8ddl9Z:ddl;Z<ddl=mZ> ddlm?Z?m@Z@ g dZAe+ddddZBe+ddejCe d ZDe-d!d"efd#ZE	 e-d$d%eDfd#ZF	 G d&d' d'eZGG d(d) d)eeB ZH	d~dd4d5ZI	d~dd8d9ZJdd<d=ZKdd>ddEdFZLddIdJZMddKdLZNddNdOZOddQdRZPddSdTZQddVdWZRddYdZZSdd^d_ZTddadbZUddcddhdiZVddkdlZWddndoZXddpdqZYddsdtZZddudvZ[ddydzZ\dd|d}Z]dS )z5Individual read functions and siuations they support.    )annotationsN)Enum)partialwraps	find_spec)chain)
itemgetter)Path)TYPE_CHECKINGAnyGenericLiteral)v1)
get_pandas
get_polars)IntoDataFrameT)	is_arrowis_csvis_jsonis_metais_not_tabular
is_parquet
is_spatialis_topois_tsv)AltairDatasetsError)      )TypeVar)r      )TypeAliasType)CallableIterableIteratorSequence)IOBase)
ModuleType)typing)ItemsMetaIs)is_availablepa_anypd_only
pd_pyarrowpl_onlyreadscanRznwt.IntoFrameT)bound	covariant
IntoFrameTzKnwt.NativeFrame | nw.DataFrame[Any] | nw.LazyFrame[Any] | nwt.DataFrameLike)r3   defaultReadzBaseImpl[IntoDataFrameT])type_paramsScanzBaseImpl[IntoFrameT]c                   @  s(   e Zd ZdZdZdddZddd	Zd
S )SkipzFalsy sentinel.r   returnLiteral[False]c                 C     dS )NF selfr>   r>   W/var/www/html/IGF-ODF-V3/venv/lib/python3.10/site-packages/altair/datasets/_readimpl.py__bool__H      zSkip.__bool__Literal['<Skip>']c                 C  r=   )Nz<Skip>r>   r?   r>   r>   rA   __repr__K   rC   zSkip.__repr__N)r;   r<   )r;   rD   )__name__
__module____qualname____doc__skiprB   rE   r>   r>   r>   rA   r:   C   s
    
r:   c                   @  s   e Zd ZU dZded< 	 ded< 	 ded< 	 d*ddZd+ddZed,ddZd-ddZ	e
d.ddZd.ddZd.dd Ze
d/d"d#Ze
d0d%d&Ze
d0d'd(Zd)S )1BaseImpla  
    A function wrapped with dataset support constraints.

    The ``include``, ``exclude`` properties form a `NIMPLY gate`_ (`Material nonimplication`_).

    Examples
    --------
    For some dataset ``D``, we can use ``fn`` if::

        impl: BaseImpl
        impl.include(D) and not impl.exclude(D)


    .. _NIMPLY gate:
        https://en.m.wikipedia.org/wiki/NIMPLY_gate
    .. _Material nonimplication:
        https://en.m.wikipedia.org/wiki/Material_nonimplication#Truth_table
    Callable[..., R]fnr*   includeexcludeMetaIs | Nonekwdsdict[str, Any]r;   Nonec                C  s   |p|   }||s&ddd ||@ D }d| d|d|}t|t| d|r4t|fi |n| t| d| t| d	| d S )
N, c                 s  "    | ]\}}| d |V  qdS =Nr>   .0kvr>   r>   rA   	<genexpr>v        z$BaseImpl.__init__.<locals>.<genexpr>zConstraints overlap at: `z
`
include=z	
exclude=rM   rN   rO   )_exclude_none
isdisjointjoin	TypeErrorobject__setattr__r   )r@   rM   rN   rO   rQ   intersectionmsgr>   r>   rA   __init__l   s   
"zBaseImpl.__init__metar)   3Callable[..., R] | type[AltairDatasetsError] | Skipc                C  s(   | j |r| j|r| jS tS tjS )a<  
        Indicate an action to take for a dataset.

        **Supports** dataset, use this function::

            Callable[..., R]

        Has explicitly marked as **not supported**::

            type[AltairDatasetsError]

        No relevant constraints overlap, safe to check others::

            Skip
        )rN   issubsetrO   r_   rM   r   r:   rJ   )r@   rg   r>   r>   rA   unwrap_or_skip}   s   zBaseImpl.unwrap_or_skipc                 C  s   t  S )zRepresents the empty set.)r   )clsr>   r>   rA   r^      s   zBaseImpl._exclude_nonenamestrvaluer   c                 C  s"   t | jd| d| }t|)Nz% is immutable.
Could not assign self.z = )typerF   ra   )r@   rl   rn   re   r>   r>   rA   rc      s   zBaseImpl.__setattr__c                 C  s   t t| jdS )NUNKNOWN)_root_package_name_unwrap_partialrM   r?   r>   r>   rA   _inferred_package   s   zBaseImpl._inferred_packagec                 C  s(   t | j d| j d}| d|  dS )N[z?]())ro   rF   rs   )r@   tp_namer>   r>   rA   rE      s   zBaseImpl.__repr__c                 C  s   t | jtr#t| j}| jj }|j dddd |D  d}n| jj d}| j| j	}}| d|r@d|d| S t
| S )	Nz(..., rT   c                 s  rU   rV   r>   rX   r>   r>   rA   r\      r]   z#BaseImpl.__str__.<locals>.<genexpr>rv   z(...)zinclude=z
, exclude=)
isinstancerM   r   rr   keywordsitemsrF   r`   rN   rO   repr)r@   rM   rQ   fn_reprincexcr>   r>   rA   __str__   s   
$*zBaseImpl.__str__Iterator[str]c                 #  s0    t d  fddt| j| jD E d H  d S )Nr   c                 3  s    | ]} |V  qd S Nr>   )rY   objrl   r>   rA   r\          z-BaseImpl._relevant_columns.<locals>.<genexpr>)r	   r   rN   rO   r?   r>   r   rA   _relevant_columns   s   &zBaseImpl._relevant_columnsnw.Exprc                 C  s&   | j r| j | j   @ S | j S r   )rO   rN   to_exprr?   r>   r>   rA   _include_expr   s
   zBaseImpl._include_exprc                 C  s,   | j r| j | j  @ S d| }t|)NzBUnable to generate an exclude expression without setting exclude

)rO   rN   r   ra   )r@   re   r>   r>   rA   _exclude_expr   s   
zBaseImpl._exclude_exprN)
rM   rL   rN   r*   rO   rP   rQ   rR   r;   rS   )rg   r)   r;   rh   )r;   r*   )rl   rm   rn   r   )r;   rm   )r;   r   )r;   r   )rF   rG   rH   rI   __annotations__rf   rj   classmethodr^   rc   propertyrs   rE   r   r   r   r   r>   r>   r>   rA   rK   O   s.   
 





rK   rN   r*   rO   rP   rM   Callable[..., IntoDataFrameT]rQ   r   r;   Read[IntoDataFrameT]c                K     t | |||S r   rK   rM   rN   rO   rQ   r>   r>   rA   r0         r0   Callable[..., IntoFrameT]Scan[IntoFrameT]c                K  r   r   r   r   r>   r>   rA   r1      r   r1   impl"Scan[nw.LazyFrame[IntoDataFrameT]]c                C  s    ddd}t || j| j| jS )NrM   r   r;   +Callable[..., nw.LazyFrame[IntoDataFrameT]]c                  s   t t d fdd}|S )	Nargsr   rQ   r;   nw.LazyFrame[IntoDataFrameT]c                    s   t  | i | S r   )nwfrom_nativelazy)r   rQ   rM   r>   rA   wrapper   s   z+into_scan.<locals>.scan_fn.<locals>.wrapper)r   r   rQ   r   r;   r   )r   rr   )rM   r   r>   r   rA   scan_fn   s   
zinto_scan.<locals>.scan_fn)rM   r   r;   r   )r1   rM   rN   rO   )r   r   r>   r>   rA   	into_scan   s   
	r   )require_all	pkg_namesstr | Iterable[str]more_pkg_namesrm   r   boolc                G  sV   |st | trt| duS t | ts| n| f}t||}|r tnt}|dd |D S )z
    Check for importable package(s), without raising on failure.

    Parameters
    ----------
    pkg_names, more_pkg_names
        One or more packages.
    require_all
        * ``True`` every package.
        * ``False`` at least one package.
    Nc                 s  s    | ]	}t |d uV  qd S r   r   )rY   rl   r>   r>   rA   r\      s    zis_available.<locals>.<genexpr>)rx   rm   r   r   allany)r   r   r   
pkgs_namesnamesrM   r>   r>   rA   r+      s   
r+   r   r6   c                C  sN   t | dr| jdd S ddlm} || }|r%|j }r%|dd S |S )NrG   .r   )	getmodule)hasattrrG   splitinspectr   __package__)r   r6   r   modulepkgr>   r>   rA   rq      s   
rq   c                C  s"   | }t |tr|j}t |ts|S r   )rx   r   func)rM   r   r>   r>   rA   rr   
  s
   

rr   Atuple[Sequence[Read[pl.DataFrame]], Sequence[Scan[pl.LazyFrame]]]c                  C  s   dd l } ttt t}tdrt t |f}n|f}t| jt	ddg|t| jt
dddt| jtt| jtR }t| jtf}||fS )Nr   	polars_stT)try_parse_dates	)	separatorr   )polarsr0   _pl_read_json_roundtripr   r   r+   !_pl_read_json_polars_st_topo_impl_pl_read_json_polars_st_implread_csvr   r   read_ipcr   read_parquetr   r1   scan_parquet)plpl_read_jsonfn_jsonread_fnsscan_fnsr>   r>   rA   r/     s(   

r/   Sequence[Read[pd.DataFrame]]c                  C  s   dd l } tdrt| jtt| jtf}ntdr!t| jtf}nd}ttt t	t
d}tdr7t |f}n|f}t| jtg|t| jtdd|R S )	Nr   pyarrowfastparquetr>   rO   	geopandasr   )sep)pandasr+   r0   read_featherr   r   r   _pd_read_jsonr   r   r   _pd_read_json_geopandas_implr   r   r   )pdoptpd_read_jsonr   r>   r>   rA   r-   *  s(   
r-   c                  C  s   dd l } ddi}ttt tfdti|}tdr t |f}n|f}t| jt	fi |g|t| jt
fddi|t| jtfi |t| jtfi |R S )Nr   dtype_backendr   rO   r   r   r   )r   r0   r   r   r   r   r+   r   r   r   r   r   r   r   r   )r   rQ   r   r   r>   r>   rA   r.   D  s0   
r.   Sequence[Read[pa.Table]]c                  C  sP   ddl m} m}m} t| jtt t| jt| j	dddt|j
tt|j
tfS )Nr   csvfeatherparquetr   )	delimiter)parse_options)r   r   r   r   r0   r   r   _pa_read_json_implr   ParseOptions
read_tabler   r   r   r>   r>   rA   r,   [  s   


r,   Read[pa.Table]c                  C  sZ   t drt } | durtt| tS t dr&t }|dur&tt|ttdS tttt	dS )z
    Mitigating ``pyarrow``'s `line-delimited`_ JSON requirement.

    .. _line-delimited:
        https://arrow.apache.org/docs/python/json.html#reading-json-files
    r   Nr   r   )
r+   r   r0    _pl_read_json_roundtrip_to_arrowr   r   _pd_read_json_to_arrowr   _stdlib_read_json_to_arrowr   )	polars_ns	pandas_nsr>   r>   rA   r   g  s   r   nsr'   Callable[..., pd.DataFrame]c                  s   t  jd	 fdd}|S )
Nsource
Path | AnyrQ   r   r;   pd.DataFramec                  s"   t  j| fi |fi | S r   )_pd_fix_dtypes_nw	read_json	to_nativer   rQ   r   r>   rA   rM   z  s   "z_pd_read_json.<locals>.fn)r   r   rQ   r   r;   r   r   r   r   rM   r>   r   rA   r   y  s   r   Read[pd.DataFrame]c                    4   dd l  t jdd fd	d
} t| tdddS )Nr   schemar   r   r   rQ   r;   r   c                      j | fi |S r   	read_filer   r   rQ   r   r>   rA   rM        z(_pd_read_json_geopandas_impl.<locals>.fnT.jsonr   suffixr   )r   r   r   r   rQ   r   r;   r   )r   r   r   r0   r   r   r>   r   rA   r        r   r   dfr   r   nw.DataFrame[pd.DataFrame]c               K  sD   |rd|ini }| j di |jtjddtjtjtj	S )Nr   T)
eager_onlyr>   )
convert_dtypespiper   r   with_columns	selectorsby_dtypeObjectcastString)r   r   rQ   r>   r>   rA   r     s   r   Callable[..., pa.Table]c                  s"   t  jd dd fd	d
}|S )N)r   r   r   r   r   rQ   r;   pa.Tablec                 s     j | fi |jtdd S )z?``schema`` is only here to swallow the ``SchemaCache`` if used.r   r   )r   r  r   to_arrowr   r   r>   rA   rM     s
   z"_pd_read_json_to_arrow.<locals>.fn)r   r   r   r   rQ   r   r;   r	  r   r   r>   r   rA   r     s   r   Read[pl.DataFrame]c                    r   )Nr   r   r   r   r   rQ   r;   pl.DataFramec                  r   r   r   r   str>   rA   rM     r   z(_pl_read_json_polars_st_impl.<locals>.fnTr   r   r   r   r   r   r   rQ   r   r;   r  )r   r   r   r0   r   r   r>   r  rA   r     r   r   c                    s,   dd l  t jdd fd	d
} t| tS )Nr   r   r   r   r   rQ   r;   r  c                  s0   t | tr| drd|  }  j| fi |S )Nhttpz	TopoJSON:)rx   rm   
startswithr   r   r  r>   rA   rM     s   
z-_pl_read_json_polars_st_topo_impl.<locals>.fnr   r  )r   r   r   r0   r   r   r>   r  rA   r     s   
r   Callable[..., pl.DataFrame]c                  s*   ddl m  tjd fd	d
}|S )a+  
    Try to utilize better date parsing available in `pl.read_csv`_.

    `pl.read_json`_ has few options when compared to `pl.read_csv`_.

    Chaining the two together - *where possible* - is still usually faster than `pandas.read_json`_.

    .. _pl.read_json:
        https://docs.pola.rs/api/python/stable/reference/api/polars.read_json.html
    .. _pl.read_csv:
        https://docs.pola.rs/api/python/stable/reference/api/polars.read_csv.html
    .. _pandas.read_json:
        https://pandas.pydata.org/docs/reference/api/pandas.read_json.html
    r   )BytesIOr   Path | IOBaserQ   r   r;   r  c                  sv   j | fi |}tdd |j D r|S  }|| |r0h d  fdd| D }j|fddi|S )Nc                 s  s    | ]}|  V  qd S r   )	is_nested)rY   tpr>   r>   rA   r\     r   z6_pl_read_json_roundtrip.<locals>.fn.<locals>.<genexpr>>   r   schema_overridesinfer_schema_lengthc                   s   i | ]\}}| v r||qS r>   r>   rX   SHARED_KWDSr>   rA   
<dictcomp>  s    z7_pl_read_json_roundtrip.<locals>.fn.<locals>.<dictcomp>r   T)r   r   r   dtypes	write_csvrz   r   )r   rQ   r   bufr  r   r  rA   rM     s   
z#_pl_read_json_roundtrip.<locals>.fnN)r   r  rQ   r   r;   r  )ior  r   r   r   r>   r  rA   r     s   r   c                  s$   t |  t| jd	 fdd}|S )
Nr   r  rQ   r   r;   r	  c                  s    |   S r   )r
  r   eagerr>   rA   rM     s   z,_pl_read_json_roundtrip_to_arrow.<locals>.fn)r   r  rQ   r   r;   r	  )r   r   r   r   r>   r!  rA   r     s   r   r   r   c                C  sZ   dd l }t| ts|| S t| jdd}||W  d    S 1 s&w   Y  d S )Nr   zutf-8)encoding)jsonrx   r
   loadopen)r   r$  fr>   r>   rA   _stdlib_read_json  s   

$r(  r	  c          
      K  s   dd l }t| }z|jj|fi |W S  ty~   dd l}dd l}ddl m} | G}|j||d 	 |j
d}|  || ||  }	||	W  d    W  d     Y S 1 sew   Y  W d    Y d S 1 svw   Y  Y d S w )Nr   )r   )dialect)r   r(  Tablefrom_pylistra   r   r   StringIO
DictWriterkeysunix_dialectwriteheader	writerowsr  getvalueencoder   )
r   rQ   parowsr   r   pa_csvr'  writerf2r>   r>   rA   r     s&   

&r   r   )
rN   r*   rO   rP   rM   r   rQ   r   r;   r   )
rN   r*   rO   rP   rM   r   rQ   r   r;   r   )r   r   r;   r   )r   r   r   rm   r   r   r;   r   )r   r   r6   rm   r;   rm   )rM   r   r;   r   )r;   r   )r;   r   )r;   r   )r;   r   )r   r'   r;   r   )r;   r   )r   r   r   r   rQ   r   r;   r   )r   r'   r;   r  )r;   r  )r   r'   r;   r  )r   r   r;   r   )r   r   rQ   r   r;   r	  )^rI   
__future__r   sysenumr   	functoolsr   r   importlib.utilr   	itertoolsr   operatorr	   pathlibr
   r(   r   r   r   r   narwhals.stabler   r   narwhals.stable.v1.dependenciesr   r   narwhals.stable.v1.typingr   altair.datasets._constraintsr   r   r   r   r   r   r   r   r   altair.datasets._exceptionsr   version_infor   typing_extensionsr!   collections.abcr"   r#   r$   r%   r   r&   typesr'   r   r   r   r   r   r4  narwhals.stable.v1nwtr)   r*   __all__r2   	LazyFramer5   r7   r9   r:   rK   r0   r1   r   r+   rq   rr   r/   r-   r.   r,   r   r   r   r   r   r   r   r   r   r(  r   r>   r>   r>   rA   <module>   s    ,

{
















 


