Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
More login fixes (#202)
Browse files Browse the repository at this point in the history
* More login fixes

* fix formatting on colab
  • Loading branch information
raubitsj committed Sep 11, 2020
1 parent a48f04b commit dc8f20e
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
1 change: 1 addition & 0 deletions wandb/errors/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@


LOG_STRING = click.style('wandb', fg='blue', bold=True)
LOG_STRING_NOCOLOR = 'wandb'
ERROR_STRING = click.style('ERROR', bg='red', fg='green')
WARN_STRING = click.style('WARNING', fg='yellow')
PRINTED_MESSAGES = set()
Expand Down
17 changes: 7 additions & 10 deletions wandb/lib/apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from six.moves import input
import wandb
from wandb.apis import InternalApi
from wandb.errors.term import LOG_STRING
from wandb.errors import term
from wandb.util import isatty


Expand Down Expand Up @@ -43,7 +43,7 @@ def _prompt_choice():
return (
int(
input(
"%s: Enter your choice: " % LOG_STRING
"%s: Enter your choice: " % term.LOG_STRING
)
)
- 1 # noqa: W503
Expand All @@ -69,6 +69,7 @@ def prompt_api_key( # noqa: C901
False - if unconfigured (notty)
"""
input_callback = input_callback or getpass.getpass
log_string = term.LOG_STRING
api = api or InternalApi()
anon_mode = _fixup_anon_mode(settings.anonymous)
jupyter = settings._jupyter or False
Expand All @@ -84,6 +85,7 @@ def prompt_api_key( # noqa: C901
choices.remove(LOGIN_CHOICE_NEW)

if jupyter and 'google.colab' in sys.modules:
log_string = term.LOG_STRING_NOCOLOR
key = wandb.jupyter.attempt_colab_login(api.app_url)
if key is not None:
write_key(settings, key)
Expand All @@ -110,6 +112,7 @@ def prompt_api_key( # noqa: C901
result = choices[idx]
wandb.termlog("You chose '%s'" % result)

api_ask = "%s: Paste an API key from your profile and hit enter: " % log_string
if result == LOGIN_CHOICE_ANON:
key = api.create_anonymous_api_key()

Expand All @@ -122,10 +125,7 @@ def prompt_api_key( # noqa: C901
wandb.termlog(
"Create an account here: {}/authorize?signup=true".format(app_url)
)
key = input_callback(
"%s: Paste an API key from your profile and hit enter"
% LOG_STRING
).strip()
key = input_callback(api_ask).strip()

write_key(settings, key)
return key
Expand All @@ -138,10 +138,7 @@ def prompt_api_key( # noqa: C901
app_url
)
)
key = input_callback(
"%s: Paste an API key from your profile and hit enter"
% LOG_STRING
).strip()
key = input_callback(api_ask).strip()
write_key(settings, key)
return key
elif result == LOGIN_CHOICE_NOTTY:
Expand Down
4 changes: 2 additions & 2 deletions wandb/lib/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class ServerError(Exception):


class Server(object):
def __init__(self, api=None):
self._api = api or InternalApi()
def __init__(self, api=None, settings=None):
self._api = api or InternalApi(default_settings=settings)
self._error_network = None
self._viewer = {}
self._flags = {}
Expand Down
17 changes: 13 additions & 4 deletions wandb/sdk/wandb_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

logger = logging.getLogger("wandb")

if wandb.TYPE_CHECKING: # type: ignore
from typing import Dict # noqa: F401 pylint: disable=unused-import


def _validate_anonymous_setting(anon_str):
return anon_str in ["must", "allow", "never"]
Expand Down Expand Up @@ -55,7 +58,7 @@ def _login(
wandb.termwarn("Calling wandb.login() after wandb.init() is a no-op.")
return True

settings = {}
settings_dict: Dict = {}
api = Api()

if anonymous is not None:
Expand All @@ -66,14 +69,20 @@ def _login(
"wandb.login(). Can be 'must', 'allow', or 'never'."
)
return False
settings.update({"anonymous": anonymous})
settings_dict.update({"anonymous": anonymous})

if key:
settings_dict.update({"api_key": key})

# Note: This won't actually do anything if called from a codepath where
# wandb.setup was previously called. If wandb.setup is called further up,
# you must make sure the anonymous setting (and any other settings) are
# already properly set up there.
wl = wandb.setup()
settings = _settings or wl.settings()
wl = wandb.setup(settings=wandb.Settings(**settings_dict))
wl_settings = wl.settings()
if _settings:
wl_settings._apply_settings(settings=_settings)
settings = wl_settings

if settings._offline:
return False
Expand Down
6 changes: 3 additions & 3 deletions wandb/sdk/wandb_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, settings=None, environ=None):
_set_logger(self._early_logger)

# Have to load viewer before setting up settings.
self._load_viewer()
self._load_viewer(settings=settings)

self._settings_setup(settings, self._early_logger)
self._settings.freeze()
Expand Down Expand Up @@ -150,8 +150,8 @@ def _get_entity(self):
def _get_user_flags(self):
return self._server._flags

def _load_viewer(self):
s = server.Server()
def _load_viewer(self, settings=None):
s = server.Server(settings=settings)
s.query_with_timeout()
self._server = s
# if self.mode != "dryrun" and not self._api.disabled() and self._api.api_key:
Expand Down
17 changes: 13 additions & 4 deletions wandb/sdk_py27/wandb_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

logger = logging.getLogger("wandb")

if wandb.TYPE_CHECKING: # type: ignore
from typing import Dict # noqa: F401 pylint: disable=unused-import


def _validate_anonymous_setting(anon_str):
return anon_str in ["must", "allow", "never"]
Expand Down Expand Up @@ -55,7 +58,7 @@ def _login(
wandb.termwarn("Calling wandb.login() after wandb.init() is a no-op.")
return True

settings = {}
settings_dict = {}
api = Api()

if anonymous is not None:
Expand All @@ -66,14 +69,20 @@ def _login(
"wandb.login(). Can be 'must', 'allow', or 'never'."
)
return False
settings.update({"anonymous": anonymous})
settings_dict.update({"anonymous": anonymous})

if key:
settings_dict.update({"api_key": key})

# Note: This won't actually do anything if called from a codepath where
# wandb.setup was previously called. If wandb.setup is called further up,
# you must make sure the anonymous setting (and any other settings) are
# already properly set up there.
wl = wandb.setup()
settings = _settings or wl.settings()
wl = wandb.setup(settings=wandb.Settings(**settings_dict))
wl_settings = wl.settings()
if _settings:
wl_settings._apply_settings(settings=_settings)
settings = wl_settings

if settings._offline:
return False
Expand Down
6 changes: 3 additions & 3 deletions wandb/sdk_py27/wandb_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, settings=None, environ=None):
_set_logger(self._early_logger)

# Have to load viewer before setting up settings.
self._load_viewer()
self._load_viewer(settings=settings)

self._settings_setup(settings, self._early_logger)
self._settings.freeze()
Expand Down Expand Up @@ -150,8 +150,8 @@ def _get_entity(self):
def _get_user_flags(self):
return self._server._flags

def _load_viewer(self):
s = server.Server()
def _load_viewer(self, settings=None):
s = server.Server(settings=settings)
s.query_with_timeout()
self._server = s
# if self.mode != "dryrun" and not self._api.disabled() and self._api.api_key:
Expand Down

0 comments on commit dc8f20e

Please sign in to comment.