a
    h                     @   s<  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 eeZz ddlmZ ddlmZ dZ W n e!y   dZ Y n0 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 ) a  Contains commands to authenticate to the Hugging Face Hub and interact with your repositories.

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

    # switch between tokens
    hf auth switch

    # list all tokens
    hf auth list

    # logout from all tokens
    hf auth logout

    # check which account you are logged in as
    hf auth whoami
    )_SubParsersAction)ListOptional)	HTTPError)BaseHuggingfaceCLICommand)ENDPOINT)HfApi   )	auth_listauth_switchloginlogout)get_stored_tokens	get_tokenlogging   )ANSI)inquirerChoiceTFc                   @   s   e Zd ZeedddZdS )AuthCommands)parserc                    s  | j ddd  jdd} j fd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t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d |jdd d |j ddd}|jd d d d S )!Nauthz,Manage authentication (login, logout, etc.).)helpzAuthentication subcommandsc                    s      S N)
print_helpargsZauth_parser T/var/www/html/assistant/venv/lib/python3.9/site-packages/huggingface_hub/cli/auth.py<lambda>B       z2AuthCommands.register_subcommand.<locals>.<lambda>)funcr   z8Log in using a token from huggingface.co/settings/tokensz--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 r   )	AuthLoginr   r   r   r    r!   R   r"   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                 S   s   t | S r   )
AuthLogoutr   r   r   r    r!   [   r"   whoamiz;Find out which huggingface.co account you are logged in as.c                 S   s   t | S r   )
AuthWhoamir   r   r   r    r!   a   r"   switchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                 S   s   t | S r   )
AuthSwitchr   r   r   r    r!   o   r"   listzList all stored access tokensc                 S   s   t | S r   )AuthListr   r   r   r    r!   r   r"   )
add_parseradd_subparsersset_defaultsadd_argumentstr)r   Zauth_subparsersZlogin_parserZlogout_parserZwhoami_parserZauth_switch_parserZauth_list_parserr   r   r    register_subcommand;   sT    z AuthCommands.register_subcommandN)__name__
__module____qualname__staticmethodr   r4   r   r   r   r    r   :   s   r   c                   @   s   e Zd Zdd ZdS )BaseAuthCommandc                 C   s   || _ t | _d S r   )r   r   _api)selfr   r   r   r    __init__v   s    zBaseAuthCommand.__init__N)r5   r6   r7   r<   r   r   r   r    r9   u   s   r9   c                   @   s   e Zd Zdd ZdS )r'   c                 C   s    t   t| jj| jjd d S )N)tokenadd_to_git_credential)r   set_verbosity_infor   r   r=   r>   r;   r   r   r    run|   s
    zAuthLogin.runNr5   r6   r7   rA   r   r   r   r    r'   {   s   r'   c                   @   s   e Zd Zdd ZdS )r(   c                 C   s   t   t| jjd d S )N)
token_name)r   r?   r   r   rC   r@   r   r   r    rA      s    zAuthLogout.runNrB   r   r   r   r    r(      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   sJ   t   | jj}|d u r |  }|d u r6td t  t|| jjd d S )Nz!No token name provided. Aborting.)r>   )	r   r?   r   rC   _select_token_nameprintexitr   r>   )r;   rC   r   r   r    rA      s    zAuthSwitch.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rt|k rn n
|| W S td W q\ ty   td	 Y q\0 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_tuirE   	enumerateinputlowerintlen
ValueError)r;   token_namesirC   choiceindexr   r   r    rD      s&    


zAuthSwitch._select_token_name)rT   rG   c                 C   sH   dd |D }zt jd|d d W S  tyB   td Y d S 0 d S )Nc                 S   s   g | ]}t ||d qS )namer   ).0rC   r   r   r    
<listcomp>   r"   z5AuthSwitch._select_token_name_tui.<locals>.<listcomp>zSelect a token to switch to:)messagechoicesdefaultzToken selection cancelled.)r   selectexecuteKeyboardInterruptrJ   info)r;   rT   r]   r   r   r    rM      s    
z!AuthSwitch._select_token_name_tuiN)	r5   r6   r7   rA   r   r3   rD   r   rM   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 S r   )r   r?   r
   r@   r   r   r    rA      s    zAuthList.runNrB   r   r   r   r    r.      s   r.   c                   @   s   e Zd Zdd ZdS )r*   c              
   C   s   t  }|d u rtd t  z^| j|}t|d  dd |d D }|rbttdd| tdkrxtd	t  W nF t	y } z.t| tt
|jj td
 W Y d }~n
d }~0 0 d S )NzNot logged inrY   c                 S   s   g | ]}|d  qS rX   r   )rZ   orgr   r   r    r[      r"   z"AuthWhoami.run.<locals>.<listcomp>orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   rE   rF   r:   r)   r   boldjoinr   r   redresponsetext)r;   r=   rb   rd   er   r   r    rA      s     zAuthWhoami.runNrB   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   Z
get_loggerr5   rJ   Z
InquirerPyr   ZInquirerPy.base.controlr   rL   ImportErrorr   r9   r'   r(   r,   r.   r*   r   r   r   r    <module>   s.   

;	3