Skip to content

Commit

Permalink
fix codacy issues + refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
asampat3090 committed Jun 9, 2018
1 parent 0c5eda7 commit dbaef6c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 64 deletions.
20 changes: 2 additions & 18 deletions datmo/cli/command/environment.py
Expand Up @@ -24,25 +24,9 @@ def setup(self, **kwargs):
name = kwargs.get("name", None)
available_environments = self.environment_controller.get_supported_environments(
)
available_environment_names, available_environment_description = zip(*available_environments)
if not name:
for idx, environment_name_description in enumerate(available_environments):
environment_name = environment_name_description[0]
environment_description = environment_name_description[1]
self.cli_helper.echo("(%s) %s: %s" % (idx + 1, environment_name, environment_description))
input_name = self.cli_helper.prompt(
__("prompt", "cli.environment.setup.name"))
try:
name_index = int(input_name)
except ValueError:
name_index = 0
if 0 < name_index < len(available_environments):
name = available_environment_names[name_index - 1]
elif name_index == 0:
name = input_name
else:
self.cli_helper.echo(
__("error", "cli.environment.setup.argument", input_name))
name = self.cli_helper.prompt_available_environments(
available_environments)
try:
options = {"name": name}
environment_obj = self.environment_controller.setup(
Expand Down
35 changes: 13 additions & 22 deletions datmo/cli/command/project.py
Expand Up @@ -6,6 +6,7 @@
from datmo.core.controller.environment.environment import EnvironmentController, EnvironmentDoesNotExist
from datmo.core.controller.task import TaskController


class ProjectCommand(BaseCommand):
# NOTE: dal_driver is not passed into the project because it is created
# first by ProjectController and then passed down to all other Controllers
Expand Down Expand Up @@ -105,24 +106,10 @@ def init(self, name, description):
if environment_setup:
# Setting up the environment definition file
self.environment_controller = EnvironmentController(home=self.home)
available_environments = self.environment_controller.get_supported_environments()
available_environment_names, available_environment_description = zip(*available_environments)
for idx, environment_name_description in enumerate(available_environments):
environment_name = environment_name_description[0]
environment_description = environment_name_description[1]
self.cli_helper.echo("(%s) %s: %s" % (idx + 1, environment_name, environment_description))
input_environment_name = self.cli_helper.prompt(
__("prompt", "cli.environment.setup.name"))
try:
name_environment_index = int(input_environment_name)
except ValueError:
name_environment_index = 0
if 0 < name_environment_index < len(available_environments):
input_environment_name = available_environment_names[name_environment_index - 1]
else:
self.cli_helper.echo(
__("error", "cli.environment.setup.argument", input_environment_name))

available_environments = self.environment_controller.get_supported_environments(
)
input_environment_name = self.cli_helper.prompt_available_environments(
available_environments)
try:
options = {"name": input_environment_name}
environment_obj = self.environment_controller.setup(
Expand All @@ -132,7 +119,8 @@ def init(self, name, description):
(environment_obj.name, environment_obj.id)))
except EnvironmentDoesNotExist:
self.cli_helper.echo(
__("error", "cli.environment.setup.argument", input_environment_name))
__("error", "cli.environment.setup.argument",
input_environment_name))
return self.project_controller.model

def version(self):
Expand Down Expand Up @@ -208,7 +196,8 @@ def cleanup(self):

def notebook(self, **kwargs):
self.cli_helper.echo(__("info", "cli.project.notebook"))
self.task_controller = TaskController(home=self.project_controller.home)
self.task_controller = TaskController(
home=self.project_controller.home)

# Creating input dictionaries
snapshot_dict = {}
Expand All @@ -234,9 +223,11 @@ def notebook(self, **kwargs):
task_obj.id, snapshot_dict=snapshot_dict, task_dict=task_dict)
except Exception as e:
self.logger.error("%s %s" % (e, task_dict))
self.cli_helper.echo(__("error", "cli.project.notebook", task_obj.id))
self.cli_helper.echo(
__("error", "cli.project.notebook", task_obj.id))
return False

self.cli_helper.echo("Ran notebook with task id: %s" % updated_task_obj.id)
self.cli_helper.echo(
"Ran notebook with task id: %s" % updated_task_obj.id)

return updated_task_obj
23 changes: 23 additions & 0 deletions datmo/cli/driver/helper.py
Expand Up @@ -114,3 +114,26 @@ def get_command_choices(self):
"init", "version", "--version", "-v", "status", "cleanup",
"snapshot", "task", "notebook", "environment"
]

def prompt_available_environments(self, available_environments):
"""Prompt user to choose an available environment. Returns the environment name"""
for idx, environment_name_description in enumerate(
available_environments):
environment_name = environment_name_description[0]
environment_description = environment_name_description[1]
self.echo("(%s) %s: %s" % (idx + 1, environment_name,
environment_description))
input_environment_name = self.prompt(
__("prompt", "cli.environment.setup.name"))
try:
name_environment_index = int(input_environment_name)
except ValueError:
name_environment_index = 0
if 0 < name_environment_index < len(available_environments):
input_environment_name = available_environments[
name_environment_index - 1][0]
else:
self.echo(
__("error", "cli.environment.setup.argument",
input_environment_name))
return input_environment_name
43 changes: 24 additions & 19 deletions datmo/cli/parser.py
Expand Up @@ -19,33 +19,36 @@ def get_datmo_parser():
cleanup_parser = subparsers.add_parser("cleanup", help="remove project")

# Notebook
notebook_parser = subparsers.add_parser("notebook", help="To run jupyter notebook")
notebook_parser = subparsers.add_parser(
"notebook", help="To run jupyter notebook")
notebook_parser.add_argument(
"--gpu",
dest="gpu",
action="store_true",
help="boolean if you want to run using GPUs")
"--gpu",
dest="gpu",
action="store_true",
help="boolean if you want to run using GPUs")
notebook_parser.add_argument(
"--environment-id",
dest="environment_id",
default=None,
help="environment id from environment object")
"--environment-id",
dest="environment_id",
default=None,
help="environment id from environment object")
notebook_parser.add_argument(
"--environment-paths",
dest="environment_paths",
default=None,
action="append",
type=str,
help=
"list of absolute or relative filepaths and/or dirpaths to collect; can specify destination names with '>' (e.g. /path/to/file>hello, /path/to/file2, /path/to/dir>newdir)"
)
"--environment-paths",
dest="environment_paths",
default=None,
action="append",
type=str,
help=
"list of absolute or relative filepaths and/or dirpaths to collect; can specify destination names with '>' (e.g. /path/to/file>hello, /path/to/file2, /path/to/dir>newdir)"
)
notebook_parser.add_argument(
"--mem-limit",
"-m",
dest="mem_limit",
default=None,
type=str,
help="maximum amount of memory the notebook environment can use (these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes)")
help=
"maximum amount of memory the notebook environment can use (these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes)"
)

# Session
session_parser = subparsers.add_parser("session", help="session module")
Expand Down Expand Up @@ -347,7 +350,9 @@ def get_datmo_parser():
dest="mem_limit",
default=None,
type=str,
help="maximum amount of memory the task environment can use (these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes)")
help=
"maximum amount of memory the task environment can use (these options take a positive integer, followed by a suffix of b, k, m, g, to indicate bytes, kilobytes, megabytes, or gigabytes. e.g. 4g)"
)
# task_run.add_argument(
# "--environment-name",
# dest="environment_name",
Expand Down
13 changes: 8 additions & 5 deletions datmo/core/controller/environment/driver/dockerenv.py
Expand Up @@ -31,8 +31,8 @@ def to_bytes(val):
GPUSupportNotEnabled, EnvironmentDoesNotExist)
from datmo.core.controller.environment.driver import EnvironmentDriver


docker_config_filepath = os.path.join(os.path.split(__file__)[0], "config", "docker.json")
docker_config_filepath = os.path.join(
os.path.split(__file__)[0], "config", "docker.json")


class DockerEnvironmentDriver(EnvironmentDriver):
Expand Down Expand Up @@ -137,10 +137,13 @@ def get_supported_environments(self):
def setup(self, options, definition_path):
name = options.get("name", None)
available_environments = self.get_supported_environments()
available_environment_names, available_environment_description = zip(*available_environments)
# Validate that the name exists
if not name or name not in available_environment_names:
raise EnvironmentDoesNotExist(__("error", "controller.environment.driver.docker.setup.dne", name))
if not name or name not in [
name for name, desc in available_environments
]:
raise EnvironmentDoesNotExist(
__("error", "controller.environment.driver.docker.setup.dne",
name))

# Validate the given definition path exists
if not os.path.isdir(definition_path):
Expand Down

0 comments on commit dbaef6c

Please sign in to comment.