a
    &(a                     @   sT  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"eee# dddZ$eee%ee& dddZ'e%e%dddZ(e%e%e%dddZ)eee%dddZ*eeed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)DownloadProgressProvider)NetworkConnectionError)PyPI)Link)is_from_cache)
PipSession)HEADERSraise_for_statusresponse_chunks)format_sizeredact_auth_from_urlsplitext)respreturnc              
   C   s0   zt | jd W S  tttfy*   Y d S 0 d S )Nzcontent-length)intheaders
ValueErrorKeyError	TypeError)r    r   v/private/var/folders/m4/wm8f9dms7t78045sws6ggdl80000gn/T/pip-unpacked-wheel-vn48x2xj/pip/_internal/network/download.py_get_http_response_size   s    r   )r   linkprogress_barr   c                 C   s   t | }|jtjkr|j}n|j}t|}|r>d|t|}t	| rTt
d| nt
d| t
 tjkrtd}n*t	| rd}n|sd}n|dkrd}nd}t| t}|s|S t||d|S )Nz{} ({})zUsing cached %szDownloading %sFTi@  )max)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chunksr   r   r   _prepare_download   s.    
r/   )filenamer   c                 C   s   t j| S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ospathbasename)r0   r   r   r   sanitize_content_filenameG   s    r4   )content_dispositiondefault_filenamer   c                 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.
    r0   )cgiparse_headergetr4   )r5   r6   _typeparamsr0   r   r   r   parse_content_dispositionN   s
    
r<   )r   r   r   c                 C   s   |j }| jd}|r t||}t|d }|sPt| jdd}|rP||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 )
r0   r   r9   r<   r   	mimetypesguess_extensionr+   r1   r2   )r   r   r0   r5   extr   r   r   _get_http_response_filename\   s    
rB   )sessionr   r   c                 C   s.   |j ddd }| j|tdd}t| |S )N#r=   r   T)r   stream)r+   splitr9   r   r   )rC   r   
target_urlr   r   r   r   _http_get_downloadq   s    rH   c                   @   s8   e Zd ZeeddddZeeeeef dddZdS )
DownloaderNrC   r   r   c                 C   s   || _ || _d S N_session_progress_barselfrC   r   r   r   r   __init__y   s    zDownloader.__init__)r   locationr   c              
   C   s   zt | j|}W nF tyV } z.|jdus.J td|jj|  W Y d}~n
d}~0 0 t||}tj	
||}t||| j}t|d$}|D ]}	||	 qW d   n1 s0    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>   rH   rM   r   responser%   criticalstatus_coderB   r1   r2   joinr/   rN   openwriter   r9   )rP   r   rR   r   er0   filepathr.   content_filechunkcontent_typer   r   r   __call__   s     

*zDownloader.__call__)	__name__
__module____qualname__r   strrQ   r
   r   rb   r   r   r   r   rI   x   s
   rI   c                   @   sH   e Zd ZeeddddZee eeeeeeef f  dddZ	dS )BatchDownloaderNrJ   c                 C   s   || _ || _d S rK   rL   rO   r   r   r   rQ      s    zBatchDownloader.__init__)linksrR   r   c                 c   s   |D ]}zt | j|}W nF ty^ } z.|jdus6J td|jj|  W Y d}~n
d}~0 0 t||}tj	
||}t||| j}t|d$}	|D ]}
|	|
 qW d   n1 s0    Y  |jdd}|||ffV  qdS )z0Download the files given by links into location.NrS   rT   rU   r>   rV   )rP   rh   rR   r   r   r]   r0   r^   r.   r_   r`   ra   r   r   r   rb      s&    
*zBatchDownloader.__call__)
rc   rd   re   r   rf   rQ   r   r
   r   rb   r   r   r   r   rg      s   	rg   ).__doc__r7   r(   r?   r1   typingr   r   r   Z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   	getLoggerrc   r%   r   r   rf   bytesr/   r4   r<   rB   rH   rI   rg   r   r   r   r   <module>   s6   
)