HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/env/lib64/python3.10/site-packages/cachelib/__pycache__/file.cpython-310.pyc
o

!wez-�@s�ddlZddlZddlZddlZddlZddlZddlZddlZddl	m
Z
ddlmZddl
mZddlmZddlmZddlmZddlmZGd	d
�d
e�ZdS)�N)�contextmanager)�md5)�Path)�sleep)�time)�	BaseCache)�FileSystemSerializerc@s�eZdZdZdZdZe�Zdddefde	de
d	e
d
eje
dej
f
dd
�Zde
fdd�Zede
fdd��Z	dAdeje
deje
ddfdd�Zdeje
de
fdd�Zde	defdd�Zdeje	ddffdd�Zdefdd �Zd!eddfd"d#�Zdefd$d%�ZdBd&d'�Zdefd(d)�Zd*e	de	fd+d,�Zd*e	dej
fd-d.�ZdCd*e	dej
deje
defd/d0�Z 		1dDd*e	dej
deje
d2edef
d3d4�Z!dEd*e	d2edefd5d6�Z"d*e	defd7d8�Z#d9ej$d:ej
d;ej
dej
fd<d=�Z%e&d>e	d
e	dejfd?d@��Z'dS)F�FileSystemCachea�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.
    z.__wz_cache�__wz_cache_counti�i,N�	cache_dir�	threshold�default_timeout�mode�hash_methodc
Cs�t�||�||_||_||_||_|jdur|��|_zt�|j�Wnt	y=}z
|j
t
jkr3�WYd}~nd}~ww|jdkrQ|jt
t|����d�dSdS)Nr��value)r�__init__�_path�
_threshold�_hash_method�_mode�_get_compatible_platform_mode�os�makedirs�OSError�errno�EEXIST�
_update_count�len�list�	_list_dir)�selfrrr
rr�ex�r#�F/home/arjun/projects/env/lib/python3.10/site-packages/cachelib/file.pyr,s$

���
�zFileSystemCache.__init__�returncCsd}t��dkrtj}|S)Ni��Windows)�platform�system�stat�S_IWRITE)r!rr#r#r$rKsz-FileSystemCache._get_compatible_platform_modecCs|�|j�pdS�Nr)�get�_fs_count_file�r!r#r#r$�_file_countQszFileSystemCache._file_count�deltarcCs<|jdkrdS|r|j|}n|pd}|j|j|dd�dS)NrT)�mgmt_element)rr/�setr-)r!r0r�	new_countr#r#r$rUs
zFileSystemCache._update_count�timeoutcCs*t�||�}|dkrtt��|}t|�Sr+)r�_normalize_timeout�intr)r!r4r#r#r$r5asz"FileSystemCache._normalize_timeout�namecCs,|�|j��tj�d}||kp|�|j�S)N���)�
_get_filenamer-�splitr�sep�endswith�_fs_transaction_suffix)r!r7�fshashr#r#r$�_is_mgmtgszFileSystemCache._is_mgmtcs�fdd�t��j�D�S)z2return a list of (fully qualified) cache filenamesc3s*�|]}��|�stj��j|�VqdS�N)r?r�path�joinr)�.0�fnr.r#r$�	<genexpr>ms��
�z,FileSystemCache._list_dir.<locals>.<genexpr>)r�listdirrr.r#r.r$r ks

�zFileSystemCache._list_dircCs|jdko
|j|jkSr+)rr/r.r#r#r$�_over_thresholdsszFileSystemCache._over_threshold�nowcCs�|��D]U}z6|�|d��}t�d|�d��d}Wd�n1s#wY|dkr;||kr;t�|�|jdd�WqtyEYqt	t
tjfyYtj
d|dd	�YqwdS)
N�rb�I�rr8�r0�/Exception raised while handling cache file '%s'T��exc_info)r �_safe_stream_open�struct�unpack�readr�remover�FileNotFoundErrorr�EOFError�error�logging�warning)r!rH�fname�f�expiresr#r#r$�_remove_expiredvs&�
�
���zFileSystemCache._remove_expiredcCs(g}|��D]I}z*|�|d��}t�d|�d��d}|�||f�Wd�n1s,wYWqty;Yqtttj	fyOt
jd|dd�Yqwdd	�t|d
d�d�D�}|D]2}z
t
�|�|jd
d�WntywYnty�t
jd|dd�YdSw|��s�dSq_dS)NrIrJrKrrMTrNcss�|]\}}|VqdSr@r#)rC�_rZr#r#r$rE�s
��
�z0FileSystemCache._remove_older.<locals>.<genexpr>cSs|dSr+r#)�itemr#r#r$�<lambda>�sz/FileSystemCache._remove_older.<locals>.<lambda>)�keyr8rLF)r rPrQrRrS�appendrUrrVrWrXrY�sortedrrTrrG)r!�exp_fname_tuplesrZr[�	timestamp�fname_sortedr#r#r$�
_remove_older�sR��
����
���zFileSystemCache._remove_oldercCs0|��rt�}|�|�|��r|��dSdSr@)rGrr]rg)r!rHr#r#r$�_prune�s
�zFileSystemCache._prunec	Csxt|���D]-\}}zt�|�WqtyYqty3tjd|dd�|j|d�YdSw|jdd�dS)NrMTrNrLFrr)	�	enumerater rrTrUrrXrYr)r!�irZr#r#r$�clear�s ��zFileSystemCache.clearracCs2t|t�r|�d�}|�|���}tj�|j|�S)Nzutf-8)	�
isinstance�str�encoder�	hexdigestrrArBr)r!ra�bkey�	bkey_hashr#r#r$r9�s

zFileSystemCache._get_filenamec
Cs�|�|�}z=|�|d��,}t�d|�d��d}|dks!|t�kr/|j�|�Wd�WSWd�WdS1s;wYWdStyLYdSt	t
tjfyatj
d|dd�YdSw)NrIrJrKrrMTrN)r9rPrQrRrSr�
serializer�loadrUrrVrWrXrY�r!ra�filenamer[�pickle_timer#r#r$r,�s.

�������zFileSystemCache.getcCs(|�|�}tj�|�s|�|||�SdS�NF)r9rrA�existsr2)r!rarr4rur#r#r$�add�s
zFileSystemCache.addFr1cCs
|rd}n|��|�|�}|�|�}tj�|�}zKtj|j|j	d�\}}t�
|d��}	|	�t�
d|��|j�||	�Wd�n1sFwY|�tj||�|�tj||j�t|���j}
Wntyvtjd|dd�YdSw|s�|s�|jd	d
�|
dkS)Nr)�suffix�dir�wbrJrMTrNF�rL)rhr5r9rrA�isfile�tempfile�mkstempr=r�fdopen�writerQ�packrr�dump�_run_safely�replace�chmodrrr)�st_sizerrXrYr)r!rarr4r1ru�	overwrite�fd�tmpr[�fsizer#r#r$r2�s8


����	zFileSystemCache.setcCs^z
t�|�|��WntyYdSty$tjddd�YdSw|s-|jdd�dS)NTz*Exception raised while handling cache filerNFr8rL)rrTr9rUrrXrYr)r!rar1r#r#r$�deletes�zFileSystemCache.deletec
Cs�|�|�}z:|�|d��)}t�d|�d��d}|dks!|t�kr+	Wd�WdS	Wd�WdS1s8wYWdStyIYdStttj	fy^t
jd|dd�YdSw)	NrIrJrKrTFrMrN)r9rPrQrRrSrrUrrVrWrXrYrtr#r#r$�hass&
�(���zFileSystemCache.hasrD�args�kwargscOs�t��dkr9d}d}d}d}||kr7z||i|��}W	|Sty2t|�||7}|d9}Ynw||ks|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!rDr�r��output�	wait_step�max_sleep_time�total_sleep_timer#r#r$r�/s&����zFileSystemCache._run_safelyrAccs<�|�t||�}|durt�z
|VW|��dS|��wr@)r��openr�close)r!rAr�fsr#r#r$rPFs�z!FileSystemCache._safe_stream_open)NN)r%Nr@rw)F)(�__name__�
__module__�__qualname__�__doc__r=r-rrrrrmr6�_t�Optional�Anyrr�propertyr/rr5�boolr?�	Generatorr rG�floatr]rgrhrkr9r,ryr2r�r��Callabler�rrPr#r#r#r$r	sx�����
����
�
&$
�����
�+"r	)rrXrr'r)rQr�typingr��
contextlibr�hashlibr�pathlibrrr�
cachelib.baser�cachelib.serializersrr	r#r#r#r$�<module>s