Ë
    #täi6!  ã                  óÊ   — 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 	 d dl	Z
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y# e$ r	 esd dl
Z
Y Œ<w xY w)é    )ÚannotationsN)ÚTYPE_CHECKING)Úatomic_write)ÚIterator)ÚAnyÚLiteralÚ	TypeAliasé   )ÚCachingFileSystemr	   ÚDetailc                  ó~   — 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y)Ú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.
    c                óJ   — |st        d«      ‚|| _        i g| _        d| _        y)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     út/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/fsspec/implementations/cache_metadata.pyÚ__init__zCacheMetadata.__init__$   s.   € ñ ÜÐRÓSÐSàˆŒØ+-¨$ˆÔð #(ˆÕó    c                óŒ  — 	 t        |d«      5 }t        j                  |«      }ddd«       j                  «       D ]3  }t        |j                  d«      t        «      sŒ#t        |d   «      |d<   Œ5 |S # 1 sw Y   ŒQxY w# t        $ r: t        |d«      5 }t	        j                  |«      }ddd«       n# 1 sw Y   nxY wY Œ–w xY w)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   Ú_loadzCacheMetadata._load7   s™   € ð	(Ü�b˜#” !ÜŸ™ 1›�÷ ð
 —‘–ˆAÜ˜!Ÿ%™% ›/¬4Õ0Ü! ! H¡+Ó.��(’ð !ð ˆ÷ �ûäò 	(Ü�b˜$” 1ÜŸ™ Q›�÷  —‘üð	(ús?   ‚B  ŽA4¤B  Á4A=Á9B  Â CÂB4Â+	CÂ4B=	Â9CÃCc                óú   — | j                   r+t        |«      5 }t        j                  ||«       ddd«       yt        |d¬«      5 }t	        j                  ||«       ddd«       y# 1 sw Y   yxY w# 1 sw Y   yxY w)z4Low-level function to save metadata to specific fileNÚw)Úmode)r   r   r    Údumpr   )r   Úmetadata_to_saver&   r'   s       r   Ú_savezCacheMetadata._saveD   sc   € à×"Ò"Ü˜bÔ! QÜ—‘Ð,¨aÔ0÷ "Ð!ô ˜b sÕ+¨qÜ—	‘	Ð*¨AÔ.÷ ,Ð+÷ "Ð!ú÷ ,Ð+ús   ˜A%ÁA1Á%A.Á1A:c              #  óÔ   K  — t        | j                  «      }t        | j                  «      D ]7  \  }}||dz
  k(  }|r|sŒt        j                  j                  |d«      ||f–— Œ9 y­w)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   Úwritable_onlyÚnÚir   Úwritables         r   Ú_scan_locationszCacheMetadata._scan_locationsM   s^   è ø€ ô �—‘ÓˆÜ# D§M¡MÖ2‰JˆAˆwØ˜A ™E‘zˆHÙ¡XØÜ—'‘'—,‘,˜w¨Ó0°'¸8ÐCÓCñ	 3ùs   ‚A&A(c                óì  — t        | j                  «       | j                  «      D ]Í  \  \  }}}}||vrŒ||   j                  «       }|�b|j                  r"|d   |j
                  j                  |«      k7  rŒR|j                  r(t        j                  «       |d   z
  |j                  kD  rŒ†t        j                  j                  ||d   «      }t        j                  j                  |«      sŒÉ||fc S  y)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.
        ÚuidÚtimer&   F)Úzipr<   r   ÚcopyÚcheck_filesÚfsÚukeyÚexpiryr?   r5   r6   r7   Úexists)r   r6   Úcfsr&   ÚbaseÚ_r2   Údetails           r   Ú
check_filezCacheMetadata.check_filec   sÏ   € ô %(¨×(<Ñ(<Ó(>À×@QÑ@QÖ$RÑ ‰MˆR��q˜5Ø˜5Ñ ØØ˜4‘[×%Ñ%Ó'ˆFàˆØ—?’? v¨e¡}¸¿¹¿¹ÀDÓ8IÒ'IàØ—:’:¤$§)¡)£+°°v±Ñ">ÀÇÁÒ"Kàä—‘—‘˜d F¨4¡LÓ1ˆBÜ�w‰w�~‰~˜bÕ!Ø˜r�zÒ!ð %Sð  r   c                ó‚  — g }| j                   d   j                  «       j                  «       D ]¡  \  }}t        j                  «       |d   z
  |kD  sŒ$|j	                  dd«      }|st        d|› �«      ‚t        j                  j                  | j                  d   |«      }|j                  |«       | j                   d   j                  |«       Œ£ | j                   d   rLt        j                  j                  | j                  d   d«      }| j                  | 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 r2   )r   rA   Úitemsr?   r#   ÚRuntimeErrorr5   r6   r7   r   ÚappendÚpopr0   )r   Úexpiry_timeÚexpired_filesr6   rJ   r&   Ú
cache_pathÚwritable_cache_emptys           r   Úclear_expiredzCacheMetadata.clear_expired~   s"  € ð ˆØ ×-Ñ-¨bÑ1×6Ñ6Ó8×>Ñ>Ö@‰LˆD�&Ü�y‰y‹{˜V F™^Ñ+¨kÓ9Ø—Z‘Z  bÓ)�ÙÜ&ØCÀDÀ6ÐJóð ô —W‘W—\‘\ $§-¡-°Ñ"3°RÓ8�Ø×$Ñ$ RÔ(Ø×!Ñ! "Ñ%×)Ñ)¨$Õ/ð Að ×Ñ˜RÒ ÜŸ™Ÿ™ d§m¡m°BÑ&7¸ÓAˆJØ�J‰J�t×(Ñ(¨Ñ,¨jÔ9à#'×#4Ñ#4°RÑ#8Ð8ÐØÐ2Ð2Ð2r   c                óô   — g }| j                  «       D ]W  \  }}}t        j                  j                  |«      r!|j	                  | j                  |«      «       ŒG|j	                  i «       ŒY |xs i g| _        y)z>Load all metadata from disk and store in ``self.cached_files``N)r<   r5   r6   rF   rQ   r*   r   )r   r   r&   rI   s       r   r   zCacheMetadata.load˜   se   € àˆØ×,Ñ,Ö.‰HˆB��1Ü�w‰w�~‰~˜bÔ!à×#Ñ# D§J¡J¨r£NÕ3à×#Ñ# BÕ'ð /ð )Ò0¨R¨DˆÕr   c                ó”   — | j                   d   |   }|d   dur/t        |d   «      |j                  z  |j                  k\  rd|d<   yyy)z‹Perform side-effect actions on closing a cached file.

        The actual closing of the file is the responsibility of the caller.
        rM   r   TN)r   r3   Ú	blocksizeÚsize)r   r'   r6   r)   s       r   Úon_close_cached_filez"CacheMetadata.on_close_cached_file£   sT   € ð ×Ñ˜bÑ! $Ñ'ˆØˆX‰;˜dÑ"¤s¨1¨X©;Ó'7¸!¿+¹+Ñ'EÈÏÉÒ'OØˆAˆhŠKð (PÐ"r   c                óè   — | j                  |d«      }|sy|\  }}|j                  | j                  d   «      r0| j                  d   j	                  |«       | j                  «        |S t        d«      ‚)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.
        NrM   z<Can only delete cached file in last, writable cache location)rK   Ú
startswithr   r   rR   ÚsaveÚPermissionError)r   r6   ÚdetailsrI   r&   s        r   Úpop_filezCacheMetadata.pop_file­   ss   € ð —/‘/ $¨Ó-ˆÙØØ‰ˆˆ2Ø�=‰=˜Ÿ™ rÑ*Ô+Ø×Ñ˜bÑ!×%Ñ% dÔ+Ø�I‰IŒKð
 ˆ	ô "ØNóð r   c                ó,  — t        | j                  «       | j                  «      D �]f  \  \  }}}}|sŒt        j                  j                  |«      r¬| j                  |«      }|j                  «       D ]e  \  }}||v sŒ|d   du s
||   d   du rd|d<   n!||   d   }|j                  |d   «       ||d<   t        |d   ||   d   «      |d<   ||   d   |d<   Œg |j                  «       D ]  \  }}||vsŒ|||<   Œ n|}|j                  «       D ��	ci c]  \  }}	||	j                  «       “Œ }}}	|j                  «       D ]'  }t        |d   t        «      sŒt        |d   «      |d<   Œ) | j                  ||«       || j                  d<   �Œi yc c}	}w )zSave metadata to diskr   Tr?   r>   rM   N)r@   r<   r   r5   r6   rF   r*   rO   ÚupdateÚmaxrA   r!   r"   r%   r$   r0   )
r   r&   rI   r;   r2   r   Úkr)   r   Úvs
             r   r_   zCacheMetadata.saveÁ   sž  € ä(+¨D×,@Ñ,@Ó,BÀD×DUÑDU×(VÑ$ÑˆR��H˜uÙØä�w‰w�~‰~˜bÔ!Ø#Ÿz™z¨"›~�Ø(×.Ñ.Ö0‘D�A�qØ˜E’zØ˜X™;¨$Ñ.°%¸±(¸8Ñ2DÈÑ2LØ*.˜A˜hšKð &+¨1¡X¨hÑ%7˜FØ"ŸM™M¨!¨H©+Ô6Ø*0˜A˜h™KÜ$'¨¨&©	°5¸±8¸FÑ3CÓ$D˜˜&™	Ø#(¨¡8¨E¡?˜˜%šð 1ð  "ŸK™KžM‘D�A�qØ Ò,Ø*+˜ Qšñ *ð  %�Ø-9×-?Ñ-?Ô-AÔBÑ-A¡T Q¨�Q˜Ÿ™›‘[Ð-AˆEÑBØ—\‘\–^�Ü˜a ™k¬3Õ/Ü"& q¨¡{Ó"3�A�h’Kð $ð �J‰J�u˜bÔ!Ø$0ˆD×Ñ˜bÓ!ñA )Wùó6 Cs   ÄFc                ó(   — || j                   d   |<   y)z8Update metadata for specific file in memory, do not saverM   N)r   )r   r6   rJ   s      r   Úupdate_filezCacheMetadata.update_fileå   s   € à&,ˆ×Ñ˜"Ñ˜dÒ#r   N)r   z	list[str])r&   ÚstrÚreturnr   )r/   r   r&   rj   rk   ÚNone)F)r8   Úboolrk   zIterator[tuple[str, str, bool]])r6   rj   rG   zCachingFileSystem | Nonerk   z#Literal[False] | tuple[Detail, str])rS   Úintrk   ztuple[list[str], bool])rk   rl   )r'   r   r6   rj   rk   rl   )r6   rj   rk   z
str | None)r6   rj   rJ   r   rk   rl   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r0   r<   rK   rW   r   r\   rb   r_   ri   © r   r   r   r      so   „ ñó(ó&ó/ð %*ðDØ!ðDà	(óDð,ØðØ6ðà	,óó63ó4	1óóó("1ôH-r   r   )Ú
__future__r   r5   r    r?   Útypingr   Úfsspec.utilsr   Úujsonr   ÚImportErrorÚcollections.abcr   r   r   r	   Úcachedr   Údictrj   r   Ú__annotations__r   rs   r   r   Ú<module>r}      sf   ðÞ "ã 	Û Û Ý  å %ðÛñ
 Ý(ß.Ñ.å)à˜S #˜X™€FˆIÓ&÷N-ò N-øð ò ÙÛùðús   ¡A ÁA"Á!A"