a
    
d                      @   sh   d Z ddlZddl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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)_SubParsersAction)Optional   )CacheNotFoundHFCacheInfoscan_cache_dir   )BaseHuggingfaceCLICommand)ANSItabulatec                   @   s>   e Zd ZeedddZ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   l/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/huggingface_hub/commands/scan_cache.pyregister_subcommand!   s    z$ScanCacheCommand.register_subcommandc                 C   s   |j | _|j| _d S N)verbose	verbositydir	cache_dir)selfargsr   r   r   __init__4   s    zScanCacheCommand.__init__c              
   C   s  zt   }t| j}t   }W n: tyX } z"|j}td|  W Y d }~d S d }~0 0 | | tdt|| d dt|j dt	
|j d t|jdkr dt|j d	}| jd
krtt	| |jD ]}tt	| qntt	|d  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
   Zredsize_on_disk_strwarningsr   Zgray)r    t0hf_cache_infot1excr   messagewarningr   r   r   run8   s*    




zScanCacheCommand.runN)r.   returnc                 C   sh   | j dkr8ttdd t|jdd dD g dd n,ttd	d t|jd
d dD g dd d S )Nr   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>U   s   
zBScanCacheCommand._print_hf_cache_info_as_table.<locals>.<listcomp>c                 S   s   | j S r   r?   rA   r   r   r   <lambda>`       z@ScanCacheCommand._print_hf_cache_info_as_table.<locals>.<lambda>key)REPO ID	REPO TYPESIZE ON DISKNB FILESZLAST_ACCESSEDLAST_MODIFIEDREFS
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   rE   }   rF   zKScanCacheCommand._print_hf_cache_info_as_table.<locals>.<listcomp>.<lambda>rG   r5   r6   )r=   Z	revisionsr7   r8   rR   r9   r+   r:   r;   r<   r>   r   Zsnapshot_path)r@   rA   rS   r   r   r   rB   q   s   
c                 S   s   | j S r   rC   rD   r   r   r   rE   |   rF   )rI   rJ   ZREVISIONrK   rL   rM   rN   rO   )r   r&   r   r=   r*   )r    r.   r   r   r   r'   Q   s"    
z.ScanCacheCommand._print_hf_cache_info_as_table)
__name__
__module____qualname__staticmethodr   r   r"   r3   r   r'   r   r   r   r   r       s
   r   )__doc__r%   argparser   typingr   utilsr   r   r    r	   Z
_cli_utilsr
   r   r   r   r   r   r   <module>   s   