a
    ˆÀàg  ã                   @   s.  d Z ddlZddlZddlZddl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 dd
lmZ erÐddlmZmZmZ ddlmZ ddlmZ ddl 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e+ƒZ,G dd„ de+ƒZ-dS )z)Download files with progress indicators.
é    N)ÚCONTENT_CHUNK_SIZE)ÚDownloadProgressProvider)ÚNetworkConnectionError)ÚPyPI)Úis_from_cache)ÚHEADERSÚraise_for_statusÚresponse_chunks)Úformat_sizeÚredact_auth_from_urlÚsplitext)ÚMYPY_CHECK_RUNNING)ÚIterableÚOptionalÚTuple)ÚResponse)ÚLink)Ú
PipSessionc              
   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)Úresp© r   úW/var/www/sistema_ama/venv/lib/python3.9/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ÚurlZ
logged_urlZshow_progressÚchunksr   r   r   Ú_prepare_download%   s6    
ÿþ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_filenameQ   s    r2   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.
    r1   )ÚcgiÚparse_headerÚgetr2   )Úcontent_dispositionZdefault_filenameÚ_typeÚparamsr1   r   r   r   Úparse_content_dispositionY   s
    
r9   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Ú )
r1   r   r5   r9   r   Ú	mimetypesÚguess_extensionr+   r.   r/   )r   r(   r1   r6   Úextr   r   r   Ú_get_http_response_filenameh   s     
ÿr?   c                 C   s.   |j  dd¡d }| j|tdd�}t|ƒ |S )Nú#r:   r   T)r   Ústream)r+   Úsplitr5   r   r   )Úsessionr(   Z
target_urlr   r   r   r   Ú_http_get_download€   s    rD   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
Downloaderc                 C   s   || _ || _d S ©N©Ú_sessionÚ_progress_bar©ÚselfrC   r)   r   r   r   Ú__init__‰   s    zDownloader.__init__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 ]}	| |	¡ qŽW d  ƒ n1 s²0    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;   )rD   rH   r   Úresponser#   ÚcriticalÚstatus_coder?   r.   r/   Újoinr-   rI   ÚopenÚwriter   r5   )rK   r(   Úlocationr   Úer1   Úfilepathr,   Úcontent_fileÚchunkÚcontent_typer   r   r   Ú__call__’   s     
ÿ
*zDownloader.__call__N©Ú__name__Ú
__module__Ú__qualname__rL   r\   r   r   r   r   rE   ˆ   s   	rE   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBatchDownloaderc                 C   s   || _ || _d S rF   rG   rJ   r   r   r   rL   «   s    zBatchDownloader.__init__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 ]}
|	 |
¡ q–W d  ƒ n1 sº0    Y  |j dd¡}|j||ffV  qdS )z0Download the files given by links into location.NrM   rN   rO   r;   )rD   rH   r   rP   r#   rQ   rR   r?   r.   r/   rS   r-   rI   rT   rU   r   r5   r+   )rK   ÚlinksrV   r(   r   rW   r1   rX   r,   rY   rZ   r[   r   r   r   r\   ´   s$    þ
*zBatchDownloader.__call__Nr]   r   r   r   r   ra   ©   s   	ra   ).Ú__doc__r3   r&   r<   r.   Úpip._vendor.requests.modelsr   Úpip._internal.cli.progress_barsr   Úpip._internal.exceptionsr   Úpip._internal.models.indexr   Úpip._internal.network.cacher   Úpip._internal.network.utilsr   r   r	   Úpip._internal.utils.miscr
   r   r   Úpip._internal.utils.typingr   Útypingr   r   r   r   Úpip._internal.models.linkr   Úpip._internal.network.sessionr   Ú	getLoggerr^   r#   r   r-   r2   r9   r?   rD   ÚobjectrE   ra   r   r   r   r   Ú<module>   s4   
,!