o
    Nia                     @   sL   d dl Z d dlZd dlZd dlmZ ddlmZ e eZ	G dd dZ
dS )    N)Path   )	PathOrStrc                	   @   s<   e Zd ZdZddedededdfd	d
Zdd Zdd ZdS )	CacheFilea`  
    This is a context manager that makes robust caching easier.

    On `__enter__`, an IO handle to a temporarily file is returned, which can
    be treated as if it's the actual cache file.

    On `__exit__`, the temporarily file is renamed to the cache file. If anything
    goes wrong while writing to the temporary file, it will be removed.
    w+b.tmpcache_filenamemodesuffixreturnNc                 C   s<   t || _tj| j| _|| _tj| j| jd|d| _	d S )NF)dirdeleter
   )
r   r   ospathdirnamecache_directoryr	   tempfileNamedTemporaryFile	temp_file)selfr   r	   r
    r   J/home/ubuntu/.local/lib/python3.10/site-packages/cached_path/cache_file.py__init__   s   
zCacheFile.__init__c                 C   s   | j S )N)r   )r   r   r   r   	__enter__   s   zCacheFile.__enter__c                 C   s   ~~|d u r0| j   ttdrt| j  | j   td| j j| j	 t
| j j| j	 dS td| j j | j   t| j j dS )N	fdatasyncz$Renaming temp file %s to cache at %sTzremoving temp file %sF)r   flushhasattrr   r   closeloggerdebugnamer   replaceremove)r   exc_type	exc_value	tracebackr   r   r   __exit__!   s   



zCacheFile.__exit__)r   r   )	__name__
__module____qualname____doc__r   strr   r   r&   r   r   r   r   r      s
    
r   )loggingr   r   pathlibr   commonr   	getLoggerr'   r   r   r   r   r   r   <module>   s    
