o
    ni*                     @   s  d Z ddlZddlZddlmZmZmZ ddlZddl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 d%dee deddfddZ							d&deed  dee dee dee dee dee dedefddZG dd dejZG dd dZddddddddddd
deed  dee dee dee dee dee d ee d!ee d"ee fd#d$ZdS )'zZLog in to Weights & Biases.

This authenticates your machine to log data to your account.
    N)LiteralOptionalTuple)AuthenticationError
UsageError)Settings   )InternalApi   )Api)apikeyFhostcloudreturnc                 C   sx   t  }| dks| du r(|r(|jdddd tjt r&|jddd dS dS | r:| d} |jd| ddd dS dS )zWrite the host parameter to the global settings file.

    This takes the parameter from wandb.login or wandb login for use by the
    application's APIs.
    zhttps://api.wandb.aiNbase_urlT)globallypersist)r   /)	r	   clear_settingospathexistsOldSettings_local_pathrstripset_setting)r   r   _api r   S/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/sdk/wandb_login.py_handle_host_wandb_setting   s   
r   	anonymous)mustallowneverkeyreloginforcetimeoutverifyc                 C   sp   t | t jjrdS t| |||||d}|r2ddlm} |jj	}	|	dus(J |	j
j}
|
s2td|r6dS dS )a^  Set up W&B login credentials.

    By default, this will only store credentials locally without
    verifying them with the W&B server. To verify credentials, pass
    `verify=True`.

    Args:
        anonymous: (string, optional) Can be "must", "allow", or "never".
            If set to "must", always log a user in anonymously. If set to
            "allow", only create an anonymous user if the user
            isn't already logged in. If set to "never", never log a
            user anonymously. Default set to "never".
        key: (string, optional) The API key to use.
        relogin: (bool, optional) If true, will re-prompt for API key.
        host: (string, optional) The host to connect to.
        force: (bool, optional) If true, will force a relogin.
        timeout: (int, optional) Number of seconds to wait for user input.
        verify: (bool) Verify the credentials with the W&B server.

    Returns:
        bool: if key is configured

    Raises:
        AuthenticationError - if api_key fails verification with the server
        UsageError - if api_key cannot be configured and no tty
    Tr    r$   r%   r   r&   r'   r
   )wandb_setupNz=API key verification failed. Make sure your API key is valid.F)r   wandbsetup	_settings_noop_login r*   _WandbSetup	_instance_server_viewerr   )r    r$   r%   r   r&   r'   r(   
configuredr*   	singletonviewerr   r   r   login(   s*   #	r8   c                   @   s   e Zd ZdZdZdZdZdS )ApiKeyStatusr
   r         N)__name__
__module____qualname__VALIDNOTTYOFFLINEDISABLEDr   r   r   r   r9   e   s
    r9   c                   @   s  e Zd Zdd Zddddddddeed  dee dee d	ee d
ee dee ddfddZ	defddZ
dd Zdd ZdeddfddZdeddfddZdefddZd(ddZdeddfdd Zejfdee d!eddfd"d#Zdeee ef fd$d%Zd(d&d'ZdS ))_WandbLoginc                 C   s.   d | _ d | _d | _d | _d | _d | _d | _d S N)r-   _backend_silent_entity_wl_key_reloginselfr   r   r   __init__m   s   
z_WandbLogin.__init__Nr)   r    r"   r!   r#   r$   r%   r   r&   r'   r   c                C   sL   || _ |||||d}tjtjdi dd | D d| _| jj| _dS )z:Updates login-related settings on the global setup object.)r    api_keyr   r&   login_timeoutc                 S   s   i | ]\}}|d ur||qS rD   r   ).0kvr   r   r   
<dictcomp>   s    z%_WandbLogin.setup.<locals>.<dictcomp>settingsNr   )rJ   r+   r,   r   itemsrH   rV   r-   )rL   r    r$   r%   r   r&   r'   login_settingsr   r   r   r,   v   s   	z_WandbLogin.setupc                 C   s   t j| jdduS )z5Returns whether an API key is set or can be inferred.rU   N)r   rO   r-   rK   r   r   r   is_apikey_configured   s   z _WandbLogin.is_apikey_configuredc                 C   s   | j jd uS rD   )r-   identity_token_filerK   r   r   r   should_use_identity_token   s   z%_WandbLogin.should_use_identity_tokenc                 C   
   || _ d S rD   )rE   )rL   backendr   r   r   set_backend      
z_WandbLogin.set_backendsilentc                 C   r\   rD   )rF   )rL   r`   r   r   r   
set_silent   r_   z_WandbLogin.set_silententityc                 C   r\   rD   )rG   )rL   rb   r   r   r   
set_entity   r_   z_WandbLogin.set_entityc                 C   s4   |   }| jjs| jrd}|sdS | js|   |S )zReturns whether the user is logged in (i.e. an API key exists).

        If the user is logged in, this also prints an informational message.
        F)rY   r-   r%   rJ   rF   _print_logged_in_message)rL   apikey_configuredr   r   r   r8      s   z_WandbLogin.loginc                 C   s   | j  }|r6| jp| j  }d}|r)|| j  v r)||kr)dtj|dd d}dtj|dd | }nd}dtjd	d
d d}tj| d| dd dS )z5Prints a message telling the user they are logged in.r0   z (yellow)fg)zCurrently logged in as: zW&B API key is configuredzUse z`wandb login --relogin`T)boldz to force reloginz. F)repeatN)	rH   _get_usernamerG   _get_entity
_get_teamsclickstyler+   termlog)rL   usernamerb   
entity_strlogin_state_strlogin_info_strr   r   r   rd      s   

z$_WandbLogin._print_logged_in_messagec                 C   s<   | j jr| j jstd t| j | | | || _dS )z:Saves the API key and updates the the global setup object.zIf you're specifying your api key in code, ensure this code is not shared publicly.
Consider setting the WANDB_API_KEY environment variable, or running `wandb login` from the command line.N)	r-   	_notebookr`   r+   termwarnr   	write_keyupdate_sessionrI   )rL   r$   r   r   r   configure_api_key   s   

z_WandbLogin.configure_api_keystatusc                 C   sj   t  }|tjkrt dd}n|tjkrt dd}n|r t |d}| jj| | jjjs3| j	  dS dS )zUpdates mode and API key settings on the global setup object.

        If we're online, this also pulls in user settings from the server.
        offline)modedisabled)rO   N)
dictr9   rA   rB   rH   r-   update_from_dictrV   _offline_update_user_settings)rL   r$   rz   rX   r   r   r   rx      s   	



z_WandbLogin.update_sessionc              
   C   s   t | j}	 ztj| j|| jr| jjnd | jr| jjnd d}W n, ty: } zt|jd  W Y d }~qd }~w t	yL   t
d d tjf Y S w |du rVd tjfS |s]d tjfS |tjfS )NT)api
no_offline	no_creater   z"W&B disabled due to login timeout.F)r   r-   r   prompt_api_keyr&   
ValueErrorr+   	termerrorargsTimeoutErrorrp   r9   rB   r@   rA   r?   )rL   r   r$   er   r   r   _prompt_api_key   s,   





z_WandbLogin._prompt_api_keyc                 C   sJ   |   \}}|tjkr| jjrdnd}td| | j||d || _dS )z1Updates the global API key by prompting the user.zwandb login [your_api_key]zwandb.login(key=[your_api_key])z&api_key not configured (no-tty). call )rz   N)r   r9   r@   r-   x_cli_only_moder   rx   rI   )rL   r$   rz   	directiver   r   r   r     s   

z_WandbLogin.prompt_api_key)r   N)r<   r=   r>   rM   r   r   strboolintr,   rY   r[   r^   ra   rc   r8   rd   ry   r9   r?   rx   r   r   r   r   r   r   r   rC   l   sR    
	


rC   )
r    r$   r%   r   r&   r'   rE   rF   _disable_warningrG   rN   rF   r   rG   c        
         C   s   t jd ur|st d dS t }
|r|
| |r|
| |	r&|
|	 |
j| |||||d |
jj	r7dS t j
 rHt j
 sHt d dS |
 rNdS |
 }|rY|
| |r]|S |sc|
  |
jpgdS )Nz7Calling wandb.login() after wandb.init() has no effect.Tr)   FzQTo use W&B in kaggle you must enable internet in the settings panel on the right.)r+   runrv   rC   r^   ra   rc   r,   r-   r   util
_is_kaggle_has_internetr   r[   r8   ry   r   rI   )r    r$   r%   r   r&   r'   rE   rF   r   rG   wlogin	logged_inr   r   r   r/     sH   




	

r/   )F)NNNNNNF)__doc__enumr   typingr   r   r   rn   r+   wandb.errorsr   r   wandb.old.settingsr   r   apisr	   internal.internal_apir   libr   wandb_settingsr   r   r   r   r8   Enumr9   rC   r/   r   r   r   r   <module>   s    

= ,
	
