
    3j                        % S SK Jr  S SKrS SKrS SKJr  S SKJr   S SKr	\(       a"  S SKJr  S SKJrJrJr  SSKJr  \\\4   rS	\S
'    " S S5      rg! \
 a    \(       d  S SK	r	 NGf = f)    )annotationsN)TYPE_CHECKING)atomic_write)Iterator)AnyLiteral	TypeAlias   )CachingFileSystemr	   Detailc                      \ rS rSrSrSS jrSS jrSS jr S   SS jjr      SS jr	SS jr
SS	 jrSS
 jrSS jrSS jrSS jrSrg)CacheMetadata   a  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.
c                D    U(       d  [        S5      eXl        0 /U l        g)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)selfstorages     _/home/wildlama/miniconda3/lib/python3.13/site-packages/fsspec/implementations/cache_metadata.py__init__CacheMetadata.__init__"   s"     RSS+-$    c                   [        US5       n[        R                  " U5      nSSS5        WR                  5        H:  n[	        UR                  S5      [        5      (       d  M)  [        US   5      US'   M<     U$ ! , (       d  f       N^= f)z6Low-level function to load metadata from specific filerNblocks)openjsonloadvalues
isinstancegetlistset)r   fnfloadedcs        r   _loadCacheMetadata._load1   se    "c]aYYq\F A!%%/400!!H+.( !  ]s   A<<
B
c                x    [        USS9 n[        R                  " X5        SSS5        g! , (       d  f       g= f)z4Low-level function to save metadata to specific filew)modeN)r   r   dump)r   metadata_to_saver%   r&   s       r   _saveCacheMetadata._save:   s'    "3'1II&* (''s   +
9c              #     #    [        U R                  5      n[        U R                  5       H@  u  p4X2S-
  :H  nU(       a	  U(       d  M  [        R                  R                  US5      XE4v   MB     g7f)zYield 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   writable_onlynir   writables         r   _scan_locationsCacheMetadata._scan_locations?   sW      #DMM2JAEzHX'',,w0'CC	 3s   A0A2c                   [        U R                  5       U R                  5       H  u  u  p4pVX;  a  M  Xa   R                  5       nUbq  UR                  (       a$  US   UR
                  R                  U5      :w  a  MY  UR                  (       a+  [        R                  " 5       US   -
  UR                  :  a  M  [        R                  R                  XGS   5      n[        R                  R                  U5      (       d  M  Xs4s  $    g)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.
uidtimer%   F)zipr=   r   copycheck_filesfsukeyexpiryrA   r6   r7   r8   exists)r   r7   cfsr%   base_r3   details           r   
check_fileCacheMetadata.check_fileU   s     %((<(<(>@Q@Q$R MRq [%%'F??ve}D8I'I::$))+v">"Kd4L1Bww~~b!!z! %S  r   c                   / nU R                   S   R                  5       R                  5        H  u  p4[        R                  " 5       US   -
  U:  d  M&  UR	                  SS5      nU(       d  [        SU 35      e[        R                  R                  U R                  S   U5      nUR                  U5        U R                   S   R                  U5        M     U R                   S   (       aL  [        R                  R                  U R                  S   S5      nU R                  U R                   S   U5        U R                   S   (       + nX'4$ )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.
rA   r%    z)Cache metadata does not contain 'fn' for r3   )r   rC   itemsrA   r"   RuntimeErrorr6   r7   r8   r   appendpopr0   )r   expiry_timeexpired_filesr7   rL   r%   
cache_pathwritable_cache_emptys           r   clear_expiredCacheMetadata.clear_expiredp   s     --b1668>>@LDyy{VF^+k9ZZb)&CD6J  WW\\$--"3R8$$R(!!"%))$/ A R dmmB&7AJJJt((,j9#'#4#4R#8822r   c                   / nU R                  5        H^  u  n  n[        R                  R                  U5      (       a"  UR	                  U R                  U5      5        MM  UR	                  0 5        M`     U=(       d    0 /U l        g)z>Load all metadata from disk and store in ``self.cached_files``N)r=   r6   r7   rH   rT   r)   r   )r   r   r%   rK   s       r   r   CacheMetadata.load   sh    ,,.HB1ww~~b!!##DJJrN3##B' / )0RDr   c                    U R                   S   U   nUS   SLa0  [        US   5      UR                  -  UR                  :  a  SUS'   ggg)z{Perform side-effect actions on closing a cached file.

The actual closing of the file is the responsibility of the caller.
rP   r   TN)r   r4   	blocksizesize)r   r&   r7   r(   s       r   on_close_cached_file"CacheMetadata.on_close_cached_file   sT     b!$'X;d"s1X;'7!++'E'OAhK (P"r   c                    U R                  US5      nU(       d  gUu  p4UR                  U R                  S   5      (       a0  U R                  S   R	                  U5        U R                  5         U$ [        S5      e)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.
NrP   z<Can only delete cached file in last, writable cache location)rM   
startswithr   r   rU   savePermissionError)r   r7   detailsrK   r%   s        r   pop_fileCacheMetadata.pop_file   st     //$-==r*++b!%%d+IIK
 	 "N r   c                T   [        U R                  5       U R                  5       GHy  u  u  pp4U(       d  M  [        R                  R                  U5      (       a  U R                  U5      nUR                  5        Hd  u  pgXd;   d  M  US   SL d
  XF   S   SL a  SUS'   OXF   S   nUR                  US   5        XS'   [        US   XF   S   5      US'   XF   S   US'   Mf     UR                  5        H  u  pgXe;  d  M  XuU'   M     OUnUR                  5        VV	s0 s H  u  piXiR                  5       _M     nnn	UR                  5        H.  n[        US   [        5      (       d  M  [        US   5      US'   M0     U R                  XA5        XPR                  S'   GM|     gs  sn	nf )zSave metadata to diskr   TrA   r@   rP   N)rB   r=   r   r6   r7   rH   r)   rR   updatemaxrC   r    r!   r$   r#   r0   )
r   r%   rK   r<   r3   r   kr(   r   vs
             r   re   CacheMetadata.save   s   (+D,@,@,BDDUDU(V$RHww~~b!!#zz"~(..0DAzX;$.%(82D2L*.AhK &+Xh%7F"MM!H+6*0hK$'&	58F3C$D&	#(8E?% 1  "KKMDA,*+Q *  %-9-?-?-AB-ATQQ[-AEB\\^ak3//"&q{"3AhK $ JJu!$0b!A )W6 Cs   F$c                &    X R                   S   U'   g)z8Update metadata for specific file in memory, do not saverP   N)r   )r   r7   rL   s      r   update_fileCacheMetadata.update_file   s    &,"d#r   )r   r   N)r   z	list[str])r%   strreturnr   )r/   r   r%   rs   rt   None)F)r9   boolrt   zIterator[tuple[str, str, bool]])r7   rs   rI   zCachingFileSystem | Nonert   z#Literal[False] | tuple[Detail, str])rV   intrt   ztuple[list[str], bool])rt   ru   )r&   r   r7   rs   rt   ru   )r7   rs   rt   z
str | None)r7   rs   rL   r   rt   ru   )__name__
__module____qualname____firstlineno____doc__r   r)   r0   r=   rM   rZ   r   ra   rh   re   rq   __static_attributes__ r   r   r   r      so    /+ %*D!D	(D,6	,634	1("1H-r   r   )
__future__r   r6   rA   typingr   fsspec.utilsr   ujsonr   ImportErrorcollections.abcr   r   r   r	   cachedr   dictrs   r   __annotations__r   r~   r   r   <module>r      sc    " 	    %
 (..)S#XFI&A- A-  s   A A)(A)