o
    1j                     @   s   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Zd dl	Z	d dl
mZ d dlZeeejddZejdeZeedZeedZeedZd	d
 ZdededdfddZdd Zdd ZdddZG dd dZe ZdS )    N)Path~z.paddlexZPADDLE_PDX_CACHE_HOMEZfunc_retlockstempc                   C   s    t jtdd t jtdd dS )zFEnsure cache dirs exist. Safe for concurrent creation (exist_ok=True).Texist_okN)osmakedirsFUNC_CACHE_DIRFILE_LOCK_DIR r   r   T/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/utils/cache.py_ensure_persist_dirs#   s   r   	dest_path	cache_dirreturnc                 C   s   t jdd|ddd}|j}t| | W d   n1 sw   Y  z	t|| W dS  tyD   zt| W   tyC   Y  w w )zIWrite pickle atomically (temp file + os.replace) under an exclusive lock.wbFZpersist_z.tmp)modedeletedirprefixsuffixN)	tempfileNamedTemporaryFilenamepickledumpr   replaceOSErrorunlink)objr   r   tfZtmp_pathr   r   r   _atomic_write_pickle)   s,   	r"   c                  O   s.   t jtdd t jtdd t jtdd dS )zcreate cache dirTr   N)r   r	   	CACHE_DIRr
   r   argskwargsr   r   r   create_cache_dir>   s   r'   c                  O   s   t S )zget cache dir)r#   r$   r   r   r   get_cache_dirG   s   r(   c                    sH   d t ttfdu rdd dd fdd fd	d
}|S )persistzpersist_{key}.pklNc                 S   s   | d uS Nr   )retr   r   r   <lambda>V   s    zpersist.<locals>.<lambda>c                 S   s   t | dS )Nzutf-8)strencode)r    r   r   r   	_to_bytesX   s   zpersist.<locals>._to_bytesc                    s   t  }|tt|  || j |j	 D ](\}}t
| s8tt| d| dt| d|| || q| }|S )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/   r   r   	_make_key[   s   
zpersist.<locals>._make_keyc                    s    t   fdd}|S )Nc            
         s`  t   t}|j| i |}|  |}tt jt	|d}t
tt| d}|s d }d}t|r{zt|d}	t|	}W d    n1 sSw   Y  d}W n tjtttfyz   zt| W n	 tyw   Y nw Y nw |s| i |}|rt||t W d    |S W d    |S W d    |S 1 sw   Y  |S )N)keyz.lockFrbT)r   r5   	signaturebindapply_defaultsr3   joinr
   formatr-   filelockFileLockr   existsopenr   loadUnpicklingErrorEOFError
ValueErrorr   r   remover"   )
r%   r&   sigr@   rG   Zcache_file_pathlockr+   Zloadedf)FILENAME_PATTERNrF   condr?   r   r   _wrapperm   sP   





z(persist.<locals>._deco.<locals>._wrapper)	functoolswraps)r?   r\   )rZ   rF   r[   )r?   r   _decol   s   zpersist.<locals>._deco)r-   intfloat)r[   r_   r   )rZ   rE   rF   r/   r[   r   r)   M   s   
!r)   c                   @   s:   e Zd Zdd Zdd Zdd ZG dd dZd	d
 ZdS )TempFileManagerc                 C   s(   g | _ ttjddd t| j d S )NT)parentsr   )
temp_filesr   TEMP_DIRmkdiratexitregistercleanupselfr   r   r   __init__   s   zTempFileManager.__init__c                 K   s&   t jddtd|}| j| |S )NF)r   r   r   )r   r   re   rd   append)rk   r&   	temp_filer   r   r   create_temp_file   s   z TempFileManager.create_temp_filec              	   C   s@   | j D ]}z|  t|j W q ty   Y qw g | _ d S r*   )rd   closer   rV   r   FileNotFoundError)rk   rn   r   r   r   ri      s   

zTempFileManager.cleanupc                   @   s$   e Zd Zdd Zdd Zdd ZdS )z&TempFileManager.TempFileContextManagerc                 K   s   || _ || _d | _d S r*   )managerr&   rn   )rk   rr   r&   r   r   r   rl      s   
z/TempFileManager.TempFileContextManager.__init__c                 C   s   | j jdi | j| _| jS )Nr   )rr   ro   r&   rn   rj   r   r   r   	__enter__   s   z0TempFileManager.TempFileContextManager.__enter__c                 C   s   | j r
| j   d S d S r*   )rn   rp   )rk   exc_type	exc_value	tracebackr   r   r   __exit__   s   z/TempFileManager.TempFileContextManager.__exit__N)r7   
__module____qualname__rl   rs   rw   r   r   r   r   TempFileContextManager   s    rz   c                 K   s   | j | fi |S r*   )rz   )rk   r&   r   r   r   temp_file_context   s   z!TempFileManager.temp_file_contextN)r7   rx   ry   rl   ro   ri   rz   r{   r   r   r   r   rb      s    	rb   r*   )rg   r]   r0   r5   r   Zos.pathpathr3   r   r   pathlibr   rN   abspathrL   
expanduserZDEFAULT_CACHE_DIRenvirongetr#   r
   r   re   r   r-   r"   r'   r(   r)   rb   Ztemp_file_managerr   r   r   r   <module>   s,   	
C
&