a
    áñhã&  ã                	   @   sX  d dl Z d dl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	m
Z
mZmZ d dlZd dlZd dlmZ eeeegdf dœdd„ZdBeeedœd	d
„Zeeeedœdd„ZdCeee edœdd„ZdDeeee ee ddœdd„ZdEeee	e dœdd„ZdFeeee	e dœdd„Zdedœdd„ZdGeeee ee dœd d!„Zdee dœd"d#„ZdHdeedd%œd&d'„Zeed(œd)d*„Zeed(œd+d,„Z eed(œd-d.„Z!eed(œd/d0„Z"eed(œd1d2„Z#eed(œd3d4„Z$dIeee edd5œd6d7„Z%dJeeee ee ee edd8œd9d:„Z&e
ed;œd<d=„Z'ed>ee(ƒZ)dKe)ee e
e) ee e)d?œd@dA„Z*dS )Lé    N)ÚAnyÚCallableÚListÚIterableÚOptionalÚTypeVar©Útqdm)Úreturnc                     s   t d d�‰ ‡ fdd„} | S )N©Útotalc                    s0   ˆ j d u r|r|ˆ _ | | }ˆ  |ˆ j ¡ d S ©N)r   ÚupdateÚn)ÚcountÚ
block_sizeZ
total_sizeZprogress_bytes©Úpbar© úZ/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/datasets/utils.pyÚ
bar_update   s    z#gen_bar_updater.<locals>.bar_updater   )r   r   r   r   Úgen_bar_updater   s    
r   é   )ÚfpathÚ
chunk_sizer
   c                    s^   t  ¡ }t| dƒ�4‰t‡ ‡fdd„dƒD ]}| |¡ q(W d   ƒ n1 sL0    Y  | ¡ S )NÚrbc                      s
   ˆ  ˆ ¡S r   )Úreadr   ©r   Úfr   r   Ú<lambda>   ó    zcalculate_md5.<locals>.<lambda>r    )ÚhashlibÚmd5ÚopenÚiterr   Ú	hexdigest)r   r   r"   Úchunkr   r   r   Úcalculate_md5   s
    *r'   )r   r"   Úkwargsr
   c                 K   s   |t | fi |¤ŽkS r   )r'   )r   r"   r(   r   r   r   Ú	check_md5"   s    r)   )r   r"   r
   c                 C   s&   t j | ¡sdS |d u rdS t| |ƒS )NFT)ÚosÚpathÚisfiler)   )r   r"   r   r   r   Úcheck_integrity&   s
    r-   )ÚurlÚrootÚfilenamer"   r
   c              
   C   s  ddl }tj |¡}|s$tj | ¡}tj ||¡}tj|dd� t||ƒrXtd| ƒ n¸z,td|  d | ƒ |j	j
| |tƒ d� W nv |jjtfyú } zV| dd	… d
krâ|  dd¡} td|  d | ƒ |j	j
| |tƒ d� n|‚W Y d}~n
d}~0 0 t||ƒ�stdƒ‚dS )a\  Download a file from a url and place it in root.

    Args:
        url (str): URL to download file from
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the basename of the URL
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   NT©Úexist_okú$Using downloaded and verified file: zDownloading z to )Ú
reporthooké   Úhttpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading zFile not found or corrupted.)Úurllibr*   r+   Ú
expanduserÚbasenameÚjoinÚmakedirsr-   ÚprintÚrequestÚurlretriever   ÚerrorÚURLErrorÚIOErrorÚreplaceÚRuntimeError)r.   r/   r0   r"   r7   r   Úer   r   r   Údownload_url.   s>    	
þ
ÿÿÿþrE   F)r/   Úprefixr
   c                    sB   t j ˆ ¡‰ ‡ fdd„t  ˆ ¡D ƒ}|du r>‡ fdd„|D ƒ}|S )a  List all directories at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the directories found
    c                    s&   g | ]}t j t j ˆ |¡¡r|‘qS r   )r*   r+   Úisdirr:   ©Ú.0Úp©r/   r   r   Ú
<listcomp>c   r    zlist_dir.<locals>.<listcomp>Tc                    s   g | ]}t j ˆ |¡‘qS r   ©r*   r+   r:   ©rI   ÚdrK   r   r   rL   e   r    ©r*   r+   r8   Úlistdir)r/   rF   Údirectoriesr   rK   r   Úlist_dirZ   s
    rS   )r/   ÚsuffixrF   r
   c                    sD   t j ˆ ¡‰ ‡ ‡fdd„t  ˆ ¡D ƒ}|du r@‡ fdd„|D ƒ}|S )a¾  List all files ending with a suffix at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
            It uses the Python "str.endswith" method and is passed directly
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the files found
    c                    s0   g | ](}t j t j ˆ |¡¡r| ˆ¡r|‘qS r   )r*   r+   r,   r:   ÚendswithrH   ©r/   rT   r   r   rL   t   r    zlist_files.<locals>.<listcomp>Tc                    s   g | ]}t j ˆ |¡‘qS r   rM   rN   rK   r   r   rL   v   r    rP   )r/   rT   rF   Úfilesr   rV   r   Ú
list_filesi   s
    
rX   zrequests.models.Response)Úresponser
   c                 C   s
   d| j v S )NzGoogle Drive - Quota exceeded)Útext)rY   r   r   r   Ú_quota_exceededz   s    r[   )Úfile_idr/   r0   r"   c                 C   sÌ   ddl }d}tj |¡}|s | }tj ||¡}tj|dd� tj |¡r`t||ƒr`td| ƒ nh| 	¡ }|j
|d| idd�}t|ƒ}	|	r¢| |	d	œ}
|j
||
dd�}t|ƒr¾d
|› d�}t|ƒ‚t||ƒ dS )af  Download a Google Drive file from  and place it in root.

    Args:
        file_id (str): id of file to be downloaded
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the id of the file.
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   Nz*https://docs.google.com/uc?export=downloadTr1   r3   Úid)ÚparamsÚstream)r]   ZconfirmzThe daily quota of the file z} is exceeded and it can't be downloaded. This is a limitation of Google Drive and can only be overcome by trying again later.)Úrequestsr*   r+   r8   r:   r;   r,   r-   r<   ÚSessionÚgetÚ_get_confirm_tokenr[   rC   Ú_save_response_content)r\   r/   r0   r"   r`   r.   r   ÚsessionrY   Útokenr^   Úmsgr   r   r   Údownload_file_from_google_drive~   s(    


ÿrh   c                 C   s*   | j  ¡ D ]\}}| d¡r
|  S q
d S )NZdownload_warning)ÚcookiesÚitemsÚ
startswith)rY   ÚkeyÚvaluer   r   r   rc   ©   s    

rc   é €  )rY   Údestinationr   r
   c                 C   s~   t |dƒ�`}td d�}d}|  |¡D ].}|r$| |¡ |t|ƒ7 }| ||j ¡ q$| ¡  W d   ƒ n1 sp0    Y  d S )NÚwbr   r   )r#   r	   Úiter_contentÚwriteÚlenr   r   Úclose)rY   ro   r   r   r   Úprogressr&   r   r   r   rd   ±   s    

rd   )r0   r
   c                 C   s
   |   d¡S )Nz.tar.xz©rU   ©r0   r   r   r   Ú	_is_tarxz¿   s    rx   c                 C   s
   |   d¡S )Nz.tarrv   rw   r   r   r   Ú_is_tarÃ   s    ry   c                 C   s
   |   d¡S )Nú.tar.gzrv   rw   r   r   r   Ú	_is_targzÇ   s    r{   c                 C   s
   |   d¡S )Nz.tgzrv   rw   r   r   r   Ú_is_tgzË   s    r|   c                 C   s   |   d¡o|   d¡ S )Nz.gzrz   rv   rw   r   r   r   Ú_is_gzipÏ   s    r}   c                 C   s
   |   d¡S )Nz.ziprv   rw   r   r   r   Ú_is_zipÓ   s    r~   )Ú	from_pathÚto_pathÚremove_finishedr
   c              	   C   sà  |d u rt j | ¡}t| ƒrXt | d¡�}|j|d� W d   ƒ n1 sJ0    Y  �ntt| ƒsht| ƒr¤t | d¡�}|j|d� W d   ƒ n1 s–0    Y  �n(t	| ƒræt | d¡�}|j|d� W d   ƒ n1 sÚ0    Y  næt
| ƒ�rzt j |t j t j | ¡¡d ¡}t|dƒ�J}t | ¡�}| | ¡ ¡ W d   ƒ n1 �sN0    Y  W d   ƒ n1 �sn0    Y  nRt| ƒ�r¾t | d¡�}| |¡ W d   ƒ n1 �s²0    Y  ntd | ¡ƒ‚|�rÜt  | ¡ d S )NÚr)r+   zr:gzzr:xzr   rp   zExtraction of {} not supported)r*   r+   Údirnamery   Útarfiler#   Ú
extractallr{   r|   rx   r}   r:   Úsplitextr9   ÚgzipÚGzipFilerr   r   r~   ÚzipfileÚZipFileÚ
ValueErrorÚformatÚremove)r   r€   r�   ÚtarZout_fZzip_fÚzr   r   r   Úextract_archive×   s*    ..,
"P
,r�   )r.   Údownload_rootÚextract_rootr0   r"   r�   r
   c                 C   sd   t j |¡}|d u r|}|s(t j | ¡}t| |||ƒ t j ||¡}td ||¡ƒ t|||ƒ d S )NzExtracting {} to {})	r*   r+   r8   r9   rE   r:   r<   rŒ   r�   )r.   r‘   r’   r0   r"   r�   Úarchiver   r   r   Údownload_and_extract_archiveò   s    r”   )Úiterabler
   c                 C   s   dd  dd„ | D ƒ¡ d S )Nú'z', 'c                 S   s   g | ]}t |ƒ‘qS r   )Ústr)rI   Úitemr   r   r   rL     r    z#iterable_to_str.<locals>.<listcomp>)r:   )r•   r   r   r   Úiterable_to_str  s    r™   ÚT)rm   ÚargÚvalid_valuesÚ
custom_msgr
   c                 C   s€   t | tjjƒs:|d u rd}nd}|jt| ƒ|d�}t|ƒ‚|d u rF| S | |vr||d ur\|}nd}|j| |t|ƒd�}t|ƒ‚| S )Nz'Expected type str, but got type {type}.z:Expected type str for argument {arg}, but got type {type}.)Útyper›   zPUnknown value '{value}' for argument {arg}. Valid values are {{{valid_values}}}.)rm   r›   rœ   )Ú
isinstanceÚtorchZ_sixZstring_classesrŒ   rž   r‹   r™   )rm   r›   rœ   r�   rg   r   r   r   Úverify_str_arg  s"    ÿr¡   )r   )N)NN)F)F)NN)rn   )NF)NNNF)NNN)+r*   Úos.pathr!   r‡   Úerrnor„   Útypingr   r   r   r   r   r   r‰   r    Ztorch.utils.model_zoor	   Úintr   r—   r'   Úboolr)   r-   rE   rS   rX   r[   rh   rc   rd   rx   ry   r{   r|   r}   r~   r�   r”   r™   Úbytesrš   r¡   r   r   r   r   Ú<module>   sf     ,+	 ÿþ    úù ÿþ