
    wi\                        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j        e          Z	 ddlmZ ddlm Z  dZ!n# e"$ r dZ!Y nw xY w G d de	          Z# G d d          Z$ G d de$          Z% G d de$          Z& G d de$          Z' G d de$          Z( G 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                   *    e Zd Zedefd            ZdS )UserCommandsparserc                 :   |                      dd          }|                    dt          d           |                    ddd	
           |                    d            |                      dd          }|                    d            |                      dd          }|                    dt          d           |                    d            |                      dd          }|                    d          }|                     dd          }|                    dt          d           |                    ddd	
           |                    d            |                     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                      t          |           S N)LoginCommandargss    u/var/www/development/aibuddy-work/election-extract/venv/lib/python3.11/site-packages/huggingface_hub/commands/user.py<lambda>z2UserCommands.register_subcommand.<locals>.<lambda>H   s    L4F4F     )funcwhoamiz;Find out which huggingface.co account you are logged in as.c                      t          |           S r!   )WhoamiCommandr#   s    r%   r&   z2UserCommands.register_subcommand.<locals>.<lambda>J       ]45H5H r'   r   zLog outz--token-namez3Optional: Name of the access token to log out from.c                      t          |           S r!   )LogoutCommandr#   s    r%   r&   z2UserCommands.register_subcommand.<locals>.<lambda>R   r,   r'   authz%Other authentication related commandszAuthentication subcommandsswitchzSwitch between access tokensz0Optional: Name of the access token to switch to.c                      t          |           S r!   )AuthSwitchCommandr#   s    r%   r&   z2UserCommands.register_subcommand.<locals>.<lambda>a   s    :KD:Q:Q r'   listzList all stored access tokensc                      t          |           S r!   )AuthListCommandr#   s    r%   r&   z2UserCommands.register_subcommand.<locals>.<lambda>c   s    8M8M r'   )
add_parseradd_argumentstrset_defaultsadd_subparsers)r   login_parserwhoami_parserlogout_parserauth_parserauth_subparsersauth_switch_parserauth_list_parsers           r%   register_subcommandz UserCommands.register_subcommand;   s   ((7q(rr!!N 	" 	
 	
 	

 	!!%A 	" 	
 	
 	

 	!!'F'F!GGG))(9v)ww""(H(H"III))()CC""F 	# 	
 	
 	

 	""(H(H"III''5\']]%44:V4WW,77Ge7ff''C 	( 	
 	
 	

 	''%A 	( 	
 	
 	

 	''-Q-Q'RRR*55fCb5cc%%+M+M%NNNNNr'   N)__name__
__module____qualname__staticmethodr   rB    r'   r%   r   r   :   sA        'O$5 'O 'O 'O \'O 'O 'Or'   r   c                       e Zd Zd ZdS )BaseUserCommandc                 :    || _         t                      | _        d S r!   )r$   r	   _api)selfr$   s     r%   __init__zBaseUserCommand.__init__g   s    	GG			r'   N)rC   rD   rE   rM   rG   r'   r%   rI   rI   f   s#            r'   rI   c                       e Zd Zd ZdS )r"   c                     t          dd           t          j                     t          | j        j        | j        j                   d S )Nzhuggingface-cli loginzhf auth login)tokenadd_to_git_credential)r   r   set_verbosity_infor   r$   rP   rQ   rL   s    r%   runzLoginCommand.runm   sR     !8/JJJ"$$$)/"&)"A	
 	
 	
 	
 	
 	
r'   NrC   rD   rE   rT   rG   r'   r%   r"   r"   l   s#        
 
 
 
 
r'   r"   c                       e Zd Zd ZdS )r.   c                     t          dd           t          j                     t          | j        j                   d S )Nzhuggingface-cli logoutzhf auth logout)
token_name)r   r   rR   r   r$   rX   rS   s    r%   rT   zLogoutCommand.runx   s?     !9;KLLL"$$$$).//////r'   NrU   rG   r'   r%   r.   r.   w   s#        0 0 0 0 0r'   r.   c                   T    e Zd Zd Zdee         fdZdee         dee         fdZdS )r2   c                    t          dd           t          j                     | j        j        }||                                 }|t          d           t                       t          || j        j	                   d S )Nzhuggingface-cli auth switchzhf auth switchz!No token name provided. Aborting.)rQ   )
r   r   rR   r$   rX   _select_token_nameprintexitr   rQ   )rL   rX   s     r%   rT   zAuthSwitchCommand.run   s     !>@PQQQ"$$$Y)
0022J5666FFFJdi6UVVVVVVr'   returnc                 f   t          t                                                                }|st                              d           d S t
          r|                     |          S t          d           t          |d          D ]\  }}t          | d|            	 	 t          d          }|
                                dk    rd S t          |          dz
  }d|cxk    rt          |          k     rn n||         S t          d	           n# t          $ r t          d
           Y nw xY w)Nz+No stored tokens found. Please login first.zAvailable stored tokens:r   z. Tz=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.)r3   r   keysloggererror_inquirer_py_available_select_token_name_tuir\   	enumerateinputlowerintlen
ValueError)rL   token_namesirX   choiceindexs         r%   r[   z$AuthSwitchCommand._select_token_name   sk   ,..335566 	LLFGGG4! 	<..{;;;()))&{A66 	( 	(MAzQ&&*&&''''	N
N^__<<>>S((4Fa0000K 0 000000&u--@AAAA N N NLMMMMMN	Ns   #'D 6D D D/.D/rl   c                     d |D             }	 t          j        d|d                                           S # t          $ r t                              d           Y d S w xY w)Nc                 0    g | ]}t          ||           S )namer   ).0rX   s     r%   
<listcomp>z<AuthSwitchCommand._select_token_name_tui.<locals>.<listcomp>   s%    UUU:6*:666UUUr'   zSelect a token to switch to:)messagechoicesdefaultzToken selection cancelled.)r   selectexecuteKeyboardInterruptrb   info)rL   rl   rw   s      r%   re   z(AuthSwitchCommand._select_token_name_tui   s~    UUUUU	?6   gii	
 ! 	 	 	KK455544	s   (7 $AAN)	rC   rD   rE   rT   r   r8   r[   r   re   rG   r'   r%   r2   r2      st        W W WNHSM N N N N4
$s) 
 
 
 
 
 
 
r'   r2   c                       e Zd Zd ZdS )r5   c                 h    t          dd           t          j                     t                       d S )Nzhuggingface-cli auth listzhf auth list)r   r   rR   r   rS   s    r%   rT   zAuthListCommand.run   s/     !<nMMM"$$$r'   NrU   rG   r'   r%   r5   r5      s#            r'   r5   c                       e Zd Zd ZdS )r+   c                 |   t          dd           t                      }|t          d           t                       	 | j                            |          }t          |d                    d |d         D             }|r5t          t          j        d          d                    |                     t          d	k    rt          d
t                      d S d S # t          $ rT}t          |           t          t          j        |j        j                             t          d           Y d }~d S d }~ww xY w)Nzhuggingface-cli whoamizhf auth whoamizNot logged inrs   c                     g | ]
}|d          S rr   rG   )rt   orgs     r%   ru   z%WhoamiCommand.run.<locals>.<listcomp>   s    888CCK888r'   orgszorgs: ,zhttps://huggingface.coz(Authenticated through private endpoint: r   )r   r   r\   r]   rK   r)   r   boldjoinr   r   redresponsetext)rL   rP   r|   r   es        r%   rT   zWhoamiCommand.run   s6    !9;KLLL=/"""FFF	9##E**D$v,884<888D ;di))388D>>:::333KKKLLLLL 43 	 	 	!HHH$(1:?++,,,GGGGGGGGG	s   BC 
D;'A	D66D;NrU   rG   r'   r%   r+   r+      s#            r'   r+   N)*__doc__argparser   typingr   r   requests.exceptionsr   huggingface_hub.commandsr   huggingface_hub.constantsr   huggingface_hub.hf_apir	   _loginr   r   r   r   utilsr   r   r   
_cli_utilsr   r   
get_loggerrC   rb   
InquirerPyr   InquirerPy.base.controlr   rd   ImportErrorr   rI   r"   r.   r2   r5   r+   rG   r'   r%   <module>r      s   & ' & & & & & ! ! ! ! ! ! ! ! ) ) ) ) ) ) > > > > > > . . . . . . ( ( ( ( ( ( : : : : : : : : : : : : 9 9 9 9 9 9 9 9 9 9 6 6 6 6 6 6 6 6 
	H	%	%#######......! # # #"#)O )O )O )O )O, )O )O )OX       
 
 
 
 
? 
 
 
0 0 0 0 0O 0 0 02 2 2 2 2 2 2 2j    o       O     s   A' 'A10A1