Skip to content

Commit

Permalink
implement the rest of workspace operations for webapi
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-permana committed Feb 10, 2017
1 parent e402027 commit 4f8d20c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
18 changes: 12 additions & 6 deletions dedop/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ def _get_workspace_name(command_args):
workspace_name = command_args.workspace_name
if workspace_name:
return workspace_name
return _WORKSPACE_MANAGER.get_current_workspace_name()
workspace = _WORKSPACE_MANAGER.get_current_workspace()
return workspace.name


def _get_workspace_and_config_name(command_args):
workspace_name = command_args.workspace_name
if not workspace_name:
workspace_name = _WORKSPACE_MANAGER.get_current_workspace_name()
workspace = _WORKSPACE_MANAGER.get_current_workspace()
workspace_name = workspace.name
config_name = command_args.config_name
if workspace_name and not config_name:
config_name = _WORKSPACE_MANAGER.get_current_config_name(workspace_name)
Expand Down Expand Up @@ -270,7 +272,8 @@ def execute_remove(cls, command_args):
try:
_WORKSPACE_MANAGER.delete_workspace(workspace_name)
print('removed workspace "%s"' % workspace_name)
if workspace_name == _WORKSPACE_MANAGER.get_current_workspace_name():
workspace = _WORKSPACE_MANAGER.get_current_workspace()
if workspace_name == workspace.name:
workspace_names = _WORKSPACE_MANAGER.get_workspace_names()
if 'default' in workspace_names:
cls.set_current_workspace('default')
Expand Down Expand Up @@ -305,7 +308,8 @@ def execute_rename(cls, command_args):
try:
_WORKSPACE_MANAGER.rename_workspace(workspace_name, new_name)
print('renamed workspace "%s" to "%s"' % (workspace_name, new_name))
if workspace_name == _WORKSPACE_MANAGER.get_current_workspace_name():
workspace = _WORKSPACE_MANAGER.get_current_workspace()
if workspace_name == workspace.name:
cls.set_current_workspace(new_name)
except WorkspaceError as error:
raise CommandError(error)
Expand All @@ -316,7 +320,8 @@ def execute_current(cls, command_args):
if command_args.workspace_name:
cls.set_current_workspace(command_args.workspace_name)
else:
workspace_name = _WORKSPACE_MANAGER.get_current_workspace_name()
workspace = _WORKSPACE_MANAGER.get_current_workspace()
workspace_name = workspace.name
if workspace_name:
print('current workspace is "%s"' % workspace_name)
else:
Expand Down Expand Up @@ -1008,7 +1013,8 @@ def execute(self, command_args):
workspace_names = ', '.join(workspace_names)
else:
workspace_names = '(not set)'
cur_workspace_name = _WORKSPACE_MANAGER.get_current_workspace_name()
workspace = _WORKSPACE_MANAGER.get_current_workspace()
cur_workspace_name = workspace.name
if cur_workspace_name:
cur_config_name = _WORKSPACE_MANAGER.get_current_config_name(cur_workspace_name)
if not cur_config_name:
Expand Down
3 changes: 2 additions & 1 deletion dedop/conf/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

DEFAULT_DATA_DIR_NAME = '.dedop'
DEFAULT_DATA_PATH = os.path.join(os.path.expanduser('~'), DEFAULT_DATA_DIR_NAME)
DEFAULT_WORKSPACE_PATH = os.path.join(DEFAULT_DATA_PATH, 'workspaces')
DEFAULT_WORKSPACE_NAME = 'default'
DEFAULT_WORKSPACE_PATH = os.path.join(DEFAULT_DATA_PATH, 'workspaces', DEFAULT_WORKSPACE_NAME)

#: allow a 100 ms period between two progress messages sent to the client
WEBAPI_PROGRESS_DEFER_PERIOD = 0.5
Expand Down
6 changes: 3 additions & 3 deletions dedop/ui/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import sys
from collections import OrderedDict

from dedop.conf.defaults import DEFAULT_WORKSPACE_PATH


class Workspace:
def __init__(self, workspace_dir, name, is_current=True):
assert workspace_dir
assert name
self._workspace_dir = workspace_dir
self._workspace_dir = workspace_dir if workspace_dir else DEFAULT_WORKSPACE_PATH
self._name = name
self._is_current = is_current

Expand Down
13 changes: 9 additions & 4 deletions dedop/ui/workspace_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __str__(self):
return self.message


def _readline(file_path: str) -> str:
def _readline(file_path: str) -> str or None:
if os.path.exists(file_path) and os.path.isfile(file_path):
try:
with open(file_path, 'r') as fp:
Expand Down Expand Up @@ -95,8 +95,9 @@ def delete_all_workspaces(self) -> str:
except (IOError, OSError) as e:
raise WorkspaceError(str(e))

def get_current_workspace_name(self) -> str:
return _readline(os.path.join(self._workspaces_dir, _CURRENT_FILE_NAME))
def get_current_workspace(self) -> Workspace:
current_workspace = _readline(os.path.join(self._workspaces_dir, _CURRENT_FILE_NAME))
return Workspace(None, current_workspace)

def set_current_workspace_name(self, workspace_name: str):
self._assert_workspace_exists(workspace_name)
Expand Down Expand Up @@ -145,6 +146,8 @@ def copy_workspace(self, workspace_name: str, new_workspace_name: str):
os.path.join(dir_path, new_workspace_name))
except (IOError, OSError) as e:
raise WorkspaceError(str(e))
workspace_dir = self.get_workspace_path(new_workspace_name)
return Workspace(workspace_dir, new_workspace_name)

def rename_workspace(self, workspace_name: str, new_workspace_name: str):
"""
Expand All @@ -162,6 +165,8 @@ def rename_workspace(self, workspace_name: str, new_workspace_name: str):
os.path.join(dir_path, new_workspace_name))
except (IOError, OSError) as e:
raise WorkspaceError(str(e))
workspace_dir = self.get_workspace_path(new_workspace_name)
return Workspace(workspace_dir, new_workspace_name)

def get_workspace_names(self) -> List[str]:
workspaces_dir = self._workspaces_dir
Expand Down Expand Up @@ -304,7 +309,7 @@ def get_input_paths(self, workspace_name: str):
return [self.get_inputs_path(workspace_name, name) for name in
self.get_input_names(workspace_name)]

def get_workspace_path(self, workspace_name, *paths):
def get_workspace_path(self, workspace_name, *paths) -> str:
return os.path.join(self._workspaces_dir, workspace_name, *paths)

def get_config_path(self, workspace_name, config_name, *paths):
Expand Down
20 changes: 20 additions & 0 deletions dedop/webapi/websocket.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import List

from dedop.ui.workspace_manager import WorkspaceManager


Expand Down Expand Up @@ -27,3 +29,21 @@ def new_workspace(self, workspace_name) -> dict:

def delete_workspace(self, workspace_name) -> None:
self.workspace_manager.delete_workspace(workspace_name)

def copy_workspace(self, workspace_name, new_workspace_name) -> dict:
workspace = self.workspace_manager.copy_workspace(workspace_name, new_workspace_name)
return workspace.to_json_dict()

def rename_workspace(self, workspace_name, new_workspace_name) -> dict:
workspace = self.workspace_manager.rename_workspace(workspace_name, new_workspace_name)
return workspace.to_json_dict()

def get_current_workspace(self) -> dict:
workspace = self.workspace_manager.get_current_workspace()
return workspace.to_json_dict()

def set_current_workspace(self, workspace_name) -> None:
self.workspace_manager.set_current_workspace_name(workspace_name)

def get_all_workspaces(self) -> List[str]:
return self.workspace_manager.get_workspace_names()

0 comments on commit 4f8d20c

Please sign in to comment.