o
    oi                     @  sb   d dl mZ d dlZd dlZd dlZd dlmZmZ d dl	m
Z
 dgZeeZG dd dZdS )    )annotationsN)AnyOptional)kornia_configCachedDownloaderc                   @  sB   e Zd ZdZeddd	d
ZeddddZe	ddddZdS )r   zFDownloads files from URLs to the local cache or .kornia_hub directory.N
model_namestr	cache_dirOptional[str]suffixreturnc                 C  s   |du rt j}|dur||stj|d  | }ntj|d }tjjg |tj|tjdd |R  }|S )a;  Construct the file path for the ONNX model based on the model name and cache directory.

        Args:
            model_name: The name of the model or operator, typically in the format 'operators/model_name'.
            cache_dir: The directory where the model should be cached.
                Defaults to None, which will use a default `kornia.config.hub_onnx_dir` directory.
            suffix: Optional file suffix when the filename is the model name.

        Returns:
            str: The full local path where the model should be stored or loaded from.

        N)r   hub_cache_direndswithospathsplitjoinsep)clsr   r	   r   	file_name	file_path r   I/home/ubuntu/.local/lib/python3.10/site-packages/kornia/utils/download.py_get_file_path#   s   2zCachedDownloader._get_file_pathTurlnamedownloadboolkwargsr   c                 K  sT   | dr#|dd }|dd }| j|||d}| j|||d |S td| )Nzhttp:zhttps:r	   r   )r   )download_if_not_existsz-URL must start with 'http:' or 'https:'. Got )
startswithgetr   r   
ValueError)r   r   r   r   r   r	   r   r   r   r   r   download_to_cache=   s   
z"CachedDownloader.download_to_cacher   r!   Nonec              
   C  s   t j|rtd| d| d dS |std| dt jt j|dd |d	r\ztd
| d| d t	j
|| W dS  t	jjy[ } z	td| d|d}~ww td)aa  Download an ONNX model from the specified URL and save it to the specified file path.

        Args:
            url: The URL of the ONNX model to download.
            file_path: The local path where the downloaded model should be saved.
            download_if_not_exists: If True, the file will be downloaded if it's not already downloaded.

        z	Loading `z` from `z`.N`z)` not found. You may set `download=True`.T)exist_okr    zDownloading `z` to `zError in resolving `z'URL must start with 'http:' or 'https:')r   r   existsloggerinfor$   makedirsdirnamer"   urllibrequesturlretrieveerror	HTTPError)r   r   r   r!   er   r   r   r   G   s   
zCachedDownloader.download)N)r   r   r	   r
   r   r
   r   r   )T)
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r!   r   r   r&   )__name__
__module____qualname____doc__classmethodr   r%   r   r   r   r   r   r       s    	)
__future__r   loggingr   urllib.requestr.   typingr   r   kornia.configr   __all__	getLoggerr4   r*   r   r   r   r   r   <module>   s   
