o
    i$                     @   s  d dl Z d dlmZ d dlm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mZ d dlmZ G d	d
 d
eZG dd dZdeeeeef   dee defddZG dd dZG dd deZG dd deZG dd deZG dd deZG dd deZdS )    N)ArgumentParser)getpass)ListUnion)BaseHuggingfaceCLICommand)REPO_TYPE_DATASETREPO_TYPE_DATASET_URL_PREFIX
REPO_TYPES)HfApiHfFolder)	HTTPErrorc                   @   s   e Zd ZedefddZdS )UserCommandsparserc                 C   s  | j ddd}|jdd d | j ddd}|jd	d d | j d
dd}|jdd d | j ddd}|jdd}|j ddd}|jdtdd |jdd d |j ddd}|jdtdd |jdtdd |jdtdd |jddddd  |jd!d d d S )"Nloginz6Log in using the same credentials as on huggingface.co)helpc                 S      t | S N)LoginCommandargs r   X/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/huggingface_hub/commands/user.py<lambda>$       z2UserCommands.register_subcommand.<locals>.<lambda>)funcwhoamiz;Find out which huggingface.co account you are logged in as.c                 S   r   r   )WhoamiCommandr   r   r   r   r   (   r   logoutzLog outc                 S   r   r   )LogoutCommandr   r   r   r   r   *   r   repozG{create, ls-files} Commands to interact with your huggingface.co repos.z%huggingface.co repos related commandszls-filesz%List all your files on huggingface.coz--organizationz!Optional: organization namespace.)typer   c                 S   r   r   )ListReposObjsCommandr   r   r   r   r   :   r   createz#Create a new repo on huggingface.conamezPName for your repo. Will be namespaced under your username to build the repo id.z--typezNOptional: repo_type: set to "dataset" if creating a dataset, default is model.z-yz--yes
store_truez"Optional: answer Yes to the prompt)actionr   c                 S   r   r   )RepoCreateCommandr   r   r   r   r   Q   r   )
add_parserset_defaultsadd_subparsersadd_argumentstr)r   login_parserwhoami_parserlogout_parserrepo_parserrepo_subparsers	ls_parserrepo_create_parserr   r   r   register_subcommand   s^   z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r3   r   r   r   r   r      s    r   c                   @   sD   e Zd ZdZdZdZdZdZedd Z	edd	 Z
ed
d ZdS )ANSIz;
    Helper for en.wikipedia.org/wiki/ANSI_escape_code
    z[1mz[31mz[90mz[0mc                 C      d | j|| jS Nz{}{}{})format_bold_resetclssr   r   r   bold^      z	ANSI.boldc                 C   s   d | j| j || jS r:   )r;   r<   _redr=   r>   r   r   r   redb   s   zANSI.redc                 C   r9   r:   )r;   _grayr=   r>   r   r   r   grayf   rB   z	ANSI.grayN)r4   r5   r6   __doc__r<   rC   rE   r=   classmethodrA   rD   rF   r   r   r   r   r8   T   s    

r8   rowsheadersreturnc                 C   s   dd t g | |R  D }dt| j| }g }||j|  ||jdd |D   | D ]
}||j|  q0d|S )z
    Inspired by:

    - stackoverflow.com/a/8356620/593036
    - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
    c                 S   s   g | ]}t d d |D qS )c                 s   s    | ]	}t t|V  qd S r   )lenr+   ).0xr   r   r   	<genexpr>r   s    z&tabulate.<locals>.<listcomp>.<genexpr>)max)rM   colr   r   r   
<listcomp>r   s    ztabulate.<locals>.<listcomp>z{{:{}}} c                 S   s   g | ]}d | qS )-r   )rM   wr   r   r   rR   v   s    
)ziprL   r;   appendjoin)rI   rJ   
col_widths
row_formatlinesrowr   r   r   tabulatek   s   
r]   c                   @      e Zd Zdd ZdS )BaseUserCommandc                 C   s   || _ t | _d S r   )r   r
   _api)selfr   r   r   r   __init__}   s   zBaseUserCommand.__init__N)r4   r5   r6   rb   r   r   r   r   r_   |       r_   c                   @   r^   )r   c              
   C   s   t d td}t }z	| j||}W n# ty7 } zt | t t|jj	 t
d W Y d }~nd }~ww t| t d t d|d t dtj d S )NaY  
        _|    _|  _|    _|    _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|_|_|_|    _|_|      _|_|_|  _|_|_|_|
        _|    _|  _|    _|  _|        _|          _|    _|_|    _|  _|            _|        _|    _|  _|        _|
        _|_|_|_|  _|    _|  _|  _|_|  _|  _|_|    _|    _|  _|  _|  _|  _|_|      _|_|_|    _|_|_|_|  _|        _|_|_|
        _|    _|  _|    _|  _|    _|  _|    _|    _|    _|    _|_|  _|    _|      _|        _|    _|  _|        _|
        _|    _|    _|_|      _|_|_|    _|_|_|  _|_|_|  _|      _|    _|_|_|      _|        _|    _|    _|_|_|  _|_|_|_|

        z
Username:    zLogin successfulzYour token:rU   zYour token has been saved to)printinputr   r`   r   r   r8   rD   responsetextexitr   
save_token
path_token)ra   usernamepasswordtokener   r   r   run   s"   

zLoginCommand.runNr4   r5   r6   rp   r   r   r   r   r      rc   r   c                   @   r^   )r   c              
   C   s   t  }|d u rtd t  z| j|\}}t| |r,ttdd| W d S W d S  t	yR } zt| tt
|jj td W Y d }~d S d }~ww )NNot logged inzorgs: ,rd   )r   	get_tokenre   ri   r`   r   r8   rA   rX   r   rD   rg   rh   )ra   rn   userorgsro   r   r   r   rp      s    zWhoamiCommand.runNrq   r   r   r   r   r      rc   r   c                   @   r^   )r   c                 C   s>   t  }|d u rtd t  t   | j| td d S )Nrr   zSuccessfully logged out.)r   rt   re   ri   delete_tokenr`   r   )ra   rn   r   r   r   rp      s   zLogoutCommand.runNrq   r   r   r   r   r      rc   r   c                   @   r^   )r!   c              
   C   s   t  }|d u rtd td z| jj|| jjd}W n# ty? } zt| tt	
|jj td W Y d }~nd }~ww t|dkrMtd t  dd |D }tt|g dd	 d S )
Nrr   rd   )organizationr   zNo shared file yetc                 S   s    g | ]}|j |j|j|jgqS r   )filenamelastModifiedcommitsize)rM   objr   r   r   rR      s     z,ListReposObjsCommand.run.<locals>.<listcomp>)FilenameLastModifiedz
Commit-ShaSize)rJ   )r   rt   re   ri   r`   list_repos_objsr   rx   r   r8   rD   rg   rh   rL   r]   )ra   rn   objsro   rI   r   r   r   rp      s&   zListReposObjsCommand.runNrq   r   r   r   r   r!      rc   r!   c                   @   r^   )r&   c           
   
   C   s  t  }|d u rtd td ztddgd}tt|	  W n t
y2   td Y nw ztddgd}tt|	  W n t
yX   ttd Y nw td	 | j|\}}| jjd uro| jjn|}| d
| jj }| jjtvrtd td | jjtkrt| }tdt| | jjstd }|d	ks|dks|dkstd t  z| jj|| jj| jj| jjd}W n# ty }	 zt|	 tt|	jj td W Y d }	~	nd }	~	ww td tdt| td td|  td	 d S )Nrr   rd   gitz	--versionzutf-8z9Looks like you do not have git installed, please install.zgit-lfszLooks like you do not have git-lfs installed, please install. You can install from https://git-lfs.github.com/. Then run `git lfs install` (you only have to do this once). /zInvalid repo --typezYou are about to create {}zProceed? [Y/n] yyesAbort)r#   rx   	repo_typez
Your repo now lives at:z  {}zK
You can clone it locally with the command below, and commit/push as usual.z
  git clone )r   rt   re   ri   
subprocesscheck_outputdecoder8   rF   stripFileNotFoundErrorrD   r`   r   r   rx   r#   r    r	   r   r   r;   rA   r   rf   lowercreate_repor   rg   rh   )
ra   rn   stdoutru   _	namespacerepo_idchoiceurlro   r   r   r   rp      sp   
zRepoCreateCommand.runNrq   r   r   r   r   r&      rc   r&   )r   argparser   r   typingr   r   huggingface_hub.commandsr   huggingface_hub.constantsr   r   r	   huggingface_hub.hf_apir
   r   requests.exceptionsr   r   r8   r+   intr]   r_   r   r   r   r!   r&   r   r   r   r   <module>   s"   6*