
    ̒ge!                        U d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ 	 d dl	Z
n# e$ r	 esd dl
Z
Y nw xY wer'd dlmZmZmZmZ d dlmZ ddlmZ eeef         Zd	ed
<    G d d          ZdS )    )annotationsN)TYPE_CHECKING)atomic_write)AnyDictIteratorLiteral)	TypeAlias   )CachingFileSystemr
   Detailc                  n    e Zd ZdZd%dZd&d	Zd'dZ	 d(d)dZd*dZd+dZ	d,dZ
d-dZd.d Zd,d!Zd/d#Zd$S )0CacheMetadataa\  Cache metadata.

    All reading and writing of cache metadata is performed by this class,
    accessing the cached files and blocks is not.

    Metadata is stored in a single file per storage directory in JSON format.
    For backward compatibility, also reads metadata stored in pickle format
    which is converted to JSON when next saved.
    storage	list[str]c                T    |st          d          || _        i g| _        d| _        dS )z

        Parameters
        ----------
        storage: list[str]
            Directories containing cached files, must be at least one. Metadata
            is stored in the last of these directories by convention.
        z3CacheMetadata expects at least one storage locationFN)
ValueError_storagecached_files_force_save_pickle)selfr   s     Elib/python3.11/site-packages/fsspec/implementations/cache_metadata.py__init__zCacheMetadata.__init__%   s=      	TRSSS+-$ #(    fnstrreturnr   c                   	 t          |d          5 }t          j        |          cddd           S # 1 swxY w Y   dS # t          $ rB t          |d          5 }t	          j        |          cddd           cY S # 1 swxY w Y   Y dS w xY w)z6Low-level function to load metadata from specific filerNrb)openjsonloadr   pickle)r   r   fs      r   _loadzCacheMetadata._load8   s%   	&b# $!y||$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 	& 	& 	&b$ &1{1~~& & & & & & & & & & & & & & & & & & & & &	&sP   A  3A  7A  7A   BA=.B=B	BB	BBmetadata_to_saveNonec                   | j         r?t          |          5 }t          j        ||           ddd           dS # 1 swxY w Y   dS t          |d          5 }t	          j        ||           ddd           dS # 1 swxY w Y   dS )z4Low-level function to save metadata to specific fileNw)mode)r   r   r$   dumpr"   )r   r'   r   r%   s       r   _savezCacheMetadata._saveA   s   " 	/b!! 1Q,a0001 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 bs+++ /q	*A.../ / / / / / / / / / / / / / / / / /s   :>>A;;A?A?Fwritable_onlyboolIterator[tuple[str, str, bool]]c              #     K   t          | j                  }t          | j                  D ]8\  }}||dz
  k    }|r|st          j                            |d          ||fV  9dS )a  Yield locations (filenames) where metadata is stored, and whether
        writable or not.

        Parameters
        ----------
        writable: bool
            Set to True to only yield writable locations.

        Returns
        -------
        Yields (str, str, bool)
        r   cacheN)lenr   	enumerateospathjoin)r   r.   nir   writables         r   _scan_locationszCacheMetadata._scan_locationsJ   s       #DM22 	D 	DJAwAEzH X ',,w00'8CCCCC		D 	Dr   r6   cfsCachingFileSystem | None#Literal[False] | tuple[Detail, str]c                   t          |                                 | j                  D ]\  \  }}}}||vr||                                         }|Y|j        r%|d         |j                            |          k    rV|j        r&t          j                    |d         z
  |j        k    rt          j
                            ||d                   }t          j
                            |          r||fc S dS )zIf path is in cache return its details, otherwise return ``False``.

        If the optional CachingFileSystem is specified then it is used to
        perform extra checks to reject possible matches, such as if they are
        too old.
        Nuidtimer   F)zipr;   r   copycheck_filesfsukeyexpiryrA   r5   r6   r7   exists)r   r6   r<   r   base_r2   details           r   
check_filezCacheMetadata.check_file`   s     %((<(<(>(>@Q$R$R 	" 	" MRq55  4[%%''F? ve}D8I8I'I'I: $)++v">"K"KdF4L11Bw~~b!! "rz!!!"ur   expiry_timeinttuple[list[str], bool]c                   g }| j         d                                                                         D ]\  }}t          j                    |d         z
  |k    r|                    dd          }|st          d|           t          j                            | j	        d         |          }|
                    |           | j         d                             |           | j         d         rLt          j                            | j	        d         d          }|                     | j         d         |           | j         d          }||fS )zRemove expired metadata from the cache.

        Returns names of files corresponding to expired metadata and a boolean
        flag indicating whether the writable cache is empty. Caller is
        responsible for deleting the expired files.
        rA   r    z)Cache metadata does not contain 'fn' for r2   )r   rC   itemsrA   getRuntimeErrorr5   r6   r7   r   appendpopr-   )r   rM   expired_filesr6   rK   r   
cache_pathwritable_cache_emptys           r   clear_expiredzCacheMetadata.clear_expired{   sB     -b16688>>@@ 		0 		0LD&y{{VF^+k99ZZb)) &JDJJ   W\\$-"3R88$$R(((!"%))$///R  	:dmB&7AAJJJt(,j999#'#4R#88222r   c                   g }|                                  D ]\  }}}t          j                            |          ru|                     |          }|                                D ]5}t          |d         t                    rt          |d                   |d<   6|	                    |           |	                    i            |pi g| _
        dS )z>Load all metadata from disk and store in ``self.cached_files``blocksN)r;   r5   r6   rH   r&   values
isinstancelistsetrV   r   )r   r   r   rJ   loaded_cached_filescs         r   r#   zCacheMetadata.load   s    ,,.. 		( 		(HB1w~~b!! (&*jjnn#,3355 7 7A!!H+t44 7&)!H+&6&6(##$78888##B''''(0RDr   r%   r   c                    | j         d         |         }|d         dur-t          |d                   |j        z  |j        k    r	d|d<   dS dS dS )zPerform side-effect actions on closing a cached file.

        The actual closing of the file is the responsibility of the caller.
        rQ   r]   TN)r   r3   	blocksizesize)r   r%   r6   rc   s       r   on_close_cached_filez"CacheMetadata.on_close_cached_file   s_     b!$'X;d""s1X;'7'7!+'E'O'OAhKKK #"'O'Or   
str | Nonec                   |                      |d          }|sdS |\  }}|                    | j        d                   r5| j        d                             |           |                                  nt          d          |S )zRemove metadata of cached file.

        If path is in the cache, return the filename of the cached file,
        otherwise return ``None``.  Caller is responsible for deleting the
        cached file.
        NrQ   z<Can only delete cached file in last, writable cache location)rL   
startswithr   r   rW   savePermissionError)r   r6   detailsrJ   r   s        r   pop_filezCacheMetadata.pop_file   s     //$-- 	42==r*++ 	b!%%d+++IIKKKK!N   	r   c                z   t          |                                 | j                  D ]\  \  }}}}|st          j                            |          r|                     |          }|                                D ]\  }}||v r|d         du s||         d         du rd|d<   n.||         d         }|                    |d                    ||d<   t          |d         ||         d                   |d<   ||         d         |d<   |                                D ]\  }}||vr|||<   n|}d |                                D             }|
                                D ]5}t          |d         t                    rt          |d                   |d<   6|                     ||           || j        d<   dS )zSave metadata to diskr]   TrA   r@   c                >    i | ]\  }}||                                 S  )rC   ).0kvs      r   
<dictcomp>z&CacheMetadata.save.<locals>.<dictcomp>   s&    BBBTQQBBBr   rQ   N)rB   r;   r   r5   r6   rH   r&   rS   updatemaxr^   r_   ra   r`   r-   )	r   r   rJ   r:   r2   r   rs   rc   r]   s	            r   rk   zCacheMetadata.save   s   (+D,@,@,B,BDDU(V(V  	1  	1$RHu w~~b!! %#zz"~~(..00 3 3DAqEzzX;$..%(82D2L2L*.AhKK &+1Xh%7F"MM!H+666*0AhK$'&	58F3C$D$D&	#(8E?% "KKMM , ,DAq,,*+Q,  %BB\-?-?-A-ABBBE\\^^ 4 4ak3// 4"&q{"3"3AhKJJub!!!$0Db!!A 	1  	1r   rK   c                &    || j         d         |<   dS )z8Update metadata for specific file in memory, do not saverQ   N)r   )r   r6   rK   s      r   update_filezCacheMetadata.update_file   s    &,"d###r   N)r   r   )r   r   r   r   )r'   r   r   r   r   r(   )F)r.   r/   r   r0   )r6   r   r<   r=   r   r>   )rM   rN   r   rO   )r   r(   )r%   r   r6   r   r   r(   )r6   r   r   rh   )r6   r   rK   r   r   r(   )__name__
__module____qualname____doc__r   r&   r-   r;   rL   r[   r#   rg   rn   rk   ry   rq   r   r   r   r      s
        ( ( ( (&& & & &/ / / / %*D D D D D,   63 3 3 341 1 1 1      ("1 "1 "1 "1H- - - - - -r   r   )
__future__r   r5   r$   rA   typingr   fsspec.utilsr   ujsonr"   ImportErrorr   r   r   r	   typing_extensionsr
   cachedr   r   r   __annotations__r   rq   r   r   <module>r      sB   " " " " " " " 				               % % % % % %      '333333333333++++++))))))S#XF&&&&N- N- N- N- N- N- N- N- N- N-s   & 44