o
    ]fz-                     @   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	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lmZ G d	d
 d
eZdS )    N)contextmanager)md5)Path)sleep)time)	BaseCache)FileSystemSerializerc                   @   s  e Zd ZdZdZdZe Zdddefd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	dAd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dBd&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dCd*e	dejdeje
 defd/d0Z 		1dDd*e	dejdeje
 d2edef
d3d4Z!dEd*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 )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              
   C   s   t | | || _|| _|| _|| _| jd u r|  | _zt| j W n t	y= } z|j
t
jkr3 W Y d }~nd }~ww | jdkrQ| jtt|  d d S d S )Nr   value)r   __init___path
_threshold_hash_method_mode_get_compatible_platform_modeosmakedirsOSErrorerrnoEEXIST_update_countlenlist	_list_dir)selfr   r   r   r   r   ex r#   F/home/ubuntu/webapp/venv/lib/python3.10/site-packages/cachelib/file.pyr   ,   s$   


zFileSystemCache.__init__returnc                 C   s   d}t  dkrtj}|S )Ni  Windows)platformsystemstatS_IWRITE)r!   r   r#   r#   r$   r   K   s   z-FileSystemCache._get_compatible_platform_modec                 C   s   |  | jpdS Nr   )get_fs_count_filer!   r#   r#   r$   _file_countQ   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   r/   setr-   )r!   r0   r   	new_countr#   r#   r$   r   U   s   
zFileSystemCache._update_counttimeoutc                 C   s*   t | |}|dkrtt | }t|S r+   )r   _normalize_timeoutintr   )r!   r4   r#   r#   r$   r5   a   s   z"FileSystemCache._normalize_timeoutnamec                 C   s,   |  | jtjd }||kp|| jS )N)_get_filenamer-   splitr   sependswith_fs_transaction_suffix)r!   r7   fshashr#   r#   r$   _is_mgmtg   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)r?   r   pathjoinr   ).0fnr.   r#   r$   	<genexpr>m   s    
z,FileSystemCache._list_dir.<locals>.<genexpr>)r   listdirr   r.   r#   r.   r$   r    k   s   

zFileSystemCache._list_dirc                 C   s   | j dko
| j| j kS r+   )r   r/   r.   r#   r#   r$   _over_thresholds   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   r8   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_expiredv   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 )NrI   rJ   rK   r   rM   TrN   c                 s   s    | ]\}}|V  qd S r@   r#   )rC   _rZ   r#   r#   r$   rE      s
    
z0FileSystemCache._remove_older.<locals>.<genexpr>c                 S   s   | d S r+   r#   )itemr#   r#   r$   <lambda>   s    z/FileSystemCache._remove_older.<locals>.<lambda>)keyr8   rL   F)r    rP   rQ   rR   rS   appendrU   r   rV   rW   rX   rY   sortedr   rT   r   rG   )r!   exp_fname_tuplesrZ   r[   	timestampfname_sortedr#   r#   r$   _remove_older   sR   

zFileSystemCache._remove_olderc                 C   s0   |   rt }| | |   r|   d S d S r@   )rG   r   r]   rg   )r!   rH   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 )NrM   TrN   rL   Fr   r   )		enumerater    r   rT   rU   r   rX   rY   r   )r!   irZ   r#   r#   r$   clear   s    zFileSystemCache.clearra   c                 C   s2   t |tr|d}| | }tj| j|S )Nzutf-8)	
isinstancestrencoder   	hexdigestr   rA   rB   r   )r!   ra   bkey	bkey_hashr#   r#   r$   r9      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 )NrI   rJ   rK   r   rM   TrN   )r9   rP   rQ   rR   rS   r   
serializerloadrU   r   rV   rW   rX   rY   r!   ra   filenamer[   pickle_timer#   r#   r$   r,      s.   

zFileSystemCache.getc                 C   s(   |  |}tj|s| |||S dS NF)r9   r   rA   existsr2   )r!   ra   r   r4   ru   r#   r#   r$   add   s   
zFileSystemCache.addFr1   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wbrJ   rM   TrN   F   rL   )rh   r5   r9   r   rA   isfiletempfilemkstempr=   r   fdopenwriterQ   packrr   dump_run_safelyreplacechmodr   r   r)   st_sizer   rX   rY   r   )r!   ra   r   r4   r1   ru   	overwritefdtmpr[   fsizer#   r#   r$   r2      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 filerN   Fr8   rL   )r   rT   r9   rU   r   rX   rY   r   )r!   ra   r1   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 )	NrI   rJ   rK   r   TFrM   rN   )r9   rP   rQ   rR   rS   r   rU   r   rV   rW   rX   rY   rt   r#   r#   r$   has  s&   
(zFileSystemCache.hasrD   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&   NgMbP?g      $@g           )r'   r(   PermissionErrorr   )r!   rD   r   r   output	wait_stepmax_sleep_timetotal_sleep_timer#   r#   r$   r   /  s&   zFileSystemCache._run_safelyrA   c                 c   s<    |  t||}|d u rtz
|V  W |  d S |  w r@   )r   openr   close)r!   rA   r   fsr#   r#   r$   rP   F  s   z!FileSystemCache._safe_stream_open)NN)r%   Nr@   rw   )F)(__name__
__module____qualname____doc__r=   r-   r   rr   r   rm   r6   _tOptionalAnyr   r   propertyr/   r   r5   boolr?   	Generatorr    rG   floatr]   rg   rh   rk   r9   r,   ry   r2   r   r   Callabler   r   rP   r#   r#   r#   r$   r	      sx    


&$

+"r	   )r   rX   r   r'   r)   rQ   r   typingr   
contextlibr   hashlibr   pathlibr   r   r   cachelib.baser   cachelib.serializersr   r	   r#   r#   r#   r$   <module>   s     