U
    di!                     @   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 )zContains 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   G/tmp/pip-unpacked-wheel-ohruig0i/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| qn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   Zredsize_on_disk_strwarningsr!   Zgrayr   )r$   t0hf_cache_infot1excr#   messagewarningr   r   r   run9   s(    


.
zScanCacheCommand.run)r1   r   c                 C   s   t t|| jd d S )Nr!   )r)   	get_tabler!   )r$   r1   r   r   r   r*   T   s    z.ScanCacheCommand._print_hf_cache_info_as_table)__name__
__module____qualname__staticmethodr   r   r   r%   r6   r   r*   r   r   r   r   r   !   s
   r   r7   )r1   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   rG   rI   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)Z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   rM      rN   z&get_table.<locals>.<listcomp>.<lambda>rO   r=   r>   )rE   Z	revisionsr?   r@   rR   rA   r.   rB   rC   rD   rF   r   Zsnapshot_path)rH   rI   rS   r   r   r   rJ      s    
c                 S   s   | j S r   rK   rL   r   r   r   rM      rN   ZREVISIONN)r   rE   r-   )r1   r!   r   r   r   r8   X   s:    +r8   )__doc__r(   argparser   r   typingr   utilsr   r   r    r
   Z
_cli_utilsr   r   r   r   intr   r8   r   r   r   r   <module>   s   	7