U
    +|jc  ã                   @   s  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d„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'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Úsplitextc              
   C   s2   zt | jd ƒW S  tttfk
r,   Y d S X d S )Nzcontent-length)ÚintÚheadersÚ
ValueErrorÚKeyErrorÚ	TypeError)Úresp© r   úfC:\Users\snmko\Desktop\web_content\django_cbt\venv\Lib\site-packages\pip/_internal/network/download.pyÚ_get_http_response_size   s    r   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	| ƒr‚d}n|sŒd}n|dkršd}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   ÚlinkÚprogress_barÚtotal_lengthÚurlÚ
logged_urlÚshow_progressÚchunksr   r   r   Ú_prepare_download   s8    
 ÿþr.   c                 C   s   t j | ¡S )zJ
    Sanitize the "filename" value from a Content-Disposition header.
    )ÚosÚpathÚbasename)Úfilenamer   r   r   Úsanitize_content_filenameK   s    r3   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Úgetr3   )Úcontent_dispositionÚdefault_filenameÚ_typeÚparamsr2   r   r   r   Úparse_content_dispositionS   s
    
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   r6   r;   r   Ú	mimetypesÚguess_extensionr*   r/   r0   )r   r'   r2   r7   Úextr   r   r   Ú_get_http_response_filenameb   s     
ÿrA   c                 C   s.   |j  dd¡d }| j|tdd�}t|ƒ |S )Nú#r<   r   T)r   Ústream)r*   Úsplitr6   r   r   )Úsessionr'   Ú
target_urlr   r   r   r   Ú_http_get_downloadz   s    rG   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
Downloaderc                 C   s   || _ || _d S ©N©Ú_sessionÚ_progress_bar©ÚselfrE   r(   r   r   r   Ú__init__ƒ   s    zDownloader.__init__c              
   C   s¼   zt | j|ƒ}W nD tk
rT } z&|jdk	s0t‚t d|jj|¡ ‚ W 5 d}~X Y nX t||ƒ}t	j
 ||¡}t||| jƒ}t|dƒ�}|D ]}	| |	¡ qŒW 5 Q R X |j dd¡}
||
fS )z.Download the file given by link into location.NúHTTP error %s while getting %sÚwbúContent-Typer=   ©rG   rK   r   ÚresponseÚAssertionErrorr"   ÚcriticalÚstatus_coderA   r/   r0   Újoinr.   rL   ÚopenÚwriter   r6   )rN   r'   Úlocationr   Úer2   Úfilepathr-   Úcontent_fileÚchunkÚcontent_typer   r   r   Ú__call__Œ   s$      ÿ
zDownloader.__call__N©Ú__name__Ú
__module__Ú__qualname__rO   ra   r   r   r   r   rH   ‚   s   	rH   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBatchDownloaderc                 C   s   || _ || _d S rI   rJ   rM   r   r   r   rO   ¥   s    zBatchDownloader.__init__c                 c   sÐ   |D ]Æ}zt | j|ƒ}W nD tk
r\ } z&|jdk	s8t‚t d|jj|¡ ‚ W 5 d}~X Y nX t||ƒ}t	j
 ||¡}t||| jƒ}t|dƒ�}	|D ]}
|	 |
¡ q”W 5 Q R X |j dd¡}|||ffV  qdS )z0Download the files given by links into location.NrP   rQ   rR   r=   rS   )rN   Úlinksr[   r'   r   r\   r2   r]   r-   r^   r_   r`   r   r   r   ra   ®   s&     þ
zBatchDownloader.__call__Nrb   r   r   r   r   rf   £   s   	rf   )+Ú__doc__r4   r%   r>   r/   Ú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.   r3   r;   rA   rG   rH   rf   r   r   r   r   Ú<module>   s.   
,!