Skip to content

Commit

Permalink
feat(cli): catch the known custom exception and print error message
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmallows committed Aug 17, 2021
1 parent 9a0f568 commit 83fd1f4
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 193 deletions.
61 changes: 32 additions & 29 deletions tensorbay/cli/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import click

from ..exception import UnauthorizedError
from ..exception import TensorBayException, UnauthorizedError
from .utility import (
ContextInfo,
error,
Expand All @@ -35,38 +35,41 @@ def _implement_auth( # pylint: disable=too-many-arguments
unset: bool,
is_all: bool,
) -> None:
_check_args_and_options(arg1, arg2, get, status, unset, is_all)

if get:
_get_auth(obj, is_all)
return

if status:
_status_auth(obj, is_all)
return

if unset:
_unset_auth(obj, is_all)
return
try:
_check_args_and_options(arg1, arg2, get, status, unset, is_all)

if not arg1 and not arg2:
arg1 = _interactive_auth()
if get:
_get_auth(obj, is_all)
return

elif is_accesskey(arg1):
if _is_gas_url(arg2):
error('Please use "gas auth [url] [accessKey]" to specify the url and accessKey')
if arg2:
error(f'Redundant argument "{arg2}"')
if status:
_status_auth(obj, is_all)
return

elif _is_gas_url(arg1):
if not arg2:
arg2 = _interactive_auth(arg1)
elif not is_accesskey(arg2):
error("Wrong accesskey format")
else:
error(f'Invalid argument "{arg1}"')
if unset:
_unset_auth(obj, is_all)
return

_update_profile(obj, arg1, arg2)
if not arg1 and not arg2:
arg1 = _interactive_auth()

elif is_accesskey(arg1):
if _is_gas_url(arg2):
error('Please use "gas auth [url] [accessKey]" to specify the url and accessKey')
if arg2:
error(f'Redundant argument "{arg2}"')

elif _is_gas_url(arg1):
if not arg2:
arg2 = _interactive_auth(arg1)
elif not is_accesskey(arg2):
error("Wrong accesskey format")
else:
error(f'Invalid argument "{arg1}"')

_update_profile(obj, arg1, arg2)
except TensorBayException as err:
error(str(err))


def _get_auth(obj: ContextInfo, is_all: bool) -> None:
Expand Down
44 changes: 23 additions & 21 deletions tensorbay/cli/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import click

from ..exception import TensorBayException
from .tbrn import TBRN, TBRNType
from .utility import ContextInfo, edit_message, error, format_hint, get_dataset_client, get_gas

Expand All @@ -21,24 +22,25 @@


def _implement_commit(obj: ContextInfo, tbrn: str, message: Tuple[str, ...]) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info)

if info.type != TBRNType.DATASET:
error(f'To operate a commit, "{info}" must be a dataset')

if not info.is_draft:
error(f'To commit, "{info}" must be in draft status, like "{info}#1"')

dataset_client.checkout(draft_number=info.draft_number)

draft = dataset_client.get_draft()
hint_message = format_hint(draft.title, draft.description, _COMMIT_HINT)
title, description = edit_message(message, hint_message, obj.config_parser)
if not title:
error("Aborting commit due to empty commit message")

dataset_client.commit(title, description)
commit_tbrn = TBRN(info.dataset_name, revision=dataset_client.status.commit_id).get_tbrn()
click.echo(f"Committed successfully: {tbrn} -> {commit_tbrn}")
try:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info)

if info.type != TBRNType.DATASET:
error(f'To operate a commit, "{info}" must be a dataset')
if not info.is_draft:
error(f'To commit, "{info}" must be in draft status, like "{info}#1"')

dataset_client.checkout(draft_number=info.draft_number)
draft = dataset_client.get_draft()
hint_message = format_hint(draft.title, draft.description, _COMMIT_HINT)
title, description = edit_message(message, hint_message, obj.config_parser)
if not title:
error("Aborting commit due to empty commit message")

dataset_client.commit(title, description)
commit_tbrn = TBRN(info.dataset_name, revision=dataset_client.status.commit_id).get_tbrn()
click.echo(f"Committed successfully: {tbrn} -> {commit_tbrn}")
except TensorBayException as err:
error(str(err))
54 changes: 29 additions & 25 deletions tensorbay/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,41 @@

import click

from ..exception import TensorBayException
from .utility import ContextInfo, error, write_config


def _implement_config(obj: ContextInfo, key: str, value: str, unset: bool) -> None:
_check_args_and_options(key, value, unset)

config_parser = obj.config_parser

if not config_parser.has_section("config"):
config_parser.add_section("config")
config_section = config_parser["config"]

if not key:
for config_key, config_value in config_section.items():
click.echo(f"{config_key} = {config_value}\n")
return

if not value:
if key not in config_section:
error(f"{key} has not been configured yet")
if unset:
del config_section[key]
write_config(config_parser, show_message=False)
click.echo(f'Unset "{key}" successfully')
try:
_check_args_and_options(key, value, unset)

config_parser = obj.config_parser

if not config_parser.has_section("config"):
config_parser.add_section("config")
config_section = config_parser["config"]

if not key:
for config_key, config_value in config_section.items():
click.echo(f"{config_key} = {config_value}\n")
return

click.echo(f"{key} = {config_section[key]}\n")
else:
_check_key_and_value(key, value)
config_section[key] = value
write_config(config_parser)
if not value:
if key not in config_section:
error(f"{key} has not been configured yet")
if unset:
del config_section[key]
write_config(config_parser, show_message=False)
click.echo(f'Unset "{key}" successfully')
return

click.echo(f"{key} = {config_section[key]}\n")
else:
_check_key_and_value(key, value)
config_section[key] = value
write_config(config_parser)
except TensorBayException as err:
error(str(err))


def _check_args_and_options(key: str, value: str, unset: bool) -> None:
Expand Down
52 changes: 30 additions & 22 deletions tensorbay/cli/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from typing import Iterable

from ..dataset import Data, Segment
from ..exception import TensorBayException
from .tbrn import TBRN, TBRNType
from .utility import ContextInfo, error, get_dataset_client, get_gas

Expand All @@ -22,28 +23,35 @@ def _implement_cp( # pylint: disable=too-many-arguments
jobs: int,
skip_uploaded_files: bool,
) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)

dataset_client = get_dataset_client(gas, info, is_fusion=False)

if info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
error(f'"{tbrn}" is not a segment or file type')

target_remote_path = info.remote_path if info.type == TBRNType.NORMAL_FILE else ""

local_abspaths = [os.path.abspath(local_path) for local_path in local_paths]
if (
len(local_abspaths) == 1
and not os.path.isdir(local_abspaths[0])
and target_remote_path
and not target_remote_path.endswith("/")
):
segment_client = dataset_client.get_or_create_segment(info.segment_name)
segment_client.upload_file(local_abspaths[0], target_remote_path)
else:
segment = _get_segment(info.segment_name, local_abspaths, target_remote_path, is_recursive)
dataset_client.upload_segment(segment, jobs=jobs, skip_uploaded_files=skip_uploaded_files)
try:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)

dataset_client = get_dataset_client(gas, info, is_fusion=False)

if info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
error(f'"{tbrn}" is not a segment or file type')

target_remote_path = info.remote_path if info.type == TBRNType.NORMAL_FILE else ""

local_abspaths = [os.path.abspath(local_path) for local_path in local_paths]
if (
len(local_abspaths) == 1
and not os.path.isdir(local_abspaths[0])
and target_remote_path
and not target_remote_path.endswith("/")
):
segment_client = dataset_client.get_or_create_segment(info.segment_name)
segment_client.upload_file(local_abspaths[0], target_remote_path)
else:
segment = _get_segment(
info.segment_name, local_abspaths, target_remote_path, is_recursive
)
dataset_client.upload_segment(
segment, jobs=jobs, skip_uploaded_files=skip_uploaded_files
)
except TensorBayException as err:
error(str(err))


def _get_segment(
Expand Down
48 changes: 22 additions & 26 deletions tensorbay/cli/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ..client.gas import DatasetClientType
from ..client.struct import ROOT_COMMIT_ID
from ..exception import ResourceNotExistError
from ..exception import StatusError, TensorBayException
from .auth import INDENT
from .tbrn import TBRN, TBRNType
from .utility import ContextInfo, edit_message, error, format_hint, get_dataset_client, get_gas
Expand All @@ -38,21 +38,26 @@ def _implement_draft( # pylint: disable=too-many-arguments
close: bool,
message: Tuple[str, ...],
) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info)

if info.type != TBRNType.DATASET:
error(f'To operate a draft, "{info}" must be a dataset')

if is_list:
_list_drafts(dataset_client, info)
elif edit:
_edit_draft(dataset_client, info, message, obj.config_parser)
elif close:
_close_draft(dataset_client, info)
else:
_create_draft(dataset_client, info, message, obj.config_parser)
try:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info)

if info.type != TBRNType.DATASET:
error(f'To operate a draft, "{info}" must be a dataset')

if is_list:
_list_drafts(dataset_client, info)
elif edit:
_edit_draft(dataset_client, info, message, obj.config_parser)
elif close:
_close_draft(dataset_client, info)
else:
_create_draft(dataset_client, info, message, obj.config_parser)
except StatusError:
error("Draft number is required when editing draft")
except TensorBayException as err:
error(str(err))


def _create_draft(
Expand Down Expand Up @@ -98,10 +103,7 @@ def _echo_draft(
if not branch_name:
error("Draft should be created based on a branch.")

try:
branch = dataset_client.get_branch(branch_name)
except ResourceNotExistError:
error(f'The branch "{branch_name}" does not exist')
branch = dataset_client.get_branch(branch_name)

if branch.commit_id != ROOT_COMMIT_ID:
commit_id = f"({branch.commit_id})"
Expand All @@ -128,9 +130,6 @@ def _edit_draft(
message: Tuple[str, ...],
config_parser: ConfigParser,
) -> None:
if not info.is_draft:
error("Draft number is required when editing draft")

draft = dataset_client.get_draft()
hint_message = format_hint(draft.title, draft.description, _DRAFT_HINT)
title, description = edit_message(message, hint_message, config_parser)
Expand All @@ -143,8 +142,5 @@ def _edit_draft(


def _close_draft(dataset_client: DatasetClientType, info: TBRN) -> None:
if not info.is_draft:
error("Draft number is required when editing draft")

dataset_client.close_draft()
click.echo(f"{info.get_tbrn()} is closed.")
Loading

0 comments on commit 83fd1f4

Please sign in to comment.