
    =pj                        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rS SK	r	S SK
Jr  S SKr\R                  " \R                  " \R                  R                  S5      S5      5      r\R"                  R%                  S\5      r\R                  " \S5      r\R                  " \S5      r\R                  " \S5      rS	 rS
\S\SS4S jrS rS rSS jr " S S5      r\" 5       rg)    N)Path~z.paddlexPADDLE_PDX_CACHE_HOMEfunc_retlockstempc                  h    [         R                  " [        SS9  [         R                  " [        SS9  g)zFEnsure cache dirs exist. Safe for concurrent creation (exist_ok=True).Texist_okN)osmakedirsFUNC_CACHE_DIRFILE_LOCK_DIR     O/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddlex/utils/cache.py_ensure_persist_dirsr   #   s    KK.KK-r   	dest_path	cache_dirreturnc                 H   [         R                  " SSUSSS9 nUR                  n[        R                  " X5        SSS5         [
        R                  " WU5        g! , (       d  f       N'= f! [         a)     [
        R                  " W5        e ! [         a     e f = ff = f)zIWrite pickle atomically (temp file + os.replace) under an exclusive lock.wbFpersist_z.tmp)modedeletedirprefixsuffixN)	tempfileNamedTemporaryFilenamepickledumpr   replaceOSErrorunlink)objr   r   tftmp_paths        r   _atomic_write_pickler*   )   s    		$	$
 
77C



8Y'
 
  	IIh 	  		s;   #AA. 
A+.
B!9BB!
BB!BB!c                      [         R                  " [        SS9  [         R                  " [        SS9  [         R                  " [        SS9  g)zcreate cache dirTr
   N)r   r   	CACHE_DIRr   r   argskwargss     r   create_cache_dirr0   >   s.     KK	D)KK.KK-r   c                      [         $ )zget cache dir)r,   r-   s     r   get_cache_dirr2   G   s
     r   c                 f   ^ ^^^^ Sm[         [        [        4mT c  S m S mUU4S jmUUU 4S jnU$ )persistzpersist_{key}.pklc                 
    U S L$ Nr   )rets    r   <lambda>persist.<locals>.<lambda>V   s    3d?r   c                 6    [        U 5      R                  S5      $ )Nzutf-8)strencode)r'   s    r   	_to_bytespersist.<locals>._to_bytesX   s    3xw''r   c           
      "  > [         R                  " 5       nUR                  T" [        R                  " [
        R                  " U 5      5      5      5        UR                  T" U R                  5      5        UR                  R                  5        Hj  u  p4[        UT5      (       d&  [        [        U5       SU S[        U5       S35      eUR                  T" U5      5        UR                  T" U5      5        Ml     UR                  5       nU$ )Nz: z, z' is unhashable or not a supported type.)hashlibmd5updateosprealpathinspectgetsourcefile__name__	argumentsitems
isinstance	TypeErrorreprtype	hexdigest)funcbnd_argsmkvhash_SUPPORTED_ARG_TYPESr=   s         r   	_make_keypersist.<locals>._make_key[   s     KKM	3<<(=(=d(CDEF	4==)*&&,,.DAa!455Awir!BtAwi/VW  HHYq\"HHYq\" / r   c                 N   >^  [         R                  " T 5      UUUU 4S j5       nU$ )Nc                  V  > [        5         [        R                  " T5      nUR                  " U 0 UD6nUR	                  5         T" TU5      n[
        R                  " [        T
R                  [        U5      S95      n[        R                  " [
        R                  " [        U S35      5      nU   S nSn[
        R                  " U5      (       a.   [        US5       n	[        R                   " U	5      nS S S 5        SnU(       d%  T" U 0 UD6nT" U5      (       a  [/        Xu[        5        S S S 5        U$ ! , (       d  f       NF= f! [        R"                  [$        [&        [(        4 a+     [*        R,                  " U5         N! [(         a      Nf = ff = f! , (       d  f       W$ = f)N)keyz.lockFrbT)r   rE   	signaturebindapply_defaultsrC   joinr   formatr;   filelockFileLockr   existsopenr"   loadUnpicklingErrorEOFError
ValueErrorr%   r   remover*   )r.   r/   sigrP   rZ   cache_file_pathlockr7   loadedfFILENAME_PATTERNrV   condrO   s             r   _wrapper(persist.<locals>._deco.<locals>._wrapperm   s^    "##D)Cxx00H##%D(+C!hh 0 7 7CH 7 EO $$SXXmuE]%KLD::o..!!/48A"(++a.C 9!% //CCyy,S>R! " J 98 #22Hj'R !!IIo6& ! !! " Jsf   2 FED66
E ,F6
E	 E$F,FF
FFFFFF
F()	functoolswraps)rO   rq   ro   rV   rp   s   ` r   _decopersist.<locals>._decol   s%    			 
	: r   )r;   intfloat)rp   ru   ro   rU   rV   r=   s   ` @@@@r   r4   r4   M   s9    
 +U+|*("B Lr   c                   @    \ rS rSrS rS rS r " S S5      rS rSr	g	)
TempFileManager   c                     / U l         [        [        5      R                  SSS9  [        R
                  " U R                  5        g )NT)parentsr   )
temp_filesr   TEMP_DIRmkdiratexitregistercleanupselfs    r   __init__TempFileManager.__init__   s0    XTD9%r   c                 v    [         R                  " SS[        S.UD6nU R                  R	                  U5        U$ )NF)r   r   r   )r   r    r   r~   append)r   r/   	temp_files      r   create_temp_file TempFileManager.create_temp_file   s4    //Uu(UfU	y)r   c                     U R                    H4  n UR                  5         [        R                  " UR                  5        M6     / U l         g ! [
         a     MM  f = fr6   )r~   closer   ri   r!   FileNotFoundError)r   r   s     r   r   TempFileManager.cleanup   sL    I!		)..) )  % s   0A
AAc                   &    \ rS rSrS rS rS rSrg)&TempFileManager.TempFileContextManager   c                 *    Xl         X l        S U l        g r6   )managerr/   r   )r   r   r/   s      r   r   /TempFileManager.TempFileContextManager.__init__   s    "L K!DNr   c                 p    U R                   R                  " S0 U R                  D6U l        U R                  $ )Nr   )r   r   r/   r   r   s    r   	__enter__0TempFileManager.TempFileContextManager.__enter__   s)    !\\::IT[[IDN>>!r   c                 \    U R                   (       a  U R                   R                  5         g g r6   )r   r   )r   exc_type	exc_value	tracebacks       r   __exit__/TempFileManager.TempFileContextManager.__exit__   s    ~~$$& r   )r/   r   r   N)rG   
__module____qualname____firstlineno__r   r   r   __static_attributes__r   r   r   TempFileContextManagerr      s    	"
	"	'r   r   c                 (    U R                   " U 40 UD6$ r6   )r   )r   r/   s     r   temp_file_context!TempFileManager.temp_file_context   s    **4:6::r   )r~   N)
rG   r   r   r   r   r   r   r   r   r   r   r   r   rz   rz      s     &

' ';r   rz   r6   )r   rs   r@   rE   r   os.pathpathrC   r"   r   pathlibr   ra   abspathr_   
expanduserDEFAULT_CACHE_DIRenvirongetr,   r   r   r   r   r;   r*   r0   r2   r4   rz   temp_file_managerr   r   r   <module>r      s         	     KK););C)@* MN JJNN24EF	)Z0G,88Iv&.   *.@F#; #;L $% r   