Skip to content

Commit

Permalink
Add command to store evalai authentication token(#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
radiantly authored and RishabhJain2018 committed Feb 7, 2019
1 parent d3e75d8 commit b272489
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ source = evalai/
exclude_lines =
import
@click.*
main.add_command([a-z]*?)

omit =
*/python?.?/*
Expand Down
44 changes: 44 additions & 0 deletions evalai/add_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import click
import os
import validators

from click import echo, style

from evalai.utils.config import (AUTH_TOKEN_DIR,
AUTH_TOKEN_PATH,
LEN_OF_TOKEN,)


@click.group(invoke_without_command=True)
@click.argument('add_token')
def token(add_token):
"""
View and configure the EvalAI Token.
"""
"""
Invoked by `evalai token <your_token_here>`.
"""
if add_token is not None:
if validators.length(add_token, min=LEN_OF_TOKEN, max=LEN_OF_TOKEN):
if not os.path.exists(AUTH_TOKEN_DIR):
os.makedirs(AUTH_TOKEN_DIR)
with open(AUTH_TOKEN_PATH, 'w+') as fw:
try:
fw.write(add_token)
except (OSError, IOError) as e:
echo(e)
echo(style("Token successfully set!", bold=True))
else:
echo(style("Error: Invalid Length. Enter a valid token of length: {}".format(LEN_OF_TOKEN), bold=True))
else:
if not os.path.exists(AUTH_TOKEN_PATH):
echo(style("\nThe authentication token json file doesn't exist 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 <token> to add it.\n\n", bold=True))
else:
with open(AUTH_TOKEN_PATH, 'r') as fr:
try:
data = fr.read()
echo(style("Current token: {}".format(data), bold=True))
except (OSError, IOError) as e:
echo(e)
2 changes: 2 additions & 0 deletions evalai/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .challenges import challenge, challenges
from .set_host import host
from .add_token import token
from .submissions import submission
from .teams import teams

Expand All @@ -30,5 +31,6 @@ def main(ctx):
main.add_command(challenges)
main.add_command(challenge)
main.add_command(host)
main.add_command(token)
main.add_command(submission)
main.add_command(teams)
2 changes: 2 additions & 0 deletions evalai/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from os.path import expanduser


LEN_OF_TOKEN = 40

AUTH_TOKEN_FILE_NAME = 'token.json'

HOST_URL_FILE_NAME = 'host_url'
Expand Down

0 comments on commit b272489

Please sign in to comment.