Skip to content

Commit

Permalink
Refactored out home variable to Config().home
Browse files Browse the repository at this point in the history
  • Loading branch information
Arron committed May 17, 2018
1 parent 0d8899e commit fe5c0eb
Show file tree
Hide file tree
Showing 38 changed files with 160 additions and 214 deletions.
5 changes: 3 additions & 2 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 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
6 changes: 3 additions & 3 deletions datmo/cli/command/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,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()

def init(self, name, description):
"""Initialize command
Expand Down
6 changes: 3 additions & 3 deletions datmo/cli/command/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@


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

def session(self):
self.parse(["--help"])
Expand Down
6 changes: 3 additions & 3 deletions datmo/cli/command/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@


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

def usage(self):
self.cli_helper.echo(__("argparser", "cli.snapshot.usage"))
Expand Down
6 changes: 3 additions & 3 deletions datmo/cli/command/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@


class TaskCommand(ProjectCommand):
def __init__(self, home, cli_helper):
super(TaskCommand, self).__init__(home, cli_helper)
self.task_controller = TaskController(home=home)
def __init__(self, cli_helper):
super(TaskCommand, self).__init__(cli_helper)
self.task_controller = TaskController()

def task(self):
self.parse(["--help"])
Expand Down
6 changes: 4 additions & 2 deletions datmo/cli/command/tests/test_datmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tempfile
import platform

from datmo.config import Config
from datmo.cli.driver.helper import Helper
from datmo.cli.command.datmo_command import DatmoCommand

Expand All @@ -29,17 +30,18 @@ def setup_class(self):
test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
tempfile.gettempdir())
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli = Helper()

def teardown_class(self):
pass

def test_datmo_base(self):
base = DatmoCommand(self.temp_dir, self.cli)
base = DatmoCommand(self.cli)
base.parse([])
assert base.execute()

def test_datmo_help(self):
base = DatmoCommand(self.temp_dir, self.cli)
base = DatmoCommand(self.cli)
base.parse(["--help"])
assert base.execute()
4 changes: 3 additions & 1 deletion datmo/cli/command/tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import tempfile
import platform

from datmo.config import Config
from datmo import __version__
from datmo.cli.driver.helper import Helper
from datmo.cli.command.project import ProjectCommand
Expand All @@ -30,8 +31,9 @@ def setup_method(self):
test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
tempfile.gettempdir())
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli_helper = Helper()
self.project = ProjectCommand(self.temp_dir, self.cli_helper)
self.project = ProjectCommand(self.cli_helper)

def teardown_method(self):
pass
Expand Down
8 changes: 5 additions & 3 deletions datmo/cli/command/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
to_unicode = str

import os
from datmo.config import Config
from datmo.cli.driver.helper import Helper
from datmo.cli.command.session import SessionCommand
from datmo.cli.command.project import ProjectCommand
Expand All @@ -27,22 +28,23 @@ def setup_class(self):
test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
tempfile.gettempdir())
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli_helper = Helper()

def teardown_class(self):
pass

def __set_variables(self):
self.project = ProjectCommand(self.temp_dir, self.cli_helper)
self.project = ProjectCommand(self.cli_helper)
self.project.parse(
["init", "--name", "foobar", "--description", "test model"])
self.project.execute()
self.session_command = SessionCommand(self.temp_dir, self.cli_helper)
self.session_command = SessionCommand(self.cli_helper)

def test_session_project_not_init(self):
failed = False
try:
self.snapshot = SessionCommand(self.temp_dir, self.cli_helper)
self.snapshot = SessionCommand(self.cli_helper)
except ProjectNotInitializedException:
failed = True
assert failed
Expand Down
12 changes: 7 additions & 5 deletions datmo/cli/command/tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
except NameError:
to_unicode = str

from datmo.config import Config
from datmo.cli.driver.helper import Helper
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.snapshot import SnapshotCommand
Expand All @@ -38,17 +39,18 @@
class TestSnapshot():
def setup_class(self):
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli_helper = Helper()

def teardown_class(self):
pass

def __set_variables(self):
self.project = ProjectCommand(self.temp_dir, self.cli_helper)
self.project = ProjectCommand(self.cli_helper)
self.project.parse(
["init", "--name", "foobar", "--description", "test model"])
self.project.execute()
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper)
self.snapshot = SnapshotCommand(self.cli_helper)

# Create environment_driver definition
self.env_def_path = os.path.join(self.temp_dir, "Dockerfile")
Expand Down Expand Up @@ -78,7 +80,7 @@ def __set_variables(self):
def test_snapshot_project_not_init(self):
failed = False
try:
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper)
self.snapshot = SnapshotCommand(self.cli_helper)
except ProjectNotInitializedException:
failed = True
assert failed
Expand Down Expand Up @@ -191,7 +193,7 @@ def test_snapshot_create_from_task(self):
# create task
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.task = TaskCommand(self.temp_dir, self.cli_helper)
self.task = TaskCommand(self.cli_helper)
self.task.parse([
"task", "run", "--environment-def", test_dockerfile, test_command
])
Expand Down Expand Up @@ -221,7 +223,7 @@ def test_snapshot_create_from_task_fail_user_inputs(self):
# create task
test_command = "sh -c 'echo accuracy:0.45'"
test_dockerfile = os.path.join(self.temp_dir, "Dockerfile")
self.task = TaskCommand(self.temp_dir, self.cli_helper)
self.task = TaskCommand(self.cli_helper)
self.task.parse([
"task", "run", "--environment-def", test_dockerfile, test_command
])
Expand Down
10 changes: 6 additions & 4 deletions datmo/cli/command/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
except NameError:
to_unicode = str

from datmo.config import Config
from datmo.cli.driver.helper import Helper
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.task import TaskCommand
Expand All @@ -39,18 +40,19 @@
class TestTaskCommand():
def setup_class(self):
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
self.cli_helper = Helper()

def teardown_class(self):
pass

def __set_variables(self):
self.project = ProjectCommand(self.temp_dir, self.cli_helper)
self.project = ProjectCommand(self.cli_helper)
self.project.parse(
["init", "--name", "foobar", "--description", "test model"])
self.project.execute()

self.task = TaskCommand(self.temp_dir, self.cli_helper)
self.task = TaskCommand(self.cli_helper)

# Create environment_driver definition
env_def_path = os.path.join(self.temp_dir, "Dockerfile")
Expand All @@ -60,7 +62,7 @@ def __set_variables(self):
def test_task_project_not_init(self):
failed = False
try:
self.task = TaskCommand(self.temp_dir, self.cli_helper)
self.task = TaskCommand(self.cli_helper)
except ProjectNotInitializedException:
failed = True
assert failed
Expand Down Expand Up @@ -159,7 +161,7 @@ def test_task_run_string_command(self):
#
# def task_exec_func(procnum, return_dict):
# print("Creating Task object")
# task = TaskCommand(self.temp_dir, self.cli_helper)
# task = TaskCommand(self.cli_helper)
# print("Parsing command")
# task.parse(
# ["task", "run", "--environment-def", test_dockerfile, test_command])
Expand Down
1 change: 1 addition & 0 deletions datmo/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def main():
# This is required for logging to place the logs in a
# place for the user.
config = Config()
config.home = os.getcwd()

log = DatmoLogger.get_logger(__name__)
log.info("handling command %s", config.home)
Expand Down
3 changes: 1 addition & 2 deletions datmo/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ def __init__(self):
def home(self):
return self._home

@home.setter
def home(self, home_path):
def set_home(self, home_path):
self._home = home_path

def get_cache_item(self, key):
Expand Down
4 changes: 2 additions & 2 deletions datmo/core/controller/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class BaseController(object):
Return the configuration defaults
"""

def __init__(self, home):
self.home = home
def __init__(self):
self.home = Config().home
if not os.path.isdir(self.home):
raise InvalidProjectPathException(
__("error", "controller.base.__init__", home))
Expand Down
4 changes: 2 additions & 2 deletions datmo/core/controller/code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class CodeController(BaseController):
delete the specified code object from the project
"""

def __init__(self, home):
def __init__(self):
try:
super(CodeController, self).__init__(home)
super(CodeController, self).__init__()
except EnvironmentInitFailed:
self.logger.warning(
__("warn", "controller.general.environment.failed"))
Expand Down
6 changes: 4 additions & 2 deletions datmo/core/controller/code/tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
except NameError:
to_unicode = str

from datmo.config import Config
from datmo.core.controller.project import ProjectController
from datmo.core.controller.code.code import CodeController
from datmo.core.util.exceptions import (EntityNotFound, GitCommitDoesNotExist)
Expand All @@ -23,8 +24,9 @@ def setup_method(self):
test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
tempfile.gettempdir())
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
self.project = ProjectController(self.temp_dir)
self.code = CodeController(self.temp_dir)
Config().set_home(self.temp_dir)
self.project = ProjectController()
self.code = CodeController()

def teardown_method(self):
pass
Expand Down
6 changes: 3 additions & 3 deletions datmo/core/controller/environment/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class EnvironmentController(BaseController):
Delete the specified environment from the project
"""

def __init__(self, home):
super(EnvironmentController, self).__init__(home)
self.file_collection = FileCollectionController(home)
def __init__(self):
super(EnvironmentController, self).__init__()
self.file_collection = FileCollectionController()

def create(self, dictionary):
"""Create an environment
Expand Down
6 changes: 4 additions & 2 deletions datmo/core/controller/environment/tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
except NameError:
to_unicode = str

from datmo.config import Config
from datmo.core.controller.project import ProjectController
from datmo.core.controller.environment.environment import \
EnvironmentController
Expand All @@ -30,8 +31,9 @@
class TestEnvironmentController():
def setup_method(self):
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
self.project = ProjectController(self.temp_dir)
self.environment = EnvironmentController(self.temp_dir)
Config().set_home(self.temp_dir)
self.project = ProjectController()
self.environment = EnvironmentController()

def teardown_method(self):
pass
Expand Down
1 change: 1 addition & 0 deletions datmo/core/controller/file/driver/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from datmo.core.util.exceptions import (PathDoesNotExist, FileIOException,
FileStructureException)
from datmo.core.controller.file.driver import FileDriver
from datmo.config import Config


class LocalFileDriver(FileDriver):
Expand Down
5 changes: 5 additions & 0 deletions datmo/core/controller/file/driver/tests/test_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from datmo.core.controller.file.driver.local import LocalFileDriver
from datmo.core.util.exceptions import PathDoesNotExist
from datmo.core.controller.project import ProjectController
from datmo.config import Config


class TestLocalFileDriver():
Expand All @@ -27,6 +29,9 @@ def setup_method(self):
test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
tempfile.gettempdir())
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
Config().set_home(self.temp_dir)
project = ProjectController()
project.init("testing", "test-project")
self.local_file_driver = LocalFileDriver(filepath=self.temp_dir)

def teardown_method(self):
Expand Down

0 comments on commit fe5c0eb

Please sign in to comment.