o
    hs!                     @   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 G d
d deZddde	dedef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tabulatec                   @   sJ   e Zd ZedefddZdeddfddZd	d
 Zde	ddf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   scan_cache_parser r   w/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/huggingface_hub/commands/scan_cache.pyregister_subcommand"   s   z$ScanCacheCommand.register_subcommandargsreturnNc                 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  zt   }t| j}t   }W n ty, } z|j}td|  W Y d }~d S d }~ww | | tdt|| d dt|j dt	
|j d t|jdkrdt|j d	}| jd
krwtt	| |jD ]	}tt	| qkd S tt	|d  d S d S )Nz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.)timer   r#   r   print_print_hf_cache_info_as_tableroundlenreposr   redsize_on_disk_strwarningsr!   gray)r$   t0hf_cache_infot1excr#   messagewarningr   r   r   run9   s2   




zScanCacheCommand.runr3   c                 C   s   t t|| jd d S )Nr!   )r)   	get_tabler!   )r$   r3   r   r   r   r*   R   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   sX   |dkrt dd t| jdd dD g ddS t d	d t| jd
d dD g d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_fileslast_accessed_strlast_modified_strjoinsortedrefsr   	repo_path).0repor   r   r   
<listcomp>   s    
zget_table.<locals>.<listcomp>c                 S      | j S r   rJ   rL   r   r   r   <lambda>       zget_table.<locals>.<lambda>key)REPO ID	REPO TYPESIZE ON DISKNB FILESLAST_ACCESSEDLAST_MODIFIEDREFS
LOCAL PATH)rowsheadersc                 S   s\   g | ]*}t |jd d dD ]}|j|j|jd|j|j|jd	t |j
t|jgqqS )c                 S   rN   r   )commit_hash)revisionr   r   r   rQ      rR   z&get_table.<locals>.<listcomp>.<lambda>rS   r?   r@   )rH   	revisionsrA   rB   r_   rC   r/   rD   rF   rG   rI   r   snapshot_path)rK   rL   r`   r   r   r   rM      s    
c                 S   rN   r   rO   rP   r   r   r   rQ      rR   )rU   rV   REVISIONrW   rX   rZ   r[   r\   )r   rH   r-   )r3   r!   r   r   r   r:   V   s   +r:   )__doc__r(   argparser   r   typingr   utilsr   r   r    r
   
_cli_utilsr   r   r   intr   r:   r   r   r   r   <module>   s   	 5