a
    
d                     @   s   d dl mZ d dlZd dlZd dlmZ d dlZd dlZd dl	Z	d dl
Z
ddlmZ eeddZeeszee W n ey   Y n0 dddZdd
dZdddZdS )    )print_functionN   )download~z.cache/gdownc                    sj    d u rd t  }t| d4t fdddD ]}|| q4W d    n1 sX0    Y  | S )Ni   rbc                      s
     S )N)read 	blocksizefr   ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/gdown/cached_download.py<lambda>       zmd5sum.<locals>.<lambda>r   )hashlibmd5openiterupdate	hexdigest)filenamer
   hashblockr   r	   r   md5sum   s    *r   Fc                 C   sp   t |trt|dks$td||s6td|  t| }||kr\|sXtd|  dS td||d S )N    zMD5 must be 32 chars: {}zComputing MD5: {}zMD5 matches: {}Tz*MD5 doesn't match:
actual: {}
expected: {})
isinstancestrlen
ValueErrorformatprintr   AssertionError)r   r   quietr
   Z
md5_actualr   r   r   assert_md5sum!   s    
r"   c              
   K   s  |du r8|  dd dd dd dd	}tt|}t|r\|s\|sXtd
| |S t|r|rzt|||d |W S  ty } zt|t	j
d W Y d}~n
d}~0 0 ttd}ztt| W n ty   Y n0 tjtd}zt|d}	|s6d}
|rd|
|}
n
d|
}
t|
t	j
d t| |	fd|i| t| t|	| W d   n1 sz0    Y  W n  ty   t|  Y n0 |rt|||d |dur|| |S )a#  Cached download from URL.

    Parameters
    ----------
    url: str
        URL. Google Drive URL is also supported.
    path: str, optional
        Output filename. Default is basename of URL.
    md5: str, optional
        Expected MD5 for specified file.
    quiet: bool
        Suppress terminal output. Default is False.
    postprocess: callable
        Function called with filename as postprocess.
    kwargs: dict
        Keyword arguments to be passed to `download`.

    Returns
    -------
    path: str
        Output filename.
    N/z-SLASH-:z-COLON-=z-EQUAL-?z
-QUESTION-zFile exists: {})r!   )fileZ_dl_lock)dirdlzCached Downloadingz{}: {}z{}...r!   )replaceospjoin
cache_rootexistsr   r   r"   r    sysstderrosmakedirsdirnameOSErrortempfilemkdtempr   filelockZFileLockshutilmove	Exceptionrmtree)urlpathr   r!   postprocesskwargseZ	lock_pathZ	temp_rootZ	temp_pathmsgr   r   r   cached_download3   sZ    $
0

rB   )N)FN)NNNFN)
__future__r   r   r1   os.pathr=   r+   r8   r/   r5   r7   r   r,   
expanduserr-   r.   r2   r4   r   r"   rB   r   r   r   r   <module>   s$   


 