
    YhX!                        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 d dl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)Iterator)Any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     w/var/www/tools.fuzzalab.pt/emblema-extractor/venv/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._load9   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._saveF   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   r5   nir   writables         r   _scan_locationszCacheMetadata._scan_locationsO   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)ziprB   r   copycheck_filesfsukeyexpiryrH   r<   r=   r>   exists)r   r=   rC   r   base_r9   details           r   
check_filezCacheMetadata.check_filee   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.
        rH   r    z)Cache metadata does not contain 'fn' for r9   )r   rJ   itemsrH   r'   RuntimeErrorr<   r=   r>   r   appendpopr4   )r   rT   expired_filesr=   rR   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)rB   r<   r=   rO   r\   r-   r   )r   r   r   rQ   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.
        rX   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.
        NrX   z<Can only delete cached file in last, writable cache location)rS   
startswithr   r   r]   savePermissionError)r   r=   detailsrQ   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    TrH   rG   c                >    i | ]\  }}||                                 S  )rJ   ).0kvs      r   
<dictcomp>z&CacheMetadata.save.<locals>.<dictcomp>   s&    BBBTQQBBBr   rX   N)rI   rB   r   r<   r=   rO   r-   rZ   updatemaxr%   r&   r)   r(   r4   )	r   r   rQ   rA   r9   r   rr   r,   r    s	            r   rj   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   rR   c                &    || j         d         |<   dS )z8Update metadata for specific file in memory, do not saverX   N)r   )r   r=   rR   s      r   update_filezCacheMetadata.update_file   s    &,"d###r   N)r   r   )r   r   r   r   )r.   r   r   r   r   r/   )F)r5   r6   r   r7   )r=   r   rC   rD   r   rE   )rT   rU   r   rV   )r   r/   )r*   r   r=   r   r   r/   )r=   r   r   rg   )r=   r   rR   r   r   r/   )__name__
__module____qualname____doc__r   r-   r4   rB   rS   ra   r#   rf   rm   rj   rx   rp   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$   rH   typingr   fsspec.utilsr   ujsonr"   ImportErrorcollections.abcr   r   r   typing_extensionsr	   cachedr   dictr   r   __annotations__r   rp   r   r   <module>r      sH   " " " " " " " 				               % % % % % %      '((((((########++++++))))))S#XF&&&&N- N- N- N- N- N- N- N- N- N-s   & 44