Skip to content
This repository was archived by the owner on Aug 11, 2020. It is now read-only.

Commit a90fd38

Browse files
BartoszCkidandruszak
authored andcommitted
Commands moved to submodule
1 parent 5c3bee0 commit a90fd38

File tree

6 files changed

+51
-50
lines changed

6 files changed

+51
-50
lines changed

paperspace/cli.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import click
55

6-
from paperspace import commands, constants
6+
from paperspace import constants
7+
from paperspace.commands import experiments as experiments_commands
78

89

910
class ChoiceType(click.Choice):
@@ -41,7 +42,7 @@ def cli():
4142
pass
4243

4344

44-
@cli.group()
45+
@cli.group("experiments")
4546
def experiments():
4647
pass
4748

@@ -230,7 +231,7 @@ def common_experiments_create_single_node_options(f):
230231
@common_experiment_create_multi_node_options
231232
def create_multi_node(**kwargs):
232233
del_if_value_is_none(kwargs)
233-
commands.create_experiment(kwargs)
234+
experiments_commands.create_experiment(kwargs)
234235

235236

236237
@create.command(name="singlenode")
@@ -239,15 +240,15 @@ def create_multi_node(**kwargs):
239240
def create_single_node(**kwargs):
240241
kwargs["experimentTypeId"] = constants.ExperimentType.SINGLE_NODE
241242
del_if_value_is_none(kwargs)
242-
commands.create_experiment(kwargs)
243+
experiments_commands.create_experiment(kwargs)
243244

244245

245246
@create_and_start.command(name="multinode")
246247
@common_experiments_create_options
247248
@common_experiment_create_multi_node_options
248249
def create_and_start_multi_node(**kwargs):
249250
del_if_value_is_none(kwargs)
250-
commands.create_and_start_experiment(kwargs)
251+
experiments_commands.create_and_start_experiment(kwargs)
251252

252253

253254
@create_and_start.command(name="singlenode")
@@ -256,32 +257,32 @@ def create_and_start_multi_node(**kwargs):
256257
def create_and_start_single_node(**kwargs):
257258
kwargs["experimentTypeId"] = constants.ExperimentType.SINGLE_NODE
258259
del_if_value_is_none(kwargs)
259-
commands.create_and_start_experiment(kwargs)
260+
experiments_commands.create_and_start_experiment(kwargs)
260261

261262

262263
@experiments.command()
263264
@click.argument("experiment-handle")
264265
def start(experiment_handle):
265-
commands.start_experiment(experiment_handle)
266+
experiments_commands.start_experiment(experiment_handle)
266267

267268

268269
@experiments.command()
269270
@click.argument("experiment-handle")
270271
def stop(experiment_handle):
271-
commands.stop_experiment(experiment_handle)
272+
experiments_commands.stop_experiment(experiment_handle)
272273

273274

274-
@click.option("--projectHandle", "-p", "project_handles", multiple=True)
275275
@experiments.command("list")
276+
@click.option("--projectHandle", "-p", "project_handles", multiple=True)
276277
def list_experiments(project_handles):
277-
command = commands.ListExperimentsCommand()
278+
command = experiments_commands.ListExperimentsCommand()
278279
command.execute(project_handles)
279280

280281

281282
@experiments.command("details")
282283
@click.argument("experiment-handle")
283284
def get_experiment_details(experiment_handle):
284-
commands.get_experiment_details(experiment_handle)
285+
experiments_commands.get_experiment_details(experiment_handle)
285286

286287
# TODO: delete experiment - not implemented in the api
287288
# TODO: modify experiment - not implemented in the api

paperspace/commands/__init__.py

Whitespace-only changes.

paperspace/commands.py renamed to paperspace/commands/experiments.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,16 @@
22

33
import terminaltables
44

5-
from paperspace import version, logger, constants, client
6-
from paperspace.config import config
5+
from paperspace import config, version, client, logger, constants
6+
from paperspace.logger import log_response
77
from paperspace.utils import get_terminal_lines
88

99
default_headers = {"X-API-Key": config.PAPERSPACE_API_KEY,
1010
"ps_client_name": "paperspace-python",
1111
"ps_client_version": version.version}
12-
1312
experiments_api = client.API(config.CONFIG_EXPERIMENTS_HOST, headers=default_headers)
1413

1514

16-
def _log_response(response, success_msg, error_msg, logger_=logger):
17-
if response.ok:
18-
logger_.log(success_msg)
19-
else:
20-
try:
21-
data = response.json()
22-
logger_.log_error_response(data)
23-
except ValueError:
24-
logger_.log(error_msg)
25-
26-
2715
def _log_create_experiment(response, success_msg_template, error_msg, logger_=logger):
2816
if response.ok:
2917
j = response.json()
@@ -56,13 +44,13 @@ def create_and_start_experiment(json_, api=experiments_api):
5644
def start_experiment(experiment_handle, api=experiments_api):
5745
url = "/experiments/{}/start/".format(experiment_handle)
5846
response = api.put(url)
59-
_log_response(response, "Experiment started", "Unknown error while starting the experiment")
47+
log_response(response, "Experiment started", "Unknown error while starting the experiment")
6048

6149

6250
def stop_experiment(experiment_handle, api=experiments_api):
6351
url = "/experiments/{}/stop/".format(experiment_handle)
6452
response = api.put(url)
65-
_log_response(response, "Experiment stopped", "Unknown error while stopping the experiment")
53+
log_response(response, "Experiment stopped", "Unknown error while stopping the experiment")
6654

6755

6856
class ListExperimentsCommand(object):
@@ -195,4 +183,4 @@ def get_experiment_details(experiment_handle, api=experiments_api):
195183
logger.log("Error parsing response data")
196184
logger.debug(e)
197185

198-
_log_response(response, details, "Unknown error while retrieving details of the experiment")
186+
log_response(response, details, "Unknown error while retrieving details of the experiment")

paperspace/logger.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,14 @@ def log_error_response(data):
3939
def debug(messages):
4040
if config.DEBUG:
4141
log("DEBUG: {}".format(messages))
42+
43+
44+
def log_response(response, success_msg, error_msg):
45+
if response.ok:
46+
log(success_msg)
47+
else:
48+
try:
49+
data = response.json()
50+
log_error_response(data)
51+
except ValueError:
52+
log(error_msg)

tests/test_click_commands.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from paperspace import cli, constants
55

66

7-
@mock.patch("paperspace.cli.commands")
7+
@mock.patch("paperspace.cli.experiments_commands")
88
def test_should_execute_create_experiment_command_when_cli_singlenode_command_was_executed(commands_patched):
99
runner = CliRunner()
1010
command = "experiments create singlenode " \
@@ -29,7 +29,7 @@ def test_should_execute_create_experiment_command_when_cli_singlenode_command_wa
2929
commands_patched.create_experiment.assert_called_once_with(expected_kwargs)
3030

3131

32-
@mock.patch("paperspace.cli.commands")
32+
@mock.patch("paperspace.cli.experiments_commands")
3333
def test_should_execute_create_experiment_command_when_cli_multinode_mpi_command_was_executed(commands_patched):
3434
runner = CliRunner()
3535
command = "experiments create multinode " \
@@ -65,7 +65,7 @@ def test_should_execute_create_experiment_command_when_cli_multinode_mpi_command
6565
commands_patched.create_experiment.assert_called_once_with(expected_kwargs)
6666

6767

68-
@mock.patch("paperspace.cli.commands")
68+
@mock.patch("paperspace.cli.experiments_commands")
6969
def test_should_execute_create_experiment_command_when_cli_multinode_grpc_command_was_executed(commands_patched):
7070
runner = CliRunner()
7171
command = "experiments create multinode " \
@@ -101,7 +101,7 @@ def test_should_execute_create_experiment_command_when_cli_multinode_grpc_comman
101101
commands_patched.create_experiment.assert_called_once_with(expected_kwargs)
102102

103103

104-
@mock.patch("paperspace.cli.commands")
104+
@mock.patch("paperspace.cli.experiments_commands")
105105
def test_should_execute_create_experiment_command_when_cli_create_and_start_singlenode_command_was_executed(
106106
commands_patched):
107107
runner = CliRunner()
@@ -127,7 +127,7 @@ def test_should_execute_create_experiment_command_when_cli_create_and_start_sing
127127
commands_patched.create_and_start_experiment.assert_called_once_with(expected_kwargs)
128128

129129

130-
@mock.patch("paperspace.cli.commands")
130+
@mock.patch("paperspace.cli.experiments_commands")
131131
def test_should_execute_create_experiment_command_when_cli_create_and_start_multinode_mpi_command_was_executed(
132132
commands_patched):
133133
runner = CliRunner()

tests/test_experiments_functional.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import mock
22
from click.testing import CliRunner
33

4-
from paperspace import cli, constants, commands
4+
from paperspace import cli, constants
5+
from paperspace.commands import experiments as experiments_commands
56
from tests import example_responses
67

78

@@ -22,7 +23,7 @@ def json(self):
2223

2324
class TestExperimentsCreateSingleNode(object):
2425
URL = "https://services.paperspace.io/experiments/v1/experiments/"
25-
EXPECTED_HEADERS = commands.default_headers
26+
EXPECTED_HEADERS = experiments_commands.default_headers
2627
BASIC_OPTIONS_COMMAND = [
2728
"experiments", "create", "singlenode",
2829
"--name", "exp1",
@@ -87,7 +88,7 @@ class TestExperimentsCreateSingleNode(object):
8788
RESPONSE_CONTENT_404_PROJECT_NOT_FOUND = b'{"details":{"handle":"wrong_handle"},"error":"Project not found"}\n'
8889
EXPECTED_STDOUT_PROJECT_NOT_FOUND = "Project not found\nhandle: wrong_handle\n"
8990

90-
@mock.patch("paperspace.cli.commands.client.requests.post")
91+
@mock.patch("paperspace.cli.experiments_commands.client.requests.post")
9192
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_basic_options(self,
9293
post_patched):
9394
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, self.RESPONSE_CONTENT_200)
@@ -102,7 +103,7 @@ def test_should_send_proper_data_and_print_message_when_create_experiment_was_ru
102103
assert result.output == self.EXPECTED_STDOUT
103104
assert result.exit_code == 0
104105

105-
@mock.patch("paperspace.cli.commands.client.requests.post")
106+
@mock.patch("paperspace.cli.experiments_commands.client.requests.post")
106107
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_full_options(self,
107108
post_patched):
108109
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, self.RESPONSE_CONTENT_200)
@@ -117,7 +118,7 @@ def test_should_send_proper_data_and_print_message_when_create_experiment_was_ru
117118
assert result.output == self.EXPECTED_STDOUT
118119
assert result.exit_code == 0
119120

120-
@mock.patch("paperspace.cli.commands.client.requests.post")
121+
@mock.patch("paperspace.cli.experiments_commands.client.requests.post")
121122
def test_should_send_proper_data_and_print_message_when_create_wrong_project_handle_was_given(self, post_patched):
122123
post_patched.return_value = MockResponse(self.RESPONSE_JSON_404_PROJECT_NOT_FOUND, 404,
123124
self.RESPONSE_CONTENT_404_PROJECT_NOT_FOUND)
@@ -135,7 +136,7 @@ def test_should_send_proper_data_and_print_message_when_create_wrong_project_han
135136

136137
class TestExperimentsCreateMultiNode(object):
137138
URL = "https://services.paperspace.io/experiments/v1/experiments/"
138-
EXPECTED_HEADERS = commands.default_headers
139+
EXPECTED_HEADERS = experiments_commands.default_headers
139140
BASIC_OPTIONS_COMMAND = [
140141
"experiments", "create", "multinode",
141142
"--name", "multinode_mpi",
@@ -226,7 +227,7 @@ class TestExperimentsCreateMultiNode(object):
226227
RESPONSE_CONTENT_200 = b'{"handle":"sadkfhlskdjh","message":"success"}\n'
227228
EXPECTED_STDOUT = "New experiment created with handle: sadkfhlskdjh\n"
228229

229-
@mock.patch("paperspace.cli.commands.client.requests.post")
230+
@mock.patch("paperspace.cli.experiments_commands.client.requests.post")
230231
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_basic_options(self,
231232
post_patched):
232233
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, self.RESPONSE_CONTENT_200)
@@ -241,7 +242,7 @@ def test_should_send_proper_data_and_print_message_when_create_experiment_was_ru
241242
assert result.output == self.EXPECTED_STDOUT
242243
assert result.exit_code == 0
243244

244-
@mock.patch("paperspace.cli.commands.client.requests.post")
245+
@mock.patch("paperspace.cli.experiments_commands.client.requests.post")
245246
def test_should_send_proper_data_and_print_message_when_create_experiment_was_run_with_full_options(self,
246247
post_patched):
247248
post_patched.return_value = MockResponse(self.RESPONSE_JSON_200, 200, self.RESPONSE_CONTENT_200)
@@ -505,7 +506,7 @@ class TestExperimentDetail(object):
505506
+---------------------+----------------+
506507
"""
507508

508-
@mock.patch("paperspace.cli.commands.client.requests.get")
509+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
509510
def test_should_send_get_request_and_print_single_node_experiment_details_in_a_table(self, get_patched):
510511
get_patched.return_value = MockResponse(self.SINGLE_NODE_RESPONSE_JSON, 200, "fake content")
511512

@@ -515,7 +516,7 @@ def test_should_send_get_request_and_print_single_node_experiment_details_in_a_t
515516
assert result.output == self.SINGLE_NODE_DETAILS_STDOUT
516517
assert result.exit_code == 0
517518

518-
@mock.patch("paperspace.cli.commands.client.requests.get")
519+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
519520
def test_should_send_get_request_and_print_multi_node_experiment_details_in_a_table(self, get_patched):
520521
get_patched.return_value = MockResponse(self.MULTI_NODE_DETAILS_JSON, 200, "fake content")
521522

@@ -525,7 +526,7 @@ def test_should_send_get_request_and_print_multi_node_experiment_details_in_a_ta
525526
assert result.output == self.MULTI_NODE_DETAILS_STDOUT
526527
assert result.exit_code == 0
527528

528-
@mock.patch("paperspace.cli.commands.client.requests.get")
529+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
529530
def test_should_send_get_request_and_print_request_content_when_response_data_was_malformed(self, get_patched):
530531
get_patched.return_value = MockResponse({}, 200, "fake content")
531532
g = """Error parsing response data
@@ -715,7 +716,7 @@ class TestExperimentList(object):
715716
+---------------+---------------+---------+
716717
"""
717718

718-
@mock.patch("paperspace.cli.commands.client.requests.get")
719+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
719720
def test_should_send_get_request_and_print_list_of_experiments(self, get_patched):
720721
get_patched.return_value = MockResponse(self.LIST_JSON, 200, "fake content")
721722

@@ -724,8 +725,8 @@ def test_should_send_get_request_and_print_list_of_experiments(self, get_patched
724725

725726
assert result.output == self.DETAILS_STDOUT
726727

727-
@mock.patch("paperspace.cli.commands.pydoc")
728-
@mock.patch("paperspace.cli.commands.client.requests.get")
728+
@mock.patch("paperspace.cli.experiments_commands.pydoc")
729+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
729730
def test_should_send_get_request_and_paginate_list_when_output_table_len_is_gt_lines_in_terminal(self, get_patched,
730731
pydoc_patched):
731732
list_json = {"data": self.LIST_JSON["data"] * 40}
@@ -737,7 +738,7 @@ def test_should_send_get_request_and_paginate_list_when_output_table_len_is_gt_l
737738
pydoc_patched.pager.assert_called_once()
738739
assert result.exit_code == 0
739740

740-
@mock.patch("paperspace.cli.commands.client.requests.get")
741+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
741742
def test_should_send_get_request_and_print_list_of_experiments_filtered_with_two_projects(self, get_patched):
742743
get_patched.return_value = MockResponse(example_responses.LIST_OF_EXPERIMENTS_FILTERED_WITH_TWO_PROJECTS, 200,
743744
"fake content")
@@ -747,7 +748,7 @@ def test_should_send_get_request_and_print_list_of_experiments_filtered_with_two
747748

748749
assert result.output == example_responses.LIST_OF_EXPERIMENTS_FILTERED_WITH_TWO_PROJECTS_STDOUT
749750

750-
@mock.patch("paperspace.cli.commands.client.requests.get")
751+
@mock.patch("paperspace.cli.experiments_commands.client.requests.get")
751752
def test_should_send_get_request_and_print_list_of_experiments_filtered_with_two_projects_but_none_found(
752753
self, get_patched):
753754
get_patched.return_value = MockResponse(example_responses.LIST_OF_EXPERIMENTS_FILTERED_BUT_NONE_FOUND, 200,
@@ -764,7 +765,7 @@ class TestStartExperiment(object):
764765
RESPONSE_JSON = {"message": "success"}
765766
START_STDOUT = "Experiment started\n"
766767

767-
@mock.patch("paperspace.cli.commands.client.requests.put")
768+
@mock.patch("paperspace.cli.experiments_commands.client.requests.put")
768769
def test_should_send_put_request_and_print_confirmation(self, put_patched):
769770
put_patched.return_value = MockResponse(self.RESPONSE_JSON, 200, "fake content")
770771

0 commit comments

Comments
 (0)