
    +d                         d dl Z d dlZd dlZddlmZmZ ddlmZmZ ddl	m
Z
mZ ddlmZ  G d de          Z e            ZdS )	    N   )CatalogEntryYAMLFileCatalog)conflogger)
DataSourceimport_name)make_path_posixc                   l     e Zd ZdZdgZd Zd fd	Z fdZd Zd Z	d Z
dd
Zd Zd Zd Zd Z xZS )PersistStorez8
    Specialised catalog for persisted data-sources
    Nc                     | j         d         2t                              |           }||_        ||_        || j         d<   | j         d         S )Nr   )
_singletonobject__new___captured_init_args_captured_init_kwargs)clsargskwargsos       8lib/python3.11/site-packages/intake/container/persist.pyr   zPersistStore.__new__   sK    >! 	"s##A$(A!&,A# !CN1~a      c                 6   ddl m} ddlm} t	          |pt          j        d                    | _         || j                  \  }}t          j	        | j        d          } ||fi || _
        t          t          |                               |           d S )Nr   )
filesystem)split_protocolpersist_pathzcat.yaml)fsspecr   fsspec.corer   r
   r   getpdir	posixpathjoinfssuperr   __init__)selfpathstorage_optionsr   r   protocol_	__class__s          r   r%   zPersistStore.__init__#   s    %%%%%%......#D$DDH^,D,DEE	$nTY//!~di44*X9999lD!!**400000r   c                     	 | j                             | j                   n# t          t          f$ r Y nw xY w	 t          t          |                                            d S # t          $ r i | _	        Y d S w xY wN)
r#   mkdirsr    OSErrorIOErrorr$   r   _load	Exception_entries)r&   r+   s    r   r1   zPersistStore._load-   s    	GNN49%%%%! 	 	 	D		,%%++----- 	 	 	 DMMMM	s   # 77'A$ $A98A9c                 "   ddl m} t          j        | j         ||                    }	 | j                            |d           n.# t          $ r!}t          j	        d|z             Y d}~nd}~ww xY w| j        
                    |           |S )z:Clear/create a directory to store a persisted dataset intor   tokenizeTzDirectory clear failed: %sN)	dask.baser6   r!   r"   r    r#   rmr2   r   debugr.   )r&   sourcer6   subdires        r   getdirzPersistStore.getdir:   s    &&&&&&	88F+;+;<<	;GJJvt$$$$ 	; 	; 	;L59::::::::	;vs   A 
A2A--A2c           	      x   ddl m} 	 | j                            | j        d          5 }t          j        |          }ddd           n# 1 swxY w Y   n# t          $ r di i}Y nw xY w|                                d         |j	                 }||d         |<   | j                            | j        d          5 }|
                    t          j        |d                                                     ddd           n# 1 swxY w Y    |d|d	         d
         dg g dd|d         |         | j        |<   dS )a  Add the persisted source to the store under the given key

        key : str
            The unique token of the un-persisted, original source
        source : DataSource instance
            The thing to add to the persisted catalogue, referring to persisted
            data
        r   )LocalCatalogEntryrbNsourceswbFdefault_flow_stylemetadataoriginal_nameT)namedirect_accesscache
parameterscatalog_dir )intake.catalog.localr?   r#   openr'   yaml	safe_loadr0   _yamlrG   writedumpencoder3   )r&   keyr:   r?   fdatadsfos           r   addzPersistStore.addF   s    	;:::::	#di.. )!~a(() ) ) ) ) ) ) ) ) ) ) ) ) ) ) 	# 	# 	#r?DDD	#\\^^I&v{3!YW\\$)T** 	IbHHTYt>>>EEGGHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I.. 
J0B[]ko
 
sw  yB  tC  DG  tH
 
csE    A A	A 	AA AA A&%A&9<DDDc                 .   ddl m} t          |t                    r|S t          |t                    r$|j                            d ||                    S t          |t                    r$|j                            d ||                    S t          )zGet string token from object

        Strings are assumed to already be a token; if source or entry, see
        if it is a persisted thing ("original_tok" is in its metadata), else
        generate its own token.
        r   r5   original_tok)
r7   r6   
isinstancestrr   	_metadatar   r   rE   
IndexError)r&   r:   r6   s      r   get_tokzPersistStore.get_tok^   s     	'&&&&&fc"" 	Mfl++ 	J#''8H8HIIIfj)) 	I?&&~xx7G7GHHHr   Tc                 $   |                      |          }| j                            | j        d          5 }t	          j        |                                                                          }ddd           n# 1 swxY w Y   |d                             |d           | j                            | j        d          5 }|	                    t	          j
        |d                                                     ddd           n# 1 swxY w Y   |r^t          j        | j        |          }	 | j                            |d           n'# t           $ r t#          j        d|z             Y nw xY w| j                            |d           dS )	a~  Remove a dataset from the persist store

        source : str or DataSource or Lo
            If a str, this is the unique ID of the original source, which is
            the key of the persisted dataset within the store. If a source,
            can be either the original or the persisted source.
        delfiles : bool
            Whether to remove the on-disc artifact
        r@   NrA   rB   FrC   Tz&Failed to delete persisted data dir %s)ra   r#   rN   r'   rO   rP   readdecodepoprR   rS   rT   r!   r"   r    r8   r2   r   r9   r3   )r&   r:   delfilesrV   rW   rY   r'   s          r   removezPersistStore.removeq   s    f%%W\\$)T** 	5a>!&&((//"3"344D	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5YFD)))W\\$)T** 	IbHHTYt>>>EEGGHHH	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	N>$)V44DN

4&&&& N N NELMMMMMN&$'''''s5   9A;;A?A?<D

DD2E !E21E2c                 F    | j                             | j        d           dS )z/Remove all persisted sources, files and catalogTN)r#   r8   r    )r&   s    r   clearzPersistStore.clear   s     

49d#####r   c                    |                      |          } | |                     }|j        d         }|d         }|d         }|d         }t          |          } ||i |}|j        d         |_        |j        d         |_        |S )z9Given a unique key in the store, recreate original sourceoriginal_sourcer   r   r   original_metadatarF   )ra   rE   r	   rG   )	r&   r:   rU   smetar   r   r   souts	            r   	backtrackzPersistStore.backtrack   s    ll6""DIKKz+,5kF|h#sD#F##
#67J/	r   c                 p    | |         }|                      |          } |j        di |j        d          dS )z:Recreate and re-persist the source for the given unique IDpersist_kwargsNrL   )rp   persistrE   )r&   rU   s0rm   s       r   refreshzPersistStore.refresh   sB    #YNN3	22BK 0122222r   c                     ddl m} t          j                    } ||          }|| v rN| |         }| |         j                            dd          r#|j        d         }|j        d         ||z
  k     rdS dS dS )zHas the (persisted) source expired in the store

        Will return True if the source is not in the store at all, if it's
        TTL is set to None, or if more seconds have passed than the TTL.
        r   r5   ttlN	timestampTF)r7   r6   timerE   r   )r&   r:   r6   nowtokenrt   thens          r   needs_refreshzPersistStore.needs_refresh   s     	'&&&&&ikk  D= 	eBE{#''t44  {;/;u%s
2  45tr   r-   )T)__name__
__module____qualname____doc__r   r   r%   r1   r=   rZ   ra   rg   ri   rp   ru   r}   __classcell__)r+   s   @r   r   r      s          J! ! !1 1 1 1 1 1    
 
 

 
 
0  &( ( ( (0$ $ $  3 3 3      r   r   )r!   ry   rO   catalog.localr   r   configr   r   r:   r   r	   utilsr
   r   storerL   r   r   <module>r      s          9 9 9 9 9 9 9 9 ! ! ! ! ! ! ! ! , , , , , , , , # # # # # #_ _ _ _ _? _ _ _D 	r   