From 67ed89e8eeb865aa5a3dbbca4a3b16e57bf6c6c8 Mon Sep 17 00:00:00 2001 From: Roman Donchenko Date: Mon, 16 Dec 2024 14:21:17 +0200 Subject: [PATCH] CLI make the output of `create` commands machine-readable The underlying SDK functions already emit human-friendly log messages with the ID of the created resource. Instead of printing largely the same message twice, we can just print the ID. That way, the CLI can be more easily integrated into other software. --- .../20241216_144316_roman_machine_readable_create.md | 6 ++++++ cvat-cli/src/cvat_cli/_internal/commands_projects.py | 2 +- cvat-cli/src/cvat_cli/_internal/commands_tasks.py | 4 ++-- tests/python/cli/test_cli_projects.py | 4 ++-- tests/python/cli/test_cli_tasks.py | 6 +++--- 5 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 changelog.d/20241216_144316_roman_machine_readable_create.md diff --git a/changelog.d/20241216_144316_roman_machine_readable_create.md b/changelog.d/20241216_144316_roman_machine_readable_create.md new file mode 100644 index 000000000000..f85c493ea5a4 --- /dev/null +++ b/changelog.d/20241216_144316_roman_machine_readable_create.md @@ -0,0 +1,6 @@ +### Changed + +- \[CLI\] The output of the `task create`, `task create-from-backup` and + `project create` commands is now just the created resource ID, + making it machine-readable + () diff --git a/cvat-cli/src/cvat_cli/_internal/commands_projects.py b/cvat-cli/src/cvat_cli/_internal/commands_projects.py index 06b55e572995..b6c39eeef434 100644 --- a/cvat-cli/src/cvat_cli/_internal/commands_projects.py +++ b/cvat-cli/src/cvat_cli/_internal/commands_projects.py @@ -83,7 +83,7 @@ def execute( dataset_format=dataset_format, status_check_period=status_check_period, ) - print(f"Created project ID {project.id}") + print(project.id) @COMMANDS.command_class("delete") diff --git a/cvat-cli/src/cvat_cli/_internal/commands_tasks.py b/cvat-cli/src/cvat_cli/_internal/commands_tasks.py index 811a864e12ca..8c6782887d97 100644 --- a/cvat-cli/src/cvat_cli/_internal/commands_tasks.py +++ b/cvat-cli/src/cvat_cli/_internal/commands_tasks.py @@ -226,7 +226,7 @@ def execute( status_check_period=status_check_period, pbar=DeferredTqdmProgressReporter(), ) - print("Created task id", task.id) + print(task.id) @COMMANDS.command_class("delete") @@ -406,7 +406,7 @@ def execute(self, client: Client, *, filename: str, status_check_period: int) -> status_check_period=status_check_period, pbar=DeferredTqdmProgressReporter(), ) - print(f"Created task ID", task.id) + print(task.id) @COMMANDS.command_class("auto-annotate") diff --git a/tests/python/cli/test_cli_projects.py b/tests/python/cli/test_cli_projects.py index acb1d9db2a5f..032085b52d56 100644 --- a/tests/python/cli/test_cli_projects.py +++ b/tests/python/cli/test_cli_projects.py @@ -35,7 +35,7 @@ def test_can_create_project(self): "https://bugs.example/", ) - project_id = int(stdout.split()[-1]) + project_id = int(stdout.rstrip("\n")) created_project = self.client.projects.retrieve(project_id) assert created_project.name == "new_project" assert created_project.bug_tracker == "https://bugs.example/" @@ -52,7 +52,7 @@ def test_can_create_project_from_dataset(self, fxt_coco_dataset): "COCO 1.0", ) - project_id = int(stdout.split()[-1]) + project_id = int(stdout.rstrip("\n")) created_project = self.client.projects.retrieve(project_id) assert created_project.name == "new_project" assert {label.name for label in created_project.get_labels()} == {"car", "person"} diff --git a/tests/python/cli/test_cli_tasks.py b/tests/python/cli/test_cli_tasks.py index 8e01c2828b88..d0af410a7c99 100644 --- a/tests/python/cli/test_cli_tasks.py +++ b/tests/python/cli/test_cli_tasks.py @@ -74,7 +74,7 @@ def test_can_create_task_from_local_images(self): "0.01", ) - task_id = int(stdout.split()[-1]) + task_id = int(stdout.rstrip("\n")) assert self.client.tasks.retrieve(task_id).size == 5 def test_can_create_task_from_local_images_with_parameters(self): @@ -102,7 +102,7 @@ def test_can_create_task_from_local_images_with_parameters(self): "http://localhost/bug", ) - task_id = int(stdout.split()[-1]) + task_id = int(stdout.rstrip("\n")) task = self.client.tasks.retrieve(task_id) frames = task.get_frames_info() assert [f.name for f in frames] == [ @@ -191,7 +191,7 @@ def test_can_upload_annotations(self, fxt_new_task: Task, fxt_coco_file: Path): def test_can_create_from_backup(self, fxt_new_task: Task, fxt_backup_file: Path): stdout = self.run_cli("task", "create-from-backup", str(fxt_backup_file)) - task_id = int(stdout.split()[-1]) + task_id = int(stdout.rstrip("\n")) assert task_id assert task_id != fxt_new_task.id assert self.client.tasks.retrieve(task_id).size == fxt_new_task.size