
    Yj                        U d dl m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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. No longer supports pickle.
    storage	list[str]c                F    |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   s     i/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/fsspec/implementations/cache_metadata.py__init__zCacheMetadata.__init__"   s3      	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   |                                D ]B}t	          |                    d          t                    rt          |d                   |d<   C|S )z6Low-level function to load metadata from specific filerNblocks)openjsonloadvalues
isinstancegetlistset)r   r   floadedcs        r   _loadzCacheMetadata._load1   s    "c]] 	"aYq\\F	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	/ 	/A!%%//400 /!!H+..(s   266metadata_to_saveNonec                    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 filew)modeN)r   r    dump)r   r+   r   r'   s       r   _savezCacheMetadata._save:   s    "3''' 	+1I&***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+s   599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   r2   nir   writables         r   _scan_locationszCacheMetadata._scan_locations?   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)zipr?   r   copycheck_filesfsukeyexpiryrE   r9   r:   r;   exists)r   r:   r@   r   base_r6   details           r   
check_filezCacheMetadata.check_fileU   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.
        rE   r    z)Cache metadata does not contain 'fn' for r6   )r   rG   itemsrE   r$   RuntimeErrorr9   r:   r;   r   appendpopr1   )r   rQ   expired_filesr:   rO   r   
cache_pathwritable_cache_emptys           r   clear_expiredzCacheMetadata.clear_expiredp   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)r?   r9   r:   rL   rY   r*   r   )r   r   r   rN   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.
        rU   r   TN)r   r7   	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.
        NrU   z<Can only delete cached file in last, writable cache location)rP   
startswithr   r   rZ   savePermissionError)r   r:   detailsrN   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   TrE   rD   c                >    i | ]\  }}||                                 S  )rG   ).0kvs      r   
<dictcomp>z&CacheMetadata.save.<locals>.<dictcomp>   s&    BBBTQQBBBr   rU   N)rF   r?   r   r9   r:   rL   r*   rW   updatemaxr"   r#   r&   r%   r1   )	r   r   rN   r>   r6   r   ro   r)   r   s	            r   rg   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   rO   c                &    || j         d         |<   dS )z8Update metadata for specific file in memory, do not saverU   N)r   )r   r:   rO   s      r   update_filezCacheMetadata.update_file   s    &,"d###r   N)r   r   )r   r   r   r   )r+   r   r   r   r   r,   )F)r2   r3   r   r4   )r:   r   r@   rA   r   rB   )rQ   rR   r   rS   )r   r,   )r'   r   r:   r   r   r,   )r:   r   r   rd   )r:   r   rO   r   r   r,   )__name__
__module____qualname____doc__r   r*   r1   r?   rP   r^   r!   rc   rj   rg   ru   rm   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   r9   rE   typingr   fsspec.utilsr   ujsonr    ImportErrorcollections.abcr   r   r   r	   cachedr   dictr   r   __annotations__r   rm   r   r   <module>r      s3   " " " " " " " 				              % % % % % %      '((((((..........))))))S#XF&&&&A- A- A- A- A- A- A- A- A- A-s   " 00