
    7Th?!                        % S SK Jr  S SK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JrJrJr  S SKJr  SSKJr  \\\4   rS	\S
'    " S S5      rg! \ a    \(       d  S SK
r
 NIf = f)    )annotationsN)TYPE_CHECKING)atomic_write)AnyDictIterator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   aD  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.
c                R    U(       d  [        S5      eXl        0 /U l        S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 locationFN)
ValueError_storagecached_files_force_save_pickle)selfstorages     ]/var/www/auris/envauris/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! [         aA    [        US5       n[        R                  " U5      nSSS5         M  ! , (       d  f        N= ff = f)z6Low-level function to load metadata from specific filerNrbblocks)
openjsonloadr   picklevalues
isinstancegetlistset)r   fnfloadedcs        r   _loadCacheMetadata._load8   s    	(b#!1 
 A!%%/400!!H+.( !   	(b$1Q  	(sE   B A=B =
BB B C$C;C
C	CCc                   U R                   (       a+  [        U5       n[        R                  " X5        SSS5        g[        USS9 n[        R                  " X5        SSS5        g! , (       d  f       g= f! , (       d  f       g= f)z4Low-level function to save metadata to specific fileNw)mode)r   r   r#   dumpr!   )r   metadata_to_saver)   r*   s       r   _saveCacheMetadata._saveE   s_    ""b!Q,0 "! bs+q		*. ,+ "! ,+s   A'A8'
A58
B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_locationsN   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)ziprA   r   copycheck_filesfsukeyexpiryrE   r:   r;   r<   exists)r   r;   cfsr)   base_r7   details           r   
check_fileCacheMetadata.check_filed   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.
rE   r)    z)Cache metadata does not contain 'fn' for r7   )r   rG   itemsrE   r&   RuntimeErrorr:   r;   r<   r   appendpopr4   )r   expiry_timeexpired_filesr;   rP   r)   
cache_pathwritable_cache_emptys           r   clear_expiredCacheMetadata.clear_expired   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)rA   r:   r;   rL   rX   r-   r   )r   r   r)   rO   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.
rT   r   TN)r   r8   	blocksizesize)r   r*   r;   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.
NrT   z<Can only delete cached file in last, writable cache location)rQ   
startswithr   r   rY   savePermissionError)r   r;   detailsrO   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   TrE   rD   rT   N)rF   rA   r   r:   r;   rL   r-   rV   updatemaxrG   r$   r%   r(   r'   r4   )
r   r)   rO   r@   r7   r   kr,   r   vs
             r   ri   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 saverT   N)r   )r   r;   rP   s      r   update_fileCacheMetadata.update_file   s    &,"d#r   )r   r   r   N)r   z	list[str])r)   strreturnr   )r3   r   r)   rw   rx   None)F)r=   boolrx   zIterator[tuple[str, str, bool]])r;   rw   rM   zCachingFileSystem | Nonerx   z#Literal[False] | tuple[Detail, str])rZ   intrx   ztuple[list[str], bool])rx   ry   )r*   r   r;   rw   rx   ry   )r;   rw   rx   z
str | None)r;   rw   rP   r   rx   ry   )__name__
__module____qualname____firstlineno____doc__r   r-   r4   rA   rQ   r^   r"   re   rl   ri   ru   __static_attributes__ r   r   r   r      so    (&/ %*D!D	(D,6	,634	1("1H-r   r   )
__future__r   r:   r#   rE   typingr   fsspec.utilsr   ujsonr!   ImportErrorr   r   r   r	   typing_extensionsr
   cachedr   rw   r   __annotations__r   r   r   r   <module>r      sf    " 	     %
 33+)S#XFI&N- N-  s   A A/.A/