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

Commit 19a6193

Browse files
committed
Move common objects to avoid circular imports
1 parent f68ae5e commit 19a6193

File tree

7 files changed

+72
-67
lines changed

7 files changed

+72
-67
lines changed

paperspace/cli/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
import click
2-
3-
api_key_option = click.option(
4-
"--apiKey",
5-
"api_key",
6-
help="API key to use this time only",
7-
)

paperspace/cli/cli.py

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
import collections
22
import functools
3-
import json
4-
import re
53

64
import click
75

86
from paperspace import constants, client, config
9-
from paperspace.cli import api_key_option
10-
from paperspace.cli.jobs.commands import jobs_group
7+
from paperspace.cli.common import api_key_option, del_if_value_is_none
8+
from paperspace.cli.jobs import jobs_group
9+
from paperspace.cli.types import ChoiceType, json_string
10+
from paperspace.cli.validators import validate_mutually_exclusive, validate_email
1111
from paperspace.commands import experiments as experiments_commands, deployments as deployments_commands, \
1212
machines as machines_commands, login as login_commands
1313

14-
15-
class ChoiceType(click.Choice):
16-
"""Takes a string-keyed map and converts cli-provided parameter to corresponding value"""
17-
18-
def __init__(self, type_map, case_sensitive=True):
19-
super(ChoiceType, self).__init__(tuple(type_map.keys()), case_sensitive=case_sensitive)
20-
self.type_map = type_map
21-
22-
def convert(self, value, param, ctx):
23-
value = super(ChoiceType, self).convert(value, param, ctx).upper()
24-
return self.type_map[value]
25-
26-
2714
MULTI_NODE_EXPERIMENT_TYPES_MAP = collections.OrderedDict(
2815
(
2916
("GRPC", constants.ExperimentType.GRPC_MULTI_NODE),
@@ -32,48 +19,6 @@ def convert(self, value, param, ctx):
3219
)
3320

3421

35-
class Number(click.ParamType):
36-
name = "number"
37-
38-
def convert(self, value, param, ctx):
39-
try:
40-
number = int(value)
41-
except ValueError:
42-
try:
43-
number = float(value)
44-
except ValueError:
45-
self.fail('{} is not a valid number'.format(value), param, ctx)
46-
47-
return number
48-
49-
50-
def json_string(val):
51-
"""Wraps json.loads so the cli help shows proper option's type name instead of 'LOADS'"""
52-
return json.loads(val)
53-
54-
55-
def del_if_value_is_none(dict_):
56-
"""Remove all elements with value == None"""
57-
for key, val in list(dict_.items()):
58-
if val is None:
59-
del dict_[key]
60-
61-
62-
def validate_mutually_exclusive(options_1, options_2, error_message):
63-
used_option_in_options_1 = any(option is not None for option in options_1)
64-
used_option_in_options_2 = any(option is not None for option in options_2)
65-
if used_option_in_options_1 and used_option_in_options_2:
66-
raise click.UsageError(error_message)
67-
68-
69-
def validate_email(ctx, param, value):
70-
if value is not None \
71-
and not re.match(r"[^@]+@[^@]+\.[^@]+", value):
72-
raise click.BadParameter("Bad email address format")
73-
74-
return value
75-
76-
7722
@click.group()
7823
def cli():
7924
pass

paperspace/cli/common.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import click
2+
3+
api_key_option = click.option(
4+
"--apiKey",
5+
"api_key",
6+
help="API key to use this time only",
7+
)
8+
9+
10+
def del_if_value_is_none(dict_):
11+
"""Remove all elements with value == None"""
12+
for key, val in list(dict_.items()):
13+
if val is None:
14+
del dict_[key]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import click
22

33
from paperspace import client, config
4-
from paperspace.cli import api_key_option
4+
from paperspace.cli.common import api_key_option
55
from paperspace.commands import jobs as jobs_commands
66

77

paperspace/cli/jobs/__init__.py

Whitespace-only changes.

paperspace/cli/types.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import json
2+
3+
import click
4+
5+
6+
class ChoiceType(click.Choice):
7+
"""Takes a string-keyed map and converts cli-provided parameter to corresponding value"""
8+
9+
def __init__(self, type_map, case_sensitive=True):
10+
super(ChoiceType, self).__init__(tuple(type_map.keys()), case_sensitive=case_sensitive)
11+
self.type_map = type_map
12+
13+
def convert(self, value, param, ctx):
14+
value = super(ChoiceType, self).convert(value, param, ctx).upper()
15+
return self.type_map[value]
16+
17+
18+
class Number(click.ParamType):
19+
name = "number"
20+
21+
def convert(self, value, param, ctx):
22+
try:
23+
number = int(value)
24+
except ValueError:
25+
try:
26+
number = float(value)
27+
except ValueError:
28+
self.fail('{} is not a valid number'.format(value), param, ctx)
29+
30+
return number
31+
32+
33+
def json_string(val):
34+
"""Wraps json.loads so the cli help shows proper option's type name instead of 'LOADS'"""
35+
return json.loads(val)

paperspace/cli/validators.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import re
2+
3+
import click
4+
5+
6+
def validate_mutually_exclusive(options_1, options_2, error_message):
7+
used_option_in_options_1 = any(option is not None for option in options_1)
8+
used_option_in_options_2 = any(option is not None for option in options_2)
9+
if used_option_in_options_1 and used_option_in_options_2:
10+
raise click.UsageError(error_message)
11+
12+
13+
def validate_email(ctx, param, value):
14+
if value is not None \
15+
and not re.match(r"[^@]+@[^@]+\.[^@]+", value):
16+
raise click.BadParameter("Bad email address format")
17+
18+
return value

0 commit comments

Comments
 (0)