-
Notifications
You must be signed in to change notification settings - Fork 62
/
teams.py
225 lines (204 loc) · 6.51 KB
/
teams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import json
import requests
import sys
from beautifultable import BeautifulTable
from click import echo, style
from evalai.utils.auth import get_request_header, get_host_url
from evalai.utils.common import validate_token
from evalai.utils.urls import URLS
from evalai.utils.config import EVALAI_ERROR_CODES
requests.packages.urllib3.disable_warnings()
def pretty_print_team_data(teams, is_host):
"""
Function to print the team data
"""
table = BeautifulTable(max_width=200)
attributes = ["id", "team_name", "created_by"]
columns_attributes = [
"ID",
"Team Name",
"Created By",
"Members",
"Team URL",
]
table.column_headers = columns_attributes
for team in teams:
values = list(map(lambda item: team[item], attributes))
if is_host:
members = ", ".join(
map(lambda member: member["user"], team["members"])
)
else:
members = ", ".join(
map(lambda member: member["member_name"], team["members"])
)
values.append(members)
if team["team_url"]:
values.append(team["team_url"])
else:
values.append("None")
table.append_row(values)
echo(table)
def display_teams(is_host):
"""
Function to display all the participant or host teams of a user
"""
url = "{}{}"
headers = get_request_header()
if is_host:
url = url.format(get_host_url(), URLS.host_team_list.value)
else:
url = url.format(get_host_url(), URLS.participant_team_list.value)
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
echo(
style(
"Error: {}".format(response.json()["error"]),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
response = response.json()
teams = response["results"]
if len(teams) != 0:
pretty_print_team_data(teams, is_host)
else:
echo(style("Sorry, no teams found.", bold=True, fg="red"))
def create_team(team_name, team_url, is_host):
"""
Function to create a new team by taking in the team name as input.
"""
url = "{}{}"
if is_host:
url = url.format(get_host_url(), URLS.create_host_team.value)
else:
url = url.format(get_host_url(), URLS.participant_team_list.value)
headers = get_request_header()
headers["Content-Type"] = "application/json"
data = {}
data["team_name"] = team_name
if team_url:
data["team_url"] = team_url
data = json.dumps(data)
try:
response = requests.post(url, headers=headers, data=data)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
if "team_name" in response.json().keys():
validate_token(response.json())
echo(
style(
"Error: {}".format(response.json()["team_name"][0]),
fg="red",
bold=True,
)
)
else:
echo(
style(
"Error: {}".format(response.json()["error"]),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
if response.status_code == 201:
response = response.json()
if is_host:
echo(
style(
"\nYour host team {} was successfully created.\n".format(
response["team_name"]
),
fg="green",
bold=True,
)
)
else:
echo(
style(
"\nYour participant team {} was successfully created.\n".format(
response["team_name"]
),
fg="green",
bold=True,
)
)
def participate_in_a_challenge(challenge_id, participant_team_id):
"""
Function to participate in a particular challenge.
"""
url = "{}{}".format(get_host_url(), URLS.participate_in_a_challenge.value)
url = url.format(challenge_id, participant_team_id)
headers = get_request_header()
headers["Content-Type"] = "application/json"
try:
response = requests.post(url, headers=headers)
response.raise_for_status()
except requests.exceptions.HTTPError as err:
if response.status_code in EVALAI_ERROR_CODES:
validate_token(response.json())
echo(
style(
"\nError: {}\n"
"\nUse `evalai challenges` to fetch the active challenges.\n"
"\nUse `evalai teams` to fetch your participant "
"teams.\n".format(response.json()["error"]),
fg="red",
bold=True,
)
)
else:
echo(err)
sys.exit(1)
except requests.exceptions.RequestException:
echo(
style(
"\nCould not establish a connection to EvalAI."
" Please check the Host URL.\n",
bold=True,
fg="red",
)
)
sys.exit(1)
if response.status_code == 201:
echo(
style(
"Your team id {} is now participating in this challenge.".format(
participant_team_id
),
fg="green",
bold=True,
)
)