o
    ¼ÎhX!  ã                   @  sÈ   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 zd dl	Z
W n ey4   es2d dl
Z
Y n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„ dƒZdS )é    )ÚannotationsN)ÚTYPE_CHECKING)Úatomic_write)ÚIterator)ÚAnyÚLiteral)Ú	TypeAliasé   )ÚCachingFileSystemr   ÚDetailc                   @  s‚   e Zd ZdZd0dd„Zd1d
d„Zd2dd„Z	d3d4dd„Zd5dd„Zd6dd „Z	d7d!d"„Z
d8d%d&„Zd9d(d)„Zd7d*d+„Zd:d-d.„Zd/S );Ú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                 C  s$   |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   © r   úk/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/fsspec/implementations/cache_metadata.pyÚ__init__&   s
   	
zCacheMetadata.__init__ÚfnÚstrÚreturnr   c                 C  s²   zt |dƒ}t |¡}W d  ƒ n1 sw   Y  W n# ty?   t |dƒ}t |¡}W d  ƒ n1 s8w   Y  Y nw | ¡ D ]}t| d¡tƒrVt	|d ƒ|d< qD|S )z6Low-level function to load metadata from specific fileÚrNÚrbÚblocks)
ÚopenÚjsonÚloadr   ÚpickleÚvaluesÚ
isinstanceÚgetÚlistÚset)r   r   ÚfZloadedÚcr   r   r   Ú_load9   s    ÿ€ÿ€ÿ€zCacheMetadata._loadÚmetadata_to_saveÚNonec                 C  s~   | j r t|ƒ}t ||¡ W d  ƒ dS 1 sw   Y  dS t|dd}t ||¡ W d  ƒ dS 1 s8w   Y  dS )z4Low-level function to save metadata to specific fileNÚw)Úmode)r   r   r    Údumpr   )r   r)   r   r&   r   r   r   Ú_saveF   s   
"ÿ"ÿzCacheMetadata._saveFÚwritable_onlyÚboolúIterator[tuple[str, str, bool]]c                 c  sP    t | jƒ}t| jƒD ]\}}||d k}|r|sqtj |d¡||fV  qd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   Úwritabler   r   r   Ú_scan_locationsO   s   €
üzCacheMetadata._scan_locationsr6   ÚcfsúCachingFileSystem | Noneú#Literal[False] | tuple[Detail, str]c                 C  s¦   t |  ¡ | jƒD ]H\\}}}}||vrq||  ¡ }|dur;|jr,|d |j |¡kr,q|jr;t ¡ |d  |jkr;qt	j
 ||d ¡}t	j
 |¡rP||f  S qdS )zõIf 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   ÚcopyZcheck_filesÚfsZukeyZexpiryr@   r5   r6   r7   Úexists)r   r6   r<   r   ÚbaseÚ_r2   Údetailr   r   r   Ú
check_filee   s   	ÿzCacheMetadata.check_fileÚexpiry_timeÚintútuple[list[str], bool]c                 C  sÄ   g }| j d  ¡  ¡ D ]4\}}t ¡ |d  |kr?| dd¡}|s(td|› ƒ‚tj | j	d |¡}| 
|¡ | j d  |¡ q| j d rXtj | j	d d¡}|  | j d |¡ | j d  }||fS )zûRemove 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.
        éÿÿÿÿr@   r   Ú z)Cache metadata does not contain 'fn' for r2   )r   rB   Úitemsr@   r#   ÚRuntimeErrorr5   r6   r7   r   ÚappendÚpopr.   )r   rI   Zexpired_filesr6   rG   r   Ú
cache_pathZwritable_cache_emptyr   r   r   Úclear_expired€   s"   ÿ
€
zCacheMetadata.clear_expiredc                 C  sP   g }|   ¡ D ]\}}}tj |¡r| |  |¡¡ q| i ¡ q|p$i g| _dS )z>Load all metadata from disk and store in ``self.cached_files``N)r;   r5   r6   rD   rP   r(   r   )r   r   r   rF   r   r   r   r   š   s   zCacheMetadata.loadr&   r   c                 C  sF   | j d | }|d durt|d ƒ|j |jkr!d|d< dS dS dS )z‹Perform side-effect actions on closing a cached file.

        The actual closing of the file is the responsibility of the caller.
        rL   r   TN)r   r3   Ú	blocksizeÚsize)r   r&   r6   r'   r   r   r   Úon_close_cached_file¥   s   $ÿz"CacheMetadata.on_close_cached_fileú
str | Nonec                 C  sP   |   |d¡}|s
dS |\}}| | jd ¡r$| jd  |¡ |  ¡  |S tdƒ‚)zÒRemove 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.
        NrL   z<Can only delete cached file in last, writable cache location)rH   Ú
startswithr   r   rQ   ÚsaveÚPermissionError)r   r6   ÚdetailsrF   r   r   r   r   Úpop_file¯   s   ýÿzCacheMetadata.pop_filec           	      C  sH  t |  ¡ | jƒD ]™\\}}}}|sqtj |¡ru|  |¡}| ¡ D ]A\}}||v rb|d du s7|| d du r<d|d< n|| d }| |d ¡ ||d< t	|d || d ƒ|d< || d |d< q!| ¡ D ]\}}||vrs|||< qgn|}dd„ | ¡ D ƒ}| 
¡ D ]}t|d tƒr•t|d ƒ|d< q„|  ||¡ || jd< qdS )	zSave metadata to diskr   Tr@   r?   c                 S  s   i | ]	\}}||  ¡ “qS r   )rB   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>à   s    z&CacheMetadata.save.<locals>.<dictcomp>rL   N)rA   r;   r   r5   r6   rD   r(   rN   ÚupdateÚmaxr!   r"   r%   r$   r.   )	r   r   rF   r:   r2   r   r^   r'   r   r   r   r   rY   Ã   s:   

€€þ€àzCacheMetadata.saverG   c                 C  s   || j d |< dS )z8Update metadata for specific file in memory, do not saverL   N)r   )r   r6   rG   r   r   r   Úupdate_fileç   s   zCacheMetadata.update_fileN)r   r   )r   r   r   r   )r)   r   r   r   r   r*   )F)r/   r0   r   r1   )r6   r   r<   r=   r   r>   )rI   rJ   r   rK   )r   r*   )r&   r   r6   r   r   r*   )r6   r   r   rW   )r6   r   rG   r   r   r*   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r(   r.   r;   rH   rS   r   rV   r\   rY   rc   r   r   r   r   r      s    




ÿ






$r   )Ú
__future__r   r5   r    r@   Útypingr   Zfsspec.utilsr   Zujsonr   ÚImportErrorÚcollections.abcr   r   r   Ztyping_extensionsr   Úcachedr
   Údictr   r   Ú__annotations__r   r   r   r   r   Ú<module>   s(    €þ