From 8abcb37ada1209d94903cf45174bd826a8a7c961 Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 14 Nov 2023 15:50:02 +0000 Subject: [PATCH 1/3] extra authentication options --- darwin/cli.py | 11 +++++++++-- darwin/cli_functions.py | 11 ++++++++--- darwin/options.py | 7 +++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/darwin/cli.py b/darwin/cli.py index 3f3f5221b..9c9c9f709 100644 --- a/darwin/cli.py +++ b/darwin/cli.py @@ -6,6 +6,7 @@ from argparse import ArgumentParser, Namespace from datetime import datetime from json import dumps +from pathlib import Path import requests.exceptions from rich.console import Console @@ -13,6 +14,7 @@ import darwin.cli_functions as f from darwin import __version__ from darwin.exceptions import GracefulExit, InvalidTeam, Unauthenticated, Unauthorized +from darwin.future.core import datasets from darwin.options import Options @@ -60,7 +62,9 @@ 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") else: @@ -69,7 +73,10 @@ def _run(args: Namespace, parser: ArgumentParser) -> None: if api_key == "": print("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 DARWIN_DATASETS_DIR") + datasets_dir = Path(datasets_dir).resolve() + f.authenticate(api_key, default_team, datasets_dir) print("Authentication succeeded.") # Select / List team elif args.command == "team": diff --git a/darwin/cli_functions.py b/darwin/cli_functions.py index 9bd6fc903..131f475b3 100644 --- a/darwin/cli_functions.py +++ b/darwin/cli_functions.py @@ -93,7 +93,7 @@ def validate_api_key(api_key: str) -> None: _error(f"Expected key prefix to be 7 characters long\n(example: {example_key})") -def authenticate(api_key: str, default_team: Optional[bool] = None, datasets_dir: Optional[Path] = None) -> Config: +def authenticate(api_key: str, default_team: Optional[Union[str, bool]] = None, datasets_dir: Optional[Path] = None) -> Config: """ Authenticate the API key against the server and creates a configuration file for it. @@ -121,7 +121,13 @@ def authenticate(api_key: str, default_team: Optional[bool] = None, datasets_dir 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")) @@ -130,7 +136,6 @@ def authenticate(api_key: str, default_team: Optional[bool] = None, datasets_dir 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: diff --git a/darwin/options.py b/darwin/options.py index e8b6ab1ea..28077023b 100644 --- a/darwin/options.py +++ b/darwin/options.py @@ -19,8 +19,11 @@ 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("compression", help="Set compression level.") parser_compression.add_argument( From fa0e0096073eecc5dae8f2cd50dbe7575e28261c Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Fri, 8 Dec 2023 18:02:04 +0000 Subject: [PATCH 2/3] linting --- darwin/cli.py | 1 - darwin/cli_functions.py | 13 +++++++++++-- darwin/options.py | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/darwin/cli.py b/darwin/cli.py index fdfcf124a..16a932fbf 100644 --- a/darwin/cli.py +++ b/darwin/cli.py @@ -11,7 +11,6 @@ import darwin.cli_functions as f from darwin import __version__ from darwin.exceptions import GracefulExit, InvalidTeam, Unauthenticated, Unauthorized -from darwin.future.core import datasets from darwin.options import Options diff --git a/darwin/cli_functions.py b/darwin/cli_functions.py index 78d4ddd78..8317c3eaa 100644 --- a/darwin/cli_functions.py +++ b/darwin/cli_functions.py @@ -93,7 +93,11 @@ def validate_api_key(api_key: str) -> None: _error(f"Expected key prefix to be 7 characters long\n(example: {example_key})") -def authenticate(api_key: str, default_team: Optional[Union[str, bool]] = None, datasets_dir: Optional[Path] = None) -> Config: +def authenticate( + api_key: str, + default_team: Optional[Union[str, bool]] = None, + datasets_dir: Optional[Path] = None, +) -> Config: """ Authenticate the API key against the server and creates a configuration file for it. @@ -121,7 +125,12 @@ def authenticate(api_key: str, default_team: Optional[Union[str, bool]] = None, config_path.parent.mkdir(exist_ok=True) if default_team is None: - default_team_name = client.default_team if input(f"Make {client.default_team} the default team? [y/N] ") in ["Y", "y"] else None + 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: diff --git a/darwin/options.py b/darwin/options.py index 4716f98ba..0a9b5151f 100644 --- a/darwin/options.py +++ b/darwin/options.py @@ -23,7 +23,7 @@ def __init__(self) -> None: 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( "compression", help="Set compression level." From 8c06a6a98b9388fa73a590f84d199b81f00349cf Mon Sep 17 00:00:00 2001 From: Nathan Perkins Date: Tue, 12 Dec 2023 14:20:07 +0000 Subject: [PATCH 3/3] print message changes --- darwin/cli.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/darwin/cli.py b/darwin/cli.py index 16a932fbf..cafb80cbf 100644 --- a/darwin/cli.py +++ b/darwin/cli.py @@ -68,7 +68,7 @@ def _run(args: Namespace, parser: ArgumentParser) -> None: 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() @@ -78,8 +78,10 @@ def _run(args: Namespace, parser: ArgumentParser) -> None: ) return if datasets_dir is not None: - print("Using datasets directory from DARWIN_DATASETS_DIR") + 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