U
    ”Ê¹jã!  ã                   @   sˆ   d Z ddlZddlmZmZ ddlmZ ddlmZm	Z	m
Z
 ddlmZ dd	lmZmZmZ G d
d„ deƒZddœe	eedœdd„ZdS )zÜContains command to scan the HF cache directory.

Usage:
    huggingface-cli scan-cache
    huggingface-cli scan-cache -v
    huggingface-cli scan-cache -vvv
    huggingface-cli scan-cache --dir ~/.cache/huggingface/hub
é    N)Ú	NamespaceÚ_SubParsersAction)ÚOptionalé   )ÚCacheNotFoundÚHFCacheInfoÚscan_cache_diré   )ÚBaseHuggingfaceCLICommand)ÚANSIÚshow_deprecation_warningÚtabulatec                   @   sF   e Zd Zeedœdd„ƒZeddœdd„Zdd	„ Ze	dd
œdd„Z
dS )ÚScanCacheCommand)Úparserc                 C   sD   | j ddd�}|jdtd dd� |jddd	d
dd� |jtd� d S )Nz
scan-cachezScan cache directory.)Úhelpz--dirzMcache directory to scan (optional). Default to the default HuggingFace cache.)ÚtypeÚdefaultr   z-vz	--verboseÚcountr   zshow a more verbose output)Úactionr   r   )Úfunc)Ú
add_parserÚadd_argumentÚstrÚset_defaultsr   )r   Zscan_cache_parser© r   úb/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/commands/scan_cache.pyÚregister_subcommand"   s    üûz$ScanCacheCommand.register_subcommandN)ÚargsÚreturnc                 C   s   |j | _|j| _d S ©N)ÚverboseÚ	verbosityÚdirÚ	cache_dir)Úselfr   r   r   r   Ú__init__5   s    zScanCacheCommand.__init__c              
   C   s  t ddƒ zt ¡ }t| jƒ}t ¡ }W n< tk
rd } z|j}td|› �ƒ W Y ¢d S d }~X Y nX |  |¡ tdt|| dƒ› dt|j	ƒ› dt
 |j¡› d�ƒ t|jƒd	k�rd
t|jƒ› d�}| jdk�r tt
 |¡ƒ |jD ]}tt
 t|ƒ¡ƒ qæntt
 |d ¡ƒ d S )Nzhuggingface-cli scan-cachezhf cache scanzCache directory not found: z	
Done in r	   zs. Scanned z repo(s) for a total of Ú.r   zGot z warning(s) while scanning.é   z Use -vvv to print details.)r   Útimer   r#   r   ÚprintÚ_print_hf_cache_info_as_tableÚroundÚlenÚreposr   ÚredÚsize_on_disk_strÚwarningsr!   Úgrayr   )r$   Út0Úhf_cache_infoÚt1Úexcr#   ÚmessageÚwarningr   r   r   Úrun9   s(    


.ÿ
zScanCacheCommand.run)r3   r   c                 C   s   t t|| jd�ƒ d S )N©r!   )r)   Ú	get_tabler!   )r$   r3   r   r   r   r*   T   s    z.ScanCacheCommand._print_hf_cache_info_as_table)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r   r   r%   r8   r   r*   r   r   r   r   r   !   s
   r   r9   )r3   r!   r   c             
   C   st   |dkr<t dd„ t| jdd„ d�D ƒddd	d
ddddgd�S t dd„ t| jdd„ d�D ƒdddd	d
dddgd�S dS )a:  Generate a table from the [`HFCacheInfo`] object.

    Pass `verbosity=0` to get a table with a single row per repo, with columns
    "repo_id", "repo_type", "size_on_disk", "nb_files", "last_accessed", "last_modified", "refs", "local_path".

    Pass `verbosity=1` to get a table with a row per repo and revision (thus multiple rows can appear for a single repo), with columns
    "repo_id", "repo_type", "revision", "size_on_disk", "nb_files", "last_modified", "refs", "local_path".

    Example:
    ```py
    >>> from huggingface_hub.utils import scan_cache_dir
    >>> from huggingface_hub.commands.scan_cache import get_table

    >>> hf_cache_info = scan_cache_dir()
    HFCacheInfo(...)

    >>> print(get_table(hf_cache_info, verbosity=0))
    REPO ID                                             REPO TYPE SIZE ON DISK NB FILES LAST_ACCESSED LAST_MODIFIED REFS LOCAL PATH
    --------------------------------------------------- --------- ------------ -------- ------------- ------------- ---- --------------------------------------------------------------------------------------------------
    roberta-base                                        model             2.7M        5 1 day ago     1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--roberta-base
    suno/bark                                           model             8.8K        1 1 week ago    1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--suno--bark
    t5-base                                             model           893.8M        4 4 days ago    7 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-base
    t5-large                                            model             3.0G        4 5 weeks ago   5 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-large

    >>> print(get_table(hf_cache_info, verbosity=1))
    REPO ID                                             REPO TYPE REVISION                                 SIZE ON DISK NB FILES LAST_MODIFIED REFS LOCAL PATH
    --------------------------------------------------- --------- ---------------------------------------- ------------ -------- ------------- ---- -----------------------------------------------------------------------------------------------------------------------------------------------------
    roberta-base                                        model     e2da8e2f811d1448a5b465c236feacd80ffbac7b         2.7M        5 1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--roberta-base\snapshots\e2da8e2f811d1448a5b465c236feacd80ffbac7b
    suno/bark                                           model     70a8a7d34168586dc5d028fa9666aceade177992         8.8K        1 1 week ago    main C:\Users\admin\.cache\huggingface\hub\models--suno--bark\snapshots\70a8a7d34168586dc5d028fa9666aceade177992
    t5-base                                             model     a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1       893.8M        4 7 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-base\snapshots\a9723ea7f1b39c1eae772870f3b547bf6ef7e6c1
    t5-large                                            model     150ebc2c4b72291e770f58e6057481c8d2ed331a         3.0G        4 5 months ago  main C:\Users\admin\.cache\huggingface\hub\models--t5-large\snapshots\150ebc2c4b72291e770f58e6057481c8d2ed331a                                                 ```
    ```

    Args:
        hf_cache_info ([`HFCacheInfo`]):
            The HFCacheInfo object to print.
        verbosity (`int`, *optional*):
            The verbosity level. Defaults to 0.

    Returns:
        `str`: The table as a string.
    r   c                 S   sD   g | ]<}|j |jd  |j¡|j|j|jd t|j	ƒ¡t
|jƒg‘qS )ú{:>12}ú, )Úrepo_idÚ	repo_typeÚformatr/   Únb_filesZlast_accessed_strÚlast_modified_strÚjoinÚsortedÚrefsr   Ú	repo_path)Ú.0Úrepor   r   r   Ú
<listcomp>…   s   ÷
øzget_table.<locals>.<listcomp>c                 S   s   | j S r   ©rI   ©rK   r   r   r   Ú<lambda>�   ó    zget_table.<locals>.<lambda>©ÚkeyzREPO IDz	REPO TYPEzSIZE ON DISKzNB FILESZLAST_ACCESSEDZLAST_MODIFIEDZREFSz
LOCAL PATH)ÚrowsÚheadersc                 S   s\   g | ]T}t |jd d„ d�D ]<}|j|j|jd |j¡|j|jd 	t |j
ƒ¡t|jƒg‘qqS )c                 S   s   | j S r   )Úcommit_hash)Úrevisionr   r   r   rO   «   rP   z&get_table.<locals>.<listcomp>.<lambda>rQ   r?   r@   )rG   Z	revisionsrA   rB   rU   rC   r/   rD   rE   rF   rH   r   Zsnapshot_path)rJ   rK   rV   r   r   r   rL   Ÿ   s    ö
øc                 S   s   | j S r   rM   rN   r   r   r   rO   ª   rP   ZREVISIONN)r   rG   r-   )r3   r!   r   r   r   r:   X   s:    +õøòõøñr:   )Ú__doc__r(   Úargparser   r   Útypingr   Úutilsr   r   r   Ú r
   Z
_cli_utilsr   r   r   r   Úintr   r:   r   r   r   r   Ú<module>   s   	7