B
    He                 @   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   s.   yt | jd S  tttfk
r(   d S X d S )Nzcontent-length)intheaders
ValueErrorKeyError	TypeError)r    r   r/var/www/downstreamdata.science/rtclock/rtclock-venv/lib/python3.7/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   s4   t j }| |d< |d}|r,tt|}|p2|S )z
    Parse the "filename" value from a Content-Disposition header, and
    return the default filename if the result is empty.
    zcontent-typer2   )emailmessageMessage	get_paramr6   str)r7   r8   mr2   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   getr?   r   	mimetypesguess_extensionr,   r3   r4   )r   r   r2   r7   extr   r   r   _get_http_response_filename^   s    
rF   )sessionr   r   c             C   s.   |j ddd }| j|tdd}t| |S )N#r@   r   T)r   stream)r,   splitrB   r   r   )rG   r   
target_urlr   r   r   r   _http_get_downloads   s    rL   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)rG   r   r   c             C   s   || _ || _d S )N)_session_progress_bar)selfrG   r   r   r   r   __init__{   s    zDownloader.__init__)r   locationr   c          
   C   s   yt | j|}W nD tk
rT } z&|jdk	s0ttd|jj|  W dd}~X Y nX t||}t	j
||}t||| j}t|d}x|D ]}	||	 qW W dQ R X |jdd}
||
fS )z.Download the file given by link into location.NzHTTP error %s while getting %swbzContent-TyperA   )rL   rN   r   responseAssertionErrorr&   criticalstatus_coderF   r3   r4   joinr1   rO   openwriter   rB   )rP   r   rR   r   er2   filepathr/   content_filechunkcontent_typer   r   r   __call__   s    

zDownloader.__call__)	__name__
__module____qualname__r   r=   rQ   r
   r   r`   r   r   r   r   rM   z   s   rM   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 )BatchDownloaderN)rG   r   r   c             C   s   || _ || _d S )N)rN   rO   )rP   rG   r   r   r   r   rQ      s    zBatchDownloader.__init__)linksrR   r   c             c   s   x|D ]}yt | j|}W nD tk
r^ } z&|jdk	s:ttd|jj|  W dd}~X Y nX t||}t	j
||}t||| j}t|d}	x|D ]}
|	|
 qW W dQ R X |jdd}|||ffV  qW dS )z0Download the files given by links into location.NzHTTP error %s while getting %srS   zContent-TyperA   )rL   rN   r   rT   rU   r&   rV   rW   rF   r3   r4   rX   r1   rO   rY   rZ   r   rB   )rP   re   rR   r   r   r[   r2   r\   r/   r]   r^   r_   r   r   r   r`      s$    


zBatchDownloader.__call__)
ra   rb   rc   r   r=   rQ   r   r
   r   r`   r   r   r   r   rd      s
   rd   )/__doc__email.messager9   r)   rC   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   	getLoggerra   r&   r   r   r=   bytesr1   r6   r?   rF   rL   rM   rd   r   r   r   r   <module>   s4   
&