U
    4Af                     @   s   d dl Z d dlmZ d dlmZmZ d dlmZmZm	Z	 d dl
mZ ddlmZ G dd	 d	eZG d
d dZeeeeef   ee ed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dS )    N)ArgumentParser)ListUnion)HfFoldercreate_repowhoami)	HTTPError   )BaseTransformersCLICommandc                   @   s   e Zd Zeed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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   s   t | S N)LoginCommandargs r   >/tmp/pip-unpacked-wheel-zw5xktn0/transformers/commands/user.py<lambda>       z2UserCommands.register_subcommand.<locals>.<lambda>)funcr   z;Find out which huggingface.co account you are logged in as.c                 S   s   t | S r   )WhoamiCommandr   r   r   r   r      r   ZlogoutzLog outc                 S   s   t | S r   )LogoutCommandr   r   r   r   r   !   r   repoz_Deprecated: use `huggingface-cli` instead. Commands to interact with your huggingface.co repos.zPDeprecated: use `huggingface-cli` instead. huggingface.co repos related commandscreatezNDeprecated: use `huggingface-cli` instead. Create a new repo on huggingface.conamezYName for your model's repo. Will be namespaced under your username to build the model id.)typer   z--organizationz!Optional: organization namespace.z-yz--yes
store_truez"Optional: answer Yes to the prompt)actionr   c                 S   s   t | S r   )RepoCreateCommandr   r   r   r   r   5   r   )
add_parserset_defaultsadd_subparsersadd_argumentstr)r   Zlogin_parserZwhoami_parserZlogout_parserZrepo_parserZrepo_subparsersZrepo_create_parserr   r   r   register_subcommand   s2     z UserCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r&   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   s   | j  | | j S r   )_bold_resetclssr   r   r   boldB   s    z	ANSI.boldc                 C   s   | j  | j | | j S r   )r,   _redr-   r.   r   r   r   redF   s    zANSI.redc                 C   s   | j  | | j S r   )_grayr-   r.   r   r   r   grayJ   s    z	ANSI.grayN)r'   r(   r)   __doc__r,   r2   r4   r-   classmethodr1   r3   r5   r   r   r   r   r+   8   s   

r+   )rowsheadersreturnc                 C   s|   dd t | |f D }dt| j| }g }||j|  ||jdd |D   | D ]}||j|  q\d|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>V   s     z&tabulate.<locals>.<listcomp>.<genexpr>)max)r<   colr   r   r   
<listcomp>V   s     ztabulate.<locals>.<listcomp>z{{:{}}} c                 S   s   g | ]}d | qS )-r   )r<   wr   r   r   rA   Z   s     
)zipr;   formatappendjoin)r8   r9   Z
col_widthsZ
row_formatlinesrowr   r   r   tabulateO   s    rK   c                   @   s   e Zd Zdd ZdS )BaseUserCommandc                 C   s
   || _ d S r   r   )selfr   r   r   r   __init__a   s    zBaseUserCommand.__init__N)r'   r(   r)   rN   r   r   r   r   rL   `   s   rL   c                   @   s   e Zd Zdd ZdS )r   c                 C   s   t td d S )NzERROR! `huggingface-cli login` uses an outdated login mechanism that is not compatible with the Hugging Face Hub backend anymore. Please use `huggingface-cli login instead.printr+   r3   rM   r   r   r   runf   s
    zLoginCommand.runNr'   r(   r)   rR   r   r   r   r   r   e   s   r   c                   @   s   e Zd Zdd ZdS )r   c              
   C   s   t td t }|d kr,t d t  z2t|\}}t | |r\t tdd| W nD t	k
r } z&t | t t|j
j td W 5 d }~X Y nX d S )NzxWARNING! `transformers-cli whoami` is deprecated and will be removed in v5. Please use `huggingface-cli whoami` instead.Not logged inzorgs: ,r	   )rP   r+   r3   r   	get_tokenexitr   r1   rH   r   responsetext)rM   tokenuserZorgser   r   r   rR   q   s$    zWhoamiCommand.runNrS   r   r   r   r   r   p   s   r   c                   @   s   e Zd Zdd ZdS )r   c                 C   s   t td d S )NzERROR! `transformers-cli logout` uses an outdated logout mechanism that is not compatible with the Hugging Face Hub backend anymore. Please use `huggingface-cli logout instead.rO   rQ   r   r   r   rR      s
    zLogoutCommand.runNrS   r   r   r   r   r      s   r   c                   @   s   e Zd Zdd ZdS )r    c           
   
   C   s  t td t }|d kr.t d td z*tddgd}t t	|
  W n tk
rt   t d Y nX z*tddgd}t t	|
  W n" tk
r   t td	 Y nX t d
 t|\}}| jjd k	r| jjn|}| d| jj }t dt|  | jjsXtd }|d
ksX|dksX|dksXt d t  zt||d}W nF tk
r }	 z&t |	 t t|	jj td W 5 d }	~	X Y nX t d t dt|  t d t d|  t d
 d S )NzlWARNING! Managing repositories through transformers-cli is deprecated. Please use `huggingface-cli` instead.rT   r	   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You are about to create zProceed? [Y/n] yyesZAbort)Zrepo_idrZ   z
Your repo now lives at:z  zK
You can clone it locally with the command below, and commit/push as usual.z
  git clone )rP   r+   r3   r   rV   rW   
subprocesscheck_outputdecoder5   stripFileNotFoundErrorr   r   Zorganizationr   r1   ra   inputlowerr   r   rX   rY   )
rM   rZ   stdoutr[   _	namespaceZ	full_namechoiceurlr\   r   r   r   rR      sX    

zRepoCreateCommand.runNrS   r   r   r   r   r       s   r    )rb   argparser   typingr   r   Zhuggingface_hub.hf_apir   r   r   Zrequests.exceptionsr   r^   r
   r   r+   r%   intrK   rL   r   r   r   r    r   r   r   r   <module>   s   &