
    çg?!                        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     a/var/www/html/ai-engine/env/lib/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        |          }ddd           n# 1 swxY w Y   nL# t          $ r? t          |d          5 }t	          j        |          }ddd           n# 1 swxY w Y   Y nw xY w|                                D ]B}t          |                    d          t                    rt          |d                   |d<   C|S )z6Low-level function to load metadata from specific filerNrbblocks)
openjsonloadr   picklevalues
isinstancegetlistset)r   r   floadedcs        r   _loadzCacheMetadata._load8   sO   	(b# &!1& & & & & & & & & & & & & & & 	( 	( 	(b$ (1Q( ( ( ( ( ( ( ( ( ( ( ( ( ( (	(  	/ 	/A!%%//400 /!!H+..(sK   ? 3? 7? 7? BA:.B:A>	>BA>	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._saveE   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   r6   nir   writables         r   _scan_locationszCacheMetadata._scan_locationsN   s       #DM22 	D 	DJAwAEzH X ',,w00'8CCCCC		D 	Dr   r>   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)ziprC   r   copycheck_filesfsukeyexpiryrI   r=   r>   r?   exists)r   r>   rD   r   base_r:   details           r   
check_filezCacheMetadata.check_filed   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.
        rI   r    z)Cache metadata does not contain 'fn' for r:   )r   rK   itemsrI   r(   RuntimeErrorr=   r>   r?   r   appendpopr5   )r   rU   expired_filesr>   rS   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 ]c\  }}}t          j                            |          r)|                    |                     |                     N|                    i            d|pi g| _        dS )z>Load all metadata from disk and store in ``self.cached_files``N)rC   r=   r>   rP   r]   r.   r   )r   r   r   rR   s       r   r$   zCacheMetadata.load   s    ,,.. 	( 	(HB1w~~b!! (##DJJrNN3333##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.
        rY   r!   TN)r   r;   	blocksizesize)r   r+   r>   r-   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.
        NrY   z<Can only delete cached file in last, writable cache location)rT   
startswithr   r   r^   savePermissionError)r   r>   detailsrR   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!   TrI   rH   c                >    i | ]\  }}||                                 S  )rK   ).0kvs      r   
<dictcomp>z&CacheMetadata.save.<locals>.<dictcomp>   s&    BBBTQQBBBr   rY   N)rJ   rC   r   r=   r>   rP   r.   r[   updatemaxr&   r'   r*   r)   r5   )	r   r   rR   rB   r:   r   rs   r-   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   rS   c                &    || j         d         |<   dS )z8Update metadata for specific file in memory, do not saverY   N)r   )r   r>   rS   s      r   update_filezCacheMetadata.update_file   s    &,"d###r   N)r   r   )r   r   r   r   )r/   r   r   r   r   r0   )F)r6   r7   r   r8   )r>   r   rD   rE   r   rF   )rU   rV   r   rW   )r   r0   )r+   r   r>   r   r   r0   )r>   r   r   rh   )r>   r   rS   r   r   r0   )__name__
__module____qualname____doc__r   r.   r5   rC   rT   rb   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   r=   r%   rI   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