o
    -wi0,                     @   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m	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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ded$ee d%ee d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)ConnectionError)AuthenticationError
UsageError)Settings)wandb_setup   )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    R/home/ubuntu/sommelier/.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verifyreferrerc              
   C   s    t | t| |||||||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.
        referrer: (string, optional) The referrer to use in the URL login request.

    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
    r#   r(   r)   r   r*   r+   r,   r-   )r"   _loginr.   r    r    r!   login)   s   %r0   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!   r1   [   s
    r1   c                   @   s
  e Zd Z						ddeed  dee dee dee dee dee fd	d
Zd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 dee deee ef fddZ	d dee deee ef fddZdeddfddZdS )!_WandbLoginNr#   r$   r*   r   r(   r)   r+   c                 C   sF   || _ |||||d}|dk| _t | _| jj| | jj| _d S )N)r#   api_keyr   r*   login_timeoutr%   )_reloginis_anonymousr	   	singleton_wandb_setupsettingsupdate_from_dict	_settings)selfr#   r*   r   r(   r)   r+   login_settingsr    r    r!   __init__c   s   	

z_WandbLogin.__init__r   c                 C   s6   t  }| jr|jddddd d S |jdddd d S )Nr#   r%   Tr   )r   r?   r   r   )rE   apir    r    r!    _update_global_anonymous_setting{   s   z,_WandbLogin._update_global_anonymous_settingc                 C   s   t j| jdduS )z5Returns whether an API key is set or can be inferred.rB   N)r   r<   rD   )rE   r    r    r!   is_apikey_configured   s   z _WandbLogin.is_apikey_configuredc                 C   s   | j  }|rK| jjrdtj| jjdd nd}| j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.z to green)fg z (yellow)zCurrently logged in as: zW&B API key is configuredzUse z`wandb login --relogin`T)boldz to force reloginz. F)repeatN)rA   _get_usernamerD   r   clickstyleentity_get_entity
_get_teamswandbtermlog)rE   usernamehost_strrV   
entity_strlogin_state_strlogin_info_strr    r    r!   _print_logged_in_message   s*   

z$_WandbLogin._print_logged_in_messagec              
   C   sn   | j jr| j jstd |r5z
t| j | W dS  tjy4 } ztt| W Y d}~dS d}~ww dS )z)Saves the API key to disk for future use.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)	rD   	_notebooksilentrY   termwarnr   	write_keyWriteNetrcErrorstr)rE   r(   er    r    r!   try_save_api_key   s   z_WandbLogin.try_save_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r<   N)	dictr1   r9   r:   rA   rB   rC   _offline_update_user_settings)rE   r(   ri   rF   r    r    r!   update_session   s   	



z_WandbLogin.update_sessionr-   c              
   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	yM   t
d d tjf Y S w |du rWd tjfS |s^d tjfS |tjfS )NT)rH   
no_offline	no_creater-   r   z"W&B disabled due to login timeout.F)r   rD   r   prompt_api_keyr*   
ValueErrorrY   	termerrorargsTimeoutErrorrZ   r1   r:   r8   r9   r7   )rE   r-   rH   r(   rg   r    r    r!   _prompt_api_key   s.   





z_WandbLogin._prompt_api_keyc                 C   s<   |  |\}}|tjkr| jjrdnd}td| ||f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 )ry   r1   r8   rD   x_cli_only_moder   )rE   r-   r(   ri   	directiver    r    r!   rt      s   
z_WandbLogin.prompt_api_keyc                 C   sR   t |d}z| }W n ty   td ty    tdw |s'tdd S )Nrm   z0Unable to connect to server to verify API token.z.An error occurred while verifying the API key.z=API key verification failed. Make sure your API key is valid.)r   validate_api_keyr   r   	Exception)rE   r(   rH   is_api_key_validr    r    r!   _verify_login   s   
z_WandbLogin._verify_login)NNNNNN)r   N)N)r4   r5   r6   r   r   boolrf   intrG   rI   rK   r`   rh   r1   r7   rq   r   ry   rt   r   r    r    r    r!   r;   b   sZ    



#


r;   models)
r#   r(   r)   r   r*   r+   r,   r-   _silent_disable_warning)r&   r%   r'   r   r   c        
         C   s  t jd ur|	st d dS t| |||||d}
|
jjrdS |
jjr-|
jjs-t d dS t j	 r>t j
 s>t d dS |
jjrDdS d}d }|d u rbtj|
jd}|rZ|sZd}n|
j|d\}}|ri|
| |s{|
| |
j||d	 |
  |r|s|
  |d uS )
Nz7Calling wandb.login() after wandb.init() has no effect.T)r#   r*   r   r(   r)   r+   z'Unable to verify login in offline mode.FzQTo use W&B in kaggle you must enable internet in the settings panel on the right.rJ   )r-   )ri   )rY   runrc   r;   rD   _noopro   rz   util
_is_kaggle_has_internetrv   identity_token_filer   r<   rt   r   rh   rq   rI   r`   )r#   r(   r)   r   r*   r+   r,   r-   r   r   wloginkey_is_pre_configured
key_statusr    r    r!   r/   	  sP   

	


r/   )F)NNNNNNFN)"__doc__enumr   typingr   r   r   rT   requests.exceptionsr   rY   wandb.errorsr   r   wandb.old.settingsr   r   	wandb.sdkr	   apisr   internal.internal_apir   libr   rf   r   r"   r   r0   Enumr1   r;   r/   r    r    r    r!   <module>   s    
	
2 *
	
