-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
annotation_specs export
コマンドの作成 (#1178)
* print_according_to_format関数の引数arg_formatをformatに変更 * コマンド追加 * add document * add test * pylintrc
- Loading branch information
1 parent
addf12b
commit 1c9fe17
Showing
11 changed files
with
202 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from __future__ import annotations | ||
|
||
import argparse | ||
import logging | ||
import sys | ||
from typing import Optional | ||
|
||
import annofabcli | ||
import annofabcli.common.cli | ||
from annofabcli.common.cli import ( | ||
COMMAND_LINE_ERROR_STATUS_CODE, | ||
AbstractCommandLineInterface, | ||
ArgumentParser, | ||
build_annofabapi_resource_and_login, | ||
) | ||
from annofabcli.common.enums import FormatArgument | ||
from annofabcli.common.facade import AnnofabApiFacade | ||
from annofabcli.common.utils import print_according_to_format | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class ListAttributeRestriction(AbstractCommandLineInterface): | ||
COMMON_MESSAGE = "annofabcli annotation_specs export: error:" | ||
|
||
def get_history_id_from_before_index(self, project_id: str, before: int) -> Optional[str]: | ||
histories, _ = self.service.api.get_annotation_specs_histories(project_id) | ||
if before + 1 > len(histories): | ||
logger.warning(f"アノテーション仕様の履歴は{len(histories)}個のため、最新より{before}個前のアノテーション仕様は見つかりませんでした。") | ||
return None | ||
history = histories[-(before + 1)] | ||
return history["history_id"] | ||
|
||
def main(self) -> None: | ||
args = self.args | ||
|
||
history_id = None | ||
if args.history_id is not None: | ||
history_id = args.history_id | ||
|
||
if args.before is not None: | ||
history_id = self.get_history_id_from_before_index(args.project_id, args.before) | ||
if history_id is None: | ||
print( | ||
f"{self.COMMON_MESSAGE} argument --before: 最新より{args.before}個前のアノテーション仕様は見つかりませんでした。", | ||
file=sys.stderr, | ||
) | ||
sys.exit(COMMAND_LINE_ERROR_STATUS_CODE) | ||
|
||
query_params = {"v": "3"} | ||
if history_id is not None: | ||
query_params["history_id"] = history_id | ||
|
||
annotation_specs, _ = self.service.api.get_annotation_specs(args.project_id, query_params=query_params) | ||
|
||
print_according_to_format(annotation_specs, format=FormatArgument(args.format), output=args.output) | ||
|
||
|
||
def parse_args(parser: argparse.ArgumentParser) -> None: | ||
argument_parser = ArgumentParser(parser) | ||
|
||
argument_parser.add_project_id() | ||
|
||
# 過去のアノテーション仕様を参照するためのオプション | ||
old_annotation_specs_group = parser.add_mutually_exclusive_group() | ||
old_annotation_specs_group.add_argument( | ||
"--history_id", | ||
type=str, | ||
help=( | ||
"出力したいアノテーション仕様のhistory_idを指定してください。 " | ||
"history_idは ``annotation_specs list_history`` コマンドで確認できます。 " | ||
"指定しない場合は、最新のアノテーション仕様が出力されます。 " | ||
), | ||
) | ||
|
||
old_annotation_specs_group.add_argument( | ||
"--before", | ||
type=int, | ||
help=( | ||
"出力したい過去のアノテーション仕様が、最新よりいくつ前のアノテーション仕様であるかを指定してください。 " | ||
"たとえば ``1`` を指定した場合、最新より1個前のアノテーション仕様を出力します。 " | ||
"指定しない場合は、最新のアノテーション仕様が出力されます。 " | ||
), | ||
) | ||
|
||
argument_parser.add_output() | ||
|
||
parser.add_argument( | ||
"-f", | ||
"--format", | ||
type=str, | ||
choices=[FormatArgument.JSON.value, FormatArgument.PRETTY_JSON.value], | ||
default=FormatArgument.JSON.value, | ||
help="出力フォーマット", | ||
) | ||
|
||
parser.set_defaults(subcommand_func=main) | ||
|
||
|
||
def main(args: argparse.Namespace) -> None: | ||
service = build_annofabapi_resource_and_login(args) | ||
facade = AnnofabApiFacade(service) | ||
ListAttributeRestriction(service, facade, args).main() | ||
|
||
|
||
def add_parser(subparsers: Optional[argparse._SubParsersAction] = None) -> argparse.ArgumentParser: | ||
subcommand_name = "export" | ||
|
||
subcommand_help = "アノテーション仕様の情報をエクスポートします。" | ||
|
||
parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help) | ||
parse_args(parser) | ||
return parser |
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
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,57 @@ | ||
==================================================================================== | ||
annotation_specs export | ||
==================================================================================== | ||
|
||
Description | ||
================================= | ||
アノテーション仕様の情報をJSON形式でエクスポートします。 | ||
このJSONは、`アノテーション仕様のインポート機能 <https://annofab.readme.io/docs/annotation-specs#%E3%82%A4%E3%83%B3%E3%83%9D%E3%83%BC%E3%83%88>`_ で利用できます。 | ||
|
||
|
||
Examples | ||
================================= | ||
|
||
基本的な使い方 | ||
-------------------------- | ||
|
||
|
||
.. code-block:: | ||
$ annofabcli annotation_specs export --project_id prj1 --out out.json --format pretty_json | ||
.. code-block:: | ||
:caption: out.json | ||
{ | ||
"labels": [ | ||
"label_id": "763e1659-94c8-4424-9fc8-11b8fbcb115f", | ||
"label_name": {...}, | ||
... | ||
], | ||
"additionals": [...], | ||
... | ||
} | ||
.. warning:: | ||
|
||
``annotation_specs export`` コマンドの出力結果は、アノテーション仕様のインポート機能で利用することを目的として作られています。 | ||
|
||
JSON構造は、将来変更される可能性があります。 | ||
``annotation_specs export`` コマンドの出力結果であるJSONの構造に直接依存したプログラムを作成する場合は、ご注意ください。 | ||
|
||
|
||
|
||
|
||
Usage Details | ||
================================= | ||
|
||
.. argparse:: | ||
:ref: annofabcli.annotation_specs.export_annotation_specs.add_parser | ||
:prog: annofabcli annotation_specs export | ||
:nosubcommands: | ||
:nodefaultconst: | ||
|
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