From 25b543893daa82543db5842c40cb63ed9814c094 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Thu, 16 Jan 2020 10:38:47 +0530 Subject: [PATCH 1/5] Remove code duplication for get_token --- evalai/get_token.py | 15 ++++++--------- evalai/utils/auth.py | 11 +---------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/evalai/get_token.py b/evalai/get_token.py index b841ea1d6..4c97a3022 100644 --- a/evalai/get_token.py +++ b/evalai/get_token.py @@ -3,6 +3,7 @@ from click import echo, style +from evalai.utils.auth import get_user_auth_token from evalai.utils.config import AUTH_TOKEN_PATH import json @@ -15,18 +16,14 @@ def get_token(): if not os.path.exists(AUTH_TOKEN_PATH): echo( style( - "\nThe authentication token json file doesn't exist at the required path. " + "\nThe authentication token json file doesn't exists at the required path. " "Please download the file from the Profile section of the EvalAI webapp and " - "place it at ~/.evalai/token.json or use evalai -t to add it.\n\n", + "place it at ~/.evalai/token.json or use evalai set_token to add it.\n", bold=True, fg="red", ) ) + sys.exit(1) else: - with open(AUTH_TOKEN_PATH, "r") as fr: - try: - data = fr.read() - tokendata = json.loads(data) - echo("Current token is {}".format(tokendata["token"])) - except (OSError, IOError) as e: - echo(e) + token = get_user_auth_token() + echo(f"Current token is {token}") diff --git a/evalai/utils/auth.py b/evalai/utils/auth.py index e18f41bbc..0c79c8ae9 100644 --- a/evalai/utils/auth.py +++ b/evalai/utils/auth.py @@ -64,16 +64,7 @@ def get_user_auth_token(): token = data["token"] return token else: - echo( - style( - "\nThe authentication token json file doesn't exists at the required path. " - "Please download the file from the Profile section of the EvalAI webapp and " - "place it at ~/.evalai/token.json\n", - bold=True, - fg="red", - ) - ) - sys.exit(1) + raise Exception(f"Token file not found at: {AUTH_TOKEN_PATH}") def get_request_header(): From 185457aedf3ed0d2b860104327c46ac255aa5e66 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Thu, 16 Jan 2020 10:47:46 +0530 Subject: [PATCH 2/5] Fix syntax --- evalai/get_token.py | 16 ++-------------- evalai/utils/auth.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/evalai/get_token.py b/evalai/get_token.py index 4c97a3022..fe253ae5d 100644 --- a/evalai/get_token.py +++ b/evalai/get_token.py @@ -13,17 +13,5 @@ def get_token(): """ Get the EvalAI token. """ - if not os.path.exists(AUTH_TOKEN_PATH): - echo( - style( - "\nThe authentication token json file doesn't exists at the required path. " - "Please download the file from the Profile section of the EvalAI webapp and " - "place it at ~/.evalai/token.json or use evalai set_token to add it.\n", - bold=True, - fg="red", - ) - ) - sys.exit(1) - else: - token = get_user_auth_token() - echo(f"Current token is {token}") + token = get_user_auth_token() + echo(f"Current token is {token}") diff --git a/evalai/utils/auth.py b/evalai/utils/auth.py index 0c79c8ae9..2227d7b69 100644 --- a/evalai/utils/auth.py +++ b/evalai/utils/auth.py @@ -64,7 +64,16 @@ def get_user_auth_token(): token = data["token"] return token else: - raise Exception(f"Token file not found at: {AUTH_TOKEN_PATH}") + echo( + style( + "\nThe authentication token json file doesn't exists at the required path. " + "Please download the file from the Profile section of the EvalAI webapp and " + "place it at ~/.evalai/token.json or use evalai set_token to add it.\n", + bold=True, + fg="red", + ) + ) + sys.exit(1) def get_request_header(): From 106d8d057cc85beea50fa3b61a781017af37bb55 Mon Sep 17 00:00:00 2001 From: nikochiko Date: Thu, 16 Jan 2020 10:52:03 +0530 Subject: [PATCH 3/5] Fix tests --- tests/test_auth.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_auth.py b/tests/test_auth.py index ab5cde3a0..67b10300d 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -38,7 +38,7 @@ def test_get_user_auth_token_when_file_does_not_exist(self): expected = ( "\nThe authentication token json file doesn't exists at the required path. " "Please download the file from the Profile section of the EvalAI webapp and " - "place it at ~/.evalai/token.json\n\n" + "place it at ~/.evalai/token.json or use evalai set_token to add it.\n\n" ) runner = CliRunner() result = runner.invoke(challenges) From cea6057b1d9594337c622837f4da2e5f62e2f970 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Sat, 18 Jan 2020 07:41:58 +0530 Subject: [PATCH 4/5] printf -> format --- evalai/get_token.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evalai/get_token.py b/evalai/get_token.py index fe253ae5d..1b55d8809 100644 --- a/evalai/get_token.py +++ b/evalai/get_token.py @@ -14,4 +14,4 @@ def get_token(): Get the EvalAI token. """ token = get_user_auth_token() - echo(f"Current token is {token}") + echo("Current token is {0}".format(token)) From a09880205088e055dcd8018fefd7262507b69231 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Sat, 18 Jan 2020 07:48:51 +0530 Subject: [PATCH 5/5] Remove unused imports --- evalai/get_token.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/evalai/get_token.py b/evalai/get_token.py index 1b55d8809..bb36d83a1 100644 --- a/evalai/get_token.py +++ b/evalai/get_token.py @@ -1,11 +1,8 @@ import click -import os -from click import echo, style +from click import echo from evalai.utils.auth import get_user_auth_token -from evalai.utils.config import AUTH_TOKEN_PATH -import json @click.group(invoke_without_command=True)