o
    Vh                     @  s   d dl mZ d dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ e	rBd dlmZ d d	lmZ G d
d dZG dd deeZG dd deeZdddZdS )    )annotationsN)dedent)IOTYPE_CHECKING)Path)	BaseCacheSeparateBodyBaseCache)CacheController)datetime)BaseFileLockc                   @  sh   e Zd ZdZ				d+d,ddZed-ddZd.ddZd/ddZ	d0d1d"d#Z	d2d&d'Z
d3d)d*ZdS )4_FileCacheMixinz2Shared implementation for both FileCache variants.F    N	directory
str | Pathforeverboolfilemodeintdirmode
lock_classtype[BaseFileLock] | NonereturnNonec                 C  s^   z|d u rddl m} |}W n ty   td}t|w || _|| _|| _|| _|| _d S )Nr   )FileLockz
            NOTE: In order to use the FileCache you must have
            filelock installed. You can install it via pip:
              pip install cachecontrol[filecache]
            )	filelockr   ImportErrorr   r   r   r   r   r   )selfr   r   r   r   r   r   notice r   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/cachecontrol/caches/file_cache.py__init__   s    

z_FileCacheMixin.__init__xstrc                 C  s   t |   S N)hashlibsha224encode	hexdigest)r"   r   r   r    r'   6   s   z_FileCacheMixin.encodenamec                 C  s6   |  |}t|d d |g }tjj| jg|R  S )N   )r'   listospathjoinr   )r   r)   hashedpartsr   r   r    _fn:   s   
z_FileCacheMixin._fnkeybytes | Nonec                 C  sZ   |  |}zt|d}| W  d    W S 1 sw   Y  W d S  ty,   Y d S w )Nrb)r1   openreadFileNotFoundError)r   r2   r)   fhr   r   r    getA   s   
(z_FileCacheMixin.getvaluebytesexpiresint | datetime | Nonec                 C  s   |  |}| || d S r$   r1   _write)r   r2   r:   r<   r)   r   r   r    setJ   s   
z_FileCacheMixin.setr-   datac              
   C  s   t j|}t j|| jdd | |d 2 tj|d\}}zt || W t 	| nt 	| w t 
|| j t || W d   dS 1 sKw   Y  dS )z:
        Safely write the data to the given path.
        T)exist_okz.lock)dirN)r,   r-   dirnamemakedirsr   r   tempfilemkstempwriteclosechmodr   replace)r   r-   rA   rD   fdr)   r   r   r    r?   P   s   "z_FileCacheMixin._writesuffixc                 C  s>   |  || }| jszt| W d S  ty   Y d S w d S r$   )r1   r   r,   remover7   )r   r2   rM   r)   r   r   r    _deleteb   s   z_FileCacheMixin._delete)Fr   r   N)r   r   r   r   r   r   r   r   r   r   r   r   )r"   r#   r   r#   )r)   r#   r   r#   )r2   r#   r   r3   r$   )r2   r#   r:   r;   r<   r=   r   r   )r-   r#   rA   r;   r   r   )r2   r#   rM   r#   r   r   )__name__
__module____qualname____doc__r!   staticmethodr'   r1   r9   r@   r?   rO   r   r   r   r    r      s    



r   c                   @  s   e Zd ZdZd	ddZdS )
	FileCachezc
    Traditional FileCache: body is stored in memory, so not suitable for large
    downloads.
    r2   r#   r   r   c                 C  s   |  |d d S )N rO   r   r2   r   r   r    deleteq   s   zFileCache.deleteNr2   r#   r   r   )rP   rQ   rR   rS   rY   r   r   r   r    rU   k   s    rU   c                   @  s.   e Zd ZdZdddZdddZdddZdS )SeparateBodyFileCachezh
    Memory-efficient FileCache: body is stored in a separate file, reducing
    peak memory usage.
    r2   r#   r   IO[bytes] | Nonec                 C  s0   |  |d }zt|dW S  ty   Y d S w )N.bodyr4   )r1   r5   r7   )r   r2   r)   r   r   r    get_body{   s   zSeparateBodyFileCache.get_bodybodyr;   r   c                 C  s   |  |d }| || d S )Nr]   r>   )r   r2   r_   r)   r   r   r    set_body   s   zSeparateBodyFileCache.set_bodyc                 C  s   |  |d |  |d d S )NrV   r]   rW   rX   r   r   r    rY      s   zSeparateBodyFileCache.deleteN)r2   r#   r   r\   )r2   r#   r_   r;   r   r   rZ   )rP   rQ   rR   rS   r^   r`   rY   r   r   r   r    r[   u   s
    

r[   urlr#   	filecacher   c                 C  s   t | }||S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    )r	   	cache_urlr1   )ra   rb   r2   r   r   r    url_to_file_path   s   

rd   )ra   r#   rb   rU   r   r#   )
__future__r   r%   r,   rF   textwrapr   typingr   r   pathlibr   cachecontrol.cacher   r   cachecontrol.controllerr	   r
   r   r   r   rU   r[   rd   r   r   r   r    <module>   s    U
