
    d                        d dl m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
mZ d dlmZ erd dlmZ d dlmZ ddZ G d d          Z G d dee
          Z G d dee          ZddZdS )    )annotationsN)dedent)IOTYPE_CHECKING)	BaseCacheSeparateBodyBaseCache)CacheController)datetime)BaseFileLockfilenamestrfmodeintreturn	IO[bytes]c                   t           j        }|t           j        t           j        z  z  }t	          t           d          r|t           j        z  }t	          t           d          r|t           j        z  }	 t          j        |            n# t          $ r Y nw xY wt          j	        | ||          }	 t          j
        |d          S #  t          j        |            xY w)N
O_NOFOLLOWO_BINARYwb)osO_WRONLYO_CREATO_EXCLhasattrr   r   removeOSErroropenfdopenclose)r   r   flagsfds       b/home/feoh/.local/pipx/venvs/poetry/lib/python3.11/site-packages/cachecontrol/caches/file_cache.py_secure_open_writer#      s    KE 
RZ")##E r<    r: 
	(    
5%	(	(ByT"""
s   2B 
BB.C Cc                  h    e Zd ZdZ	 	 	 	 d#d$dZed%d            Zd&dZd'dZ	 d(d)dZ	d*d Z
d+d"ZdS ),_FileCacheMixinz2Shared implementation for both FileCache variants.F    N	directoryr   foreverboolfilemoder   dirmode
lock_classtype[BaseFileLock] | Noner   Nonec                    	 |ddl m} |}n,# t          $ r t          d          }t          |          w xY 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 filelock
            )	filelockr1   ImportErrorr   r(   r)   r+   r,   r-   )selfr(   r)   r+   r,   r-   r1   notices           r"   __init__z_FileCacheMixin.__init__@   s    	&!------%
 	& 	& 	& F f%%%	& # $s   
 )6xc                r    t          j        |                                                                           S N)hashlibsha224encode	hexdigest)r7   s    r"   r<   z_FileCacheMixin.encode]   s&    ~ahhjj))33555    namec                    |                      |          }t          |d d                   |gz   }t          j        j        | j        g|R  S )N   )r<   listr   pathjoinr(   )r4   r?   hashedpartss       r"   _fnz_FileCacheMixin._fna   sM     T""VBQBZ  F8+w|DN3U3333r>   keybytes | Nonec                    |                      |          }	 t          |d          5 }|                                cd d d            S # 1 swxY w Y   d S # t          $ r Y d S w xY w)Nrb)rG   r   readFileNotFoundError)r4   rH   r?   fhs       r"   getz_FileCacheMixin.geth   s    xx}}	dD!! !Rwwyy! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 	 	 	44	s3   A AA AA AA 
A#"A#valuebytesexpiresint | datetime | Nonec                \    |                      |          }|                     ||           d S r9   rG   _write)r4   rH   rP   rR   r?   s        r"   setz_FileCacheMixin.setq   s-     xx}}D%     r>   rC   datac                ~   	 t          j        t           j                            |          | j                   n# t
          $ r Y nw xY w|                     |dz             5  t          || j                  5 }|	                    |           ddd           n# 1 swxY w Y   ddd           dS # 1 swxY w Y   dS )z:
        Safely write the data to the given path.
        z.lockN)
r   makedirsrC   dirnamer,   r   r-   r#   r+   write)r4   rC   rX   rN   s       r"   rV   z_FileCacheMixin._writew   s;   
	K--t|<<<< 	 	 	D	 __TG^,, 	 	#D$-88 B              	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sE   7: 
AA"B28BB2B	B2!B	"B22B69B6suffixc                    |                      |          |z   }| j        s(	 t          j        |           d S # t          $ r Y d S w xY wd S r9   )rG   r)   r   r   rM   )r4   rH   r]   r?   s       r"   _deletez_FileCacheMixin._delete   sb    xx}}v%| 		$$   	 	s   7 
AA)Fr&   r'   N)r(   r   r)   r*   r+   r   r,   r   r-   r.   r   r/   )r7   r   r   r   )r?   r   r   r   )rH   r   r   rI   r9   )rH   r   rP   rQ   rR   rS   r   r/   )rC   r   rX   rQ   r   r/   )rH   r   r]   r   r   r/   )__name__
__module____qualname____doc__r6   staticmethodr<   rG   rO   rW   rV   r_    r>   r"   r%   r%   =   s        <<
 04% % % % %: 6 6 6 \64 4 4 4    HL! ! ! ! !        r>   r%   c                      e Zd ZdZddZdS )		FileCachezc
    Traditional FileCache: body is stored in memory, so not suitable for large
    downloads.
    rH   r   r   r/   c                2    |                      |d           d S )N r_   r4   rH   s     r"   deletezFileCache.delete   s    S"r>   NrH   r   r   r/   )r`   ra   rb   rc   rl   re   r>   r"   rg   rg      s2         
     r>   rg   c                  *    e Zd ZdZddZdd
ZddZdS )SeparateBodyFileCachezh
    Memory-efficient FileCache: body is stored in a separate file, reducing
    peak memory usage.
    rH   r   r   IO[bytes] | Nonec                v    |                      |          dz   }	 t          |d          S # t          $ r Y d S w xY w)N.bodyrK   )rG   r   rM   )r4   rH   r?   s      r"   get_bodyzSeparateBodyFileCache.get_body   sM    xx}}w&	d###  	 	 	44	s   * 
88bodyrQ   r/   c                b    |                      |          dz   }|                     ||           d S )Nrr   rU   )r4   rH   rt   r?   s       r"   set_bodyzSeparateBodyFileCache.set_body   s0    xx}}w&D$r>   c                ^    |                      |d           |                      |d           d S )Nri   rr   rj   rk   s     r"   rl   zSeparateBodyFileCache.delete   s0    S"S'"""""r>   N)rH   r   r   rp   )rH   r   rt   rQ   r   r/   rm   )r`   ra   rb   rc   rs   rv   rl   re   r>   r"   ro   ro      sZ         
          # # # # # #r>   ro   url	filecachec                T    t          j        |           }|                    |          S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    )r	   	cache_urlrG   )rx   ry   rH   s      r"   url_to_file_pathr|      s&    
 
#C
(
(C==r>   )r   r   r   r   r   r   )rx   r   ry   rg   r   r   )
__future__r   r:   r   textwrapr   typingr   r   cachecontrol.cacher   r   cachecontrol.controllerr	   r
   r2   r   r#   r%   rg   ro   r|   re   r>   r"   <module>r      sh   # " " " " "  				       $ $ $ $ $ $ $ $ ? ? ? ? ? ? ? ? 3 3 3 3 3 3 &!!!!!!%%%%%%& & & &RO O O O O O O Od       # # # # #O-B # # #,     r>   