o
    "g                     @  s   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
 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d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)BaseFileLockfilenamestrfmodeintreturn	IO[bytes]c                 C  s   t j}|t jt jB O }tt dr|t jO }tt dr|t jO }zt |  W n	 ty/   Y nw t 	| ||}zt 
|dW S    t |  )N
O_NOFOLLOWO_BINARYwb)osO_WRONLYO_CREATO_EXCLhasattrr   r   removeOSErroropenfdopenclose)r   r   flagsfd r!   i/root/parts/websockify/install/lib/python3.10/site-packages/pip/_vendor/cachecontrol/caches/file_cache.py_secure_open_write   s"   




r#   c                   @  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filemoder   dirmode
lock_classtype[BaseFileLock] | Noner   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]
            )	filelockr0   ImportErrorr   r'   r)   r+   r,   r-   )selfr'   r)   r+   r,   r-   r0   noticer!   r!   r"   __init__A   s    

z_FileCacheMixin.__init__xr   c                 C  s   t |   S N)hashlibsha224encode	hexdigest)r6   r!   r!   r"   r:   ^   s   z_FileCacheMixin.encodenamec                 C  s6   |  |}t|d d |g }tjj| jg|R  S )N   )r:   listr   pathjoinr'   )r3   r<   hashedpartsr!   r!   r"   _fnb   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)rC   r   readFileNotFoundError)r3   rD   r<   fhr!   r!   r"   geti   s   
(z_FileCacheMixin.getvaluebytesexpiresint | datetime | Nonec                 C  s   |  |}| || d S r7   rC   _write)r3   rD   rK   rM   r<   r!   r!   r"   setr   s   
z_FileCacheMixin.setr?   datac              	   C  s   zt t j|| j W n	 ty   Y nw | |d , t|| j}|	| W d   n1 s5w   Y  W d   dS W d   dS 1 sMw   Y  dS )z:
        Safely write the data to the given path.
        z.lockN)
r   makedirsr?   dirnamer,   r   r-   r#   r+   write)r3   r?   rR   rI   r!   r!   r"   rP   x   s   "z_FileCacheMixin._writesuffixc                 C  s>   |  || }| jszt| W d S  ty   Y d S w d S r7   )rC   r)   r   r   rH   )r3   rD   rV   r<   r!   r!   r"   _delete   s   z_FileCacheMixin._delete)Fr%   r&   N)r'   r(   r)   r*   r+   r   r,   r   r-   r.   r   r/   )r6   r   r   r   )r<   r   r   r   )rD   r   r   rE   r7   )rD   r   rK   rL   rM   rN   r   r/   )r?   r   rR   rL   r   r/   )rD   r   rV   r   r   r/   )__name__
__module____qualname____doc__r5   staticmethodr:   rC   rJ   rQ   rP   rW   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.
    rD   r   r   r/   c                 C  s   |  |d d S )N rW   r3   rD   r!   r!   r"   delete   s   zFileCache.deleteNrD   r   r   r/   )rX   rY   rZ   r[   ra   r!   r!   r!   r"   r]      s    r]   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.
    rD   r   r   IO[bytes] | Nonec                 C  s0   |  |d }zt|dW S  ty   Y d S w )N.bodyrF   )rC   r   rH   )r3   rD   r<   r!   r!   r"   get_body   s   zSeparateBodyFileCache.get_bodybodyrL   r/   c                 C  s   |  |d }| || d S )Nre   rO   )r3   rD   rg   r<   r!   r!   r"   set_body   s   zSeparateBodyFileCache.set_bodyc                 C  s   |  |d |  |d d S )Nr^   re   r_   r`   r!   r!   r"   ra      s   zSeparateBodyFileCache.deleteN)rD   r   r   rd   )rD   r   rg   rL   r   r/   rb   )rX   rY   rZ   r[   rf   rh   ra   r!   r!   r!   r"   rc      s
    

rc   url	filecachec                 C  s   t | }||S )z\Return the file cache path based on the URL.

    This does not ensure the file exists!
    )r	   	cache_urlrC   )ri   rj   rD   r!   r!   r"   url_to_file_path   s   

rl   )r   r   r   r   r   r   )ri   r   rj   r]   r   r   )
__future__r   r8   r   textwrapr   typingr   r   pathlibr   pip._vendor.cachecontrol.cacher   r   #pip._vendor.cachecontrol.controllerr	   r
   r1   r   r#   r$   r]   rc   rl   r!   r!   r!   r"   <module>   s    
)R
