-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new CLI command: datum format (#1570)
### Summary - This PR resolves https://jira.devtools.intel.com/browse/CVS-144954 and #1545 - Add a new CLI command: `datum format`. It displays a list of data format names supported by Datumaro. It can be useful for quick reference of data format name used for other CLI command such as `datum convert -if <data-format> -f <data-format>`. ### How to test Added unit tests as well. ### Checklist <!-- Put an 'x' in all the boxes that apply --> - [x] I have added unit tests to cover my changes. - [ ] I have added integration tests to cover my changes. - [x] I have added the description of my changes into [CHANGELOG](https://github.com/openvinotoolkit/datumaro/blob/develop/CHANGELOG.md). - [x] I have updated the [documentation](https://github.com/openvinotoolkit/datumaro/tree/develop/docs) accordingly ### License - [x] I submit _my code changes_ under the same [MIT License](https://github.com/openvinotoolkit/datumaro/blob/develop/LICENSE) that covers the project. Feel free to contact the maintainers if that's a concern. - [x] I have updated the license header for each file (see an example below). ```python # Copyright (C) 2024 Intel Corporation # # SPDX-License-Identifier: MIT ``` --------- Signed-off-by: Kim, Vinnam <[email protected]>
- Loading branch information
Showing
10 changed files
with
194 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Format | ||
|
||
## List Supported Data Formats | ||
|
||
This command shows a list of supported import/export data formats in Datumaro. | ||
It is useful on a quick reference of data format name used for other CLI command such as [convert](../context_free/convert.md), [import](../context/sources.md#import-dataset), or [export](../context/export.md#export-datasets). For more detailed guides on each data format, please visit [our Data Formats section](../../data-formats/formats/index.rst). | ||
|
||
Usage: | ||
|
||
```console | ||
usage: datum format [-h] [-li | -le] [-d DELIMITER] | ||
``` | ||
|
||
Parameters: | ||
- `-h, --help` - Print the help message and exit. | ||
- `-d DELIMITER, --delimiter DELIMITER` - Seperator used to list data format names (default: `\n`). For example, `datum format -d ','` command displays | ||
```console | ||
Supported import formats: | ||
ade20k2017,ade20k2020,align_celeba,... | ||
``` | ||
- `-li, --list-import` - List all supported import data format names | ||
- `-le, --list-export` - List all supported export data format names |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
=============== | ||
Helper Commands | ||
=============== | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:glob: | ||
|
||
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from . import format |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
import argparse | ||
import os | ||
|
||
from datumaro.cli.util import MultilineFormatter | ||
|
||
|
||
def build_parser(parser_ctor=argparse.ArgumentParser): | ||
parser = parser_ctor( | ||
help="List supported import/export data formats", | ||
description=""" | ||
List supported import/export data format names. | ||
For more detailed guides on each data format, | ||
please visit our documentation website: | ||
https://openvinotoolkit.github.io/datumaro/stable/docs/data-formats/formats | ||
|n | ||
|n | ||
Examples:|n | ||
- List supported import/export data format names:|n | ||
|s|s%(prog)s|n | ||
|n | ||
- List only supported import data format names:|n | ||
|s|s%(prog)s --list-import|n | ||
|n | ||
- List only supported export data format names:|n | ||
|s|s%(prog)s --list-export|n | ||
|n | ||
- List with comma delimiter:|n | ||
|s|s%(prog)s --delimiter ',' | ||
""", | ||
formatter_class=MultilineFormatter, | ||
) | ||
|
||
group = parser.add_argument_group() | ||
exclusive_group = group.add_mutually_exclusive_group(required=False) | ||
|
||
exclusive_group.add_argument( | ||
"-li", | ||
"--list-import", | ||
action="store_true", | ||
help="List all supported import data format names", | ||
) | ||
exclusive_group.add_argument( | ||
"-le", | ||
"--list-export", | ||
action="store_true", | ||
help="List all supported export data format names", | ||
) | ||
parser.add_argument( | ||
"-d", | ||
"--delimiter", | ||
type=str, | ||
default=os.linesep, | ||
help="Seperator used to list data format names (default: \\n)", | ||
) | ||
|
||
parser.set_defaults(command=format_command) | ||
return parser | ||
|
||
|
||
def get_sensitive_args(): | ||
return { | ||
format_command: ["list", "show"], | ||
} | ||
|
||
|
||
def format_command(args: argparse.Namespace) -> None: | ||
from datumaro.components.environment import DEFAULT_ENVIRONMENT | ||
|
||
delimiter = args.delimiter | ||
|
||
if args.list_import: | ||
builtin_readers = sorted( | ||
set(DEFAULT_ENVIRONMENT.importers) | set(DEFAULT_ENVIRONMENT.extractors) | ||
) | ||
print(delimiter.join(builtin_readers)) | ||
return | ||
|
||
if args.list_export: | ||
builtin_writers = sorted(DEFAULT_ENVIRONMENT.exporters) | ||
print(delimiter.join(builtin_writers)) | ||
return | ||
|
||
builtin_readers = sorted( | ||
set(DEFAULT_ENVIRONMENT.importers) | set(DEFAULT_ENVIRONMENT.extractors) | ||
) | ||
builtin_writers = sorted(DEFAULT_ENVIRONMENT.exporters) | ||
print(f"Supported import formats:\n{delimiter.join(builtin_readers)}") | ||
print(f"Supported export formats:\n{delimiter.join(builtin_writers)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Copyright (C) 2024 Intel Corporation | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from argparse import ArgumentParser, Namespace | ||
|
||
import pytest | ||
|
||
from datumaro.cli.helpers.format import build_parser, format_command | ||
|
||
# from datumaro.cli.__main__ import make_parser | ||
|
||
|
||
class FormatTest: | ||
def test_build_parser(self): | ||
parser = build_parser(lambda help, **kwargs: ArgumentParser(**kwargs)) | ||
assert isinstance(parser, ArgumentParser) | ||
|
||
args = parser.parse_args(["--list-import"]) | ||
assert args.list_import | ||
|
||
args = parser.parse_args(["--list-export"]) | ||
assert args.list_export | ||
|
||
args = parser.parse_args(["--delimiter", ","]) | ||
assert args.delimiter == "," | ||
|
||
@pytest.mark.parametrize( | ||
"list_import,list_export", [(True, False), (False, True), (False, False)] | ||
) | ||
def test_format_command( | ||
self, list_import: bool, list_export: bool, capsys: pytest.CaptureFixture | ||
): | ||
format_command(Namespace(delimiter="\n", list_import=list_import, list_export=list_export)) | ||
out, _ = capsys.readouterr() | ||
assert "coco" in out |