o
    Mf                  	   @   sj  d Z ddlZddlZddlZddlZddlmZmZmZ ddl	m
Z
mZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZmZmZ e e!Z"dedee# fddZ$dedede%dee& fddZ'de%de%fddZ(de%de%de%fddZ)dedede%fddZ*dededefdd Z+G d!d" d"Z,G d#d$ d$Z-dS )%z)Download files with progress indicators.
    N)IterableOptionalTuple)CONTENT_CHUNK_SIZEResponse)get_download_progress_renderer)NetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitextrespreturnc              
   C   s,   zt | jd W S  tttfy   Y d S w )Nzcontent-length)intheaders
ValueErrorKeyError	TypeError)r    r   `/var/www/html/analyze/labelStudio/lib/python3.10/site-packages/pip/_internal/network/download.py_get_http_response_size   s
   r   linkprogress_barc           	      C   s   t | }|jtjkr|j}n|j}t|}|rd|t|}t	| r*t
d| nt
d| t
 tjkr:d}nt	| rAd}n|sFd}n	|dkrMd}nd}t| t}|sX|S t||d}||S )Nz{} ({})zUsing cached %szDownloading %sFTi@  )bar_typesize)r   netlocr	   file_storage_domainshow_urlurl_without_fragmentr   formatr   r   loggerinfogetEffectiveLevelloggingINFOr   r   r   )	r   r   r   total_lengthurl
logged_urlshow_progresschunksrendererr   r   r   _prepare_download   s0   
r1   filenamec                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r2   r   r   r   sanitize_content_filenameH   s   r6   content_dispositiondefault_filenamec                 C   s,   t | \}}|d}|rt|}|p|S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    r2   )cgiparse_headergetr6   )r7   r8   _typeparamsr2   r   r   r   parse_content_dispositionO   s
   
r>   c                 C   s   |j }| jd}|rt||}t|d }|s(t| jdd}|r(||7 }|s?|j| jkr?tj	| jd }|r?||7 }|S )zoGet an ideal filename from the given HTTP response, falling back to
    the link filename if not provided.
    zcontent-disposition   zcontent-type )
r2   r   r;   r>   r   	mimetypesguess_extensionr,   r3   r4   )r   r   r2   r7   extr   r   r   _get_http_response_filename]   s   
rD   sessionc                 C   s.   |j ddd }| j|tdd}t| |S )N#r?   r   T)r   stream)r,   splitr;   r   r   )rE   r   
target_urlr   r   r   r   _http_get_downloadr   s   rJ   c                   @   s@   e Zd ZdededdfddZdededeeef fd	d
ZdS )
DownloaderrE   r   r   Nc                 C      || _ || _d S N_session_progress_barselfrE   r   r   r   r   __init__z      
zDownloader.__init__r   locationc              
   C   s   zt | j|}W n ty% } z|jdusJ td|jj|  d}~ww t||}tj	
||}t||| j}t|d}|D ]}	||	 qAW d   n1 sSw   Y  |jdd}
||
fS )z.Download the file given by link into location.NHTTP error %s while getting %swbContent-Typer@   rJ   rO   r   responser&   criticalstatus_coderD   r3   r4   joinr1   rP   openwriter   r;   )rR   r   rU   r   er2   filepathr/   content_filechunkcontent_typer   r   r   __call__   s(   

zDownloader.__call__)	__name__
__module____qualname__r   strrS   r
   r   re   r   r   r   r   rK   y   s    
"rK   c                   @   sP   e Zd ZdededdfddZdee dedeeeeeef f  fd	d
Z	dS )BatchDownloaderrE   r   r   Nc                 C   rL   rM   rN   rQ   r   r   r   rS      rT   zBatchDownloader.__init__linksrU   c                 c   s    |D ]h}zt | j|}W n ty* } z|jdusJ td|jj|  d}~ww t||}tj	
||}t||| j}t|d}	|D ]}
|	|
 qFW d   n1 sXw   Y  |jdd}|||ffV  qdS )z0Download the files given by links into location.NrV   rW   rX   r@   rY   )rR   rk   rU   r   r   r`   r2   ra   r/   rb   rc   rd   r   r   r   re      s2   
	zBatchDownloader.__call__)
rf   rg   rh   r   ri   rS   r   r
   r   re   r   r   r   r   rj      s    
rj   ).__doc__r9   r)   rA   r3   typingr   r   r   pip._vendor.requests.modelsr   r   pip._internal.cli.progress_barsr   pip._internal.exceptionsr   pip._internal.models.indexr	   pip._internal.models.linkr
   pip._internal.network.cacher   pip._internal.network.sessionr   pip._internal.network.utilsr   r   r   pip._internal.utils.miscr   r   r   	getLoggerrf   r&   r   r   ri   bytesr1   r6   r>   rD   rJ   rK   rj   r   r   r   r   <module>   s@    

*