
    Xf/                         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Z G d de          ZdS )    N)contextmanager)Path)sleep)time)	BaseCache)FileSystemSerializer    stringreturnc                 *    t          j        |           S )zDon'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
   s    A/var/www/piapp/venv/lib/python3.11/site-packages/cachelib/file.py	_lazy_md5r      s    
 ;vr	   c                      e Zd ZdZdZdZ ee          Z e	            Z
	 	 	 	 d-deded	ed
ej        e         dej        f
dZdefdZedefd            Z	 d.dej        e         dej        e         ddfdZdej        e         defdZdedefdZdej        eddf         fdZdefdZdeddfdZdefdZd/dZdefdZdedefdZ dedej        fd Z!d0dedej        dej        e         defd!Z"	 	 d1dedej        dej        e         d#edef
d$Z#d2ded#edefd%Z$dedefd&Z%d'ej&        d(ej        d)ej        dej        fd*Z'e(d+ed
edej        fd,            Z)dS )3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_cache__wz_cache_count  ,  N	cache_dir	thresholddefault_timeoutmodehash_methodc                    t          j        | |           || _        || _        | j        | _        ||| _        || _        | j        |                                 | _        	 t          j	        | j                   n-# t          $ r }|j        t          j        k    r Y d }~nd }~ww xY w| j        dk    rD|                     t          t          |                                                                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   exs          r   r   zFileSystemCache.__init__8   s    	4111
# 5" +D
 
:;;==DJ	K
#### 	 	 	x5<'' (''''	 ?aSdnn.>.>)?)?%@%@AAAAA  s   !A; ;
B%B  B%r   c                 P    d}t          j                    dk    rt          j        }|S )Ni  Windows)platformsystemstatS_IWRITE)r.   r   s     r   r$   z-FileSystemCache._get_compatible_platform_modeZ   s&    ?	))=Dr	   c                 :    |                      | j                  pdS Nr   )get_fs_count_filer.   s    r   _file_countzFileSystemCache._file_count`   s    xx+,,11r	   deltar   c                 |    | j         dk    rd S |r| j        |z   }n|pd}|                     | j        |d           d S )Nr   T)mgmt_element)r    r;   setr9   )r.   r<   r   	new_counts       r   r*   zFileSystemCache._update_countd   sW     ?aF 	#(50II
I$idCCCCCr	   timeoutc                     t          j        | |          }|dk    rt          t                                |z   }t          |          S r7   )r   _normalize_timeoutintr   )r.   rA   s     r   rC   z"FileSystemCache._normalize_timeoutp   s=    .tW==a<<$&&kkG+G7||r	   namec                     |                      | j                                      t          j                  d         }||k    p|                    | j                  S )N)_get_filenamer9   splitr%   sependswith_fs_transaction_suffix)r.   rE   fshashs      r   _is_mgmtzFileSystemCache._is_mgmtv   sK    ##D$788>>rvFFrJv~Kt/J!K!KKr	   c                 N      fdt          j         j                  D             S )z2return a list of (fully qualified) cache filenamesc              3      K   | ]>}                     |          t          j                            j        |          V  ?d S N)rN   r%   pathjoinr   ).0fnr.   s     r   	<genexpr>z,FileSystemCache._list_dir.<locals>.<genexpr>|   s[       
 
==$$
GLLR((
 
 
 
 
 
r	   )r%   listdirr   r:   s   `r   r-   zFileSystemCache._list_dirz   s:    
 
 
 
j,,
 
 
 	
r	   c                 8    | j         dk    o| j        | j         k    S r7   )r    r;   r:   s    r   _over_thresholdzFileSystemCache._over_threshold   s    !#J(84?(JJr	   nowc                    |                                  D ]}	 |                     |d          5 }t          j        d|                    d                    d         }d d d            n# 1 swxY w Y   |dk    r0||k     r*t          j        |           |                     d           # t          $ r Y t          t          t          j        f$ r t          j        d|d	           Y w xY wd S )
NrbI   r   rG   r<   /Exception raised while handling cache file '%s'Texc_info)r-   _safe_stream_openstructunpackreadr%   remover*   FileNotFoundErrorr'   EOFErrorerrorloggingwarning)r.   rZ   fnamefexpiress        r   _remove_expiredzFileSystemCache._remove_expired   sR   ^^%% 	 	E++E488 ?A$mC;;A>G? ? ? ? ? ? ? ? ? ? ? ? ? ? ?a<<GcMMIe$$$&&R&000$   Xv|4   E!     	 	s:   B+/A)B+)A-	-B+0A-	19B++
C,72C,+C,c                    g }|                                  D ]}	 |                     |d          5 }t          j        d|                    d                    d         }|                    ||f           d d d            n# 1 swxY w Y   w# t          $ r Y t          t          t          j	        f$ r t          j        d|d           Y w xY wd t          |d	 
          D             }|D ]x}	 t          j        |           |                     d           n4# t          $ r Y n(t          $ r t          j        d|d           Y  dS w xY w|                                 s nydS )Nr\   r]   r^   r   r`   Tra   c              3       K   | ]	\  }}|V  
d S rQ    )rT   _rm   s      r   rV   z0FileSystemCache._remove_older.<locals>.<genexpr>   s7       
 
aE
 
 
 
 
 
r	   c                     | d         S r7   rs   )items    r   <lambda>z/FileSystemCache._remove_older.<locals>.<lambda>   s    DQRG r	   )keyrG   r_   F)r-   rc   rd   re   rf   appendrh   r'   ri   rj   rk   rl   sortedr%   rg   r*   rY   )r.   exp_fname_tuplesrm   rn   	timestampfname_sorteds         r   _remove_olderzFileSystemCache._remove_older   s5   ^^%% 	 	E++E488 @A &c166!99 = =a @I$++Y,>???@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ %   Xv|4   E!     
 
"()9?S?S"T"T"T
 
 
 " 	 	E	%   """,,,,$      E!   
 uuu '')) tsS   BAB6BB	B	B	
B
C2CC3*D
E* EEc                     |                                  r#t                      }|                     |           |                                  r|                                  d S d S rQ   )rY   r   rp   r~   )r.   rZ   s     r   _prunezFileSystemCache._prune   si    !! 	&&&C  %%%!! 	!     	! 	!r	   c                 @   t          |                                           D ]e\  }}	 t          j        |           # t          $ r Y 't
          $ r3 t          j        d|d           |                     |            Y  dS w xY w|                     d           dS )Nr`   Tra   r_   Fr   r   )		enumerater-   r%   rg   rh   r'   rk   rl   r*   )r.   irm   s      r   clearzFileSystemCache.clear   s    !$.."2"233 	 	HAu	%    $      E!   
 ""!",,,uuu 	###ts   =
B	7BBrx   c                 .   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%   rR   rS   r   )r.   rx   bkey	bkey_hashs       r   rH   zFileSystemCache._get_filename   s|    c3 	P::g&&D))$//99;;IIN499NNOOOw||DJ	222r	   c                    |                      |          }	 |                     |d          5 }t          j        d|                    d                    d         }|dk    s|t                      k    r&| j                            |          cd d d            S 	 d d d            n# 1 swxY w Y   nD# t          $ r Y n8t          t          t          j        f$ r t          j        d|d           Y nw xY wd S )Nr\   r]   r^   r   r`   Tra   )rH   rc   rd   re   rf   r   
serializerloadrh   r'   ri   rj   rk   rl   r.   rx   filenamern   pickle_times        r   r8   zFileSystemCache.get   sj   %%c**	''$77 31$mC;;A>!##{dff'<'<?//223 3 3 3 3 3 3 3'<3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ! 	 	 	D6<0 	 	 	OA     	 tsA   B3 A B'B3 B3 'B++B3 .B+/B3 3
C4?2C43C4c                     |                      |          }t          j                            |          s|                     |||          S dS NF)rH   r%   rR   existsr?   )r.   rx   r   rA   r   s        r   addzFileSystemCache.add   sF    %%c**w~~h'' 	188C000ur	   Fr>   c                 X   |rd}n|                                   |                     |          }|                     |          }t          j                            |          }	 t          j        | j        | j	                  \  }}t          j
        |d          5 }	|	                    t          j        d|                     | j                            ||	           d d d            n# 1 swxY w Y   |                     t          j        ||           |                     t          j        || j                   t)          |                                          j        }
|s|s|                     d           |
dk    S # t0          $ r t3          j        d|d	           Y d
S w xY w)Nr   )suffixdirwbr]      r_   r`   Tra   F)r   rC   rH   r%   rR   isfiletempfilemkstemprL   r   fdopenwriterd   packr   dump_run_safelyreplacechmodr#   r   r4   st_sizer*   r'   rk   rl   )r.   rx   r   rA   r>   r   	overwritefdtmprn   fsizes              r   r?   zFileSystemCache.set   s     	GG KKMMM))'22%%c**GNN8,,		&2
  GB 2t$$ /C11222$$UA.../ / / / / / / / / / / / / / / RZh777RXx<<<NN''))1E  ,\ ,"""+++19  	 	 	OA   
 55	s8   $8F AC, F ,C00F 3C04A0F !F)(F)c                     	 t          j        |                     |                     |s|                     d           dS # t          $ r Y dS t
          $ r t          j        dd           Y dS w xY w)NrG   r_   Tz*Exception raised while handling cache filera   F)r%   rg   rH   r*   rh   r'   rk   rl   )r.   rx   r>   s      r   deletezFileSystemCache.delete  s    	Id((--...   -""",,,4 ! 	 	 	44 	 	 	OHSWXXXX55	s   'A 
A3A32A3c                    |                      |          }	 |                     |d          5 }t          j        d|                    d                    d         }|dk    s|t                      k    r	 d d d            dS 	 d d d            dS # 1 swxY w Y   d S # t          $ r Y dS t          t          t          j	        f$ r t          j        d|d           Y dS w xY w)	Nr\   r]   r^   r   TFr`   ra   )rH   rc   rd   re   rf   r   rh   r'   ri   rj   rk   rl   r   s        r   haszFileSystemCache.has*  sd   %%c**	''$77 !1$mC;;A>!##{dff'<'<! ! ! ! ! ! ! !
 !! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 	 	 	556<0 	 	 	OA   
 55	sG   B AB5B BB BB BB 
C *2C C rU   argskwargsc                     t          j                    dk    rHd}d}d}d}||k     r9	  ||i |}n/# t          $ r t          |           ||z  }|dz  }Y nw xY w||k     9n ||i |}|S )ztOn Windows os.replace, os.chmod and open can yield
        permission errors if executed by two different processes.r1   NgMbP?g      $@g           )r2   r3   PermissionErrorr   )r.   rU   r   r   output	wait_stepmax_sleep_timetotal_sleep_times           r   r   zFileSystemCache._run_safely=  s     ?	))FI!N""^33R000F  ' # # #)$$$$	1$NIII# #^33 R(((Fs   0 #AArR   c              #      K   |                      t          ||          }|t          	 |V  |                                 d S # |                                 w xY wrQ   )r   openr'   close)r.   rR   r   fss       r   rc   z!FileSystemCache._safe_stream_openT  sX      dD$//:M	HHHHHJJJJJBHHJJJJs   A A)r   r   NN)NN)r   NrQ   r   )F)*__name__
__module____qualname____doc__rL   r9   staticmethodr   r!   r   r   r   rD   _tOptionalAnyr   r$   propertyr;   r*   rC   boolrN   	Generatorr-   rY   floatrp   r~   r   r   rH   r8   r   r?   r   r   Callabler   r   rc   rs   r	   r   r   r      s        ( +'N'<	22%%''J
 "!%" B  B B  B 	 B
 k# B V B  B  B  BDs     2S 2 2 2 X2 IM
D 
D[%
D57[5E
D	
D 
D 
D 
D"+c*: s    LS LT L L L L
2<T48 
 
 
 
K K K K K5 T    "!t ! ! ! !F! ! ! !t    "3 3 3 3 3 3s rv    " s 26 BK4D PT     %)") )) v) S!	)
 ) 
) ) ) )V # T d    s t    &bk "& BF rv    . c      ^  r	   r   )r	   )r(   r   rk   r%   r2   r4   rd   r   typingr   
contextlibr   pathlibr   r   r   cachelib.baser   cachelib.serializersr   bytesr   r   r   rs   r	   r   <module>r      s,      				         % % % % % %                   # # # # # # 5 5 5 5 5 5 e bf    A A A A Ai A A A A Ar	   