Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PY-597] Darwin-py extra authenticate options #742

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions darwin/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import getpass
import os
from argparse import ArgumentParser, Namespace
from pathlib import Path

import requests.exceptions
from rich.console import Console
Expand Down Expand Up @@ -63,9 +64,11 @@ def _run(args: Namespace, parser: ArgumentParser) -> None:

# Authenticate user
if args.command == "authenticate":
api_key = os.getenv("DARWIN_API_KEY")
api_key = os.getenv("DARWIN_API_KEY") or args.api_key
default_team = os.getenv("DARWIN_TEAM") or args.default_team
datasets_dir = os.getenv("DARWIN_DATASETS_DIR") or args.datasets_dir
if api_key:
print("Using API key from DARWIN_API_KEY")
print("Using API key from args/env")
else:
api_key = getpass.getpass(prompt="API key: ", stream=None)
api_key = api_key.strip()
Expand All @@ -74,7 +77,12 @@ def _run(args: Namespace, parser: ArgumentParser) -> None:
"API Key needed, generate one for your team: https://darwin.v7labs.com/?settings=api-keys"
)
return
f.authenticate(api_key)
if datasets_dir is not None:
print("Using datasets directory from args/env")
datasets_dir = Path(datasets_dir).resolve()
if default_team is not None:
print("Using default team from args/env")
f.authenticate(api_key, default_team, datasets_dir)
print("Authentication succeeded.")
# Select / List team
elif args.command == "team":
Expand Down
18 changes: 13 additions & 5 deletions darwin/cli_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def validate_api_key(api_key: str) -> None:

def authenticate(
api_key: str,
default_team: Optional[bool] = None,
default_team: Optional[Union[str, bool]] = None,
datasets_dir: Optional[Path] = None,
) -> Config:
"""
Expand Down Expand Up @@ -125,9 +125,18 @@ def authenticate(
config_path.parent.mkdir(exist_ok=True)

if default_team is None:
default_team = input(
f"Make {client.default_team} the default team? [y/N] "
) in ["Y", "y"]
default_team_name = (
client.default_team
if input(f"Make {client.default_team} the default team? [y/N] ")
in ["Y", "y"]
else None
)
elif default_team is False:
default_team_name = None
elif default_team is True:
default_team_name = client.default_team
else:
default_team_name = default_team
if datasets_dir is None:
datasets_dir = Path(prompt("Datasets directory", "~/.darwin/datasets"))

Expand All @@ -136,7 +145,6 @@ def authenticate(

client.set_datasets_dir(datasets_dir)

default_team_name: Optional[str] = client.default_team if default_team else None
return persist_client_configuration(client, default_team=default_team_name)

except InvalidLogin:
Expand Down
5 changes: 4 additions & 1 deletion darwin/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def __init__(self) -> None:
subparsers.add_parser("help", help="Show this help message and exit.")

# AUTHENTICATE
subparsers.add_parser("authenticate", help="Authenticate the user. ")
auth = subparsers.add_parser("authenticate", help="Authenticate the user. ")
auth.add_argument("--api_key", type=str, help="API key to use.")
auth.add_argument("--default_team", type=str, help="Default team to use.")
auth.add_argument("--datasets_dir", type=str, help="Folder to store datasets.")

# SET COMPRESSION LEVEL
parser_compression = subparsers.add_parser(
Expand Down
Loading