Ë
    ùÇ:j  ã                  óÂ   — 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	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. No longer supports pickle.
    c                ó<   — |st        d«      ‚|| _        i g| _        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 locationN)Ú
ValueErrorÚ_storageÚcached_files)ÚselfÚstorages     úz/home/mcse/projects/srt_converter/srt-converter-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)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   Ú_loadzCacheMetadata._load1   so   € ä�"�c‹]ð 	"˜aÜ—Y‘Y˜q“\ˆF÷	"à—‘“ò 	/ˆAÜ˜!Ÿ%™% ›/¬4Õ0Ü! ! H¡+Ó.��(’ð	/ð ˆ÷	"ð 	"ús   �A3Á3A<c                ót   — t        |d¬«      5 }t        j                  ||«       ddd«       y# 1 sw Y   yxY w)z4Low-level function to save metadata to specific fileÚw)ÚmodeN)r   r   Údump)r   Úmetadata_to_saver#   r$   s       r   Ú_savezCacheMetadata._save:   s2   € ä˜" 3Ô'ð 	+¨1Ü�I‰IÐ&¨Ô*÷	+÷ 	+ñ 	+ús   Ž.®7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_locations?   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)Úzipr9   r   ÚcopyÚcheck_filesÚfsÚukeyÚexpiryr<   r2   r3   r4   Úexists)r   r3   Úcfsr#   ÚbaseÚ_r/   Údetails           r   Ú
check_filezCacheMetadata.check_fileU   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 r/   )r   r>   Úitemsr<   r    ÚRuntimeErrorr2   r3   r4   r   ÚappendÚpopr-   )r   Úexpiry_timeÚexpired_filesr3   rG   r#   Ú
cache_pathÚwritable_cache_emptys           r   Úclear_expiredzCacheMetadata.clear_expiredp   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)r9   r2   r3   rC   rN   r'   r   )r   r   r#   rF   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.
        rJ   r   TN)r   r0   Ú	blocksizeÚsize)r   r$   r3   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.
        NrJ   z<Can only delete cached file in last, writable cache location)rH   Ú
startswithr   r   rO   ÚsaveÚPermissionError)r   r3   ÚdetailsrF   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;   rJ   N)r=   r9   r   r2   r3   rC   r'   rL   ÚupdateÚmaxr>   r   r   r"   r!   r-   )
r   r#   rF   r8   r/   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 saverJ   N)r   )r   r3   rG   s      r   Úupdate_filezCacheMetadata.update_file×   s   € à&,ˆ×Ñ˜"Ñ˜dÒ#r   N)r   z	list[str])r#   ÚstrÚreturnr   )r,   r   r#   rg   rh   ÚNone)F)r5   Úboolrh   zIterator[tuple[str, str, bool]])r3   rg   rD   zCachingFileSystem | Nonerh   z#Literal[False] | tuple[Detail, str])rP   Úintrh   ztuple[list[str], bool])rh   ri   )r$   r   r3   rg   rh   ri   )r3   rg   rh   z
str | None)r3   rg   rG   r   rh   ri   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r'   r-   r9   rH   rT   r   rY   r_   r\   rf   © r   r   r   r      so   „ ñó/óó+ð %*ðDØ!ðDà	(óDð,ØðØ6ðà	,óó63ó4	1óóó("1ôH-r   r   )Ú
__future__r   r2   r<   Útypingr   Úfsspec.utilsr   Úujsonr   ÚImportErrorÚcollections.abcr   r   r   r	   Úcachedr   Údictrg   r   Ú__annotations__r   rp   r   r   ú<module>rz      sc   ðÞ "ã 	Û Ý  å %ðÛñ
 Ý(ß.Ñ.å)à˜S #˜X™€FˆIÓ&÷A-ò A-øð ò ÙÛùðús   �A ÁAÁA