
    +dg%                         d dl mZ ddlmZ ddlmZ ddlmZmZ  G d de          Z  ed	          e          Z	d
 Z
d Zd Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    )	lru_cache   )open_catalog   )import_name)
DataSourceSchemac                   T     e Zd ZdZdZdZdZd fd	Zd Zd Z	d	 Z
d
 Zd Zd Z xZS )AliasSourceac  Refer to another named source, unmodified

    The purpose of an Alias is to be able to refer to other source(s) in the
    same catalog, perhaps leaving the choice of which target to load up to the
    user. This source makes no sense outside of a catalog.

    In this case, the output of the target source is not modified, but this
    class acts as a prototype 'derived' source for processing the output of
    some standard driver.

    After initial discovery, the source's container and other details will be
    updated from the target; initially, the AliasSource container is not
    any standard.
    otherr   aliasNc                     t          t          |                               |           || _        |p||i| _        || _        || _        d| _        dS )a  

        Parameters
        ----------
        target: str
            Name of the source to load, must be a key in the same catalog
        mapping: dict or None
            If given, use this to map the string passed as ``target`` to
            entries in the catalog
        metadata: dict or None
            Extra metadata to associate
        kwargs: passed on to the target
        N)superr   __init__targetmappingkwargsmetadatasource)selfr   r   r   r   	__class__s        5lib/python3.11/site-packages/intake/source/derived.pyr   zAliasSource.__init__   sT     	k4  ))(333266"2     c                 X   | j         t          d          | j         | j         | j        | j                          dd| j        i| j        | _        | j        j                                        | _        | j        j        | _        | j        j	        | _	        | j        j
        | _
        d S d S )Nz,AliasSource cannot be used outside a catalogr    )cat
ValueErrorr   r   r   r   r   copy	containerpartition_accessdescriptionr   s    r   _get_sourcezAliasSource._get_source1   s    8 	MKLLL; 	7=$(4<#<=ddt}dX\XcddDK K05577DM![2DN$(K$@D!#{6D	7 	7r   c                 \    |                                   | j                                        S N)r#   r   discoverr"   s    r   r&   zAliasSource.discover;   s(    {##%%%r   c                 \    |                                   | j                                        S r%   )r#   r   readr"   s    r   r(   zAliasSource.read?   s(    {!!!r   c                 ^    |                                   | j                            |          S r%   )r#   r   read_partition)r   is     r   r*   zAliasSource.read_partitionC   s*    {))!,,,r   c                 \    |                                   | j                                        S r%   )r#   r   read_chunkedr"   s    r   r-   zAliasSource.read_chunkedG   s(    {'')))r   c                 \    |                                   | j                                        S r%   )r#   r   to_daskr"   s    r   r/   zAliasSource.to_daskK   s(    {""$$$r   )NN)__name__
__module____qualname____doc__r   versionnamer   r#   r&   r(   r*   r-   r/   __classcell__r   s   @r   r   r      s          IGD     *7 7 7& & &" " "- - -* * *% % % % % % %r   r   
   c                     d| v r&|                      dd          \  }} t          |fi |}|r ||          j        di |S | S )N:r   r   )rsplitcached_catsconfigure_new)r   r   r   
cat_kwargscaturls        r   
get_sourcer@   S   sd    
f} 0sA..&//J//
 3(s6{(226222Mr   c                 ^    | d         }t          |||                    |i           |          S )zA target chooser that simply picks the first from the given list

    This is the default, particularly for the case of only one element in
    the list
    r   )r@   get)targetsr   r   r>   targs        r   firstrE   ]   s/     1:DdCD"!5!5zBBBr   c           	          | D ]P}	 t          |||                    |i           |          }|                                 |c S # t          $ r Y Mw xY wt	          d          )zA target chooser: the first target for which discover() succeeds

    This may be useful where some drivers are not importable, or some
    sources can be available only sometimes.
    z"No targets succeeded at discover())r@   rB   r&   	ExceptionRuntimeError)rC   r   r   r>   tss         r   first_discoverablerK   g   s~       	1c6::a#4#4jAAAJJLLLHHH 	 	 	D	
;
<
<<s   ;A
AAc                   J     e Zd ZdZdZdZg Zi Zeddddf fd	Z	d Z
d Z xZS )DerivedSourcezBase source deriving from another source in the same catalog

    Target picking and parameter validation are performed here, but
    you probably want to subclass from one of the more specific
    classes like ``DataFrameTransform``.
    r   Nc                    || _         t          |          r|nt          |          | _        |pi | _        d| _        || _        |pi | _        |r|| _        | 	                                 t                                          |           dS )a  

        Parameters
        ----------
        targets: list of string or DataSources
            If string(s), refer to entries of the same catalog as this Source
        target_chooser: function to choose between targets
            function(targets, cat) -> source, or a fully-qualified dotted string pointing
            to it
        target_kwargs: dict of dict with keys matching items of targets
        cat_kwargs: to pass to intake.open_catalog, if the target is in
            another catalog
        container: str (optional)
            Assumed output container, if known/different from input

        [Note: the exact form of target_kwargs and cat_kwargs may be
        subject to change]
        N)r   )rC   callabler   _chooser_kwargs_source_params_cat_kwargsr   _validate_paramsr   r   )	r   rC   target_choosertarget_kwargsr>   r   r   r   r   s	           r   r   zDerivedSource.__init__   s    & *2>*B*BcTbHcHc$*%+ 	'&DN(+++++r   c                     t          | j                  t          | j                  z
  t                      k    sJ | j                                        D ]\  }}|| j        vr
|| j        |<   dS )zBThat all required params are present and that optional types matchN)setrequired_paramsrS   optional_paramsitems)r   parvals      r   rU   zDerivedSource._validate_params   sy    4'((3t|+<+<<EEEE,2244 	( 	(HC$,& ($'S!	( 	(r   c                    |                      | j        | j        | j        | j                  | _        | j        dk    r| j        j        | j        k    sJ | j        j        | j        d<   | j        | j        j        | _        dS dS )z&Pick the source from the given targetsr   r   N)	rP   rC   r   rQ   rT   rR   input_containerr   r   r"   s    r   _pickzDerivedSource._pick   s    }}T\48T\4K[\\7* 	B<)T-AAAAA"&,"7h> 	4!\3DNNN	4 	4r   )r0   r1   r2   r3   r`   r   rZ   r[   rE   r   rU   ra   r6   r7   s   @r   rM   rM   w   s          OIOO/4DUYeitx , , , , , ,<( ( (4 4 4 4 4 4 4r   rM   c                   B     e Zd ZddgZddiZ	  fdZd Zd Zd Z xZ	S )	GenericTransform	transformtransform_kwargs
allow_daskTc                     t                                                       | j        d         }t          |          r|nt	          |          | _        d S )Nrd   )r   rU   rS   rO   r   
_transform)r   rd   r   s     r   rU   z!GenericTransform._validate_params   sI      """L-	'/	':':V))I@V@Vr   c                 F    |                                   t                      S )z0We do not know the schema of a generic transform)ra   r	   r"   s    r   _get_schemazGenericTransform._get_schema   s    

xxr   c                     |                                   | j        d         st          d           | j        | j                                        fi | j        d         S )Nrf   zGThis transform is not compatible with Daskbecause it has use_dask=Falsere   )rj   rS   r   rh   rR   r/   r"   s    r   r/   zGenericTransform.to_dask   sc    |L) 	kijjjtt|3355ZZFX9YZZZr   c                     |                                    | j        | j                                        fi | j        d         S Nre   )rj   rh   rR   r(   rS   r"   s    r   r(   zGenericTransform.read   sD    tt|0022WWdlCU6VWWWr   )
r0   r1   r2   rZ   r[   rU   rj   r/   r(   r6   r7   s   @r   rc   rc      s        "$67O#T*OW W W W W
  
[ [ [X X X X X X Xr   rc   c                   4    e Zd ZdZdZdZi ZdZd Zd Z	d Z
dS )DataFrameTransformzTransform where the input and output are both Dask-compatible dataframes

    This derives from GenericTransform, and you must supply ``transform`` and
    any ``transform_kwargs``.
    	dataframeNc                     | j         I|                                   | j        | j                                        fi | j        d         | _         | j         S rm   )_dfra   rh   rR   r/   rS   r"   s    r   r/   zDataFrameTransform.to_dask   sR    8 	cJJLLL&tt|';';'='=bbN`AabbDHxr   c                     |                                   t          | j        j        dt	          | j        j                  f| j        j        | j                  S )zload metadata only if neededN)dtypeshapenpartitionsr   )r/   r	   rr   dtypeslencolumnsrv   r   r"   s    r   rj   zDataFrameTransform._get_schema   s[    DHOD#dh>N:O:O3P^b^f^r  ~B  ~K  L  L  L  	Lr   c                 N    |                                                                  S r%   )r/   computer"   s    r   r(   zDataFrameTransform.read   s    ||~~%%'''r   )r0   r1   r2   r3   r`   r   r[   rr   r/   rj   r(   r   r   r   ro   ro      sd          "OIO
C  L L L
( ( ( ( (r   ro   c                   6     e Zd ZdZdZdZdgZ fdZd Z xZ	S )ColumnszSimple dataframe transform to pick columns

    Given as an example of how to make a specific dataframe transform.
    Note that you could use DataFrameTransform directly, by writing a
    function to choose the columns instead of a method as here.
    rp   ry   c                 t    |                     | j        |i             t                      j        di | dS )zx
        columns: list of labels (usually str) or slice
            Columns to choose from the target dataframe
        )rd   ry   re   Nr   )updatepick_columnsr   r   )r   ry   r   r   s      r   r   zColumns.__init__  sD     	 17UWXXX""6"""""r   c                 (    || j         d                  S )Nry   )rS   )r   dfs     r   r   zColumns.pick_columns  s    $,y)**r   )
r0   r1   r2   r3   r`   r   rZ   r   r   r6   r7   s   @r   r}   r}      sd          "OI kO	# 	# 	# 	# 	#+ + + + + + +r   r}   N)	functoolsr    r   r   baser   r	   r   r<   r@   rE   rK   rM   rc   ro   r}   r   r   r   <module>r      s                     $ $ $ $ $ $ $ $E% E% E% E% E%* E% E% E%P iimmL))  C C C= = = :4 :4 :4 :4 :4J :4 :4 :4z$X $X $X $X $X} $X $X $XN( ( ( ( () ( ( (6+ + + + +  + + + + +r   