Skip to content

Commit

Permalink
style(cli): unify the variable name of tbrn string and TBRN instance
Browse files Browse the repository at this point in the history
PR Closed: #1002
  • Loading branch information
Lee-000 committed Sep 10, 2021
1 parent d5c6d26 commit d4a70b6
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 117 deletions.
20 changes: 10 additions & 10 deletions tensorbay/cli/branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
def _implement_branch(
obj: ContextInfo, tbrn: str, name: str, verbose: bool, is_delete: bool
) -> None:
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'To operate a branch, "{info}" must be a dataset')
tbrn_info = TBRN(tbrn=tbrn)
if tbrn_info.type != TBRNType.DATASET:
error(f'To operate a branch, "{tbrn_info}" must be a dataset')

gas = get_gas(*obj)
dataset_client = get_dataset_client(gas, info)
dataset_client = get_dataset_client(gas, tbrn_info)

if is_delete:
_delete_branch(dataset_client, info)
_delete_branch(dataset_client, tbrn_info)
return

if name:
Expand Down Expand Up @@ -53,9 +53,9 @@ def _list_branches(dataset_client: DatasetClientType, verbose: bool) -> None:
click.echo(f"{branch.name:{name_length}} {shorten(branch.commit_id)} {branch.title}")


def _delete_branch(dataset_client: DatasetClientType, info: TBRN) -> None:
if not info.revision:
error(f'To delete a branch, "{info}" must have a branch name')
def _delete_branch(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
if not tbrn_info.revision:
error(f'To delete a branch, "{tbrn_info.get_tbrn()}" must have a branch name')

dataset_client._delete_branch(info.revision) # pylint: disable=protected-access
click.echo(f'Successfully deleted branch "{info.get_colored_tbrn()}"')
dataset_client._delete_branch(tbrn_info.revision) # pylint: disable=protected-access
click.echo(f'Successfully deleted branch "{tbrn_info.get_colored_tbrn()}"')
19 changes: 10 additions & 9 deletions tensorbay/cli/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,24 @@
@exception_handler
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)
tbrn_info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, tbrn_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"')
if tbrn_info.type != TBRNType.DATASET:
error(f'To operate a commit, "{tbrn}" must be a dataset')
if not tbrn_info.is_draft:
error(f'To commit, "{tbrn}" must be in draft status, like "{tbrn}#1"')

dataset_client.checkout(draft_number=info.draft_number)
dataset_client.checkout(draft_number=tbrn_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)
commit_tbrn = TBRN(tbrn_info.dataset_name, revision=dataset_client.status.commit_id)
click.echo(
"Committed successfully: " f"{info.get_colored_tbrn()} -> {commit_tbrn.get_colored_tbrn()}"
"Committed successfully: "
f"{tbrn_info.get_colored_tbrn()} -> {commit_tbrn.get_colored_tbrn()}"
)
14 changes: 8 additions & 6 deletions tensorbay/cli/cp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def _implement_cp( # pylint: disable=too-many-arguments
skip_uploaded_files: bool,
) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
tbrn_info = TBRN(tbrn=tbrn)

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

if info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
if tbrn_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 ""
target_remote_path = tbrn_info.remote_path if tbrn_info.type == TBRNType.NORMAL_FILE else ""

local_abspaths = [os.path.abspath(local_path) for local_path in local_paths]
if (
Expand All @@ -40,10 +40,12 @@ def _implement_cp( # pylint: disable=too-many-arguments
and target_remote_path
and not target_remote_path.endswith("/")
):
segment_client = dataset_client.get_or_create_segment(info.segment_name)
segment_client = dataset_client.get_or_create_segment(tbrn_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)
segment = _get_segment(
tbrn_info.segment_name, local_abspaths, target_remote_path, is_recursive
)
dataset_client.upload_segment(segment, jobs=jobs, skip_uploaded_files=skip_uploaded_files)


Expand Down
10 changes: 5 additions & 5 deletions tensorbay/cli/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ def _implement_dataset(obj: ContextInfo, tbrn: str, is_delete: bool, yes: bool)
for dataset_name in gas.list_dataset_names():
click.echo(TBRN(dataset_name).get_tbrn())

info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
tbrn_info = TBRN(tbrn=tbrn)
if tbrn_info.type != TBRNType.DATASET:
error(f'"{tbrn}" is not a dataset')
colored_tbrn = info.get_colored_tbrn()
colored_tbrn = tbrn_info.get_colored_tbrn()

if is_delete:
if not yes:
Expand All @@ -32,9 +32,9 @@ def _implement_dataset(obj: ContextInfo, tbrn: str, is_delete: bool, yes: bool)
abort=True,
)

gas.delete_dataset(info.dataset_name)
gas.delete_dataset(tbrn_info.dataset_name)
click.echo(f'Successfully deleted dataset "{colored_tbrn}"')
return

gas.create_dataset(info.dataset_name)
gas.create_dataset(tbrn_info.dataset_name)
click.echo(f'Successfully created dataset "{colored_tbrn}"')
54 changes: 28 additions & 26 deletions tensorbay/cli/draft.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,53 +47,55 @@ def _implement_draft( # pylint: disable=too-many-arguments
message: Tuple[str, ...],
) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info)
tbrn_info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, tbrn_info)

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

if is_list:
_list_drafts(dataset_client, info)
_list_drafts(dataset_client, tbrn_info)
elif edit:
_edit_draft(dataset_client, info, message, obj.config_parser)
_edit_draft(dataset_client, tbrn_info, message, obj.config_parser)
elif close:
_close_draft(dataset_client, info)
_close_draft(dataset_client, tbrn_info)
else:
_create_draft(dataset_client, info, message, obj.config_parser)
_create_draft(dataset_client, tbrn_info, message, obj.config_parser)


def _create_draft(
dataset_client: DatasetClientType,
info: TBRN,
tbrn_info: TBRN,
message: Tuple[str, ...],
config_parser: ConfigParser,
) -> None:
if info.is_draft:
error(f'Create a draft in draft status "{info}" is not permitted')
if tbrn_info.is_draft:
error(f'Create a draft in draft status "{tbrn_info.get_tbrn()}" is not permitted')

title, description = edit_message(message, _DRAFT_HINT, config_parser)
if not title:
error("Aborting creating draft due to empty draft message")

dataset_client.create_draft(title=title, description=description)
status = dataset_client.status
draft_tbrn = TBRN(info.dataset_name, draft_number=status.draft_number).get_colored_tbrn()
draft_tbrn = TBRN(tbrn_info.dataset_name, draft_number=status.draft_number).get_colored_tbrn()
click.echo(f'Successfully created draft "{draft_tbrn}"')
_echo_draft(dataset_client, title, description, status.branch_name)


def _list_drafts(dataset_client: DatasetClientType, info: TBRN) -> None:
if info.revision:
error(f'list drafts based on given revision "{info}" is not supported')
def _list_drafts(dataset_client: DatasetClientType, tbrn_info: TBRN) -> None:
if tbrn_info.revision:
error(f'list drafts based on given revision "{tbrn_info.get_tbrn()}" is not supported')

if info.is_draft:
draft = dataset_client.get_draft(info.draft_number)
click.echo(f"Draft: {info.get_tbrn()}")
if tbrn_info.is_draft:
draft = dataset_client.get_draft(tbrn_info.draft_number)
click.echo(f"Draft: {tbrn_info.get_tbrn()}")
_echo_draft(dataset_client, draft.title, draft.description, draft.branch_name)
else:
for draft in dataset_client.list_drafts():
click.echo(f"Draft: {TBRN(info.dataset_name, draft_number=draft.number).get_tbrn()}")
click.echo(
f"Draft: {TBRN(tbrn_info.dataset_name, draft_number=draft.number).get_tbrn()}"
)
_echo_draft(dataset_client, draft.title, draft.description, draft.branch_name)


Expand Down Expand Up @@ -129,11 +131,11 @@ def _echo_draft(

def _edit_draft(
dataset_client: DatasetClientType,
info: TBRN,
tbrn_info: TBRN,
message: Tuple[str, ...],
config_parser: ConfigParser,
) -> None:
if not info.is_draft:
if not tbrn_info.is_draft:
error("Draft number is required when editing draft")

draft = dataset_client.get_draft()
Expand All @@ -143,13 +145,13 @@ def _edit_draft(
error("Aborting updating draft due to empty draft message")

dataset_client.update_draft(title=title, description=description)
click.echo(f'Successfully updated draft "{info.get_colored_tbrn()}"')
click.echo(f'Successfully updated draft "{tbrn_info.get_colored_tbrn()}"')
_echo_draft(dataset_client, title, description, dataset_client.status.branch_name)


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

dataset_client._close_draft(info.draft_number) # pylint: disable=protected-access
click.echo(f'Successfully closed draft "{info.get_colored_tbrn()}"')
dataset_client._close_draft(tbrn_info.draft_number) # pylint: disable=protected-access
click.echo(f'Successfully closed draft "{tbrn_info.get_colored_tbrn()}"')
12 changes: 7 additions & 5 deletions tensorbay/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,22 @@ def _implement_log( # pylint: disable=too-many-arguments
graph: bool,
) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
if info.type != TBRNType.DATASET:
error(f'To log commits, "{info}" must be a dataset')
tbrn_info = TBRN(tbrn=tbrn)
if tbrn_info.type != TBRNType.DATASET:
error(f'To log commits, "{tbrn}" must be a dataset')

dataset_client = gas._get_dataset_with_any_type( # pylint: disable=protected-access
info.dataset_name
tbrn_info.dataset_name
)
commit_id_to_branches: DefaultDict[str, List[str]] = defaultdict(list)
for branch in dataset_client.list_branches():
commit_id_to_branches[branch.commit_id].append(branch.name)
if is_all:
revisions: List[Optional[str]] = [branch.name for branch in dataset_client.list_branches()]
else:
revisions = [info.revision] if info.revision else [dataset_client.status.branch_name]
revisions = (
[tbrn_info.revision] if tbrn_info.revision else [dataset_client.status.branch_name]
)

Printer: Union[Type[_GraphPrinter], Type[_CommitPrinter]] = (
_GraphPrinter if graph else _CommitPrinter
Expand Down
32 changes: 16 additions & 16 deletions tensorbay/cli/ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ def _validate_dataset_client(dataset_client: DatasetClientType) -> None:
error(f'"{dataset_client.name}" has no commit history.')


def _ls_dataset(gas: GAS, info: TBRN, list_all_files: bool, show_total_num: bool) -> None:
dataset_client = get_dataset_client(gas, info)
def _ls_dataset(gas: GAS, tbrn_info: TBRN, list_all_files: bool, show_total_num: bool) -> None:
dataset_client = get_dataset_client(gas, tbrn_info)
_validate_dataset_client(dataset_client)

segment_names = dataset_client.list_segment_names()
if not list_all_files:
if show_total_num:
click.echo(f"total {len(segment_names)}")
for segment_name in segment_names:
click.echo(TBRN(info.dataset_name, segment_name).get_tbrn())
click.echo(TBRN(tbrn_info.dataset_name, segment_name).get_tbrn())
return

if isinstance(dataset_client, FusionDatasetClient):
Expand All @@ -74,40 +74,40 @@ def _ls_dataset(gas: GAS, info: TBRN, list_all_files: bool, show_total_num: bool

for segment_name, segment_path in zip(segment_names, segment_paths):
_echo_data(
info.dataset_name,
info.draft_number,
info.revision,
tbrn_info.dataset_name,
tbrn_info.draft_number,
tbrn_info.revision,
segment_name,
segment_path,
)


def _ls_segment(
gas: GAS,
info: TBRN,
tbrn_info: TBRN,
list_all_files: bool, # pylint: disable=unused-argument
show_total_num: bool,
) -> None:
dataset_client = get_dataset_client(gas, info)
dataset_client = get_dataset_client(gas, tbrn_info)
_validate_dataset_client(dataset_client)
if isinstance(dataset_client, FusionDatasetClient):
error("List fusion segment is not supported yet")

segment_path = dataset_client.get_segment(info.segment_name).list_data_paths()
segment_path = dataset_client.get_segment(tbrn_info.segment_name).list_data_paths()
if show_total_num:
click.echo(f"total {len(segment_path)}")
_echo_data(
info.dataset_name,
info.draft_number,
info.revision,
info.segment_name,
tbrn_info.dataset_name,
tbrn_info.draft_number,
tbrn_info.revision,
tbrn_info.segment_name,
segment_path,
)


def _ls_normal_file(
gas: GAS, # pylint: disable=unused-argument
info: TBRN, # pylint: disable=unused-argument
tbrn_info: TBRN, # pylint: disable=unused-argument
list_all_files: bool, # pylint: disable=unused-argument
show_total_num: bool, # pylint: disable=unused-argument
) -> None:
Expand All @@ -132,5 +132,5 @@ def _implement_ls(obj: ContextInfo, tbrn: str, list_all_files: bool, show_total_
click.echo(TBRN(dataset_name).get_tbrn())
return

info = TBRN(tbrn=tbrn)
_LS_FUNCS[info.type](gas, info, list_all_files, show_total_num)
tbrn_info = TBRN(tbrn=tbrn)
_LS_FUNCS[tbrn_info.type](gas, tbrn_info, list_all_files, show_total_num)
20 changes: 10 additions & 10 deletions tensorbay/cli/rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@
@exception_handler
def _implement_rm(obj: ContextInfo, tbrn: str, is_recursive: bool) -> None:
gas = get_gas(*obj)
info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, info, is_fusion=False)
tbrn_info = TBRN(tbrn=tbrn)
dataset_client = get_dataset_client(gas, tbrn_info, is_fusion=False)

if info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
if tbrn_info.type not in (TBRNType.SEGMENT, TBRNType.NORMAL_FILE):
error(f'"{tbrn}" is an invalid path to remove')

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

if info.type == TBRNType.SEGMENT:
if tbrn_info.type == TBRNType.SEGMENT:
if not is_recursive:
error("Please use -r option to remove the whole segment")

dataset_client.delete_segment(info.segment_name)
dataset_client.delete_segment(tbrn_info.segment_name)
else:
segment = dataset_client.get_segment(info.segment_name)
segment.delete_data(info.remote_path)
segment = dataset_client.get_segment(tbrn_info.segment_name)
segment.delete_data(tbrn_info.remote_path)

click.echo(f'Successfully deleted "{info.get_colored_tbrn()}"')
click.echo(f'Successfully deleted "{tbrn_info.get_colored_tbrn()}"')
Loading

0 comments on commit d4a70b6

Please sign in to comment.