
    h/                         S SK r S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SKr	S SK
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  SS\S	\	R*                  4S
 jjr " S S\5      rg)    N)contextmanager)Path)sleep)time)	BaseCache)FileSystemSerializerstringreturnc                 .    [         R                  " U 5      $ )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    bC:\Users\ROHAN GUPTA\OneDrive\Desktop\mathbuddy-assessment\venv\Lib\site-packages\cachelib/file.py	_lazy_md5r      s    
 ;;v    c                   `   \ rS rSrSrSrSr\" \5      r	\
" 5       r    S,S\S\S\S	\R                  \   S
\R                   4
S jjrS\4S jr\S\4S j5       r S-S\R                  \   S\R                  \   SS4S jjrS\R                  \   S\4S jrS\S\4S jrS\R2                  \SS4   4S jrS\4S jrS\SS4S jrS\4S jrS.S jrS\4S jr S\S\4S jr!S\S\R                   4S jr"S/S\S\R                   S\R                  \   S\4S  jjr#  S0S\S\R                   S\R                  \   S!\S\4
S" jjr$S1S\S!\S\4S# jjr%S\S\4S$ jr&S%\RN                  S&\R                   S'\R                   S\R                   4S( jr(\)S)\S	\S\R2                  4S* j5       r*S+r+g)2FileSystemCache   a  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                    [         R                  " X5        Xl        X l        U R                  U l        Ub  XPl        X@l        U R                  c  U R                  5       U l         [        R                  " U R                  5        U R                  S:w  a0  U R                  [        [        U R!                  5       5      5      S9  g g ! [         a)  nUR                  [        R                  :w  a  e  S nANoS nAff = f)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   FileSystemCache.__init__8   s     	41
# 55" +
 
::;;=DJ	KK

# ??aSdnn.>)?%@A    	xx5<<' (	s   & C 
C:C55C:r
   c                 \    Sn[         R                  " 5       S:X  a  [        R                  nU$ )Ni  Windows)platformsystemstatS_IWRITE)r-   r   s     r   r#   -FileSystemCache._get_compatible_platform_modeZ   s$    ??	)==Dr   c                 J    U R                  U R                  5      =(       d    S$ Nr   )get_fs_count_filer-   s    r   _file_countFileSystemCache._file_count`   s    xx++,11r   deltar   c                     U R                   S:X  a  g U(       a  U R                  U-   nOU=(       d    SnU R                  U R                  USS9  g )Nr   T)mgmt_element)r   r<   setr:   )r-   r>   r   	new_counts       r   r)   FileSystemCache._update_countd   sG     ??a((50I
I$$idCr   timeoutc                 |    [         R                  " X5      nUS:w  a  [        [        5       5      U-   n[        U5      $ r8   )r   _normalize_timeoutintr   )r-   rD   s     r   rF   "FileSystemCache._normalize_timeoutp   s3    ..t=a<$&kG+G7|r   namec                     U R                  U R                  5      R                  [        R                  5      S   nX:H  =(       d    UR                  U R                  5      $ )N)_get_filenamer:   splitr$   sependswith_fs_transaction_suffix)r-   rI   fshashs      r   _is_mgmtFileSystemCache._is_mgmtv   sH    ##D$7$78>>rvvFrJ~Kt/J/J!KKr   c                 X   ^  U 4S j[         R                  " T R                  5       5       $ )z2return a list of (fully qualified) cache filenamesc              3      >#    U  HG  nTR                  U5      (       a  M  [        R                  R                  TR                  U5      v   MI     g 7fN)rR   r$   pathjoinr   ).0fnr-   s     r   	<genexpr>,FileSystemCache._list_dir.<locals>.<genexpr>|   s<      
,==$ )BGGLLR((,s
   A1A)r$   listdirr   r;   s   `r   r,   FileSystemCache._list_dirz   s"    
jj,
 	
r   c                 `    U R                   S:g  =(       a    U R                  U R                   :  $ r8   )r   r<   r;   s    r   _over_thresholdFileSystemCache._over_threshold   s%    !#J(8(84??(JJr   nowc                    U R                  5        H|  n U R                  US5       n[        R                  " SUR	                  S5      5      S   nS S S 5        WS:w  a.  XA:  a'  [
        R                  " U5        U R                  SS9  Mz  M|  M~     g ! , (       d  f       NG= f! [         a     M  [        [        [        R                  4 a    [        R                  " SUSS	9   M  f = f)
NrbI   r   rK   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-   rb   fnamefexpiress        r   _remove_expiredFileSystemCache._remove_expired   s    ^^%E++E48A$mmC;A>G 9a<GMIIe$&&R&0 %2<	 &88
 % Xv||4 E!s.   B#*B8B#
B 	B##
C(04C('C(c                    / nU R                  5        HZ  n U R                  US5       n[        R                  " SUR	                  S5      5      S   nUR                  XB45        S S S 5        M\     S [        US	 S
9 5       nU H@  n [        R                  " U5        U R                  SS9  U R!                  5       (       a  M@    g   g! , (       d  f       M  = f! [         a     M  [        [        [        R                  4 a    [        R                  " SUSS9   GM  f = f! [         a     N[         a    [        R                  " SUSS9     gf = f)Nrd   re   rf   r   rh   Tri   c              3   *   #    U  H	  u  pUv   M     g 7frV    )rY   _ru   s      r   r[   0FileSystemCache._remove_older.<locals>.<genexpr>   s      
"ThaE"Ts   c                     U S   $ r8   r|   )items    r   <lambda>/FileSystemCache._remove_older.<locals>.<lambda>   s	    DQRGr   )keyrK   rg   F)r,   rk   rl   rm   rn   appendrp   r&   rq   rr   rs   rt   sortedr$   ro   r)   r`   )r-   exp_fname_tuplesru   rv   	timestampfname_sorteds         r   _remove_olderFileSystemCache._remove_older   sA   ^^%E++E48A &c166!9 =a @I$++Y,>? 98 &
"()9?S"T
 "E		% """, '')) " ; 98 % Xv||4 E! %  E!
 sL   C<C
%C	%D%

C	CC
D")4D"!D"%
E1EEc                     U R                  5       (       a  [        5       nU R                  U5        U R                  5       (       a  U R                  5         g g rV   )r`   r   rx   r   )r-   rb   s     r   _pruneFileSystemCache._prune   sF    !!&C  %!!  "r   c                    [        U R                  5       5       H  u  p [        R                  " U5        M     U R                  SS9  g! [         a     M=  [
         a*    [        R                  " SUSS9  U R                  U* S9     gf = f)Nrh   Tri   rg   Fr   r   )		enumerater,   r$   ro   rp   r&   rs   rt   r)   )r-   iru   s      r   clearFileSystemCache.clear   s    !$.."23HA		%  4 	# %  E!
 ""!",s   A


B
/B
	B
r   c                    [        U[        5      (       a1  UR                  S5      nU R                  U5      R	                  5       nO[        S[        U5       35      e[        R                  R                  U R                  U5      $ )Nzutf-8z$Key must be a string, received type )
isinstancestrencoder!   	hexdigest	TypeErrortyper$   rW   rX   r   )r-   r   bkey	bkey_hashs       r   rL   FileSystemCache._get_filename   sf    c3::g&D))$/99;IB49+NOOww||DJJ	22r   c                    U R                  U5      n U R                  US5       n[        R                  " SUR	                  S5      5      S   nUS:X  d  U[        5       :  a$  U R                  R                  U5      sS S S 5        $  S S S 5        g ! , (       d  f       g = f! [         a     g [        [        [        R                  4 a    [        R                  " SUSS9   g f = f)Nrd   re   rf   r   rh   Tri   )rL   rk   rl   rm   rn   r   
serializerloadrp   r&   rq   rr   rs   rt   r-   r   filenamerv   pickle_times        r   r9   FileSystemCache.get   s    %%c*	''$71$mmC;A>!#{df'<??//2 87'< 8  87  ! 	  6<<0 	OOA
 	s;   B" AB=	B" B" 
BB" B" "
C%.4C%$C%c                     U R                  U5      n[        R                  R                  U5      (       d  U R	                  XU5      $ gNF)rL   r$   rW   existsrA   )r-   r   r   rD   r   s        r   addFileSystemCache.add   s9    %%c*ww~~h''88C00r   r@   c                 ^   U(       a  SnOU R                  5         U R                  U5      nU R                  U5      n[        R                  R                  U5      n [        R                  " U R                  U R                  S9u  px[        R                  " US5       n	U	R                  [        R                  " SU5      5        U R                  R                  X)5        S S S 5        U R!                  [        R"                  X5        U R!                  [        R$                  XPR&                  5        [)        U5      R+                  5       R,                  n
U(       d  U(       d  U R/                  SS9  U
S:  $ ! , (       d  f       N= f! [0         a    [2        R4                  " SUSS	9   g
f = f)Nr   )suffixdirwbre      rg   rh   Tri   F)r   rF   rL   r$   rW   isfiletempfilemkstemprP   r   fdopenwriterl   packr   dump_run_safelyreplacechmodr"   r   r4   st_sizer)   r&   rs   rt   )r-   r   r   rD   r@   r   	overwritefdtmprv   fsizes              r   rA   FileSystemCache.set   sJ    G KKM))'2%%c*GGNN8,		&&22

GB 2t$C12$$U. % RZZ7RXXx<N'')11E \"""+19' %$  	OOA
 	s-   AF	 AE8!A5F	 8
FF	 	 F,+F,c                      [         R                  " U R                  U5      5        U(       d  U R                  SS9  g! [         a     g[
         a    [        R                  " SSS9   gf = f)NrK   rg   Tz*Exception raised while handling cache fileri   F)r$   ro   rL   r)   rp   r&   rs   rt   )r-   r   r@   s      r   deleteFileSystemCache.delete  sf    	IId((-.  """, ! 	 	OOHSWX	s   %> 
A+
A+*A+c                    U R                  U5      n U R                  US5       n[        R                  " SUR	                  S5      5      S   nUS:X  d  U[        5       :  a
   S S S 5        g S S S 5        g! , (       d  f       g = f! [         a     g[        [        [        R                  4 a    [        R                  " SUSS9   gf = f)	Nrd   re   rf   r   TFrh   ri   )rL   rk   rl   rm   rn   r   rp   r&   rq   rr   rs   rt   r   s        r   hasFileSystemCache.has*  s    %%c*	''$71$mmC;A>!#{df'< 87
 ! 877 ! 	6<<0 	OOA
 	s@   B ?A7$B -A7.B 7
BB B 
C4C
CrZ   argskwargsc                     [         R                  " 5       S:X  a  SnSnSnSnXv:  a   U" U0 UD6n U$ U$ U" U0 UD6nU$ ! [         a    [        U5        Xu-  nUS-  n Of = fXv:  a  MC  N8)zlOn 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-   rZ   r   r   output	wait_stepmax_sleep_timetotal_sleep_times           r   r   FileSystemCache._run_safely=  s     ??	)FI!N""300F  v ((F ' #)$$1$NI# #3s   ? A A rW   c              #      #    U R                  [        X5      nUc  [        e Uv   UR                  5         g ! UR                  5         f = f7frV   )r   openr&   close)r-   rW   r   fss       r   rk   !FileSystemCache._safe_stream_openT  s=     dD/:M	HHHJBHHJs    A8 AA

A)r!   r"   r   r   )i  i,  NN)NN)r
   NrV   r   )F),__name__
__module____qualname____firstlineno____doc__rP   r:   staticmethodr   r    r   r   r   rG   _tOptionalAnyr   r#   propertyr<   r)   rF   boolrR   	Generatorr,   r`   floatrx   r   r   r   rL   r9   r   rA   r   r   Callabler   r   rk   __static_attributes__r|   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   rs   r$   r2   r4   rl   r   typingr   
contextlibr   pathlibr   r   r   cachelib.baser   cachelib.serializersr   bytesr   r   r   r|   r   r   <module>r      sW       	      %    # 5e bff Ai Ar   