o
    2'hî/  ã                   @   sº   d dl Z d dlZd dlZd dlZd dlZd dl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mZ d dlmZ d dlmZ d dlmZ dd	ed
e	jfdd„ZG dd„ deƒZdS )é    N)Úcontextmanager)ÚPath)Úsleep)Útime)Ú	BaseCache)ÚFileSystemSerializeró    ÚstringÚreturnc                 C   s
   t  | ¡S )zÅDon't access ``hashlib.md5`` until runtime. FIPS builds may not include
    md5, in which case the import and use as a default would fail before the
    developer can configure something else.
    )ÚhashlibÚmd5)r	   © r   úCC:\Users\shubh\Videos\agent\nova\Lib\site-packages\cachelib/file.pyÚ	_lazy_md5   s   
r   c                   @   s  e Zd ZdZdZdZeeƒZe	ƒ Z
				dAdeded	ed
eje dejf
dd„Zdefdd„Zedefdd„ƒZ	dBdeje deje ddfdd„Zdeje defdd„Zdedefdd„Zdejeddf fdd„Zdefdd „Zd!eddfd"d#„Zdefd$d%„ZdCd&d'„Zdefd(d)„Zd*edefd+d,„Z d*edejfd-d.„Z!dDd*edejdeje defd/d0„Z"		1dEd*edejdeje d2edef
d3d4„Z#dFd*ed2edefd5d6„Z$d*edefd7d8„Z%d9ej&d:ejd;ejdejfd<d=„Z'e(d>ed
edejfd?d@„ƒZ)dS )GÚFileSystemCachea1  A cache that stores the items on the file system.  This cache depends
    on being the only user of the `cache_dir`.  Make absolutely sure that
    nobody but this cache stores files there or otherwise the cache will
    randomly delete files therein.

    :param cache_dir: the directory where cache files are stored.
    :param threshold: the maximum number of items the cache stores before
                      it starts deleting some. A threshold value of 0
                      indicates no threshold.
    :param default_timeout: the default timeout that is used if no timeout is
                            specified on :meth:`~BaseCache.set`. A timeout of
                            0 indicates that the cache never expires.
    :param mode: the file mode wanted for the cache files, default 0600
    :param hash_method: Default hashlib.md5. The hash method used to
                        generate the filename for cached results.
                        Default is lazy loaded and can be overriden by
                        seeting  `_default_hash_method`
    z.__wz_cacheZ__wz_cache_countéô  é,  NÚ	cache_dirÚ	thresholdÚdefault_timeoutÚmodeÚhash_methodc              
   C   s¶   t  | |¡ || _|| _| j| _|d ur|| _|| _| jd u r$|  ¡ | _zt 	| j¡ W n t
yE } z|jtjkr;‚ W Y d }~nd }~ww | jdkrY| jtt|  ¡ ƒƒd� d S d S )Nr   ©Úvalue)r   Ú__init__Ú_pathÚ
_thresholdÚ_default_hash_methodÚ_hash_methodÚ_modeÚ_get_compatible_platform_modeÚosÚmakedirsÚOSErrorÚerrnoÚEEXISTÚ_update_countÚlenÚlistÚ	_list_dir)Úselfr   r   r   r   r   Úexr   r   r   r   8   s(   

ÿ€ÿ
ÿzFileSystemCache.__init__r
   c                 C   s   d}t  ¡ dkrtj}|S )Ni€  ÚWindows)ÚplatformÚsystemÚstatÚS_IWRITE)r*   r   r   r   r   r    Z   s   z-FileSystemCache._get_compatible_platform_modec                 C   s   |   | j¡pdS ©Nr   )ÚgetÚ_fs_count_file©r*   r   r   r   Ú_file_count`   s   zFileSystemCache._file_countÚdeltar   c                 C   s<   | j dkrd S |r| j| }n|pd}| j| j|dd� d S )Nr   T)Úmgmt_element)r   r5   Úsetr3   )r*   r6   r   Z	new_countr   r   r   r&   d   s   
zFileSystemCache._update_countÚtimeoutc                 C   s*   t  | |¡}|dkrttƒ ƒ| }t|ƒS r1   )r   Ú_normalize_timeoutÚintr   )r*   r9   r   r   r   r:   p   s   z"FileSystemCache._normalize_timeoutÚnamec                 C   s,   |   | j¡ tj¡d }||kp| | j¡S )Néÿÿÿÿ)Ú_get_filenamer3   Úsplitr!   ÚsepÚendswithÚ_fs_transaction_suffix)r*   r<   Zfshashr   r   r   Ú_is_mgmtv   s   zFileSystemCache._is_mgmtc                    s   ‡ fdd„t  ˆ j¡D ƒS )z2return a list of (fully qualified) cache filenamesc                 3   s*   � | ]}ˆ   |¡stj ˆ j|¡V  qd S ©N)rC   r!   ÚpathÚjoinr   )Ú.0Úfnr4   r   r   Ú	<genexpr>|   s   € ý
ÿz,FileSystemCache._list_dir.<locals>.<genexpr>)r!   Úlistdirr   r4   r   r4   r   r)   z   s   

þzFileSystemCache._list_dirc                 C   s   | j dko
| j| j kS r1   )r   r5   r4   r   r   r   Ú_over_threshold‚   s   zFileSystemCache._over_thresholdÚnowc                 C   s¸   |   ¡ D ]U}z6|  |d¡�}t d| d¡¡d }W d   ƒ n1 s#w   Y  |dkr;||k r;t |¡ | jdd� W q tyE   Y q t	t
tjfyY   tjd|dd	� Y qw d S )
NÚrbÚIé   r   r=   ©r6   ú/Exception raised while handling cache file '%s'T©Úexc_info)r)   Ú_safe_stream_openÚstructÚunpackÚreadr!   Úremover&   ÚFileNotFoundErrorr#   ÚEOFErrorÚerrorÚloggingÚwarning)r*   rL   ÚfnameÚfÚexpiresr   r   r   Ú_remove_expired…   s&   ÿ
€
ýÿ÷zFileSystemCache._remove_expiredc                 C   s(  g }|   ¡ D ]I}z*|  |d¡�}t d| d¡¡d }| ||f¡ W d   ƒ n1 s,w   Y  W q ty;   Y q tttj	fyO   t
jd|dd� Y qw dd	„ t|d
d„ d�D ƒ}|D ]2}zt |¡ | jdd� W n tyw   Y n ty‰   t
jd|dd� Y  dS w |  ¡ s‘ dS q_dS )NrM   rN   rO   r   rQ   TrR   c                 s   s   � | ]\}}|V  qd S rD   r   )rG   Ú_r^   r   r   r   rI   ¥   s   € 
ÿz0FileSystemCache._remove_older.<locals>.<genexpr>c                 S   s   | d S r1   r   )Úitemr   r   r   Ú<lambda>¦   s    z/FileSystemCache._remove_older.<locals>.<lambda>)Úkeyr=   rP   F)r)   rT   rU   rV   rW   ÚappendrY   r#   rZ   r[   r\   r]   Úsortedr!   rX   r&   rK   )r*   Zexp_fname_tuplesr^   r_   Ú	timestampZfname_sortedr   r   r   Ú_remove_older–   sN   þ€
ýÿÿ
ýúþzFileSystemCache._remove_olderc                 C   s0   |   ¡ rtƒ }|  |¡ |   ¡ r|  ¡  d S d S rD   )rK   r   ra   ri   )r*   rL   r   r   r   Ú_prune¹   s   
ÿzFileSystemCache._prunec              	   C   sx   t |  ¡ ƒD ]-\}}zt |¡ W q ty   Y q ty3   tjd|dd� | j| d� Y  dS w | jdd� dS )NrQ   TrR   rP   Fr   r   )	Ú	enumerater)   r!   rX   rY   r#   r\   r]   r&   )r*   Úir^   r   r   r   ÚclearÁ   s    ýùzFileSystemCache.clearre   c                 C   sF   t |tƒr| d¡}|  |¡ ¡ }n	tdt|ƒ› �ƒ‚tj 	| j
|¡S )Nzutf-8z$Key must be a string, received type )Ú
isinstanceÚstrÚencoder   Ú	hexdigestÚ	TypeErrorÚtyper!   rE   rF   r   )r*   re   ZbkeyZ	bkey_hashr   r   r   r>   Ò   s
   

zFileSystemCache._get_filenamec              
   C   sÄ   |   |¡}z=|  |d¡�,}t d| d¡¡d }|dks!|tƒ kr/| j |¡W  d   ƒ W S W d   ƒ W d S 1 s;w   Y  W d S  tyL   Y d S  t	t
tjfya   tjd|dd� Y d S w )NrM   rN   rO   r   rQ   TrR   )r>   rT   rU   rV   rW   r   Ú
serializerÚloadrY   r#   rZ   r[   r\   r]   ©r*   re   Úfilenamer_   Zpickle_timer   r   r   r2   Ú   s.   

ýþôøúýúzFileSystemCache.getc                 C   s(   |   |¡}tj |¡s|  |||¡S dS ©NF)r>   r!   rE   Úexistsr8   )r*   re   r   r9   rw   r   r   r   Úaddë   s   
zFileSystemCache.addFr7   c                 C   s
  |rd}n|   ¡  |  |¡}|  |¡}tj |¡}zKtj| j| j	d�\}}t 
|d¡�}	|	 t d|¡¡ | j ||	¡ W d   ƒ n1 sFw   Y  |  tj||¡ |  tj|| j¡ t|ƒ ¡ j}
W n tyv   tjd|dd� Y dS w |s�|s�| jd	d
� |
dkS )Nr   )ÚsuffixÚdirÚwbrN   rQ   TrR   Fé   rP   )rj   r:   r>   r!   rE   ÚisfileÚtempfileÚmkstemprB   r   ÚfdopenÚwriterU   Úpackrt   ÚdumpÚ_run_safelyÚreplaceÚchmodr   r   r/   Úst_sizer#   r\   r]   r&   )r*   re   r   r9   r7   rw   Ú	overwriteÚfdÚtmpr_   Úfsizer   r   r   r8   ñ   s8   


ÿþýú	zFileSystemCache.setc                 C   s^   z
t  |  |¡¡ W n ty   Y dS  ty$   tjddd� Y dS w |s-| jdd� dS )NTz*Exception raised while handling cache filerR   Fr=   rP   )r!   rX   r>   rY   r#   r\   r]   r&   )r*   re   r7   r   r   r   Údelete  s   þzFileSystemCache.deletec              
   C   s¾   |   |¡}z:|  |d¡�)}t d| d¡¡d }|dks!|tƒ kr+	 W d   ƒ W dS 	 W d   ƒ W dS 1 s8w   Y  W d S  tyI   Y dS  tttj	fy^   t
jd|dd� Y dS w )	NrM   rN   rO   r   TFrQ   rR   )r>   rT   rU   rV   rW   r   rY   r#   rZ   r[   r\   r]   rv   r   r   r   Úhas*  s&   
ý(ûýúzFileSystemCache.hasrH   ÚargsÚkwargsc                 O   s„   t  ¡ dkr9d}d}d}d}||k r7z||i |¤Ž}W 	 |S  ty2   t|ƒ ||7 }|d9 }Y nw ||k s|S ||i |¤Ž}|S )ztOn Windows os.replace, os.chmod and open can yield
        permission errors if executed by two different processes.r,   Ngü©ñÒMbP?g      $@g        é   )r-   r.   ÚPermissionErrorr   )r*   rH   r�   r‘   ÚoutputZ	wait_stepZmax_sleep_timeZtotal_sleep_timer   r   r   r†   =  s&   ÷ýýþzFileSystemCache._run_safelyrE   c                 c   s<   � |   t||¡}|d u rt‚z
|V  W | ¡  d S | ¡  w rD   )r†   Úopenr#   Úclose)r*   rE   r   Úfsr   r   r   rT   T  s   €z!FileSystemCache._safe_stream_open)r   r   NN)NN)r
   NrD   rx   )F)*Ú__name__Ú
__module__Ú__qualname__Ú__doc__rB   r3   Ústaticmethodr   r   r   rt   ro   r;   Ú_tÚOptionalÚAnyr   r    Úpropertyr5   r&   r:   ÚboolrC   Ú	Generatorr)   rK   Úfloatra   ri   rj   rm   r>   r2   rz   r8   rŽ   r�   ÚCallabler†   r   rT   r   r   r   r   r      sz    úþýüû
ú"ÿÿÿ
þ
#$
ûþýüû
ú+"r   )r   )r$   r   r\   r!   r-   r/   rU   r€   Útypingr�   Ú
contextlibr   Úpathlibr   r   r   Zcachelib.baser   Zcachelib.serializersr   ÚbytesrŸ   r   r   r   r   r   r   Ú<module>   s"    