
    +dA                     \   d Z ddlmZ ddlmZmZmZ ddlmZ  G d de	          Z
 G d	 d
e          Z G d d          Z	 ddlmZ n# e$ r eZY nw xY w G d d          Z G d de          Z G d d          Z G d de          Z G d deeee          Z G d de          ZdS )z(
Base classes for Data Loader interface
    )dump   )DictSerialiseMixinmake_path_posixpretty_describe   )make_cachesc                   .     e Zd ZdZ fdZd Zd Z xZS )SchemazHolds details of data description for any type of data-source

    This should always be pickleable, so that it can be sent from a server
    to a client, and contain all information needed to recreate a RemoteSource
    on the client.
    c                      t          t          |           j        di | dD ]}|| vrd| |<   d| vrd| d<   d| vri | d<   dS dS )z^
        Parameters
        ----------
        kwargs: typically include dtype, shape
        )dtypeshapeNnpartitionsr   extra_metadata )superr   __init__)selfkwargsfield	__class__s      2lib/python3.11/site-packages/intake/source/base.pyr   zSchema.__init__   s     	%fd$..v...' 	# 	#ED  #"U$ 	$"#D4' 	(%'D!"""	( 	(    c                 N    d                     | j        | j        | j                  S )Nz2<Schema instance>
dtype: {}
shape: {}
metadata: {})formatr   r   r   r   s    r   __repr__zSchema.__repr__)   s3    RYYZ^Zdfjfprv  sF  G  G  	Gr   c                     | |         S Nr   )r   items     r   __getattr__zSchema.__getattr__,   s    Dzr   )__name__
__module____qualname____doc__r   r   r!   __classcell__r   s   @r   r   r      sc         ( ( ( ( ( G G G      r   r   c                       e Zd ZdS )NoEntryN)r"   r#   r$   r   r   r   r)   r)   0   s        Dr   r)   c                   n    e Zd ZdZdZed             Zd Zed             Zej	        d             Zd Z
dS )
CacheMixinz*Allows "old style" caching for Data SourceNc                 $    d | j         D             S )Nc                     g | ]	}|j         
S r   )
_cache_dir).0cs     r   
<listcomp>z)CacheMixin.cache_dirs.<locals>.<listcomp>;   s    111111r   )cacher   s    r   
cache_dirszCacheMixin.cache_dirs9   s    11dj1111r   c                 B    | j         D ]}t          |          |_        d S r   )r2   r   r.   )r   	cache_dirr0   s      r   set_cache_dirzCacheMixin.set_cache_dir=   s.     	6 	6A*955ALL	6 	6r   c                     | j         St          | j        | j                            d          | j                            dd           | j                  | _         | j         S )Nr2   catalog_dir)catdirstorage_options)_cacher	   namemetadatagetr:   r   s    r   r2   zCacheMixin.cacheA   sj    ; 	b%di1B1B71K1KTXTaTeTefsuyTzTz  MQ  Ma  b  b  bDK{r   c                     || _         d S r   )r;   )r   cshs     r   r2   zCacheMixin.cacheG   s    r   c                 `    t          | j                  dk    rgS fd| j        D             S )Nr   c                 :    g | ]}|                               S r   )load)r/   r0   urlpaths     r   r1   z)CacheMixin._get_cache.<locals>.<listcomp>N   s#    444Aw444r   )lenr2   r   rD   s    `r   
_get_cachezCacheMixin._get_cacheK   s<    tz??a 	944444444r   )r"   r#   r$   r%   r;   propertyr3   r6   r2   setterrG   r   r   r   r+   r+   4   s        44F2 2 X26 6 6   X
 \  \5 5 5 5 5r   r+   )hvPlotc                   j    e Zd ZdZed             Zed             Zed             Zed             ZdS )HoloviewsMixinz#Adds plotting and GUI to DataSourcec                 R    t          | j                            di                     S )z"List custom associated quick-plotsplots)listr=   r>   r   s    r   rN   zHoloviewsMixin.plotsZ   s$     DM%%gr22333r   c                     | j         j        S )z1Source GUI, with parameter selection and plotting)entryguir   s    r   rR   zHoloviewsMixin.gui_   s     z~r   c                 l   t           t          u rt          d          | j                            di           }| j                            di           }|                                D ]}d|v rt          |d                   |d<   ||d<   | j                            di           }t          | fd|i|S )at  
        Plot API accessor

        This property exposes both predefined plots (described in the source metadata)
        and general-purpose plotting via the hvPlot library. Supported containers
        are: array, dataframe and xarray,

        To display in a notebook, be sure to run ``intake.output_notebook()``
        first.

        The set of plots defined for this source can be found by

        >>> source.plots
        ["plot1", "plot2"]

        and to display one of these:

        >>> source.plot.plot1()
        <holoviews/panel output>

        To create new plot types and supply custom configuration, use one of the
        methods of ``hvplot.hvPlot``:

        >>> source.plot.line(x="fieldX", y="fieldY")

        The full set of arguments that can be passed, and the types of plot they refer to,
        can be found in the doc and attributes of ``hvplot.HoloViewsConverter``.

        Once you have found a suitable plot, you may wish to update the plots definitions
        of the source. Simply add the ``plotname=`` optional argument (this will overwrite
        any existing plot of that name). The source's YAML representation will include the
        new plot, and it could be saved into a catalog with this new definition.

        >>> source.plot.line(plotname="new", x="fieldX", y="fieldY");
        >>> source.plots
        ["plot1", "plot2", "new"]

        zThe intake plotting API requires hvplot.hvplot may be installed with:

`conda install -c pyviz hvplot` or `pip install hvplot`.plotfieldsrangerN   custom_plots)rJ   objectImportErrorr=   r>   valuestupleHVplotCapture)r   r=   rU   attrsrN   s        r   rT   zHoloviewsMixin.plotd   s    P V 	 ]   =$$VR00""8R00]]__ 	7 	7E% 7!&uW~!6!6g#!!'2..TBBBBBBr   c                     | j         S )z/
        alias for ``DataSource.plot``
        )rT   r   s    r   hvplotzHoloviewsMixin.hvplot   s    
 yr   N)	r"   r#   r$   r%   rH   rN   rR   rT   r_   r   r   r   rL   rL   W   s        --4 4 X4   X 2C 2C X2Ch   X  r   rL   c                   (     e Zd ZdZdd fd
Z xZS )r\   z<Calls hvPlot, and allows storing the plot spec in the sourceN)plotnamec                d    |r|| j         j        d         |<    t                      j        |i |S )z
        If plotname is not None, this plot spec will be stored in the original
        source's metadata's "plots" section
        rN   )_datar=   r   __call__)r   ra   argsr   r   s       r   rd   zHVplotCapture.__call__   s=    
  	<5;DJ(2uww0000r   )r"   r#   r$   r%   rd   r&   r'   s   @r   r\   r\      sK        FF'+ 1 1 1 1 1 1 1 1 1 1 1r   r\   c                   b    e Zd ZdZd Zed             Zed             Zed             Z	ddZ
dS )	PersistMixinz0Adds interaction with PersistStore to DataSourcec                 L    ddl m} ddlm}  | ||                                S Nr   tokenizer   store	dask.baserk   container.persistrm   r   rk   rm   s      r   get_persistedzPersistMixin.get_persisted   sA    &&&&&&------$uXXd^^$&&&r   c                     t           )z?To be implemented by 'container' sources for locally persistingNotImplementedError)sourcepathr   s      r   _persistzPersistMixin._persist   s
     "!r   c                 4    ddl m} ddlm}  ||           |v S ri   rn   rq   s      r   has_been_persistedzPersistMixin.has_been_persisted   s9    &&&&&&------x~~&&r   c                 H    ddl m} | j                            dd           |v S )Nr   rl   original_tok)rp   rm   r=   r>   )r   rm   s     r   is_persistedzPersistMixin.is_persisted   s1    ------}  66%??r   Nc                    ddl m} ddlm} d| j        v rt          d          |.t          |t          t          f          st          d|            |            } | j	        |
                    |           fi |}|j                            || j        i n| j                                        d	           | j        |_        |                     ||           |           |S )
a  Save data from this source to local persistent storage

        Parameters
        ----------
        ttl: numeric, optional
            Time to live in seconds. If provided, the original source will
            be accessed and a new persisted version written transparently
            when more than ``ttl`` seconds have passed since the old persisted
            version was written.
        kargs: passed to the _persist method on the base container.
        r   rj   r   )PersistStorer|   z4Cannot persist a source taken from the persist storeNzOCannot persist using a time to live that is non-numeric. User-provided ttl was )ttlcat)ro   rk   rp   r   r=   
ValueError
isinstanceintfloat_exportgetdirupdater   __getstate__r<   add)r   r   r   rk   r   rm   outs          r   persistzPersistMixin.persist   s    	'&&&&&444444T]* 	XVWWW 	y:cC<#@#@ 	ywruwwxxxdl5<<--8888Cdh0cDHLaLaLcLcddeee9		((4..#&&&
r   r   )r"   r#   r$   r%   rr   staticmethodrx   rH   rz   r}   r   r   r   r   rg   rg      s        ::' ' ' " " \" ' ' X' @ @ X@
     r   rg   c                      e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZddZd Zd Zd Zd	 Zd
 Zd Zd Zd Zed             Zed             Zd Zd Zd Zd Zd Z d Z!d Z"ed             Z#d Z$e$xZ%Z&d Z'd Z(d Z)d Z*d Z+d Z,dS ) DataSourceBaseaS  An object which can produce data

    This is the base class for all Intake plugins, including catalogs and
    remote (server) data objects. To produce a new plugin commonly involves
    subclassing this definition and overriding some or all of the methods.

    This class is not useful in itself, most methods raise NotImplemented.
    NFr   c                     |pi | _         t          | j         t                    r|| j                            di           }|| _        d S )Nr:   )r=   r   dict_captured_init_kwargsr>   r:   )r   r:   r=   s      r   r   zDataSourceBase.__init__   sR     BdmT** 	T 	T"8<<=NPRSSO.r   c                     t           )z3Subclasses should return an instance of base.Schemart   r   s    r   _get_schemazDataSourceBase._get_schema      !!r   c                     t           )zSubclasses should return a container object for this partition

        This function will never be called with an out-of-range value for i.
        rt   r   is     r   _get_partitionzDataSourceBase._get_partition	  s
    
 "!r   c                     t           )z*Subclasses should close all open resourcesrt   r   s    r   _closezDataSourceBase._close  r   r   c                     | j         r|                                 | _         | j         j        | _        | j         j        | _        | j         j        | _        | j                            | j         j                   dS dS )zload metadata only if neededN)_schemar   r   r   r   r=   r   r   r   s    r   _load_metadatazDataSourceBase._load_metadata  so    < 	>++--DL+DJ+DJ#|7DM  !<=====	> 	>r   c           	      V   dd l }| j                                        }|                    d| j                  pi }|                    t          t          |                    | j	                  j
        | j                                       d| j        | j        | j        pd||dii}|S )Nr   r=   sources )driverdescriptionr=   re   )inspectr   copypopr=   r   r   zip	signaturer   
parameters_captured_init_argsr<   	classnamer   )r   r   r   metadatas        r   _yamlzDataSourceBase._yaml  s    +0022zz*dm44:d3w00??JDLdeeffgggDI$.QUQaQgeguy  DJ  (K  (K  L  Mr   c                 L    |                                  }t          |d          S )zReturn YAML representation of this data-source

        The output may be roughly appropriate for inclusion in a YAML
        catalog. This is a best-effort implementation
        Fdefault_flow_style)r   r   )r   r   s     r   yamlzDataSourceBase.yaml&  s$     zz||DU3333r   c                     ddl m} |                                 d         }t          |d          } ||t	          |          ddd| j        iid	
           dS )z9Display the entry as a rich object in an IPython session.r   )displayr   Fr   )zapplication/yamlz
text/plainzapplication/jsonrootT)r=   rawN)IPython.displayr   r   r   r   r<   )r   r   r   contentss       r   _ipython_display_z DataSourceBase._ipython_display_/  s    ++++++zz||I&777X_X=V=VWWcux~  AE  AJ  xK  cL  RV  	W  	W  	W  	W  	W  	Wr   c                 *    |                                  S r   )r   r   s    r   r   zDataSourceBase.__repr__7  s    yy{{r   c                     dS z1The base class does not interact with persistenceFr   r   s    r   r}   zDataSourceBase.is_persisted:  	     ur   c                     dS r   r   r   s    r   rz   z!DataSourceBase.has_been_persisted?  r   r   c                     |S )z,The base class does not interact with cachesr   rF   s     r   rG   zDataSourceBase._get_cacheD  s    r   c                 x    |                                   t          | j        | j        | j        | j                  S )z1Open resource and populate the source attributes.)r   r   r   r=   )r   r   r   r   r   r=   r   s    r   discoverzDataSourceBase.discoverH  s6    $*DJDDT_c_lmmmmr   c                 ^    | j         r| j        dk    r|                     d          S t          )z2Load entire dataset into a container and return itr   r   )partition_accessr   r   ru   r   s    r   readzDataSourceBase.readN  s6    $ 	&(8A(= 	&&&q)))%%r   c              #      K   |                                   t          | j                  D ]}|                     |          V  dS )z7Return iterator over container fragments of data sourceN)r   rV   r   r   r   s     r   read_chunkedzDataSourceBase.read_chunkedU  sY      t'(( 	) 	)A%%a((((((	) 	)r   c                     |                                   |dk     s|| j        k    rt          d|z            |                     |          S )zReturn a part of the data corresponding to i-th partition.

        By default, assumes i should be an integer between zero and npartitions;
        override for more complex indexing schemes.
        r   z%d is out of range)r   r   
IndexErrorr   r   s     r   read_partitionzDataSourceBase.read_partition[  sY     	q5 	7A)) 	71A5666""1%%%r   c                     t           )z,Return a dask container for this data sourcert   r   s    r   to_daskzDataSourceBase.to_daskg  r   r   c                     t           )a  Provide an equivalent data object in Apache Spark

        The mapping of python-oriented data containers to Spark ones will be
        imperfect, and only a small number of drivers are expected to be able
        to produce Spark objects. The standard arguments may b translated,
        unsupported or ignored, depending on the specific driver.

        This method requires the package intake-spark
        rt   r   s    r   to_sparkzDataSourceBase.to_sparkk  s
     "!r   c                 <    | j         t          d          | j         S )Nz(Source was not made from a catalog entry)_entryr)   r   s    r   rQ   zDataSourceBase.entryw  s$    ; 	FDEEE{r   c                 L     j         U fd j                                        D             }|                    |             j         di |} j         |_         |S  j                                        }|                    |            t                      j        i |S )a  Create a new instance of this source with altered arguments

        Enables the picking of options and re-evaluating templates from any
        user-parameters associated with
        this source, or overriding any of the init arguments.

        Returns a new data source instance. The instance will be recreated from
        the original entry definition in a catalog **if** this source was originally
        created from a catalog.
        Nc                 .    i | ]\  }}|j         v ||S r   )_passed_kwargs)r/   kvr   s      r   
<dictcomp>z0DataSourceBase.configure_new.<locals>.<dictcomp>  s,    bbb41adNaIab!Qbbbr   r   )r   r   itemsr   r   typer   )r   r   kwobjs   `   r   configure_newzDataSourceBase.configure_new}  s     ; 		?bbbb4#=#C#C#E#EbbbBIIf$+####CCJJ+0022BIIf4::t7>2>>>r   c                 4    | j                                         S )zDescription from the entry spec)rQ   describer   s    r   r   zDataSourceBase.describe  s    z""$$$r   c                 .    |                                   dS )z7Close open resources corresponding to this data source.N)r   r   s    r   closezDataSourceBase.close  s    r   c                 .    |                                   | S r   )r   r   s    r   	__enter__zDataSourceBase.__enter__  s    r   c                 .    |                                   d S r   )r   )r   exc_type	exc_value	tracebacks       r   __exit__zDataSourceBase.__exit__  s    

r   c                      | j         |fi |S )a  Save this data for sharing with other people

        Creates a copy of the data in a format appropriate for its container,
        in the location specified (which can be remote, e.g., s3).

        Returns the resultant source object, so that you can, for instance,
        add it to a catalog (``catalog.add(source)``) or get its YAML
        representation (``.yaml()``).
        )r   )r   rw   r   s      r   exportzDataSourceBase.export  s     t|D++F+++r   c                    dd l }ddlm} ddlm} || j                 j        } || fd|i|}| j        |_        |                                 | j        |                                 | j	         ||           |d}||_        | j	        |_	        |S )Nr   rj   r   )container_maprw   )	timestamporiginal_metadataoriginal_sourceoriginal_namer|   persist_kwargs)
timero   rk   	containerr   rx   r   r=   r   r<   )	r   rw   r   r   rk   r   methodr   r=   s	            r   r   zDataSourceBase._export  s    &&&&&&------t~.7fT/////*!%#0022!Y$HTNN$
 
  9
r   )NN)-r"   r#   r$   r%   r<   versionr   r   r   r   r   r   r   	on_serverr   r   r   r   r   r   r   r   r   r   r   rH   r}   rz   rG   r   r   r   r   r   r   rQ   r   rd   r>   r   r   r   r   r   r   r   r   r   r   r      s         DGIKEEKGI
CF/ / / /" " "" " "" " "> > >  4 4 4W W W     X   X  n n n& & &) ) )
& 
& 
&" " "
" 
" 
"   X
? ? ?, #"Hs% % %  
    
, 
, 
,    r   r   c                       e Zd ZdZdS )
DataSourcezA Data Source will all optional functionality

    When subclassed, child classes will have the base data source functionality,
    plus caching, plotting and persistence abilities.
    N)r"   r#   r$   r%   r   r   r   r   r     s          	Dr   r   c                       e Zd ZdZed             Zej        d             Zed             Zej        d             Zed             ZdS )PatternMixinzEHelper class to provide file-name parsing abilities to a driver classc                 N    t          | d          r| j        S t          d          )N_path_as_patternz<Plugin needs to set `path_as_pattern` before setting urlpath)hasattrr   KeyErrorr   s    r   path_as_patternzPatternMixin.path_as_pattern  s-    4+,, 	)((XYYYr   c                     || _         d S r   )r   )r   r   s     r   r   zPatternMixin.path_as_pattern  s     /r   c                     | j         S r   )_urlpathr   s    r   rD   zPatternMixin.urlpath  s
    }r   c                    ddl m} t          | d          r	|| _        d S || _        | j        r ||          | _        n|| _        t          | j        t                    r)t          |t                    r| j        |k    rd| _        d S d S d S d S )Nr   )path_to_glob_original_urlpathF)	utilsr  r   r   r  r   r   boolstr)r   rD   r  s      r   rD   zPatternMixin.urlpath  s    ''''''4,-- 	#DMF!( 	$(L11DMM#DMd*D11 	-'3'' -DMW,D -',$$$	- 	-- - - -r   c                     ddl m} t          | j        t                    r| j        S | j        r || j        | j                  S d S )Nr   )path_to_pattern)r  r  r   r   r  r  r=   )r   r  s     r   patternzPatternMixin.pattern  s[    ******d*C00 	J''! 	J"?4#94=IIIr   N)	r"   r#   r$   r%   rH   r   rI   rD   r  r   r   r   r   r     s        OOZ Z XZ
 0 0 0   X ^- - ^-$   X  r   r   N)r%   r   r   r  r   r   r   r2   r	   r   r   AttributeErrorr)   r+   r_   rJ   rY   rX   rL   r\   rg   r   r   r   r   r   r   <module>r
     s)          H H H H H H H H H H          T   >	 	 	 	 	n 	 	 	5 5 5 5 5 5 5 5:   FFFG G G G G G G GT
1 
1 
1 
1 
1F 
1 
1 
16 6 6 6 6 6 6 6r_ _ _ _ _' _ _ _D	 	 	 	 	^\> 	 	 	, , , , ,6 , , , , ,s   A
 
AA