
    Ջg/                         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y)    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    @/var/www/html/venv/lib/python3.12/site-packages/cachelib/file.py	_lazy_md5r      s    
 ;;v    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j0                  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!d-dedej                  dej                  e   defdZ"	 	 d.dedej                  dej                  e   d edef
d!Z#d/ded edefd"Z$dedefd#Z%d$ejL                  d%ej                  d&ej                  dej                  fd'Z'e(d(ededej0                  fd)       Z)y)0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_countN	cache_dir	thresholddefault_timeoutmodehash_methodc                    t        j                  | |       || _        || _        | j                  | _        ||| _        || _        | j                  | j                         | _        	 t        j                  | j                         | j                  dk7  r3| j                  t        t        | j!                                            y y # t        $ r(}|j                  t        j                  k7  r Y d }~od }~ww xY w)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
# 55" +D
 
::;;=DJ	KK

# ??aSdnn.>)?%@A    	xx5<<' (	s   (C
 
	C;C66C;r
   c                 X    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                  | j                        xs 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y |r| j                  |z   }n|xs d}| j                  | j                  |d       y )Nr   T)mgmt_element)r   r9   setr7   )r,   r:   r   	new_counts       r   r(   zFileSystemCache._update_countd   sG     ??a((50I
I$$idCr   timeoutc                 z    t        j                  | |      }|dk7  rt        t                     |z   }t        |      S r5   )r   _normalize_timeoutintr   )r,   r?   s     r   rA   z"FileSystemCache._normalize_timeoutp   s5    ..tW=a<$&kG+G7|r   namec                     | j                  | j                        j                  t        j                        d   }||k(  xs |j                  | j                        S )N)_get_filenamer7   splitr#   sependswith_fs_transaction_suffix)r,   rC   fshashs      r   _is_mgmtzFileSystemCache._is_mgmtv   sJ    ##D$7$78>>rvvFrJv~Kt/J/J!KKr   c                 T      fdt        j                   j                        D        S )z2return a list of (fully qualified) cache filenamesc              3      K   | ]?  }j                  |      s,t        j                  j                  j                  |       A y wN)rL   r#   pathjoinr   ).0fnr,   s     r   	<genexpr>z,FileSystemCache._list_dir.<locals>.<genexpr>|   s8      
==$ GGLLR(
s   AA)r#   listdirr   r8   s   `r   r+   zFileSystemCache._list_dirz   s"    
jj,
 	
r   c                 V    | j                   dk7  xr | j                  | j                   kD  S r5   )r   r9   r8   s    r   _over_thresholdzFileSystemCache._over_threshold   s%    !#J(8(84??(JJr   nowc                    | j                         D ]w  }	 | j                  |d      5 }t        j                  d|j	                  d            d   }d d d        dk7  r,||k  r't        j                  |       | j                  d       y y # 1 sw Y   =xY w# t        $ r Y t        t        t        j                  f$ r t        j                  d|d	       Y w xY w)
NrbI   r   rE   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,   rX   fnamefexpiress        r   _remove_expiredzFileSystemCache._remove_expired   s    ^^% 	E++E48 ?A$mmC;A>G?a<GcMIIe$&&R&0	? ?
 % Xv||4 E!s.   B)B9BB	B	C#5CCc                    g }| j                         D ]Y  }	 | j                  |d      5 }t        j                  d|j	                  d            d   }|j                  ||f       d d d        [ d t        |d	 
      D        }|D ]<  }	 t        j                  |       | j                  d       | j!                         r< y y# 1 sw Y   bxY w# t        $ r Y t        t        t        j                  f$ r t        j                  d|d       Y w xY w# t        $ r Y st        $ r t        j                  d|d       Y  yw xY w)NrZ   r[   r\   r   r^   Tr_   c              3   &   K   | ]	  \  }}|  y wrO    )rR   _rk   s      r   rT   z0FileSystemCache._remove_older.<locals>.<genexpr>   s      
aE
s   c                     | d   S r5   rq   )items    r   <lambda>z/FileSystemCache._remove_older.<locals>.<lambda>   s    DQRG r   )keyrE   r]   F)r+   ra   rb   rc   rd   appendrf   r%   rg   rh   ri   rj   sortedr#   re   r(   rW   )r,   exp_fname_tuplesrk   rl   	timestampfname_sorteds         r   _remove_olderzFileSystemCache._remove_older   sX   ^^% 	E++E48 @A &c166!9 =a @I$++Y,>?@	
"()9?S"T
 " 	E		% """, '')	 ;@ @ % Xv||4 E!  %  E!
 sF   C<C%C
'DC	C	D5DD	E$ EEc                     | j                         rt               }| j                  |       | j                         r| j                          y y rO   )rW   r   rn   r|   )r,   rX   s     r   _prunezFileSystemCache._prune   s@    !&C  %!  "r   c                     t        | j                               D ]  \  }}	 t        j                  |        | j                  d       y# t        $ r Y <t
        $ r/ t        j                  d|d       | j                  |        Y  yw xY w)Nr^   Tr_   r]   Fr   r   )		enumerater+   r#   re   rf   r%   ri   rj   r(   )r,   irk   s      r   clearzFileSystemCache.clear   s    !$.."23 	HAu		% 	 	# %  E!
 ""!",s   A	B3BBrv   c                    t        |t              r1|j                  d      }| j                  |      j	                         }nt        dt        |             t        j                  j                  | j                  |      S )Nzutf-8z$Key must be a string, received type )
isinstancestrencoder    	hexdigest	TypeErrortyper#   rP   rQ   r   )r,   rv   bkey	bkey_hashs       r   rF   zFileSystemCache._get_filename   sc    c3::g&D))$/99;IB49+NOOww||DJJ	22r   c                    | j                  |      }	 | j                  |d      5 }t        j                  d|j	                  d            d   }|dk(  s|t               k\  r$| j                  j                  |      cd d d        S 	 d d d        y # 1 sw Y   y xY w# t        $ r Y y t        t        t        j                  f$ r t        j                  d|d       Y y w xY w)NrZ   r[   r\   r   r^   Tr_   )rF   ra   rb   rc   rd   r   
serializerloadrf   r%   rg   rh   ri   rj   r,   rv   filenamerl   pickle_times        r   r6   zFileSystemCache.get   s    %%c*	''$7 31$mmC;A>!#{df'<??//23 3'<3 3  ! 	  6<<0 	OOA
 	s;   B AB:	B B BB B 	C%5CCc                     | j                  |      }t        j                  j                  |      s| j	                  |||      S yNF)rF   r#   rP   existsr=   )r,   rv   r   r?   r   s        r   addzFileSystemCache.add   s8    %%c*ww~~h'88C00r   r<   c                 F   |rd}n| j                          | j                  |      }| j                  |      }t        j                  j                  |      }	 t        j                  | j                  | j                        \  }}t        j                  |d      5 }	|	j                  t        j                  d|             | j                  j                  ||	       d d d        | j!                  t        j"                  ||       | j!                  t        j$                  || j&                         t)        |      j+                         j,                  }
|s|s| j/                  d       |
dkD  S # 1 sw Y   xY w# t0        $ r t3        j4                  d|d	       Y y
w xY w)Nr   )suffixdirwbr[      r]   r^   Tr_   F)r~   rA   rF   r#   rP   isfiletempfilemkstemprJ   r   fdopenwriterb   packr   dump_run_safelyreplacechmodr!   r   r2   st_sizer(   r%   ri   rj   )r,   rv   r   r?   r<   r   	overwritefdtmprl   fsizes              r   r=   zFileSystemCache.set   sY    G KKM))'2%%c*GGNN8,		&&22

GB 2t$ /C12$$UA./ RZZh7RXXx<N'')11E \"""+19'/ /  	OOA
 	s-   AE< AE0A7E< 0E95E< <!F F c                     	 t        j                  | j                  |             |s| j                  d       y# t        $ r Y yt
        $ r t        j                  dd       Y yw xY w)NrE   r]   Tz*Exception raised while handling cache filer_   F)r#   re   rF   r(   rf   r%   ri   rj   )r,   rv   r<   s      r   deletezFileSystemCache.delete  sf    	IId((-.  """, ! 	 	OOHSWX	s   $; 	A(A('A(c                    | j                  |      }	 | j                  |d      5 }t        j                  d|j	                  d            d   }|dk(  s|t               k\  r
	 d d d        y	 d d d        y# 1 sw Y   y xY w# t        $ r Y yt        t        t        j                  f$ r t        j                  d|d       Y yw xY w)	NrZ   r[   r\   r   TFr^   r_   )rF   ra   rb   rc   rd   r   rf   r%   rg   rh   ri   rj   r   s        r   haszFileSystemCache.has*  s    %%c*	''$7 !1$mmC;A>!#{df'<! !
 !! ! ! ! 	6<<0 	OOA
 	s@   B  <A4!B  *A4+B  4A=9B  =B   	C5CCrS   argskwargsc                     t        j                         dk(  rd}d}d}d}||k  r	  ||i |}	 |S |S  ||i |}|S # t        $ r t        |       ||z  }|dz  }Y nw xY w||k  rB7)ztOn Windows os.replace, os.chmod and open can yield
        permission errors if executed by two different processes.r/   NgMbP?g      $@g           )r0   r1   PermissionErrorr   )r,   rS   r   r   output	wait_stepmax_sleep_timetotal_sleep_times           r   r   zFileSystemCache._run_safely=  s     ??	)FI!N""^300F  v ((F ' #)$$	1$NI# #^3s   = AArP   c              #      K   | j                  t        ||      }|t        	 | |j                          y # |j                          w xY wwrO   )r   openr%   close)r,   rP   r   fss       r   ra   z!FileSystemCache._safe_stream_openT  s@     dD$/:M	HHHJBHHJs    A8 AA

A)i  i,  NN)NN)r
   NrO   r   )F)*__name__
__module____qualname____doc__rJ   r7   staticmethodr   r   r   r   r   rB   _tOptionalAnyr   r"   propertyr9   r(   rA   boolrL   	Generatorr+   rW   floatrn   r|   r~   r   rF   r6   r   r=   r   r   Callabler   r   ra   rq   r   r   r   r      s   ( +'N'	2%'J
 "!%" B B  B 	 B
 kk# B VV BDs  2S 2 2 IM
D[[%
D57[[5E
D	
D"++c*: s LS LT L
2<<T48 
K K5 T "!t !F!t "3 3 3s rvv "s 266 BKK4D PT  %)")) vv) S!	)
 ) 
)V# T d s t &bkk "&& BFF rvv . c    r   r   )r   )r&   r   ri   r#   r0   r2   rb   r   typingr   
contextlibr   pathlibr   r   r   cachelib.baser   cachelib.serializersr   bytesr   r   r   rq   r   r   <module>r      sW       	      %    # 5e bff Ai Ar   