a
    
d                     @  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 erd 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 dZdS )    )annotationsN)TYPE_CHECKING)atomic_write)AnyDictIteratorLiteral)	TypeAlias   )CachingFileSystemr	   Detailc                   @  s   e Zd ZdZddddZd*d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 )+CacheMetadatazCache 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, pickled.
    z	list[str])storagec                 C  s   |st d|| _i g| _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 locationN)
ValueError_storagecached_files)selfr    r   n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/fsspec/implementations/cache_metadata.py__init__   s    	zCacheMetadata.__init__FboolzIterator[tuple[str, str, bool]])writable_onlyreturnc                 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_locations,   s    
zCacheMetadata._scan_locationsstrzCachingFileSystem | Nonez#Literal[False] | tuple[Detail, str])r   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 )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timefnF)zipr"   r   copyZcheck_filesfsZukeyZexpiryr&   r   r   r   exists)r   r   r$   r'   base_r   detailr   r   r   
check_fileB   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 rtj| j	d d}t|"}t| j d | W d   n1 s0    Y  | 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.
        r&   r'    z)Cache metadata does not contain 'fn' for r   N)r   r)   itemsr&   getRuntimeErrorr   r   r   r   appendpopr   pickledump)	r   r1   Zexpired_filesr   r.   r'   
cache_pathZfcZwritable_cache_emptyr   r   r   clear_expired]   s"    


0zCacheMetadata.clear_expiredNone)r   c              	   C  s   g }|   D ]\}}}tj|rt|dP}t|}| D ]"}t|d t	r@t
|d |d< q@|| W d   q1 s0    Y  q|i  q|pi g| _dS )z>Load all metadata from disk and store in ``self.cached_files``rbblocksN)r"   r   r   r+   openr9   loadvalues
isinstancelistsetr7   r   )r   r   r'   r-   fZloaded_cached_filescr   r   r   rA   x   s    
*zCacheMetadata.loadr   )rF   r   r   c                 C  s>   | j d | }|d dur:t|d |j |jkr:d|d< dS )zPerform side-effect actions on closing a cached file.

        The actual closing of the file is the responsibility of the caller.
        r2   r?   TN)r   r   	blocksizesize)r   rF   r   rG   r   r   r   on_close_cached_file   s    $z"CacheMetadata.on_close_cached_filez
str | None)r   r   c                 C  sR   |  |d}|sdS |\}}|| jd rF| 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.
        Nr2   z<Can only delete cached file in last, writable cache location)r/   
startswithr   r   r8   savePermissionError)r   r   detailsr-   r'   r   r   r   pop_file   s    
zCacheMetadata.pop_filec           
   	   C  s  t |  | jD ]\\}}}}|s&qtj|rt|d}t|}W d   n1 s^0    Y  |	 D ]\}}||v rp|d du s|| d du rd|d< n"|| d }	|	
|d  |	|d< t|d || d |d< || d |d< qp|	 D ]\}}||vr|||< qn|}dd |	 D }| D ]&}t|d tr6t|d |d< q6t|}t|| W d   n1 s0    Y  || jd	< qdS )
zSave metadata to diskr>   Nr?   Tr&   r%   c                 S  s   i | ]\}}||  qS r   )r)   ).0kvr   r   r   
<dictcomp>       z&CacheMetadata.save.<locals>.<dictcomp>r2   )r(   r"   r   r   r   r+   r@   r9   rA   r4   updatemaxrB   rC   rE   rD   r   r:   )
r   r'   r-   r!   r   rF   r   rQ   rG   r?   r   r   r   rL      s4     (

,zCacheMetadata.saver   )r   r.   r   c                 C  s   || j d |< dS )z8Update metadata for specific file in memory, do not saver2   N)r   )r   r   r.   r   r   r   update_file   s    zCacheMetadata.update_fileN)F)__name__
__module____qualname____doc__r   r"   r/   r<   rA   rJ   rO   rL   rW   r   r   r   r   r      s    
&r   )
__future__r   r   r9   r&   typingr   Zfsspec.utilsr   r   r   r   r   Ztyping_extensionsr	   cachedr   r#   r   __annotations__r   r   r   r   r   <module>   s   