U
    ”Ê¹j\  ã                   @   sB  d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZmZ dd
lmZmZmZ ddlmZmZ e e¡Zz ddlmZ ddlm Z  dZ!W n e"k
rÎ   dZ!Y nX G dd„ de	ƒ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 ) aD  Contains commands to authenticate to the Hugging Face Hub and interact with your repositories.

Usage:
    # login and save token locally.
    huggingface-cli login --token=hf_*** --add-to-git-credential

    # switch between tokens
    huggingface-cli auth switch

    # list all tokens
    huggingface-cli auth list

    # logout from a specific token, if no token-name is provided, all tokens will be deleted from your machine.
    huggingface-cli logout --token-name=your_token_name

    # find out which huggingface.co account you are logged in as
    huggingface-cli whoami
é    )Ú_SubParsersAction)ÚListÚOptional)Ú	HTTPError)ÚBaseHuggingfaceCLICommand)ÚENDPOINT)ÚHfApié   )Ú	auth_listÚauth_switchÚloginÚlogout)Úget_stored_tokensÚ	get_tokenÚloggingé   )ÚANSIÚshow_deprecation_warning)Úinquirer©ÚChoiceTFc                   @   s   e Zd Zeedœdd„ƒZdS )ÚUserCommands)Úparserc                 C   s  | j ddd�}|jdtdd� |jddd	d
� |jdd„ d� | j ddd�}|jdd„ d� | j ddd�}|jdt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	d
� |jdd„ d� |j ddd�}|jdd„ d� d S ) Nr   z8Log in using a token from huggingface.co/settings/tokens)Úhelpz--tokenz;Token generated from https://huggingface.co/settings/tokens)Útyper   z--add-to-git-credentialÚ
store_truez.Optional: Save token to git credential helper.)Úactionr   c                 S   s   t | ƒS ©N)ÚLoginCommand©Úargs© r!   ú\/var/www/html/bwave/python/venv/lib/python3.8/site-packages/huggingface_hub/commands/user.pyÚ<lambda>H   ó    z2UserCommands.register_subcommand.<locals>.<lambda>)ÚfuncÚwhoamiz;Find out which huggingface.co account you are logged in as.c                 S   s   t | ƒS r   )ÚWhoamiCommandr   r!   r!   r"   r#   J   r$   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                 S   s   t | ƒS r   )ÚLogoutCommandr   r!   r!   r"   r#   R   r$   Úauthz%Other authentication related commandszAuthentication subcommandsÚswitchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                 S   s   t | ƒS r   )ÚAuthSwitchCommandr   r!   r!   r"   r#   a   r$   ÚlistzList all stored access tokensc                 S   s   t | ƒS r   )ÚAuthListCommandr   r!   r!   r"   r#   c   r$   )Ú
add_parserÚadd_argumentÚstrÚset_defaultsÚadd_subparsers)r   Zlogin_parserZwhoami_parserZlogout_parserZauth_parserZauth_subparsersZauth_switch_parserZauth_list_parserr!   r!   r"   Úregister_subcommand;   sJ    ýýýýýz UserCommands.register_subcommandN)Ú__name__Ú
__module__Ú__qualname__Ústaticmethodr   r3   r!   r!   r!   r"   r   :   s   r   c                   @   s   e Zd Zdd„ ZdS )ÚBaseUserCommandc                 C   s   || _ tƒ | _d S r   )r    r   Ú_api)Úselfr    r!   r!   r"   Ú__init__g   s    zBaseUserCommand.__init__N)r4   r5   r6   r;   r!   r!   r!   r"   r8   f   s   r8   c                   @   s   e Zd Zdd„ ZdS )r   c                 C   s*   t ddƒ t ¡  t| jj| jjd� d S )Nzhuggingface-cli loginzhf auth login)ÚtokenÚadd_to_git_credential)r   r   Úset_verbosity_infor   r    r<   r=   ©r:   r!   r!   r"   Úrunm   s    
þzLoginCommand.runN©r4   r5   r6   r@   r!   r!   r!   r"   r   l   s   r   c                   @   s   e Zd Zdd„ ZdS )r(   c                 C   s$   t ddƒ t ¡  t| jjd� d S )Nzhuggingface-cli logoutzhf auth logout)Ú
token_name)r   r   r>   r   r    rB   r?   r!   r!   r"   r@   x   s    
zLogoutCommand.runNrA   r!   r!   r!   r"   r(   w   s   r(   c                   @   s>   e Zd Zdd„ Zee dœdd„Zee ee dœdd„Zd	S )
r+   c                 C   sT   t ddƒ t ¡  | jj}|d kr*|  ¡ }|d kr@tdƒ tƒ  t|| jj	d� d S )Nzhuggingface-cli auth switchzhf auth switchz!No token name provided. Aborting.)r=   )
r   r   r>   r    rB   Ú_select_token_nameÚprintÚexitr   r=   )r:   rB   r!   r!   r"   r@   €   s    
zAuthSwitchCommand.run)Úreturnc                 C   sØ   t tƒ  ¡ ƒ}|s t d¡ d S tr.|  |¡S tdƒ t|dƒD ]\}}t|› d|› �ƒ q@zXt	dƒ}| 
¡ dkrxW d S t|ƒd }d|  krœt|ƒk rªn n
|| W S tdƒ W q\ tk
rÐ   td	ƒ Y q\X q\d S )
Nz+No stored tokens found. Please login first.zAvailable stored tokens:r   z. z=Enter the number of the token to switch to (or 'q' to quit): Úqr   z$Invalid selection. Please try again.z4Invalid input. Please enter a number or 'q' to quit.)r,   r   ÚkeysÚloggerÚerrorÚ_inquirer_py_availableÚ_select_token_name_tuirD   Ú	enumerateÚinputÚlowerÚintÚlenÚ
ValueError)r:   Útoken_namesÚirB   ÚchoiceÚindexr!   r!   r"   rC   �   s&    


z$AuthSwitchCommand._select_token_name)rS   rF   c                 C   sJ   dd„ |D ƒ}zt jd|d d� ¡ W S  tk
rD   t d¡ Y d S X d S )Nc                 S   s   g | ]}t ||d �‘qS )©Únamer   )Ú.0rB   r!   r!   r"   Ú
<listcomp>¨   s     z<AuthSwitchCommand._select_token_name_tui.<locals>.<listcomp>zSelect a token to switch to:)ÚmessageÚchoicesÚdefaultzToken selection cancelled.)r   ÚselectÚexecuteÚKeyboardInterruptrI   Úinfo)r:   rS   r\   r!   r!   r"   rL   §   s    ý
z(AuthSwitchCommand._select_token_name_tuiN)	r4   r5   r6   r@   r   r0   rC   r   rL   r!   r!   r!   r"   r+      s   r+   c                   @   s   e Zd Zdd„ ZdS )r-   c                 C   s   t ddƒ t ¡  tƒ  d S )Nzhuggingface-cli auth listzhf auth list)r   r   r>   r
   r?   r!   r!   r"   r@   µ   s    
zAuthListCommand.runNrA   r!   r!   r!   r"   r-   ´   s   r-   c                   @   s   e Zd Zdd„ ZdS )r'   c              
   C   sÎ   t ddƒ tƒ }|d kr&tdƒ tƒ  z^| j |¡}t|d ƒ dd„ |d D ƒ}|rltt d¡d	 |¡ƒ t	d
kr‚tdt	› �ƒ W nD t
k
rÈ } z&t|ƒ tt |jj¡ƒ tdƒ W 5 d }~X Y nX d S )Nzhuggingface-cli whoamizhf auth whoamizNot logged inrX   c                 S   s   g | ]}|d  ‘qS rW   r!   )rY   Úorgr!   r!   r"   rZ   Ç   s     z%WhoamiCommand.run.<locals>.<listcomp>Úorgszorgs: ú,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   r   rD   rE   r9   r&   r   ÚboldÚjoinr   r   ÚredÚresponseÚtext)r:   r<   ra   rc   Úer!   r!   r"   r@   ½   s"    
zWhoamiCommand.runNrA   r!   r!   r!   r"   r'   ¼   s   r'   N)*Ú__doc__Úargparser   Útypingr   r   Zrequests.exceptionsr   Zhuggingface_hub.commandsr   Zhuggingface_hub.constantsr   Zhuggingface_hub.hf_apir   Z_loginr
   r   r   r   Úutilsr   r   r   Z
_cli_utilsr   r   Z
get_loggerr4   rI   Z
InquirerPyr   ZInquirerPy.base.controlr   rK   ÚImportErrorr   r8   r   r(   r+   r-   r'   r!   r!   r!   r"   Ú<module>   s.   

,5