annofabcli 1.99.0__py3-none-any.whl → 100.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- annofabcli/__version__.py +1 -1
- annofabcli/annotation/change_annotation_attributes.py +2 -2
- annofabcli/annotation_specs/add_attribute_restriction.py +158 -0
- annofabcli/annotation_specs/attribute_restriction.py +240 -0
- annofabcli/annotation_specs/list_annotation_specs_attribute.py +51 -33
- annofabcli/annotation_specs/list_annotation_specs_choice.py +42 -17
- annofabcli/annotation_specs/list_annotation_specs_label.py +26 -93
- annofabcli/annotation_specs/list_annotation_specs_label_attribute.py +220 -0
- annofabcli/annotation_specs/list_attribute_restriction.py +8 -209
- annofabcli/annotation_specs/subcommand_annotation_specs.py +4 -0
- annofabcli/common/annofab/annotation_specs.py +25 -0
- annofabcli/common/download.py +1 -1
- annofabcli/common/exceptions.py +2 -2
- annofabcli/common/facade.py +2 -2
- annofabcli/common/image.py +2 -2
- annofabcli/input_data/delete_input_data.py +6 -7
- annofabcli/job/delete_job.py +4 -4
- annofabcli/organization_member/list_organization_member.py +2 -3
- annofabcli/project_member/change_project_members.py +2 -3
- annofabcli/project_member/drop_project_members.py +8 -8
- annofabcli/project_member/invite_project_members.py +5 -6
- annofabcli/project_member/list_users.py +4 -4
- annofabcli/task/complete_tasks.py +2 -2
- annofabcli/task/reject_tasks.py +76 -100
- annofabcli/task/update_metadata_of_task.py +1 -1
- annofabcli/task_history/list_task_history.py +1 -1
- {annofabcli-1.99.0.dist-info → annofabcli-100.2.dist-info}/METADATA +1 -1
- {annofabcli-1.99.0.dist-info → annofabcli-100.2.dist-info}/RECORD +31 -27
- {annofabcli-1.99.0.dist-info → annofabcli-100.2.dist-info}/LICENSE +0 -0
- {annofabcli-1.99.0.dist-info → annofabcli-100.2.dist-info}/WHEEL +0 -0
- {annofabcli-1.99.0.dist-info → annofabcli-100.2.dist-info}/entry_points.txt +0 -0
|
@@ -15,6 +15,7 @@ from dataclasses_json import DataClassJsonMixin
|
|
|
15
15
|
|
|
16
16
|
import annofabcli
|
|
17
17
|
import annofabcli.common.cli
|
|
18
|
+
from annofabcli.common.annofab.annotation_specs import keybind_to_text
|
|
18
19
|
from annofabcli.common.cli import (
|
|
19
20
|
COMMAND_LINE_ERROR_STATUS_CODE,
|
|
20
21
|
ArgumentParser,
|
|
@@ -23,13 +24,13 @@ from annofabcli.common.cli import (
|
|
|
23
24
|
)
|
|
24
25
|
from annofabcli.common.enums import FormatArgument
|
|
25
26
|
from annofabcli.common.facade import AnnofabApiFacade
|
|
26
|
-
from annofabcli.common.utils import print_csv
|
|
27
|
+
from annofabcli.common.utils import print_according_to_format, print_csv
|
|
27
28
|
|
|
28
29
|
logger = logging.getLogger(__name__)
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
@dataclass
|
|
32
|
-
class
|
|
33
|
+
class FlattenChoice(DataClassJsonMixin):
|
|
33
34
|
"""
|
|
34
35
|
CSV用の選択肢情報を格納するクラスです。
|
|
35
36
|
"""
|
|
@@ -54,11 +55,13 @@ class ChoiceForCsv(DataClassJsonMixin):
|
|
|
54
55
|
choice_name_vi: Optional[str]
|
|
55
56
|
is_default: bool
|
|
56
57
|
"""初期値として設定されているかどうか"""
|
|
58
|
+
keybind: Optional[str]
|
|
59
|
+
"""キーバインド"""
|
|
57
60
|
|
|
58
61
|
|
|
59
|
-
def
|
|
62
|
+
def create_flatten_choice_list_from_additionals(additionals_v3: list[dict[str, Any]]) -> list[FlattenChoice]:
|
|
60
63
|
"""
|
|
61
|
-
APIから取得した属性情報(v3
|
|
64
|
+
APIから取得した属性情報(v3版)から、選択肢情報の一覧を生成します。
|
|
62
65
|
|
|
63
66
|
Args:
|
|
64
67
|
additionals_v3: APIから取得した属性情報(v3版)
|
|
@@ -67,7 +70,7 @@ def create_df_from_additionals(additionals_v3: list[dict[str, Any]]) -> pandas.D
|
|
|
67
70
|
def dict_choice_to_dataclass(
|
|
68
71
|
choice: dict[str, Any],
|
|
69
72
|
additional: dict[str, Any],
|
|
70
|
-
) ->
|
|
73
|
+
) -> FlattenChoice:
|
|
71
74
|
"""
|
|
72
75
|
辞書の選択肢情報をDataClassの選択肢情報に変換します。
|
|
73
76
|
"""
|
|
@@ -77,7 +80,7 @@ def create_df_from_additionals(additionals_v3: list[dict[str, Any]]) -> pandas.D
|
|
|
77
80
|
choice_id = choice["choice_id"]
|
|
78
81
|
choice_name = choice["name"]
|
|
79
82
|
is_default = additional["default"] == choice_id
|
|
80
|
-
return
|
|
83
|
+
return FlattenChoice(
|
|
81
84
|
attribute_id=attribute_id,
|
|
82
85
|
attribute_name_en=get_english_message(additional_name),
|
|
83
86
|
attribute_type=additional["type"],
|
|
@@ -86,6 +89,7 @@ def create_df_from_additionals(additionals_v3: list[dict[str, Any]]) -> pandas.D
|
|
|
86
89
|
choice_name_ja=get_message_with_lang(choice_name, lang=Lang.JA_JP),
|
|
87
90
|
choice_name_vi=get_message_with_lang(choice_name, lang=Lang.VI_VN),
|
|
88
91
|
is_default=is_default,
|
|
92
|
+
keybind=keybind_to_text(choice["keybind"]),
|
|
89
93
|
)
|
|
90
94
|
|
|
91
95
|
tmp_list = []
|
|
@@ -95,19 +99,33 @@ def create_df_from_additionals(additionals_v3: list[dict[str, Any]]) -> pandas.D
|
|
|
95
99
|
continue
|
|
96
100
|
for choice in choices:
|
|
97
101
|
tmp_list.append(dict_choice_to_dataclass(choice, additional)) # noqa: PERF401
|
|
98
|
-
|
|
99
|
-
columns = ["attribute_id", "attribute_name_en", "attribute_type", "choice_id", "choice_name_en", "choice_name_ja", "choice_name_vi", "is_default"]
|
|
100
|
-
df = pandas.DataFrame(tmp_list, columns=columns)
|
|
101
|
-
return df
|
|
102
|
+
return tmp_list
|
|
102
103
|
|
|
103
104
|
|
|
104
105
|
class PrintAnnotationSpecsAttribute(CommandLine):
|
|
105
106
|
COMMON_MESSAGE = "annofabcli annotation_specs list_choice: error:"
|
|
106
107
|
|
|
107
|
-
def print_annotation_specs_choice(self, annotation_specs_v3: dict[str, Any],
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
108
|
+
def print_annotation_specs_choice(self, annotation_specs_v3: dict[str, Any], output_format: FormatArgument, output: Optional[str] = None) -> None:
|
|
109
|
+
choice_list = create_flatten_choice_list_from_additionals(annotation_specs_v3["additionals"])
|
|
110
|
+
logger.info(f"{len(choice_list)} 件の選択肢情報を出力します。")
|
|
111
|
+
|
|
112
|
+
if output_format == FormatArgument.CSV:
|
|
113
|
+
df = pandas.DataFrame(choice_list)
|
|
114
|
+
columns = [
|
|
115
|
+
"attribute_id",
|
|
116
|
+
"attribute_name_en",
|
|
117
|
+
"attribute_type",
|
|
118
|
+
"choice_id",
|
|
119
|
+
"choice_name_en",
|
|
120
|
+
"choice_name_ja",
|
|
121
|
+
"choice_name_vi",
|
|
122
|
+
"is_default",
|
|
123
|
+
"keybind",
|
|
124
|
+
]
|
|
125
|
+
print_csv(df[columns], output)
|
|
126
|
+
|
|
127
|
+
elif output_format in [FormatArgument.JSON, FormatArgument.PRETTY_JSON]:
|
|
128
|
+
print_according_to_format([e.to_dict() for e in choice_list], format=output_format, output=output)
|
|
111
129
|
|
|
112
130
|
def get_history_id_from_before_index(self, project_id: str, before: int) -> Optional[str]:
|
|
113
131
|
histories, _ = self.service.api.get_annotation_specs_histories(project_id)
|
|
@@ -144,7 +162,7 @@ class PrintAnnotationSpecsAttribute(CommandLine):
|
|
|
144
162
|
else:
|
|
145
163
|
raise RuntimeError("'--project_id'か'--annotation_specs_json'のどちらかを指定する必要があります。")
|
|
146
164
|
|
|
147
|
-
self.print_annotation_specs_choice(annotation_specs,
|
|
165
|
+
self.print_annotation_specs_choice(annotation_specs, output_format=FormatArgument(args.format), output=args.output)
|
|
148
166
|
|
|
149
167
|
|
|
150
168
|
def parse_args(parser: argparse.ArgumentParser) -> None:
|
|
@@ -183,7 +201,14 @@ def parse_args(parser: argparse.ArgumentParser) -> None:
|
|
|
183
201
|
),
|
|
184
202
|
)
|
|
185
203
|
|
|
186
|
-
parser.add_argument(
|
|
204
|
+
parser.add_argument(
|
|
205
|
+
"-f",
|
|
206
|
+
"--format",
|
|
207
|
+
type=str,
|
|
208
|
+
choices=[FormatArgument.CSV.value, FormatArgument.JSON.value, FormatArgument.PRETTY_JSON.value],
|
|
209
|
+
default=FormatArgument.CSV.value,
|
|
210
|
+
help="出力フォーマット ",
|
|
211
|
+
)
|
|
187
212
|
|
|
188
213
|
argument_parser.add_output()
|
|
189
214
|
|
|
@@ -199,7 +224,7 @@ def main(args: argparse.Namespace) -> None:
|
|
|
199
224
|
def add_parser(subparsers: Optional[argparse._SubParsersAction] = None) -> argparse.ArgumentParser:
|
|
200
225
|
subcommand_name = "list_choice"
|
|
201
226
|
|
|
202
|
-
subcommand_help = "
|
|
227
|
+
subcommand_help = "アノテーション仕様のドロップダウンまたはラジオボタン属性の選択肢情報を出力します。"
|
|
203
228
|
|
|
204
229
|
parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help)
|
|
205
230
|
parse_args(parser)
|
|
@@ -15,6 +15,7 @@ from dataclasses_json import DataClassJsonMixin
|
|
|
15
15
|
|
|
16
16
|
import annofabcli
|
|
17
17
|
import annofabcli.common.cli
|
|
18
|
+
from annofabcli.common.annofab.annotation_specs import keybind_to_text
|
|
18
19
|
from annofabcli.common.cli import (
|
|
19
20
|
COMMAND_LINE_ERROR_STATUS_CODE,
|
|
20
21
|
ArgumentParser,
|
|
@@ -22,16 +23,16 @@ from annofabcli.common.cli import (
|
|
|
22
23
|
build_annofabapi_resource_and_login,
|
|
23
24
|
)
|
|
24
25
|
from annofabcli.common.enums import FormatArgument
|
|
25
|
-
from annofabcli.common.facade import AnnofabApiFacade
|
|
26
|
+
from annofabcli.common.facade import AnnofabApiFacade
|
|
26
27
|
from annofabcli.common.utils import print_csv
|
|
27
28
|
|
|
28
29
|
logger = logging.getLogger(__name__)
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
@dataclass
|
|
32
|
-
class
|
|
33
|
+
class FlattenLabel(DataClassJsonMixin):
|
|
33
34
|
"""
|
|
34
|
-
|
|
35
|
+
ラベル情報を格納するクラスです。
|
|
35
36
|
"""
|
|
36
37
|
|
|
37
38
|
label_id: str
|
|
@@ -50,6 +51,8 @@ class LabelForCsv(DataClassJsonMixin):
|
|
|
50
51
|
Notes:
|
|
51
52
|
APIでは`additional_data_definitions`のような名前だが、分かりにくかったので"attribute"という名前に変えた。
|
|
52
53
|
"""
|
|
54
|
+
keybind: Optional[str]
|
|
55
|
+
"""キーバインド"""
|
|
53
56
|
|
|
54
57
|
|
|
55
58
|
def decimal_to_hex_color(red: int, green: int, blue: int) -> str:
|
|
@@ -63,22 +66,22 @@ def decimal_to_hex_color(red: int, green: int, blue: int) -> str:
|
|
|
63
66
|
return f"#{red:02X}{green:02X}{blue:02X}"
|
|
64
67
|
|
|
65
68
|
|
|
66
|
-
def
|
|
69
|
+
def create_label_list(labels_v3: list[dict[str, Any]]) -> list[FlattenLabel]:
|
|
67
70
|
"""
|
|
68
|
-
APIから取得したラベル情報(v3
|
|
71
|
+
APIから取得したラベル情報(v3版)から、`FlattenLabel`のlistを生成します。
|
|
69
72
|
|
|
70
73
|
Args:
|
|
71
74
|
labels_v3: APIから取得したラベル情報(v3版)
|
|
72
75
|
"""
|
|
73
76
|
|
|
74
|
-
def dict_label_to_dataclass(label: dict[str, Any]) ->
|
|
77
|
+
def dict_label_to_dataclass(label: dict[str, Any]) -> FlattenLabel:
|
|
75
78
|
"""
|
|
76
79
|
辞書のラベル情報をDataClassのラベル情報に変換します。
|
|
77
80
|
"""
|
|
78
81
|
label_color = label["color"]
|
|
79
82
|
hex_color_code = decimal_to_hex_color(label_color["red"], label_color["green"], label_color["blue"])
|
|
80
83
|
additional_data_definitions = label["additional_data_definitions"]
|
|
81
|
-
return
|
|
84
|
+
return FlattenLabel(
|
|
82
85
|
label_id=label["label_id"],
|
|
83
86
|
label_name_en=get_message_with_lang(label["label_name"], lang=Lang.EN_US),
|
|
84
87
|
label_name_ja=get_message_with_lang(label["label_name"], lang=Lang.JA_JP),
|
|
@@ -86,21 +89,10 @@ def create_df_from_labels(labels_v3: list[dict[str, Any]]) -> pandas.DataFrame:
|
|
|
86
89
|
annotation_type=label["annotation_type"],
|
|
87
90
|
color=hex_color_code,
|
|
88
91
|
attribute_count=len(additional_data_definitions),
|
|
92
|
+
keybind=keybind_to_text(label["keybind"]),
|
|
89
93
|
)
|
|
90
94
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
columns = [
|
|
94
|
-
"label_id",
|
|
95
|
-
"label_name_en",
|
|
96
|
-
"label_name_ja",
|
|
97
|
-
"label_name_vi",
|
|
98
|
-
"annotation_type",
|
|
99
|
-
"color",
|
|
100
|
-
"attribute_count",
|
|
101
|
-
]
|
|
102
|
-
df = pandas.DataFrame(new_labels, columns=columns)
|
|
103
|
-
return df
|
|
95
|
+
return [dict_label_to_dataclass(e) for e in labels_v3]
|
|
104
96
|
|
|
105
97
|
|
|
106
98
|
class PrintAnnotationSpecsLabel(CommandLine):
|
|
@@ -110,69 +102,16 @@ class PrintAnnotationSpecsLabel(CommandLine):
|
|
|
110
102
|
|
|
111
103
|
COMMON_MESSAGE = "annofabcli annotation_specs list_label: error:"
|
|
112
104
|
|
|
113
|
-
def print_annotation_specs_label(self, annotation_specs_v3: dict[str, Any],
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
)
|
|
118
|
-
if arg_format == "text":
|
|
119
|
-
self._print_text_format_labels(labels_v1, output=output)
|
|
105
|
+
def print_annotation_specs_label(self, annotation_specs_v3: dict[str, Any], output_format: FormatArgument, output: Optional[str] = None) -> None:
|
|
106
|
+
label_list = create_label_list(annotation_specs_v3["labels"])
|
|
107
|
+
if output_format == FormatArgument.CSV:
|
|
108
|
+
df = pandas.DataFrame(label_list)
|
|
120
109
|
|
|
121
|
-
|
|
122
|
-
df
|
|
123
|
-
print_csv(df, output)
|
|
110
|
+
columns = ["label_id", "label_name_en", "label_name_ja", "label_name_vi", "annotation_type", "color", "attribute_count", "keybind"]
|
|
111
|
+
print_csv(df[columns], output)
|
|
124
112
|
|
|
125
|
-
elif
|
|
126
|
-
annofabcli.common.utils.print_according_to_format(
|
|
127
|
-
|
|
128
|
-
@staticmethod
|
|
129
|
-
def _get_name_tuple(messages: list[dict[str, Any]]) -> tuple[str, str]:
|
|
130
|
-
"""
|
|
131
|
-
日本語名、英語名のタプルを取得する。
|
|
132
|
-
"""
|
|
133
|
-
ja_name = [e["message"] for e in messages if e["lang"] == "ja-JP"][0] # noqa: RUF015
|
|
134
|
-
en_name = [e["message"] for e in messages if e["lang"] == "en-US"][0] # noqa: RUF015
|
|
135
|
-
return (ja_name, en_name)
|
|
136
|
-
|
|
137
|
-
@staticmethod
|
|
138
|
-
def _print_text_format_labels(labels: list[dict[str, Any]], output: Optional[str] = None) -> None:
|
|
139
|
-
output_lines = []
|
|
140
|
-
for label in labels:
|
|
141
|
-
output_lines.append(
|
|
142
|
-
"\t".join(
|
|
143
|
-
[
|
|
144
|
-
label["label_id"],
|
|
145
|
-
label["annotation_type"],
|
|
146
|
-
*PrintAnnotationSpecsLabel._get_name_tuple(label["label_name"]["messages"]),
|
|
147
|
-
]
|
|
148
|
-
)
|
|
149
|
-
)
|
|
150
|
-
for additional_data_definition in label["additional_data_definitions"]:
|
|
151
|
-
output_lines.append(
|
|
152
|
-
"\t".join(
|
|
153
|
-
[
|
|
154
|
-
"",
|
|
155
|
-
additional_data_definition["additional_data_definition_id"],
|
|
156
|
-
additional_data_definition["type"],
|
|
157
|
-
*PrintAnnotationSpecsLabel._get_name_tuple(additional_data_definition["name"]["messages"]),
|
|
158
|
-
]
|
|
159
|
-
)
|
|
160
|
-
)
|
|
161
|
-
if additional_data_definition["type"] in ["choice", "select"]:
|
|
162
|
-
for choice in additional_data_definition["choices"]:
|
|
163
|
-
output_lines.append( # noqa: PERF401
|
|
164
|
-
"\t".join(
|
|
165
|
-
[
|
|
166
|
-
"",
|
|
167
|
-
"",
|
|
168
|
-
choice["choice_id"],
|
|
169
|
-
"",
|
|
170
|
-
*PrintAnnotationSpecsLabel._get_name_tuple(choice["name"]["messages"]),
|
|
171
|
-
]
|
|
172
|
-
)
|
|
173
|
-
)
|
|
174
|
-
|
|
175
|
-
annofabcli.common.utils.output_string("\n".join(output_lines), output)
|
|
113
|
+
elif output_format in [FormatArgument.JSON, FormatArgument.PRETTY_JSON]:
|
|
114
|
+
annofabcli.common.utils.print_according_to_format([e.to_dict() for e in label_list], format=FormatArgument(output_format), output=output)
|
|
176
115
|
|
|
177
116
|
def get_history_id_from_before_index(self, project_id: str, before: int) -> Optional[str]:
|
|
178
117
|
histories, _ = self.service.api.get_annotation_specs_histories(project_id)
|
|
@@ -210,7 +149,7 @@ class PrintAnnotationSpecsLabel(CommandLine):
|
|
|
210
149
|
else:
|
|
211
150
|
raise RuntimeError("'--project_id'か'--annotation_specs_json'のどちらかを指定する必要があります。")
|
|
212
151
|
|
|
213
|
-
self.print_annotation_specs_label(annotation_specs,
|
|
152
|
+
self.print_annotation_specs_label(annotation_specs, output_format=FormatArgument(args.format), output=args.output)
|
|
214
153
|
|
|
215
154
|
|
|
216
155
|
def parse_args(parser: argparse.ArgumentParser) -> None:
|
|
@@ -253,13 +192,9 @@ def parse_args(parser: argparse.ArgumentParser) -> None:
|
|
|
253
192
|
"-f",
|
|
254
193
|
"--format",
|
|
255
194
|
type=str,
|
|
256
|
-
choices=[
|
|
257
|
-
default=
|
|
258
|
-
help=
|
|
259
|
-
"text: 人が見やすい形式, "
|
|
260
|
-
f"{FormatArgument.CSV.value}: ラベル情報の一覧が記載されたCSV, "
|
|
261
|
-
f"{FormatArgument.PRETTY_JSON.value}: インデントされたJSON, "
|
|
262
|
-
f"{FormatArgument.JSON.value}: フラットなJSON",
|
|
195
|
+
choices=[FormatArgument.CSV.value, FormatArgument.JSON.value, FormatArgument.PRETTY_JSON.value],
|
|
196
|
+
default=FormatArgument.CSV.value,
|
|
197
|
+
help="出力フォーマット ",
|
|
263
198
|
)
|
|
264
199
|
|
|
265
200
|
argument_parser.add_output()
|
|
@@ -276,10 +211,8 @@ def main(args: argparse.Namespace) -> None:
|
|
|
276
211
|
def add_parser(subparsers: Optional[argparse._SubParsersAction] = None) -> argparse.ArgumentParser:
|
|
277
212
|
subcommand_name = "list_label"
|
|
278
213
|
|
|
279
|
-
subcommand_help = "
|
|
280
|
-
|
|
281
|
-
description = "アノテーション仕様のラベル情報を出力する"
|
|
214
|
+
subcommand_help = "アノテーション仕様のラベル情報を出力します。"
|
|
282
215
|
|
|
283
|
-
parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help
|
|
216
|
+
parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help)
|
|
284
217
|
parse_args(parser)
|
|
285
218
|
return parser
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import json
|
|
5
|
+
import logging
|
|
6
|
+
import sys
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Any, Optional
|
|
10
|
+
|
|
11
|
+
import pandas
|
|
12
|
+
from annofabapi.models import Lang
|
|
13
|
+
from annofabapi.util.annotation_specs import get_message_with_lang
|
|
14
|
+
from dataclasses_json import DataClassJsonMixin
|
|
15
|
+
|
|
16
|
+
import annofabcli
|
|
17
|
+
import annofabcli.common.cli
|
|
18
|
+
from annofabcli.common.cli import (
|
|
19
|
+
COMMAND_LINE_ERROR_STATUS_CODE,
|
|
20
|
+
ArgumentParser,
|
|
21
|
+
CommandLine,
|
|
22
|
+
build_annofabapi_resource_and_login,
|
|
23
|
+
)
|
|
24
|
+
from annofabcli.common.enums import FormatArgument
|
|
25
|
+
from annofabcli.common.facade import AnnofabApiFacade
|
|
26
|
+
from annofabcli.common.utils import print_according_to_format, print_csv
|
|
27
|
+
|
|
28
|
+
logger = logging.getLogger(__name__)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class LabelAndAttribute(DataClassJsonMixin):
|
|
33
|
+
label_id: str
|
|
34
|
+
label_name_en: Optional[str]
|
|
35
|
+
label_name_ja: Optional[str]
|
|
36
|
+
label_name_vi: Optional[str]
|
|
37
|
+
annotation_type: str
|
|
38
|
+
|
|
39
|
+
attribute_id: str
|
|
40
|
+
"""属性ID
|
|
41
|
+
|
|
42
|
+
Notes:
|
|
43
|
+
APIレスポンスの ``additional_data_definition_id`` に相当します。
|
|
44
|
+
``additional_data_definition_id`` という名前がアノテーションJSONの `attributes` と対応していることが分かりにくかったので、`attribute_id`という名前に変えました。
|
|
45
|
+
""" # noqa: E501
|
|
46
|
+
attribute_name_en: Optional[str]
|
|
47
|
+
attribute_name_ja: Optional[str]
|
|
48
|
+
attribute_name_vi: Optional[str]
|
|
49
|
+
attribute_type: str
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def create_label_attribute_list(labels_v3: list[dict[str, Any]], additionals_v3: list[dict[str, Any]]) -> list[LabelAndAttribute]:
|
|
53
|
+
"""
|
|
54
|
+
APIから取得したラベル情報(v3版)から、`LabelAndAttribute`のlistを生成します。
|
|
55
|
+
|
|
56
|
+
Args:
|
|
57
|
+
labels_v3: APIから取得したラベル情報(v3版)
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
def to_dataclass_list(label: dict[str, Any]) -> list[LabelAndAttribute]:
|
|
61
|
+
result = []
|
|
62
|
+
for attribute_id in label["additional_data_definitions"]:
|
|
63
|
+
attribute = dict_attributes[attribute_id]
|
|
64
|
+
|
|
65
|
+
result.append(
|
|
66
|
+
LabelAndAttribute(
|
|
67
|
+
label_id=label["label_id"],
|
|
68
|
+
label_name_en=get_message_with_lang(label["label_name"], lang=Lang.EN_US),
|
|
69
|
+
label_name_ja=get_message_with_lang(label["label_name"], lang=Lang.JA_JP),
|
|
70
|
+
label_name_vi=get_message_with_lang(label["label_name"], lang=Lang.VI_VN),
|
|
71
|
+
annotation_type=label["annotation_type"],
|
|
72
|
+
attribute_id=attribute_id,
|
|
73
|
+
attribute_name_en=get_message_with_lang(attribute["name"], lang=Lang.EN_US),
|
|
74
|
+
attribute_name_ja=get_message_with_lang(attribute["name"], lang=Lang.JA_JP),
|
|
75
|
+
attribute_name_vi=get_message_with_lang(attribute["name"], lang=Lang.VI_VN),
|
|
76
|
+
attribute_type=attribute["type"],
|
|
77
|
+
)
|
|
78
|
+
)
|
|
79
|
+
return result
|
|
80
|
+
|
|
81
|
+
dict_attributes = {}
|
|
82
|
+
for elm in additionals_v3:
|
|
83
|
+
dict_attributes[elm["additional_data_definition_id"]] = elm
|
|
84
|
+
|
|
85
|
+
result = []
|
|
86
|
+
for label in labels_v3:
|
|
87
|
+
result.extend(to_dataclass_list(label))
|
|
88
|
+
return result
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class PrintAnnotationSpecsLabelAndAttribute(CommandLine):
|
|
92
|
+
COMMON_MESSAGE = "annofabcli annotation_specs list_label: error:"
|
|
93
|
+
|
|
94
|
+
def print_annotation_specs_label(self, annotation_specs_v3: dict[str, Any], output_format: FormatArgument, output: Optional[str] = None) -> None:
|
|
95
|
+
# アノテーション仕様のv2とv3はほとんど同じなので、`convert_annotation_specs_labels_v2_to_v1`にはV3のアノテーション仕様を渡す
|
|
96
|
+
label_attribute_list = create_label_attribute_list(annotation_specs_v3["labels"], annotation_specs_v3["additionals"])
|
|
97
|
+
|
|
98
|
+
if output_format == FormatArgument.CSV:
|
|
99
|
+
columns = [
|
|
100
|
+
"label_id",
|
|
101
|
+
"label_name_en",
|
|
102
|
+
"label_name_ja",
|
|
103
|
+
"label_name_vi",
|
|
104
|
+
"annotation_type",
|
|
105
|
+
"attribute_id",
|
|
106
|
+
"attribute_name_en",
|
|
107
|
+
"attribute_name_ja",
|
|
108
|
+
"attribute_name_vi",
|
|
109
|
+
"attribute_type",
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
df = pandas.DataFrame(label_attribute_list, columns=columns)
|
|
113
|
+
print_csv(df, output)
|
|
114
|
+
|
|
115
|
+
elif output_format in [FormatArgument.JSON, FormatArgument.PRETTY_JSON]:
|
|
116
|
+
print_according_to_format([e.to_dict() for e in label_attribute_list], format=output_format, output=output)
|
|
117
|
+
|
|
118
|
+
def get_history_id_from_before_index(self, project_id: str, before: int) -> Optional[str]:
|
|
119
|
+
histories, _ = self.service.api.get_annotation_specs_histories(project_id)
|
|
120
|
+
if before + 1 > len(histories):
|
|
121
|
+
logger.warning(f"アノテーション仕様の履歴は{len(histories)}個のため、最新より{before}個前のアノテーション仕様は見つかりませんでした。")
|
|
122
|
+
return None
|
|
123
|
+
history = histories[-(before + 1)]
|
|
124
|
+
logger.info(
|
|
125
|
+
f"{history['updated_datetime']}のアノテーション仕様を出力します。history_id={history['history_id']}, comment={history['comment']}"
|
|
126
|
+
)
|
|
127
|
+
return history["history_id"]
|
|
128
|
+
|
|
129
|
+
def main(self) -> None:
|
|
130
|
+
args = self.args
|
|
131
|
+
|
|
132
|
+
if args.project_id is not None:
|
|
133
|
+
if args.before is not None:
|
|
134
|
+
history_id = self.get_history_id_from_before_index(args.project_id, args.before)
|
|
135
|
+
if history_id is None:
|
|
136
|
+
print( # noqa: T201
|
|
137
|
+
f"{self.COMMON_MESSAGE} argument --before: 最新より{args.before}個前のアノテーション仕様は見つかりませんでした。",
|
|
138
|
+
file=sys.stderr,
|
|
139
|
+
)
|
|
140
|
+
sys.exit(COMMAND_LINE_ERROR_STATUS_CODE)
|
|
141
|
+
else:
|
|
142
|
+
# args.beforeがNoneならば、必ずargs.history_idはNoneでない
|
|
143
|
+
history_id = args.history_id
|
|
144
|
+
|
|
145
|
+
annotation_specs, _ = self.service.api.get_annotation_specs(args.project_id, query_params={"history_id": history_id, "v": "3"})
|
|
146
|
+
|
|
147
|
+
elif args.annotation_specs_json is not None:
|
|
148
|
+
with args.annotation_specs_json.open() as f:
|
|
149
|
+
annotation_specs = json.load(f)
|
|
150
|
+
|
|
151
|
+
else:
|
|
152
|
+
raise RuntimeError("'--project_id'か'--annotation_specs_json'のどちらかを指定する必要があります。")
|
|
153
|
+
|
|
154
|
+
self.print_annotation_specs_label(annotation_specs, output_format=FormatArgument(args.format), output=args.output)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def parse_args(parser: argparse.ArgumentParser) -> None:
|
|
158
|
+
argument_parser = ArgumentParser(parser)
|
|
159
|
+
|
|
160
|
+
required_group = parser.add_mutually_exclusive_group(required=True)
|
|
161
|
+
required_group.add_argument(
|
|
162
|
+
"-p", "--project_id", help="対象のプロジェクトのproject_idを指定します。APIで取得したアノテーション仕様情報を元に出力します。"
|
|
163
|
+
)
|
|
164
|
+
required_group.add_argument(
|
|
165
|
+
"--annotation_specs_json",
|
|
166
|
+
type=Path,
|
|
167
|
+
help="指定したアノテーション仕様のJSONファイルを指定します。"
|
|
168
|
+
"JSONファイルに記載された情報を元に出力します。ただしアノテーション仕様の ``format_version`` は ``3`` である必要があります。",
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
# 過去のアノテーション仕様を参照するためのオプション
|
|
172
|
+
old_annotation_specs_group = parser.add_mutually_exclusive_group()
|
|
173
|
+
old_annotation_specs_group.add_argument(
|
|
174
|
+
"--history_id",
|
|
175
|
+
type=str,
|
|
176
|
+
help=(
|
|
177
|
+
"出力したいアノテーション仕様のhistory_idを指定してください。 "
|
|
178
|
+
"history_idは ``annotation_specs list_history`` コマンドで確認できます。 "
|
|
179
|
+
"指定しない場合は、最新のアノテーション仕様が出力されます。 "
|
|
180
|
+
),
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
old_annotation_specs_group.add_argument(
|
|
184
|
+
"--before",
|
|
185
|
+
type=int,
|
|
186
|
+
help=(
|
|
187
|
+
"出力したい過去のアノテーション仕様が、最新よりいくつ前のアノテーション仕様であるかを指定してください。 "
|
|
188
|
+
"たとえば ``1`` を指定した場合、最新より1個前のアノテーション仕様を出力します。 "
|
|
189
|
+
"指定しない場合は、最新のアノテーション仕様が出力されます。 "
|
|
190
|
+
),
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
parser.add_argument(
|
|
194
|
+
"-f",
|
|
195
|
+
"--format",
|
|
196
|
+
type=str,
|
|
197
|
+
choices=[FormatArgument.CSV.value, FormatArgument.JSON.value, FormatArgument.PRETTY_JSON.value],
|
|
198
|
+
default=FormatArgument.CSV.value,
|
|
199
|
+
help="出力フォーマット ",
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
argument_parser.add_output()
|
|
203
|
+
|
|
204
|
+
parser.set_defaults(subcommand_func=main)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def main(args: argparse.Namespace) -> None:
|
|
208
|
+
service = build_annofabapi_resource_and_login(args)
|
|
209
|
+
facade = AnnofabApiFacade(service)
|
|
210
|
+
PrintAnnotationSpecsLabelAndAttribute(service, facade, args).main()
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def add_parser(subparsers: Optional[argparse._SubParsersAction] = None) -> argparse.ArgumentParser:
|
|
214
|
+
subcommand_name = "list_label_attribute"
|
|
215
|
+
|
|
216
|
+
subcommand_help = "アノテーション仕様のラベルとラベルに含まれている属性の一覧を出力します。"
|
|
217
|
+
|
|
218
|
+
parser = annofabcli.common.cli.add_parser(subparsers, subcommand_name, subcommand_help)
|
|
219
|
+
parse_args(parser)
|
|
220
|
+
return parser
|