o
    Zh?                     @   sl   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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   Zscan_cache_parser r   Z/var/www/html/lang_env/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.runr2   c                 C   sj   | j dkrttdd t|jdd dD g dd d S 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>V   s    
zBScanCacheCommand._print_hf_cache_info_as_table.<locals>.<listcomp>c                 S      | j S r   rB   rD   r   r   r   <lambda>a       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 |jd d dD ]}|j|j|jd|j|j|jd	t |j
t|jgqqS )c                 S   rF   r   )commit_hash)revisionr   r   r   rI   ~   rJ   zKScanCacheCommand._print_hf_cache_info_as_table.<locals>.<listcomp>.<lambda>rK   r8   r9   )r@   Z	revisionsr:   r;   rV   r<   r.   r=   r>   r?   rA   r   Zsnapshot_path)rC   rD   rW   r   r   r   rE   r   s    
c                 S   rF   r   rG   rH   r   r   r   rI   }   rJ   )rM   rN   ZREVISIONrO   rP   rQ   rR   rS   )r    r(   r   r@   r,   )r#   r2   r   r   r   r)   R   s"   
z.ScanCacheCommand._print_hf_cache_info_as_table)__name__
__module____qualname__staticmethodr   r   r   r$   r7   r   r)   r   r   r   r   r   !   s    r   )__doc__r'   argparser   r   typingr   utilsr   r   r    r
   Z
_cli_utilsr   r   r   r   r   r   r   <module>   s   	