Ë
    F^(h?!  ã                  óÎ   — 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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«      Zy# e$ r	 esd dl
Z
Y Œ>w xY w)é    )ÚannotationsN)ÚTYPE_CHECKING)Úatomic_write)ÚAnyÚDictÚIteratorÚ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     úc/var/www/skyplay_api_hub/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._load8   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._saveE   su   € à×"Ò"Ü˜bÓ!ð 1 QÜ—‘Ð,¨aÔ0÷1ð 1ô ˜b sÔ+ð /¨qÜ—	‘	Ð*¨AÔ.÷/ð /÷1ð 1ú÷/ð /ú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_locationsN   se   è ø€ ô �—‘ÓˆÜ# D§M¡MÓ2ò 	D‰JˆAˆwØ˜A ™E‘zˆHÙ¡XØÜ—'‘'—,‘,˜w¨Ó0°'¸8ÐCÓCñ		Dù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@   r6   r7   r8   Úexists)r   r7   Úcfsr'   ÚbaseÚ_r3   Údetails           r   Ú
check_filezCacheMetadata.check_filed   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Ò!ð	"ð  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 r3   )r   rB   Úitemsr@   r$   ÚRuntimeErrorr6   r7   r8   r   ÚappendÚpopr1   )r   Úexpiry_timeÚexpired_filesr7   rK   r'   Ú
cache_pathÚwritable_cache_emptys           r   Úclear_expiredzCacheMetadata.clear_expired   s&  € ð ˆØ ×-Ñ-¨bÑ1×6Ñ6Ó8×>Ñ>Ó@ò 		0‰LˆD�&Ü�y‰y‹{˜V F™^Ñ+¨kÓ9Ø—Z‘Z  bÓ)�ÙÜ&ØCÀDÀ6ÐJóð ô —W‘W—\‘\ $§-¡-°Ñ"3°RÓ8�Ø×$Ñ$ RÔ(Ø×!Ñ! "Ñ%×)Ñ)¨$Õ/ð		0ð ×Ñ˜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=   r6   r7   rG   rR   r+   r   )r   r   r'   rJ   s       r   r    zCacheMetadata.load™   sj   € àˆØ×,Ñ,Ó.ò 	(‰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.
        rN   r   TN)r   r4   Ú	blocksizeÚsize)r   r(   r7   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.
        NrN   z<Can only delete cached file in last, writable cache location)rL   Ú
startswithr   r   rS   ÚsaveÚPermissionError)r   r7   ÚdetailsrJ   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?   rN   N)rA   r=   r   r6   r7   rG   r+   rP   ÚupdateÚmaxrB   r"   r#   r&   r%   r1   )
r   r'   rJ   r<   r3   r   Úkr*   r   Úvs
             r   r`   zCacheMetadata.saveÂ   s®  € ä(+¨D×,@Ñ,@Ó,BÀD×DUÑDUÓ(Vó  	1Ñ$ÑˆR��H˜uÙØä�w‰w�~‰~˜bÔ!Ø#Ÿz™z¨"›~�Ø(×.Ñ.Ó0ò 3‘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¡?˜˜%šð3ð  "ŸK™K›Mò ,‘D�A�qØ Ò,Ø*+˜ Qšñ,ð  %�Ø-9×-?Ñ-?Ó-A×B¡T Q¨�Q˜Ÿ™›‘[ÐBˆEÑBØ—\‘\“^ò 4�Ü˜a ™k¬3Õ/Ü"& q¨¡{Ó"3�A�h’Kð4ð �J‰J�u˜bÔ!Ø$0ˆD×Ñ˜bÓ!ñA 	1ùó6 Cs   ÄFc                ó(   — || j                   d   |<   y)z8Update metadata for specific file in memory, do not saverN   N)r   )r   r7   rK   s      r   Úupdate_filezCacheMetadata.update_fileæ   s   € à&,ˆ×Ñ˜"Ñ˜dÒ#r   N)r   z	list[str])r'   ÚstrÚreturnr   )r0   r   r'   rk   rl   ÚNone)F)r9   Úboolrl   zIterator[tuple[str, str, bool]])r7   rk   rH   zCachingFileSystem | Nonerl   z#Literal[False] | tuple[Detail, str])rT   Úintrl   ztuple[list[str], bool])rl   rm   )r(   r   r7   rk   rl   rm   )r7   rk   rl   z
str | None)r7   rk   rK   r   rl   rm   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r+   r1   r=   rL   rX   r    r]   rc   r`   rj   © r   r   r   r      so   „ ñó(ó&ó/ð %*ðDØ!ðDà	(óDð,ØðØ6ðà	,óó63ó4	1óóó("1ôH-r   r   )Ú
__future__r   r6   r!   r@   Útypingr   Úfsspec.utilsr   Úujsonr   ÚImportErrorr   r   r   r	   Útyping_extensionsr
   Úcachedr   rk   r   Ú__annotations__r   rt   r   r   ú<module>r}      sf   ðÞ "ã 	Û Û Ý  å %ðÛñ
 ß3Ó3å+å)à˜S #˜X™€FˆIÓ&÷N-ò N-øð ò ÙÛùðús   ¡A ÁA$Á#A$