
    khz>                        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#dDde$d	ee$ejJ                  f   d
e&ddfdZ'dEdee$ejJ                  f   d
e&de$fdZ(dee$ejJ                  f   de$dede)fdZ*dFdee$ejJ                  f   dee$   de)fdZ+dGde$de&de$fdZ,de$dee$   fdZ-	 	 	 dHde$dee$ejJ                  f   d	eee$ejJ                  f      dee$   de&ddfdZ.dIdee$ejJ                  f   de)dee$   fdZ/dIdee$ejJ                  f   de$de)dee$   fdZ0	 	 dJde$dee$ejJ                  f   d	eee$ejJ                  f      dee$   fdZ1dee$ejJ                  f   d ee$ejJ                  f   d!ee$   ddfd"Z2ejf                  ejh                  d#Z5ee$e&f   e6d$<   dee$ejJ                  f   d ee$ejJ                  f   d!ee$   ddfd%Z7e2e7d&Z8ee$eee$ejJ                  f   ee$ejJ                  f   ee$   gdf   f   e6d'<   e jr                  ejr                  ejr                  d(Z:ee$ed)ef   f   e6d*<   d+d+d,d-Z;ee$eee$   ee$   f   f   e6d.<   d/ee$ejJ                  f   dee$ee$   ee$   f   fd0Z<	 	 dKdee$ejJ                  f   d eee$ejJ                  f      d1e)dejJ                  fd2Z=	 	 dKdee$ejJ                  f   d eee$ejJ                  f      d1e)dee$ejJ                  f   fd3Z>	 	 	 	 dLde$d4ee$ejJ                  f   d5eee$ejJ                  f      d	eee$ejJ                  f      dee$   d1e)ddfd6Z?d7ede$fd8Z@ ed9e$eA      ZB	 	 	 dMd:eBd;ee$   d<eeeB      d=ee$   deBf
d>ZCdNd?ee$ejJ                  f   d@e&dej                  fdAZEdBej                  dej                  fdCZGy)O    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                    t         j                  j                  t         j                  j                  | dt        i            5 }t        |d      5 }t        |j                  dd      5 }|j                  |      x}r?|j                  |       |j                  t        |             |j                  |      x}r?d d d        d d d        d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)N
User-AgentheaderswbBT)totalunit
unit_scale)urllibrequesturlopenRequest
USER_AGENTopenr   lengthreadwriteupdatelen)r   r   r   responsefhpbarchunks          V/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/utils.py_urlretriever/      s    			 6 6s\S]D^ 6 _	` (dl(D! 	(RHOO#Z^)_ 	(cg#==44%4CJ' $==44%4	( 	(( (	( 	( 	( 	(( (s=   C/C#+AC>C#C/C C##C,	(C//C8fpathc                 n   t         j                  dk\  rt        j                  d      }nt        j                         }t	        | d      5 }|j                  |      x}r%|j                  |       |j                  |      x}r%d d d        |j                         S # 1 sw Y   |j                         S xY w)N)   	   F)usedforsecurityrb)sysversion_infohashlibmd5r$   r&   r(   	hexdigest)r0   r   r9   fr-   s        r.   calculate_md5r<   #   s     6!kk%0kkm	eT	 avvj))e)JJu vvj))e) ==? ==?s   8BB4r9   kwargsc                      |t        | fi |k(  S N)r<   )r0   r9   r=   s      r.   	check_md5r@   1   s    -0000    c                 `    t         j                  j                  |       sy|yt        | |      S )NFT)ospathisfiler@   )r0   r9   s     r.   check_integrityrF   5   s*    77>>% 
{UC  rA   max_hopsc           	      v   | }dt         d}t        |dz         D ]}  }t        j                  j	                  t        j                  j                  | |            5 }|j                  | k(  s|j                  | cd d d        c S |j                  } d d d         t        d| d| d|  d      # 1 sw Y   xY w)	NHEAD)Methodr      r   zRequest to z
 exceeded z( redirects. The last redirect points to .)r#   ranger   r    r!   r"   r   RecursionError)r   rG   initial_urlr   _r*   s         r.   _get_redirect_urlrQ   =   s    Kz:G8a<  	
^^##FNN$:$:3$:$PQ 	U]||s"hll&:	 	 ,,C		 		
 +j
:bcfbgghi
 	
	 	s   B/B//B8	c                     t        |       }t        j                  d|j                        y t        j                  d|j                        }|y |j                  d      S )Nz(drive|docs)[.]google[.]comz/file/d/(?P<id>[^/]*)id)r   rematchnetlocrD   group)r   partsrU   s      r.   _get_google_drive_file_idrY   M   sP    SME	xx.=EHH-uzz:E};;trA   rootmax_redirect_hopsc                    t         j                  j                  |      }|st         j                  j                  |       }t        j                  t         j                  j                  ||            }t        j                  |d       t        ||      ryt               rt        ||        n5t        | |      } t        |       }|t        ||||      S 	 t        | |       t        ||      st'        d	      y# t        j                  j                   t"        f$ r3}| dd dk(  r| j%                  dd      } t        | |       n|Y d}~id}~ww xY w)
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_okN)rG      httpszhttps:zhttp:File not found or corrupted.)rC   rD   
expanduserbasenamefspathjoinmakedirsrF   r   r   rQ   rY   download_file_from_google_driver/   r   errorURLErrorOSErrorreplaceRuntimeError)r   rZ   r   r9   r[   r0   file_ides           r.   download_urlro   Z   s*     77d#D77##C(IIbggll423EKKt$ uc"$&+E37  .?@ ,C027D(CPP	e$ 5#&9:: ' %%w/ 	2Aw'!kk(G4S%( )	s   C< <#E)EEprefixc           	      z   t         j                  j                  |       } t        j                  |       D cg c]B  }t         j                  j	                  t         j                  j                  | |            sA|D }}|du r-|D cg c]"  }t         j                  j                  | |      $ }}|S c c}w c c}w )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
    T)rC   rD   rb   listdirisdirre   )rZ   rp   pdirectoriesds        r.   list_dirrw      s     77d#D jj.W"''--TST@U2V1WKW~6ABrww||D!,BB XBs   AB3:B3'B8suffixc           	         t         j                  j                  |       } t        j                  |       D cg c]T  }t         j                  j	                  t         j                  j                  | |            sA|j                  |      sS|V }}|du r-|D cg c]"  }t         j                  j                  | |      $ }}|S c c}w c c}w )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
    T)rC   rD   rb   rr   rE   re   endswith)rZ   rx   rp   rt   filesrv   s         r.   
list_filesr|      s     77d#D

4(i1BGGNN277<<a;P,QVWV`V`agVhQiEi~0561dA&66L j6s   AC:CC'C
rm   c                    	 ddl }t        j                  j                  |      }|s| }t        j                  t        j                  j                  ||            }t        j                  |d       t        ||      ry|j                  | |dt               t        ||      st        d      y# t        $ r t        d      w xY w)	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'.Tr]   F)rS   outputquiet
user_agentra   )gdownModuleNotFoundErrorrl   rC   rD   rb   rd   re   rf   rF   downloadr#   )rm   rZ   r   r9   r   r0   s         r.   rg   rg      s    
 77d#DIIbggll423EKKt$uc"	NNge5ZNP5#&9:: '#  
n
 	

s   B1 1C	from_pathto_pathcompressionc                     t        j                  | |rd|dd   nd      5 }|j                  |       d d d        y # 1 sw Y   y xY w)Nzr:rK   r)tarfiler$   
extractall)r   r   r   tars       r.   _extract_tarr      sH     
i;2k!"o%6!7C	P  TWw     s	   <A).bz2.xz_ZIP_COMPRESSION_MAPc                     t        j                  | d|r	t        |   nt         j                        5 }|j	                  |       d d d        y # 1 sw Y   y xY w)Nr   )r   )zipfileZipFiler   
ZIP_STOREDr   )r   r   r   zips       r.   _extract_zipr      sL     
3$8$EZaZlZl
  	w     s   AA).tarz.zip_ARCHIVE_EXTRACTORS)r   .gzr   ._COMPRESSED_FILE_OPENERS)r   r   )r   r   )z.tbzz.tbz2z.tgz_FILE_TYPE_ALIASESfilec                    t        j                  |       j                  }|st        d|  d      |d   }|t        v r|gt        |   S |t
        v r||dfS |t        v r(t        |      dkD  r|d   }|t
        v r||z   ||fS |d|fS t        t        t              t        t
              z  t        t              z        }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.NrK   z&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
pathlibPathsuffixesrl   r   r   r   r)   sortedset)r   r   rx   suffix2valid_suffixess        r.   _detect_file_typer      s    ||D!**HTFjk
 	
 b\F ##4+F344 $$vt## ))x=1rlG --'&88tV##C 23c:M6NNQTUmQnnoN
?xG`ao`pprs
ttrA   remove_finishedc                    t        |       \  }}}|st        d| d      |<t        j                  t	        j
                  |       j                  |||nd            }t        |   } || d      5 }t        |d      5 }|j                  |j                                ddd       ddd       |rt	        j                  |        t        j                  |      S # 1 sw Y   =xY w# 1 sw Y   AxY w)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 rL   N r5   r   )r   rl   r   r   rC   rd   rk   r   r$   r'   r&   remove)	r   r   r   rx   archive_typer   compressed_file_openerrfhwfhs	            r.   _decompressr   %  s    " ):)(D%FL+GxqQRR,,ryy3;;FT`TlLrtuv 6kB		4	0 Cgt9L PS		#((*  
		)<<     s$   1C&> CC&C#	C&&C/c           
          dt         j                  dt        t        t         j                  f   f fd}|t        j
                  j                         }t               \  }}}|sat         t        j
                  j                  |t        j
                  j                         j                  |d            |      } ||      S t        |   } | ||       |rt	        j                           |t        j                  |            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                 R    t        t              rt        j                  |       S | S r?   )
isinstancestrrC   rd   )r   r   s    r.   path_or_strz$extract_archive.<locals>.path_or_str]  s!    i%99X&&OrA   r   )r   )r   r   r   r   rC   rD   dirnamer   r   re   rc   rk   r   r   )	r   r   r   r   rx   r   r   r   	extractors	   `        r.   extract_archiver   I  s    (gll uS',,5F/G  ''//),(9)(D%FL+GGLL"''"2"29"="E"Efb"QR+

 8$$ $L1Ii+.
		)w||G,--rA   download_rootextract_rootc                    t         j                  j                  |      }||}|st         j                  j                  |       }t	        | |||       t         j                  j                  ||      }t        |||       y r?   )rC   rD   rb   rc   ro   re   r   )r   r   r   r   r9   r   archives          r.   download_and_extract_archiver   y  sh     GG&&}5M$77##C(mXs3ggll=(3GG\?;rA   iterablec           	      h    ddj                  | D cg c]  }t        |       c}      z   dz   S c c}w )N'z', ')re   r   )r   items     r.   iterable_to_strr     s-    H=Dc$i=>>DD=s   /
Tvalueargvalid_values
custom_msgc                    t        | t              s.|d}nd}|j                  t        |       |      }t	        |      || S | |vr9||}t	        |      d}|j                  | |t        |            }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   msgs        r.   verify_str_argr     s     eS!;;CNCjjd5ksj3oL !C o eC**5cP\@]*^CoLrA   	file_nameslice_channelsc                    t        | d      5 }|j                         j                         }|dvrt        d      t	        j
                  d|j                               }|st        d      d |j                         D        \  }}t        |j                         j                               }|dk  rd}| }nd	}t        j                  ||d
z         }	ddd       dk(  rdnd}
	j                  |
      j                  ddd      }	t        j                  |	d      }	|	d|ddddf   }	|	j                  t        j                        S # 1 sw Y   zxY w)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.
    r5   )   PFs   PfzInvalid PFM files   ^(\d+)\s(\d+)\s$zMalformed PFM header.c              3   2   K   | ]  }t        |        y wr?   )int).0dims     r.   	<genexpr>z_read_pfm.<locals>.<genexpr>  s     7SC7s   r   <>r;   )dtypeNr   r2   rK   r   )axis)r$   readlinerstripr   rT   rU   	Exceptiongroupsfloatnpfromfilereshape	transposeflipastypefloat32)r   r   r;   header	dim_matchwhscaleendiandatapfm_channelss              r.   	_read_pfmr     s7    
i	 2!$$&'/00HH11::<@	3447I$4$4$671ajjl))+,19FFEF{{1FSL1#2& %1QL<<1l+55aA>D774a DA%&D;;rzz""12 2s   CEEtc                 >     | j                         j                  t        j                        j                  g | j                  | j                          j                  d      j                  g | j                  d d d j                  | j                        S )Nr   )
contiguousviewtorchuint8shapeelement_sizer   r   )r   s    r.   _flip_byte_orderr     s    W-EKK(--IqwwI8HINNrRWWjYZY`Y`adbdYejgijoopqpwpwxrA   )i   )i   r?   )r2   )NNr2   )F)NN)NF)NNNF)NNN)r   )Hbz2gzipr8   lzmarC   os.pathr   rT   r6   r   r   urllib.errorurllib.requestr   typingr   r   r   r   r   r   r	   r
   r   r   urllib.parser   numpyr   r   torch.utils.model_zoor   _internally_replaced_utilsr   r   r#   r   r   r   r/   r<   boolr@   rF   rQ   rY   ro   rw   r|   rg   r   	ZIP_BZIP2ZIP_LZMAr   __annotations__r   r   r$   r   r   r   r   r   r   r   bytesr   r   ndarrayr   Tensorr    rA   r.   <module>r     s   
    	   	 
      [ [ [ !   & k
(c (U3+<%= (3 (_c (sGLL01 s UX 1U3,- 1C 13 14 1!5gll!23 !(3- !SW !
3 
# 
c 
 
3 
8C= 
  482;	2;
W\\!
"2; uS',,./02; 
#	2;
 2; 
2;j5gll*+ T d3i U3,- s D UYZ]U^ ( 48	";";
W\\!
""; uS',,./0"; 
#	";J S',,&' 27W\\8I2J YabeYf 	  ( d38n  S',,&' 27W\\8I2J YabeYf 	  w T#xsGLL/@)A5gllIZC[]efi]j(kmq(qrrs 
 HH9999: $sHS"W$556  F DeHSM8C=$@AAB (uE#w||"34 (usHSMS[\_S`?`9a (uZ 37!!!S',,&'!!eC-./!! !! \\	!!L 37!-.S',,&'-.eC-./-. -. 3	-.f 8<37!<	<gll*+< 5gll!234< uS',,./0	<
 
#< < 
<(Eh E3 E Ce
 *. $		# 8A;' 	
 8!#sGLL01 !#3 !#rzz !#H  rA   