o
    xiS(                  
   @  s   d Z ddl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mZ ddlmZmZ dd	lmZ dddddd
defd:ddZd;ddZdddddd
d d!dd"	d<d(d)Zd=d/d0Zd>d3d4Zd?d6d7Zd@d8d9ZdS )AzZLog in to Weights & Biases.

This authenticates your machine to log data to your account.
    )annotationsN)AuthenticationErrorterm)wandb_setup)settings_filewbauth)UNSETDoNotSet   )InternalApiFkey
str | Nonereloginbool | Nonehostforcetimeout
int | Noneverifyboolreferrer	anonymousr	   returnc              	   C  s   |t urtjddd tjdurtd dS t j}|jr!dS |j	r.|j
s.td dS |r5|d}t| |d t| ||||||pGd	d
\}	}
|	S )a  Log into W&B.

    You generally don't have to use this because most W&B methods that need
    authentication can log in implicitly. This is the programmatic counterpart
    to the `wandb login` CLI.

    This updates global credentials for the session (affecting all wandb usage
    in the current Python process after this call) and possibly the .netrc file.

    If the identity_token_file setting is set, like through the
    WANDB_IDENTITY_TOKEN_FILE environment variable, then this is a no-op.

    Otherwise, if an explicit API key is provided, it is used and written to
    the system .netrc file. If no key is provided, but the session is already
    authenticated, then the session key is used for verification (if verify
    is True) and the .netrc file is not updated.

    If none of the above is true, then this gets the API key from the first of:

    - The WANDB_API_KEY environment variable
    - The api_key setting in a system or workspace settings file
    - The .netrc file (either ~/.netrc, ~/_netrc or the path specified by the
      NETRC environment variable)
    - An interactive prompt (if available)

    Args:
        key: The API key to use.
        relogin: If true, get the API key from an interactive prompt, skipping
            reading .netrc, environment variables, etc.
        host: The W&B server URL to connect to.
        force: If true, disallows selecting offline mode in the interactive
            prompt.
        timeout: Number of seconds to wait for user input in the interactive
            prompt. This can be used as a failsafe if an interactive prompt
            is incorrectly shown in a non-interactive environment.
        verify: Verify the credentials with the W&B server and raise an
            AuthenticationError on failure.
        referrer: The referrer to use in the URL login request for analytics.

    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.
    z^The anonymous parameter to wandb.login() has no effect and will be removed in future versions.FrepeatNz7Calling wandb.login() after wandb.init() has no effect.z'Unable to verify login in offline mode./r   models)r   r   r   r   r   r   r   )r   r   termwarnwandbrunr   	singletonsettings_noop_offlinex_cli_only_moderstrip_update_system_settingsread_system_settings_login)r   r   r   r   r   r   r   r   global_settings	logged_in_ r-   I/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/wandb_login.pylogin   s<   8





	r/   system_settingssettings_file.SettingsFilesNonec             
   C  s   | j ddd |r|dkr| j ddd n| jd|dd z|   W dS  tjy? } ztt| W Y d}~dS d}~ww )z(Update the user's system settings files.r   T)globallyzhttps://api.wandb.aibase_urlN)clearsetsaver   SaveSettingsErrorr   r   str)r0   r   er-   r-   r.   r'   q   s   r'   r   T)	r   r   r   r   r   r   r   update_api_key_silentfloat | Noner9   r;   r<   tuple[bool, str | None]c        	         C  s  t  j}	|du rtj|	j|	jd}
nt|}
|du r|	j}|du r&|	j}|du r-|	j	}|du r4|	j
}tj rEtj sEtd dS | rQt| |
|	||d}n
t|	|
||||d}|rlt|tjrlt|j|jjd t    |s|t|	t|
d |du rdS t|tjrd	|jfS d
S )aY  Log in to W&B.

    Arguments are the same as for wandb.login() with the following additions:

    Args:
        update_api_key: If true and an explicit API key is given, it will be
            saved to the .netrc file.
        _silent: If true, will not print any messages to the console.

    Returns:
        A pair (is_successful, key).
    N)app_urlzQTo use W&B in kaggle you must enable internet in the settings panel on the right.)FN)r   r"   r;   silent)r   r   r   r   input_timeout)r   r4   r   T)TN)r   r!   r"   r   HostUrlr4   r?   r   r   login_timeoutr@   r   util
_is_kaggle_has_internetr   	termerror_use_explicit_key_find_or_prompt_for_key
isinstance
AuthApiKey_verify_loginapi_keyr   urlupdate_user_settings_print_logged_in_messager9   )r   r   r   r   r   r   r   r;   r<   r"   host_urlauthr-   r-   r.   r)      sV   

	
r)   r"   wandb.Settingswbauth.HostUrlr@   wbauth.Authc             
   C  s   |j r
|s
td tj|| d}tj|dd |rCztj|jj|j	d W |S  tj
yB } ztt| W Y d}~|S d}~ww |S )zDLog in with an explicit key.

    Same arguments as `_login()`.
    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.)r   rM   wandb.login())sourceN)	_notebookr   r   r   rK   use_explicit_authwrite_netrc_authr   rN   rM   WriteNetrcErrorr   r9   )r   r"   r   r;   r@   rR   r:   r-   r-   r.   rH      s&   
rH   rA   wbauth.Auth | Nonec             	   C  sl   d}d}zt j|d|||||d}W n ty   d}Y nw |s4|r,td d| _|S td d	| _|S )
zGLog in without an explicit key.

    Same arguments as `_login()`.
    FNrV   )r   rW   
no_offline	no_creater   rA   r   Tz"W&B disabled due to login timeout.disabledzUsing W&B in offline mode.offline)r   authenticate_sessionTimeoutErrorr   r   modetermlog)r"   r   r   r   r   rA   	timed_outrR   r-   r-   r.   rI      s.   



rI   r4   c              
   C  s   ddl m} t| d|id}z| }W n% |y) } z	td| d|d }~w ty9 } ztd|d }~ww |sFtd| d	d
 d S )Nr   )ConnectionErrorr4   )rM   default_settingszUnable to connect to z to verify API token.z.An error occurred while verifying the API key.z%API key verification failed for host .z! Make sure your API key is valid.)requests.exceptionsrf   r   validate_api_keyr   	Exception)r   r4   rf   apiis_api_key_validr:   r-   r-   r.   rL   "  s:   

rL   c          	      C  s   t  }| }|rG|rdtj|dd nd}| jp| }d}|r8|| v r8||kr8d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 )z4Print 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. Fr   N)
r   r!   _get_usernameclickstyleentity_get_entity
_get_teamsr   rd   )	r"   r   r!   usernamehost_strrw   
entity_strlogin_state_strlogin_info_strr-   r-   r.   rP   <  s    
rP   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r   r   )r0   r1   r   r   r   r2   )r   r   r   r   r   r   r   r   r   r=   r   r   r   r9   r;   r   r<   r   r   r>   )r   r9   r"   rS   r   rT   r;   r   r@   r   r   rU   )r"   rS   r   rT   r   r   r   r   r   r9   rA   r=   r   r\   )r   r9   r4   r9   r   r2   )r"   rS   r   r9   r   r2   )__doc__
__future__r   ru   r   wandb.errorsr   r   	wandb.sdkr   wandb.sdk.libr   r   wandb.sdk.lib.deprecationr   r	   apisr   r/   r'   r)   rH   rI   rL   rP   r-   r-   r-   r.   <module>   sB    
^
P
#
)