o
    ôjÈ?  ã                   @   s  U 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Z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Zd dlmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlmZ ddl m!Z!m"Z" dZ#d\d	e$d
ee$ej%f de&ddfdd„Z'd]dee$ej%f de&de$fdd„Z(dee$ej%f de$dede)fdd„Z*d^dee$ej%f dee$ de)fdd„Z+d_d	e$de&de$fdd„Z,d	e$dee$ fdd„Z-			d`d	e$dee$ej%f d
eee$ej%f  dee$ d e&ddfd!d"„Z.dadee$ej%f d$e)dee$ fd%d&„Z/dadee$ej%f d'e$d$e)dee$ fd(d)„Z0		dbd*e$dee$ej%f d
eee$ej%f  dee$ fd+d,„Z1d-ee$ej%f d.ee$ej%f d/ee$ ddfd0d1„Z2ej3ej4d2œZ5ee$e&f e6d3< d-ee$ej%f d.ee$ej%f d/ee$ ddfd4d5„Z7e2e7d6œZ8ee$eee$ej%f ee$ej%f ee$ gdf f e6d7< e j9ej9ej9d8œZ:ee$ed9ef f e6d:< d;d;d<d=œZ;ee$eee$ ee$ f f e6d>< d?ee$ej%f dee$ee$ ee$ f fd@dA„Z<		#dcd-ee$ej%f d.eee$ej%f  dBe)dej%fdCdD„Z=		#dcd-ee$ej%f d.eee$ej%f  dBe)dee$ej%f fdEdF„Z>				#ddd	e$dGee$ej%f dHeee$ej%f  d
eee$ej%f  dee$ dBe)ddfdIdJ„Z?dKede$fdLdM„Z@edNe$eAƒZB			dedOeBdPee$ dQeeeB  dRee$ deBf
dSdT„ZCdfdUee$ej%f dVe&dejDfdWdX„ZEdYejFdejFfdZd[„ZGdS )gé    N)
ÚAnyÚCallableÚDictÚIOÚIterableÚListÚOptionalÚTupleÚTypeVarÚUnion)Úurlparse)Útqdmé   )Ú#_download_file_from_remote_locationÚ_is_remote_location_availablezpytorch/visioné €  ÚurlÚfilenameÚ
chunk_sizeÚreturnc              
   C   sÜ   t j t jj| dtid�¡�V}t|dƒ�8}t|jd��"}| |¡ }r6| 	|¡ | 
t|ƒ¡ | |¡ }s#W d   ƒ n1 s@w   Y  W d   ƒ n1 sOw   Y  W d   ƒ d S W d   ƒ d S 1 sgw   Y  d S )Nú
User-Agent©ÚheadersÚwb)Útotal)ÚurllibÚrequestÚurlopenÚRequestÚ
USER_AGENTÚopenr   ÚlengthÚreadÚwriteÚupdateÚlen)r   r   r   ÚresponseÚfhÚpbarÚchunk© r*   úk/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/torchvision/datasets/utils.pyÚ_urlretrieve   s   
þ€ÿ€ ÿ"ÿr,   é   Úfpathc                 C   s’   t jdkrtjdd�}nt ¡ }t| dƒ�(}| |¡ }r3| |¡ | |¡ }sW d   ƒ | ¡ S W d   ƒ | ¡ S 1 s@w   Y  | ¡ S )N)é   é	   F)ÚusedforsecurityÚrb)ÚsysÚversion_infoÚhashlibÚmd5r    r"   r$   Ú	hexdigest)r.   r   r6   Úfr)   r*   r*   r+   Úcalculate_md5#   s   

ÿ
ÿþ
ÿýr9   r6   Úkwargsc                 K   s   |t | fi |¤ŽkS ©N)r9   )r.   r6   r:   r*   r*   r+   Ú	check_md51   s   r<   c                 C   s&   t j | ¡sdS |d u rdS t| |ƒS )NFT)ÚosÚpathÚisfiler<   )r.   r6   r*   r*   r+   Úcheck_integrity5   s
   
r@   r/   Úmax_hopsc              	   C   s¤   | }dt dœ}t|d ƒD ]6}tj tjj| |d�¡� }|j| ks&|jd u r1| W  d   ƒ   S |j} W d   ƒ n1 s>w   Y  qtd|› d|› d| › d�ƒ‚)	NÚHEAD)ÚMethodr   é   r   zRequest to z
 exceeded z( redirects. The last redirect points to Ú.)r   Úranger   r   r   r   r   ÚRecursionError)r   rA   Zinitial_urlr   Ú_r&   r*   r*   r+   Ú_get_redirect_url=   s   
þü€ÿrI   c                 C   sB   t | ƒ}t d|j¡d u rd S t d|j¡}|d u rd S | d¡S )Nz(drive|docs)[.]google[.]comz/file/d/(?P<id>[^/]*)Úid)r   ÚreÚmatchÚnetlocr>   Úgroup)r   ÚpartsrL   r*   r*   r+   Ú_get_google_drive_file_idM   s   
rP   ÚrootÚmax_redirect_hopsc              
   C   s:  t j |¡}|st j | ¡}t  t j ||¡¡}t j|dd� t||ƒr,td| ƒ dS t	ƒ r5t
|| ƒ n]t| |d�} t| ƒ}|durJt||||ƒS ztd|  d | ƒ t| |ƒ W n6 tjjtfy‘ } z&| dd… d	kr…|  d
d¡} td|  d | ƒ t| |ƒ n|‚W Y d}~nd}~ww 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
        max_redirect_hops (int, optional): Maximum number of redirect hops allowed
    T©Úexist_okz$Using downloaded and verified file: N)rA   zDownloading ú to é   Úhttpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading úFile not found or corrupted.)r=   r>   Ú
expanduserÚbasenameÚfspathÚjoinÚmakedirsr@   Úprintr   r   rI   rP   Údownload_file_from_google_driver,   r   ÚerrorÚURLErrorÚOSErrorÚreplaceÚRuntimeError)r   rQ   r   r6   rR   r.   Úfile_idÚer*   r*   r+   Údownload_urlZ   s:   
þ€ü
	ÿrg   FÚprefixc                    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©rQ   r*   r+   Ú
<listcomp>›   s   & zlist_dir.<locals>.<listcomp>Tc                    ó   g | ]	}t j ˆ |¡‘qS r*   ©r=   r>   r\   ©rk   Údrm   r*   r+   rn   �   ó    ©r=   r>   rY   Úlistdir)rQ   rh   Údirectoriesr*   rm   r+   Úlist_dir’   s
   rw   Úsuffixc                    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\   Úendswithrj   ©rQ   rx   r*   r+   rn   ¬   s   0 zlist_files.<locals>.<listcomp>Tc                    ro   r*   rp   rq   rm   r*   r+   rn   ®   rs   rt   )rQ   rx   rh   Úfilesr*   rz   r+   Ú
list_files¡   s
   
r|   re   c                 C   s¬   zddl }W n ty   tdƒ‚w tj |¡}|s| }t tj ||¡¡}tj|dd� t	||ƒrBt
d|r8dnd› d	|› �ƒ dS |j| |d
td� t	||ƒsTtdƒ‚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`To download files from GDrive, 'gdown' is required. You can install it with 'pip install gdown'.TrS   zUsing downloaded zand verified Ú zfile: F)rJ   ÚoutputÚquietÚ
user_agentrX   )ÚgdownÚModuleNotFoundErrorrd   r=   r>   rY   r[   r\   r]   r@   r^   Údownloadr   )re   rQ   r   r6   r�   r.   r*   r*   r+   r_   ²   s&   ÿÿ

ÿr_   Ú	from_pathÚto_pathÚcompressionc                 C   sR   t  | |rd|dd … › �nd¡�}| |¡ W d   ƒ d S 1 s"w   Y  d S )Nzr:rD   Úr)Útarfiler    Ú
extractall)r„   r…   r†   Útarr*   r*   r+   Ú_extract_tarØ   s   $"ÿr‹   )ú.bz2ú.xzÚ_ZIP_COMPRESSION_MAPc                 C   sN   t j| d|r
t| nt jd��}| |¡ W d   ƒ d S 1 s w   Y  d S )Nr‡   )r†   )ÚzipfileÚZipFilerŽ   Ú
ZIP_STOREDr‰   )r„   r…   r†   Úzipr*   r*   r+   Ú_extract_zipå   s   ÿ"ýr“   )ú.tarz.zipÚ_ARCHIVE_EXTRACTORS)rŒ   ú.gzr�   .Ú_COMPRESSED_FILE_OPENERS)r”   rŒ   )r”   r–   )z.tbzz.tbz2z.tgzÚ_FILE_TYPE_ALIASESÚfilec                 C   sÀ   t  | ¡j}|std| › d�ƒ‚|d }|tv r |gt| ¢R S |tv r)||dfS |tv rGt|ƒdkrB|d }|tv rB|| ||fS |d|fS tt	tƒt	tƒB t	tƒB ƒ}td|› d|› d	�ƒ‚)
a  Detect the archive type and/or compression of a file.

    Args:
        file (str): the filename

    Returns:
        (tuple): tuple of suffix, archive type, and compression

    Raises:
        RuntimeError: if file has no suffix or suffix is not supported
    zFile 'zP' has no suffixes that could be used to detect the archive type and compression.éÿÿÿÿNrD   éþÿÿÿz&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
ÚpathlibÚPathÚsuffixesrd   r˜   r•   r—   r%   ÚsortedÚset)r™   rž   rx   Zsuffix2Zvalid_suffixesr*   r*   r+   Ú_detect_file_typeþ   s$   
ÿ

r¡   Úremove_finishedc           	   	   C   sÐ   t | ƒ\}}}|std|› d�ƒ‚|du r't t | ¡ ||dur#|nd¡¡}t| }|| dƒ�$}t|dƒ�}| 	| 
¡ ¡ W d  ƒ n1 sHw   Y  W d  ƒ n1 sWw   Y  |rct | ¡ t |¡S )a©  Decompress a file.

    The compression is automatically detected from the file name.

    Args:
        from_path (str): Path to the file to be decompressed.
        to_path (str): Path to the decompressed file. If omitted, ``from_path`` without compression extension is used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the decompressed file.
    z*Couldn't detect a compression from suffix rE   Nr}   r2   r   )r¡   rd   rœ   r�   r=   r[   rc   r—   r    r#   r"   Úremove)	r„   r…   r¢   rx   Úarchive_typer†   Zcompressed_file_openerZrfhZwfhr*   r*   r+   Ú_decompress)  s   $ÿ€ 

r¥   c           	   	      s¨   dt jdttt jf f‡ fdd„}|du rtj ˆ ¡}tˆ ƒ\}}}|s<tˆ tj 	|tj 
ˆ ¡ |d¡¡|d�}||ƒS t| }|ˆ ||ƒ |rMt ˆ ¡ |t  |¡ƒS )aD  Extract an archive.

    The archive type and a possible compression is automatically detected from the file name. If the file is compressed
    but not an archive the call is dispatched to :func:`decompress`.

    Args:
        from_path (str): Path to the file to be extracted.
        to_path (str): Path to the directory the file will be extracted to. If omitted, the directory of the file is
            used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the directory the file was extracted to.
    Úret_pathr   c                    s   t ˆ tƒr
t | ¡S | S r;   )Ú
isinstanceÚstrr=   r[   )r¦   ©r„   r*   r+   Úpath_or_stra  s   

z$extract_archive.<locals>.path_or_strNr}   )r¢   )rœ   r�   r   r¨   r=   r>   Údirnamer¡   r¥   r\   rZ   rc   r•   r£   )	r„   r…   r¢   rª   rx   r¤   r†   r¦   Ú	extractorr*   r©   r+   Úextract_archiveM  s    "ý
r­   Údownload_rootÚextract_rootc                 C   sh   t j |¡}|d u r|}|st j | ¡}t| |||ƒ t j ||¡}td|› d|› �ƒ t|||ƒ d S )NzExtracting rU   )r=   r>   rY   rZ   rg   r\   r^   r­   )r   r®   r¯   r   r6   r¢   Úarchiver*   r*   r+   Údownload_and_extract_archive}  s   r±   Úiterablec                 C   s   dd  dd„ | D ƒ¡ d S )Nú'z', 'c                 S   s   g | ]}t |ƒ‘qS r*   )r¨   )rk   Úitemr*   r*   r+   rn   “  s    z#iterable_to_str.<locals>.<listcomp>)r\   )r²   r*   r*   r+   Úiterable_to_str’  s   rµ   ÚTÚvalueÚargÚvalid_valuesÚ
custom_msgc                 C   s‚   t | tƒs|d u rd}nd}|jt| ƒ|d�}t|ƒ‚|d u r!| S | |vr?|d ur/|}t|ƒ‚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}}}.)r·   r¸   r¹   )r§   r¨   Úformatr»   Ú
ValueErrorrµ   )r·   r¸   r¹   rº   Úmsgr*   r*   r+   Úverify_str_arg™  s    
þr¿   Ú	file_nameÚslice_channelsc                 C   s  t | dƒ�L}| ¡  ¡ }|dvrtdƒ‚t d| ¡ ¡}|s"tdƒ‚dd„ | ¡ D ƒ\}}t| ¡  ¡ ƒ}|dk r?d	}| }nd
}t	j
||d d�}	W d  ƒ n1 sTw   Y  |dkr_dnd}
|	 |||
¡ ddd¡}	t	j|	dd�}	|	d|…dd…dd…f }	|	 t	j¡S )aD  Read file in .pfm format. Might contain either 1 or 3 channels of data.

    Args:
        file_name (str): Path to the file.
        slice_channels (int): Number of channels to slice out of the file.
            Useful for reading different data formats stored in .pfm files: Optical Flows, Stereo Disparity Maps, etc.
    r2   )ó   PFs   PfzInvalid PFM files   ^(\d+)\s(\d+)\s$zMalformed PFM header.c                 s   s   � | ]}t |ƒV  qd S r;   )Úint)rk   Údimr*   r*   r+   Ú	<genexpr>Æ  s   € z_read_pfm.<locals>.<genexpr>r   ú<ú>r8   )ÚdtypeNrÂ   r/   rD   r   )Úaxis)r    ÚreadlineÚrstripr½   rK   rL   Ú	ExceptionÚgroupsÚfloatÚnpÚfromfileÚreshapeÚ	transposeÚflipÚastypeÚfloat32)rÀ   rÁ   r8   ÚheaderÚ	dim_matchÚwÚhÚscaleÚendianÚdataZpfm_channelsr*   r*   r+   Ú	_read_pfmµ  s(   	ïrÝ   Útc                 C   sN   |   ¡  tj¡jg | j¢|  ¡ ‘R Ž  d¡jg | jd d… ¢d‘R Ž  | j¡S )Nrš   )Ú
contiguousÚviewÚtorchÚuint8ÚshapeÚelement_sizerÓ   rÈ   )rÞ   r*   r*   r+   Ú_flip_byte_orderÙ  s   Lÿrå   )r   )r-   r;   )r/   )NNr/   )F)NN)NF)NNNF)NNN)r   )HÚbz2Úgzipr5   Úlzmar=   Úos.pathrœ   rK   r3   rˆ   r   Úurllib.errorÚurllib.requestr�   Útypingr   r   r   r   r   r   r   r	   r
   r   Úurllib.parser   ÚnumpyrÏ   rá   Ztorch.utils.model_zoor   Ú_internally_replaced_utilsr   r   r   r¨   r�   rÃ   r,   r9   Úboolr<   r@   rI   rP   rg   rw   r|   r_   r‹   Ú	ZIP_BZIP2ÚZIP_LZMArŽ   Ú__annotations__r“   r•   r    r—   r˜   r¡   r¥   r­   r±   rµ   Úbytesr¶   r¿   ÚndarrayrÝ   ÚTensorrå   r*   r*   r*   r+   Ú<module>   s(  
 0&"$&ûÿþýüû
ú&8*üÿþý
ü&ÿÿÿ
þþÿÿÿ
þ
<þý&ý.-ýÿþý
ü&ýÿþý
ü3úÿþýüûú
ùüÿþ
ýü
û$$