Skip to content

Commit

Permalink
Merge pull request #202 from datmo/simplify
Browse files Browse the repository at this point in the history
Simplify the CLI and Minor fixes
  • Loading branch information
asampat3090 committed Jun 16, 2018
2 parents c09f980 + d3e81cc commit 2082272
Show file tree
Hide file tree
Showing 56 changed files with 1,967 additions and 1,216 deletions.
12 changes: 12 additions & 0 deletions datmo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,23 @@
from __future__ import print_function

import os
from datmo.core.util.logger import DatmoLogger
from datmo.config import Config

datmo_root = os.path.dirname(os.path.abspath(__file__))
with open(os.path.join(datmo_root, 'VERSION')) as file:
__version__ = file.read()

# Config is required to run first so it can
# initialize/find datmo home directory (.datmo)
# This is required for logging to place the logs in a
# place for the user.
config = Config()
config.set_home(os.getcwd())

log = DatmoLogger.get_logger(__name__)
log.info("handling command %s", config.home)

import datmo.snapshot
import datmo.task
import datmo.config
32 changes: 20 additions & 12 deletions datmo/cli/command/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python

from datmo.config import Config
from datmo.core.util.i18n import get as __
from datmo.core.util.exceptions import ClassMethodNotFound
from datmo.cli.parser import get_datmo_parser
Expand All @@ -8,8 +9,8 @@


class BaseCommand(object):
def __init__(self, home, cli_helper):
self.home = home
def __init__(self, cli_helper):
self.home = Config().home
self.cli_helper = cli_helper
self.logger = DatmoLogger.get_logger(__name__)
self.parser = get_datmo_parser()
Expand All @@ -23,7 +24,7 @@ def parse(self, args):
pass

def display_usage_message(self, args):
""" Checks to see if --help or -h is passed in, and if so it calls our help()
""" Checks to see if --help or -h is passed in, and if so it calls our usage()
if it exists.
Since argparser thinks it is clever and automatically
Expand Down Expand Up @@ -60,21 +61,28 @@ def execute(self):
# in base.parse. Simply return True
if self.args is True: return True

if getattr(self.args, 'command') is None:
self.args.command = 'datmo'
if getattr(self.args, "command") is None:
self.args.command = "datmo"

command_args = vars(self.args).copy()
# use command name if it exists,
# otherwise use the module name
function_name = None
method = None

if "subcommand" in command_args and command_args['subcommand'] is not None:
method = getattr(self,
getattr(self.args, "subcommand",
self.args.command))
else:
method = getattr(self,
getattr(self.args, "command", self.args.command))
try:
if "subcommand" in command_args and command_args['subcommand'] is not None:
function_name = getattr(self.args, "subcommand",
self.args.command)
method = getattr(self, function_name)
else:
function_name = getattr(self.args, "command",
self.args.command)
method = getattr(self, function_name)
except AttributeError:
raise ClassMethodNotFound(
__("error", "cli.general.method.not_found",
(self.args.command, function_name)))

# remove extraneous options that the method should need to care about
if "command" in command_args:
Expand Down
4 changes: 2 additions & 2 deletions datmo/cli/command/datmo_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@


class DatmoCommand(BaseCommand):
def __init__(self, home, cli_helper):
super(DatmoCommand, self).__init__(home, cli_helper)
def __init__(self, cli_helper):
super(DatmoCommand, self).__init__(cli_helper)

def usage(self):
self.cli_helper.echo(__("argparser", "cli.datmo.usage"))
Expand Down
46 changes: 30 additions & 16 deletions datmo/cli/command/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
from datetime import datetime

from datmo.core.util.i18n import get as __
from datmo.cli.driver.helper import Helper
from datmo.core.controller.environment.environment import EnvironmentController
from datmo.cli.command.project import ProjectCommand
from datmo.core.util.exceptions import EnvironmentDoesNotExist
from datmo.core.util.misc_functions import printable_string
from datmo.core.util.misc_functions import printable_object, prettify_datetime


class EnvironmentCommand(ProjectCommand):
def __init__(self, home, cli_helper):
super(EnvironmentCommand, self).__init__(home, cli_helper)
# dest="subcommand" argument will populate a "subcommand" property with the subparsers name
# example "subcommand"="create" or "subcommand"="ls"
self.environment_controller = EnvironmentController(home=home)
def __init__(self, cli_helper):
super(EnvironmentCommand, self).__init__(cli_helper)

def environment(self):
self.parse(["--help"])
self.parse(["environment", "--help"])
return True

@Helper.notify_no_project_found
def setup(self, **kwargs):
self.environment_controller = EnvironmentController()
name = kwargs.get("name", None)
available_environments = self.environment_controller.get_supported_environments(
)
Expand All @@ -40,7 +40,9 @@ def setup(self, **kwargs):
self.cli_helper.echo(
__("error", "cli.environment.setup.argument", name))

@Helper.notify_no_project_found
def create(self, **kwargs):
self.environment_controller = EnvironmentController()
self.cli_helper.echo(__("info", "cli.environment.create"))
created_environment_obj = self.environment_controller.create(kwargs)
environments = self.environment_controller.list()
Expand All @@ -55,30 +57,42 @@ def create(self, **kwargs):
created_environment_obj.id))
return created_environment_obj

@Helper.notify_no_project_found
def update(self, **kwargs):
self.environment_controller = EnvironmentController()
environment_id = kwargs.get('id')
name = kwargs.get('name', None)
description = kwargs.get("description", None)
result = self.environment_controller.update(
environment_id, name=name, description=description)
return result

@Helper.notify_environment_active(EnvironmentController)
@Helper.notify_no_project_found
def delete(self, **kwargs):
environment_id = kwargs.get('environment_id')
self.environment_controller = EnvironmentController()
environment_id = kwargs.get('id')
if self.environment_controller.delete(environment_id):
self.cli_helper.echo(
__("info", "cli.environment.delete.success", environment_id))
return True

@Helper.notify_no_project_found
def ls(self, **kwargs):
self.environment_controller = EnvironmentController()
print_format = kwargs.get('format', "table")
download = kwargs.get('download', None)
download_path = kwargs.get('download_path', None)
environment_objs = self.environment_controller.list()
header_list = ["id", "created at", "name", "description"]
item_dict_list = []
for environment_obj in environment_objs:
environment_obj_created_at = printable_string(
environment_obj.created_at.strftime("%Y-%m-%d %H:%M:%S"))
environment_obj_name = printable_string(environment_obj.name) \
if environment_obj.name is not None else ""
environment_obj_description = printable_string(environment_obj.description) \
if environment_obj.description is not None else ""
environment_obj_name = printable_object(environment_obj.name)
environment_obj_description = printable_object(
environment_obj.description)
item_dict_list.append({
"id": environment_obj.id,
"created at": environment_obj_created_at,
"created at": prettify_datetime(environment_obj.created_at),
"name": environment_obj_name,
"description": environment_obj_description
})
Expand All @@ -90,7 +104,7 @@ def ls(self, **kwargs):
current_time_unix_time_ms = (
current_time - epoch_time).total_seconds() * 1000.0
download_path = os.path.join(
os.getcwd(),
self.environment_controller.home,
"environment_ls_" + str(current_time_unix_time_ms))
self.cli_helper.print_items(
header_list,
Expand Down
58 changes: 30 additions & 28 deletions datmo/cli/command/project.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import os

from datmo import __version__
from datmo.core.util.i18n import get as __
from datmo.cli.driver.helper import Helper
from datmo.cli.command.base import BaseCommand
from datmo.core.util.spinner import Spinner
from datmo.core.util.misc_functions import mutually_exclusive
Expand All @@ -9,11 +12,9 @@


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
def __init__(self, home, cli_helper):
super(ProjectCommand, self).__init__(home, cli_helper)
self.project_controller = ProjectController(home=home)
def __init__(self, cli_helper):
super(ProjectCommand, self).__init__(cli_helper)
self.project_controller = ProjectController()
self.spinner = Spinner()

def init(self, name, description):
Expand All @@ -37,13 +38,13 @@ def init(self, name, description):

if is_new_model: # Initialize a new project
self.cli_helper.echo(
__("info", "cli.project.init.create", {
"name": name,
"path": self.home
}))
__("info", "cli.project.init.create",
{"path": self.project_controller.home}))
if not name:
_, default_name = os.path.split(self.project_controller.home)
name = self.cli_helper.prompt(
__("prompt", "cli.project.init.name"))
__("prompt", "cli.project.init.name"),
default=default_name)
if not description:
description = self.cli_helper.prompt(
__("prompt", "cli.project.init.description"))
Expand All @@ -53,46 +54,45 @@ def init(self, name, description):
self.cli_helper.echo(
__("info", "cli.project.init.create.success", {
"name": name,
"path": self.home
"path": self.project_controller.home
}))
except Exception:
self.cli_helper.echo(
__("info", "cli.project.init.create.failure", {
"name": name,
"path": self.home
"path": self.project_controller.home
}))
return None
else: # Update the current project
self.cli_helper.echo(
__("info", "cli.project.init.update", {
"name": self.project_controller.model.name,
"path": self.home
}))
__(
"info", "cli.project.init.update", {
"name": self.project_controller.model.name,
"path": self.project_controller.home
}))
# Prompt for the name and description and add default if not given
if not name:
name = self.cli_helper.prompt(
__("prompt", "cli.project.init.name"))
__("prompt", "cli.project.init.name"),
default=self.project_controller.model.name)
if not description:
description = self.cli_helper.prompt(
__("prompt", "cli.project.init.description"))
# Update project parameter if given parameter is not falsy and different
if not name or name == self.project_controller.model.name:
name = self.project_controller.model.name
if not description or description == self.project_controller.model.description:
description = self.project_controller.model.description
__("prompt", "cli.project.init.description"),
default=self.project_controller.model.description)
# Update the project with the values given
try:
success = self.project_controller.init(name, description)
if success:
self.cli_helper.echo(
__("info", "cli.project.init.update.success", {
"name": name,
"path": self.home
"path": self.project_controller.home
}))
except Exception:
self.cli_helper.echo(
__("info", "cli.project.init.update.failure", {
"name": name,
"path": self.home
"path": self.project_controller.home
}))
return None

Expand All @@ -107,7 +107,7 @@ def init(self, name, description):
__("prompt", "cli.project.environment.setup"))
if environment_setup:
# Setting up the environment definition file
self.environment_controller = EnvironmentController(home=self.home)
self.environment_controller = EnvironmentController()
available_environments = self.environment_controller.get_supported_environments(
)
input_environment_name = self.cli_helper.prompt_available_environments(
Expand All @@ -128,6 +128,7 @@ def init(self, name, description):
def version(self):
return self.cli_helper.echo("datmo version: %s" % __version__)

@Helper.notify_no_project_found
def status(self):
status_dict, latest_snapshot, ascending_unstaged_tasks = self.project_controller.status(
)
Expand Down Expand Up @@ -196,10 +197,11 @@ def cleanup(self):
}))
return False

@Helper.notify_environment_active(ProjectController)
@Helper.notify_no_project_found
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()

# Creating input dictionaries
snapshot_dict = {}
Expand Down

0 comments on commit 2082272

Please sign in to comment.