U Pht8 @sddlmZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl m Z ddlmZmZddlmZmZmZddlmZddlmZmZddlmZdd lmZmZmZed d \Z Z!erdd l"m"Z"d Z#nedded\Z"Z#ddddddgZ$dZ%ej&ej'ej(ej)dZ*de%ddfddddddddZ+e+dZ,e$-dd dd!d"d#Z.d6dd$d%d&d'd(d)Z/d7d ddd%d+d,dZ0d8dd ddd%d.d&d/d0dZ1d9d d dddd%d&d2d3dZ2d:dd d dddd%d%d&d4 d5dZ3dS);) annotationsN)Path) TYPE_CHECKINGAny)ContentTooShortError HTTPErrorURLError)urlparse)urlopen urlretrieve)PathLike)look_up_option min_versionoptional_importgdownz4.7.3)tqdmTrz4.47.0 check_hash download_url extractalldownload_and_extract get_loggerSUPPORTED_HASH_TYPESz)%(asctime)s - %(levelname)s - %(message)s)md5sha1sha256sha512z monai.appsstrz str | Nonezlogging.Handler | Nonezlogging.Logger) module_namefmtdatefmtlogger_handlerreturncCsz|dk o|tjjjk}t|}d|_|tj|rdtt j }tj ||d}| || ||dk rv| ||S)a Get a `module_name` logger with the specified format and date format. By default, the logger will print to `stdout` at the INFO level. If `module_name` is `None`, return the root logger. `fmt` and `datafmt` are passed to a `logging.Formatter` object (https://docs.python.org/3/library/logging.html#formatter-objects). `logger_handler` can be used to add an additional handler. NF)rr)loggingrootmanager loggerDict getLogger propagatesetLevelINFO StreamHandlersysstdout Formatter setFormatter addHandler)rrrr Zadds_stdout_handlerloggerhandler formatterr3E/home/dell461/cl/sdc2/HISourceFinder-master-l/src/monai/apps/utils.pyr0s      r0r )pr!cCs*tjjtjjpdd}t||jS)zHget the last part of the path (removing the trailing slash if it exists)z/ )ospathsepaltseprrstripname)r5r9r3r3r4 _basenameQsr=rboolNone)urlfilepathprogressr!c CszltrN|rNGdddt}|ddddt|d}t|||jdW5QRXnts`|r`td t||WnFttt t fk r}z t d |d |d |W5d }~XYnXd S)zT Retrieve file from `url` to `filepath`, optionally showing a progress bar. c@s&eZdZdZd ddddddd ZdS) z)_download_with_progress..TqdmUpToz Provides `update_to(n)` which uses `tqdm.update(delta_n)`. Inspired by the example in https://github.com/tqdm/tqdm. Nintz int | Noner?)bbsizetsizer!cSs&|dk r||_||||jdS)a! Args: b: number of blocks transferred so far, default: 1. bsize: size of each block (in tqdm units), default: 1. tsize: total size (in tqdm units). if None, remains unchanged. N)totalupdaten)selfrErFrGr3r3r4 update_todsz3_download_with_progress..TqdmUpTo.update_to)rCrCN)__name__ __module__ __qualname____doc__rLr3r3r3r4TqdmUpTo^srQBTirC)unit unit_scale unit_divisorminitersdesc) reporthookzBtqdm is not installed, will not show the downloading progress bar.zDownload failed from  to .N) has_tqdmrr=r rLwarningswarnrrrOSErrorr0error)r@rArBrQter3r3r4_download_with_progressWs rbr)rAval hash_typer!c s|dkr*td|d|d|ddSt|t}tjdkrN|dd }n|}zzcheck_hash..rmzException in check_hash: zcheck_hash failed z Verified 'z', z: )r0infor lowerrr+ version_infoopeniterrI Exceptionr_ hexdigestr=)rArcrdZactual_hash_funcZ actual_hashchunkrar3rjr4rzs&     "r6r)r@rAhash_valrdrB gdown_kwargsr!c Ks |s(tdt|}td|dt|}|rzt|||sdt|d|d|d|dtd|dd Sz@t ,}t|t|}t |j d krt std d |krd |d <t j||fd| i|nt |j dkrXt|X}|} | dkr*t|d} t| ||dn"td| d|dd|dW5QRXnt|||d|std|d|d|j} | rtj| d dt||W5QRXWnttfk rYnXtd|t|||st|d|d|d|d|d d S)a* Download file from specified URL link, support process bar and hash check. Args: url: source URL link to download file. filepath: target filepath to save the downloaded file (including the filename). If undefined, `os.path.basename(url)` will be used. hash_val: expected hash value to validate the downloaded file. if None, skip hash validation. hash_type: 'md5' or 'sha1', defaults to 'md5'. progress: whether to display a progress bar. gdown_kwargs: other args for `gdown` except for the `url`, `output` and `quiet`. these args will only be used if download from google drive. details of the args of it: https://github.com/wkentaro/gdown/blob/main/gdown/download.py Raises: RuntimeError: When the hash validation of the ``filepath`` existing file fails. RuntimeError: When a network issue or denied permission prevents the file download from ``url`` to ``filepath``. URLError: See urllib.request.urlretrieve. HTTPError: See urllib.request.urlretrieve. ContentTooShortError: See urllib.request.urlretrieve. IOError: See urllib.request.urlretrieve. RuntimeError: When the hash validation of the ``url`` downloaded file fails. rZzDefault downloading to ''z) check of existing file failed: filepath= , expected =z File exists: z, skipped downloading.Nzdrive.google.comzITo download files from Google Drive, please install the gdown dependency.fuzzyTquietzcloud-api.yandex.nethref)rBzDownload of file from z, received from  rYz2 failed due to network issue or denied permission.)exist_okz Downloaded: z& check of downloaded file failed: URL=z , filepath=)rr=resolver0rnexistsr RuntimeErrortempfileTemporaryDirectoryr netloc has_gdownrdownloadr getcodejsonloadrbparentr7makedirsshutilmovePermissionErrorNotADirectoryError) r@rArvrdrBrwtmp_dirtmp_nameresponsecoderZfile_dirr3r3r4rs^#       rZ)rA output_dirrvrd file_typehas_baser!c Cs@|rt|t|dd}nt|}|rVt|ddk rVtd|ddSt|}|rt|||st |d|d|d|dtd |d| }|j d s|d krt|}|||dS|j d s|j d sd |kr&t|} | || dStd |d|ddS)aK Extract file to the output directory. Expected file types are: `zip`, `tar.gz` and `tar`. Args: filepath: the file path of compressed file. output_dir: target directory to save extracted files. hash_val: expected hash value to validate the compressed file. if None, skip hash validation. hash_type: 'md5' or 'sha1', defaults to 'md5'. file_type: string of file type for decompressing. Leave it empty to infer the type from the filepath basename. has_base: whether the extracted files have a base folder. This flag is used when checking if the existing folder is a result of `extractall`, if it is, the extraction is skipped. For example, if A.zip is unzipped to folder structure `A/*.png`, this flag should be True; if B.zip is unzipped to `*.png`, this flag should be False. Raises: RuntimeError: When the hash validation of the ``filepath`` compressed file fails. NotImplementedError: When the ``filepath`` file extension is not one of [zip", "tar.gz", "tar"]. rZrNzNon-empty folder exists in z, skipped extracting.z+ check of compressed file failed: filepath=ryrzzWriting into directory: ziptarztar.gzzMUnsupported file type, available options are: ["zip", "tar.gz", "tar"]. name=z type=)rr=splitrnextiterdirr0rnrrrostripr<endswithzipfileZipFilerclosetarfilerqNotImplementedError) rArrvrdrr cache_dirZ _file_typezip_fileZtar_filer3r3r4rs4   &  ) r@rArrvrdrrrBr!c CsPt>}|pt|t|} t|| |||dt| |||dW5QRXdS)ak Download file from URL and extract it to the output directory. Args: url: source URL link to download file. filepath: the file path of the downloaded compressed file. use this option to keep the directly downloaded compressed file, to avoid further repeated downloads. output_dir: target directory to save extracted files. default is the current directory. hash_val: expected hash value to validate the downloaded file. if None, skip hash validation. hash_type: 'md5' or 'sha1', defaults to 'md5'. file_type: string of file type for decompressing. Leave it empty to infer the type from url's base file name. has_base: whether the extracted files have a base folder. This flag is used when checking if the existing folder is a result of `extractall`, if it is, the extraction is skipped. For example, if A.zip is unzipped to folder structure `A/*.png`, this flag should be True; if B.zip is unzipped to `*.png`, this flag should be False. progress: whether to display progress bar. )r@rArvrdrB)rArrrN)rrrr=rrr) r@rArrvrdrrrBrfilenamer3r3r4r0s )T)Nr)r6NrT)rZNrr6T)r6rZNrr6TT)4 __future__rhashlibrr"r7rr+rrr\rpathlibrtypingrr urllib.errorrrr urllib.parser urllib.requestr r monai.config.type_definitionsr monai.utilsr rrrrrr[__all__Z DEFAULT_FMTrrrrrrr0appendr=rbrrrrr3r3r3r4 sl      #'X=