a
    [f                     @   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)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   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   pC:\Users\91910\OneDrive\Desktop\vts_admin_rebuild-master\vts\Lib\site-packages\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@  )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   )filenamer   c                 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_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.
    r2   )cgiparse_headergetr6   )r7   r8   _typeparamsr2   r   r   r   parse_content_dispositionO   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 )
r2   r   r;   r>   r   	mimetypesguess_extensionr,   r3   r4   )r   r   r2   r7   extr   r   r   _get_http_response_filename]   s    
rD   )sessionr   r   c                 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                   @   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rE   r   r   c                 C   s   || _ || _d S N_session_progress_barselfrE   r   r   r   r   __init__z   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@   rJ   rO   r   responser&   criticalstatus_coderD   r3   r4   joinr1   rP   openwriter   r;   )rR   r   rT   r   er2   filepathr/   content_filechunkcontent_typer   r   r   __call__   s     

*zDownloader.__call__)	__name__
__module____qualname__r   strrS   r
   r   rd   r   r   r   r   rK   y   s
   rK   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 )BatchDownloaderNrL   c                 C   s   || _ || _d S rM   rN   rQ   r   r   r   rS      s    zBatchDownloader.__init__)linksrT   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.NrU   rV   rW   r@   rX   )rR   rj   rT   r   r   r_   r2   r`   r/   ra   rb   rc   r   r   r   rd      s&    
*zBatchDownloader.__call__)
re   rf   rg   r   rh   rS   r   r
   r   rd   r   r   r   r   ri      s   	ri   ).__doc__r9   r)   rA   r3   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   	getLoggerre   r&   r   r   rh   bytesr1   r6   r>   rD   rJ   rK   ri   r   r   r   r   <module>   s6   
*