a
    ’À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yh   esdd dl
Z
Y n0 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ddœdd„Zdddœd	d
„Zddddœdd„Zd0dddœdd„Zddddœdd„Zdddœdd„Z	ddœd d!„Z
d"ddd#œd$d%„Zdd&d'œd(d)„Zddœd*d+„Zdddd,œd-d.„Zd/S )1Ú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.
    z	list[str])Ústoragec                 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   úa/var/www/html/assistant/venv/lib/python3.9/site-packages/fsspec/implementations/cache_metadata.pyÚ__init__&   s
    	zCacheMetadata.__init__Ústrr   )ÚfnÚreturnc                 C  s²   z8t |dƒ}t |¡}W d  ƒ n1 s,0    Y  W nF ty~   t |dƒ}t |¡}W d  ƒ n1 sp0    Y  Y n0 | ¡ D ]$}t| d¡tƒrˆt	|d ƒ|d< qˆ|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ÚNone)Úmetadata_to_saver   r   c                 C  sx   | j r<t|ƒ}t ||¡ W d  ƒ qt1 s00    Y  n8t|dd}t ||¡ W d  ƒ n1 sj0    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ÚboolzIterator[tuple[str, str, bool]])Úwritable_onlyr   c                 c  sN   t | jƒ}t| jƒD ]4\}}||d k}|r2|s2q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_locationszCachingFileSystem | Nonez#Literal[False] | tuple[Detail, str])r4   Úcfsr   c                 C  s¦   t |  ¡ | jƒD ]\\}}}}||vr(q||  ¡ }|durv|jrX|d |j |¡krXq|jrvt ¡ |d  |jkrvqt	j
 ||d ¡}t	j
 |¡r||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)Úzipr9   r   ÚcopyZcheck_filesÚfsZukeyZexpiryr<   r3   r4   r5   Úexists)r   r4   r:   r   ÚbaseÚ_r0   Údetailr   r   r   Ú
check_filee   s    	zCacheMetadata.check_fileÚintztuple[list[str], bool])Úexpiry_timer   c                 C  sÄ   g }| j d  ¡  ¡ D ]h\}}t ¡ |d  |kr| dd¡}|sPtd|› ƒ‚tj | j	d |¡}| 
|¡ | j d  |¡ q| j d r°t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 r0   )r   r>   Úitemsr<   r"   ÚRuntimeErrorr3   r4   r5   r   ÚappendÚpopr-   )r   rF   Zexpired_filesr4   rC   r   Ú
cache_pathZwritable_cache_emptyr   r   r   Úclear_expired€   s     ÿ

zCacheMetadata.clear_expired)r   c                 C  sP   g }|   ¡ D ]2\}}}tj |¡r4| |  |¡¡ q| i ¡ q|pHi g| _dS )z>Load all metadata from disk and store in ``self.cached_files``N)r9   r3   r4   r@   rK   r'   r   )r   r   r   rB   r   r   r   r   š   s    zCacheMetadata.loadr   )r%   r4   r   c                 C  s>   | j d | }|d dur:t|d ƒ|j |jkr:d|d< dS )z‹Perform side-effect actions on closing a cached file.

        The actual closing of the file is the responsibility of the caller.
        rG   r   TN)r   r1   Ú	blocksizeÚsize)r   r%   r4   r&   r   r   r   Úon_close_cached_file¥   s    $z"CacheMetadata.on_close_cached_filez
str | None)r4   r   c                 C  sR   |   |d¡}|sdS |\}}| | jd ¡rF| jd  |¡ |  ¡  ntdƒ‚|S )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.
        NrG   z<Can only delete cached file in last, writable cache location)rD   Ú
startswithr   r   rL   ÚsaveÚPermissionError)r   r4   ÚdetailsrB   r   r   r   r   Úpop_file¯   s    
ÿzCacheMetadata.pop_filec           	      C  sN  t |  ¡ | jƒD ]6\\}}}}|s&qtj |¡rì|  |¡}| ¡ D ]‚\}}||v rD|d du sp|| d du rzd|d< n"|| d }| |d ¡ ||d< t	|d || d ƒ|d< || d |d< qD| ¡ D ]\}}||vrÐ|||< qÐn|}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   )r>   )Ú.0ÚkÚvr   r   r   Ú
<dictcomp>à   ó    z&CacheMetadata.save.<locals>.<dictcomp>rG   N)r=   r9   r   r3   r4   r@   r'   rI   ÚupdateÚmaxr    r!   r$   r#   r-   )	r   r   rB   r8   r0   r   rX   r&   r   r   r   r   rS   Ã   s0     

zCacheMetadata.save)r4   rC   r   c                 C  s   || j d |< dS )z8Update metadata for specific file in memory, do not saverG   N)r   )r   r4   rC   r   r   r   Úupdate_fileç   s    zCacheMetadata.update_fileN)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r-   r9   rD   rN   r   rQ   rV   rS   r^   r   r   r   r   r      s   

 ÿ
$r   )Ú
__future__r   r3   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"   