o
    Ne"&                     @   s   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 g ZdZdZ	dZ
e je jdddd	Zd
d Zdd Zdd Zdd Zdd Zd%ddZdd Zdd Zdd Zd&dd Zd&d!d"Zd&d#d$ZdS )'    N)get_path_from_urlz~/.cachedependenciesz
hubconf.py~z.cacheZpaddleZhubc                 C   s8   t j| rt j| rt |  d S t|  d S d S )N)ospathexistsisfileremoveshutilrmtree)r    r   ?D:\Projects\ConvertPro\env\Lib\site-packages\paddle/hapi/hub.py_remove_if_exists   s
   r   c                 C   sZ   t jd| zt| }t j|  W n ty$   t j| tdw t j| |S )Nr   zPPlease make sure config exists or repo error messages above fixed when importing)	sysr   insert
__import__modulespopImportErrorr	   RuntimeError)namerepo_dir
hub_moduler   r   r   _import_module&   s   r   c                 C   s0   |dkrd | ||S |dkrd | ||S d S )Ngithubz'https://github.com/{}/{}/archive/{}.zipgiteez1https://gitee.com/{}/{}/repository/archive/{}.zip)format)
repo_owner	repo_namebranchsourcer   r   r   _git_archive_link6   s   r!   c                 C   sD   |dkrdnd}d| v r|  d\}}n| }| d\}}|||fS )Nr   mainZmaster:/)split)repor    r   Z	repo_infor   r   r   r   r   _parse_repo_info?   s   
r'   c                 C   sD   zddl m} W n ty   ddlm} Y nw || jdd d S )Nr   )PathT)exist_ok)pathlibr(   r   Zpathlib2mkdir)dirnamer(   r   r   r   
_make_dirsI   s   r-   Tr   c                 C   s@  t }t| t| |\}}}|dd}tj|d|||g}	| o)tj|	}
|
r9|r7tj	
d|	 |	S tj||d }t| t||||d}t||| d|dkrZdnd	d
}t|| t|}| d j}tj||}t| || W d    n1 sw   Y  t| t|	 t||	 |	S )Nr$   _zUsing cache found in {}
z.zipr    Fr   Zwgetget)Zcheck_exist
decompressmethodr   )HUB_DIRr-   r'   replacer   r   joinr   r   stderrwriter   r   r!   r   r
   movezipfileZipFileinfolistfilename
extractall)r&   force_reloadverboser    Zhub_dirr   r   r   Znormalized_brr   Z	use_cacheZcached_fileurlZfpathZcached_zipfileZextraced_repo_nameZextracted_repor   r   r   _get_cache_or_reloadQ   sB   rA   c                 C   s@   t |ts	tdt| |d}|du st|std||S )zload entry from hubconf
    z5Invalid input: model should be a str of function nameNz"Cannot find callable {} in hubconf)
isinstancestr
ValueErrorgetattrcallabler   r   )mr   funcr   r   r   _load_entry_from_hubconf   s   
rI   c                 C   s$   zt |  W dS  ty   Y dS w )NTF)r   r   )r   r   r   r   _check_module_exists   s   rJ   c                 C   sF   t | td }|d urdd |D }t|r!tdd|d S d S )Nc                 S   s   g | ]}t |s|qS r   )rJ   ).0pkgr   r   r   
<listcomp>   s
    z'_check_dependencies.<locals>.<listcomp>zMissing dependencies: {}z, )rE   VAR_DEPENDENCYlenr   r   r5   )rG   r   missing_depsr   r   r   _check_dependencies   s   rQ   Fc                    s\   |dvrt d||dv rt| |d|d} ttdd |   fdd	t D }|S )
a  
    List all entrypoints available in `github` hubconf.

    Args:
        repo_dir(str): github or local path.

            github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `main` if not specified.
            
            local path (str): local repo path
        
        source (str): `github` | `gitee` | `local`, default is `github`.
        force_reload (bool, optional): whether to discard the existing cache and force a fresh download, default is `False`.
    Returns:
        entrypoints: a list of available entrypoint names

    Example:
        .. code-block:: python

            import paddle

            paddle.hub.list('lyuwenyu/paddlehub_demo:main', source='github', force_reload=False)

    r   r   localCUnknown source: "{}". Allowed values: "github" | "gitee" | "local".r   r   Tr/   .r   c                    s(   g | ]}t t |r|d s|qS )r.   )rF   rE   
startswith)rK   fr   r   r   rM      s    zlist.<locals>.<listcomp>)rD   r   rA   r   MODULE_HUBCONFr%   dir)r   r    r>   entrypointsr   rY   r   list   s    
r]   c                 C   sR   |dvrt d||dv rt| |d|d} ttdd | }t||}|jS )a  
    Show help information of model

    Args:
        repo_dir(str): github or local path.

            github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `main` if not specified.
            
            local path (str): local repo path.
        
        model (str): model name.
        source (str): `github` | `gitee` | `local`, default is `github`.
        force_reload (bool, optional): default is `False`.
    Return:
        docs

    Example:
        .. code-block:: python

            import paddle

            paddle.hub.help('lyuwenyu/paddlehub_demo:main', model='MM', source='github')

    rR   rT   rU   Tr/   rV   r   )rD   r   rA   r   rZ   r%   rI   __doc__)r   modelr    r>   r   entryr   r   r   help   s   
ra   c                 K   sb   |dvrt d||dv rt| |d|d} ttdd | }t| t||}|d	i |S )
a  
    Load model

    Args:
        repo_dir(str): github or local path.

            github path (str): a str with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `main` if not specified.

            local path (str): local repo path.
        
        model (str): model name.
        source (str): `github` | `gitee` | `local`, default is `github`.
        force_reload (bool, optional): default is `False`.
        **kwargs: parameters using for model
    Return:
        paddle model
    Example:
        .. code-block:: python

            import paddle
            paddle.hub.load('lyuwenyu/paddlehub_demo:main', model='MM', source='github')

    rR   rT   rU   Tr/   rV   r   Nr   )rD   r   rA   r   rZ   r%   rQ   rI   )r   r_   r    r>   kwargsr   r`   r   r   r   load  s   
rc   )Tr   )r   F)r   rer   r
   r9   Zpaddle.utils.downloadr   __all__ZDEFAULT_CACHE_DIRrN   rZ   r   
expanduserr5   r3   r   r   r!   r'   r-   rA   rI   rJ   rQ   r]   ra   rc   r   r   r   r   <module>   s.   	

6

.,