Skip to content

Commit a7a5314

Browse files
committed
examplesをformat
1 parent 7b04de4 commit a7a5314

File tree

5 files changed

+72
-52
lines changed

5 files changed

+72
-52
lines changed

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.1.2'
1+
__version__ = '0.2.0'

examples/cancel_acceptance.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import requests
1010

1111
import annofabapi
12-
13-
from example_utils import read_lines, ExamplesWrapper
12+
from example_utils import ExamplesWrapper, read_lines
1413

1514
logging_formatter = '%(levelname)s : %(asctime)s : %(name)s : %(funcName)s : %(message)s'
1615
logging.basicConfig(format=logging_formatter)
@@ -25,28 +24,33 @@ def get_account_id_from_user_id(project_id: str, user_id: str):
2524
return member['account_id']
2625

2726

28-
def cancel_acceptance(project_id: str, task_id_list: List[str], acceptor_user_id: Optional[str] = None):
27+
def cancel_acceptance(project_id: str,
28+
task_id_list: List[str],
29+
acceptor_user_id: Optional[str] = None):
2930
"""
3031
タスクを受け入れ取り消しする
3132
Args:
3233
project_id:
3334
task_id_list: 受け入れ取り消しするtask_id_list
3435
acceptor_user_id: 再度受入を担当させたいユーザのuser_id
3536
"""
36-
acceptor_account_id = examples_wrapper.get_account_id_from_user_id(project_id, acceptor_user_id)
37+
acceptor_account_id = examples_wrapper.get_account_id_from_user_id(
38+
project_id, acceptor_user_id)
3739

3840
for task_id in task_id_list:
3941
try:
4042
task, _ = service.api.get_task(project_id, task_id)
4143
if task["status"] != "complete":
42-
logger.warning(f"task_id = {task_id} は受入完了でありません。status = {task['status']}, phase={task['phase']}")
44+
logger.warning(
45+
f"task_id = {task_id} は受入完了でありません。status = {task['status']}, phase={task['phase']}"
46+
)
4347
request_body = {
4448
"status": "not_started",
4549
"account_id": acceptor_account_id,
4650
"last_updated_datetime": task["updated_datetime"],
47-
4851
}
49-
operated_task, _ = service.api.operate_task(project_id, task_id, request_body=request_body)
52+
operated_task, _ = service.api.operate_task(
53+
project_id, task_id, request_body=request_body)
5054
logger.info(f"task_id = {task_id} の受け入れ取り消し完了")
5155

5256
except requests.exceptions.HTTPError as e:
@@ -73,8 +77,7 @@ def main(args):
7377
metavar='file',
7478
type=str,
7579
required=True,
76-
help=
77-
'task_idの一覧が記載されたファイル。task_idは改行(LF or CRLF)で区切る。')
80+
help='task_idの一覧が記載されたファイル。task_idは改行(LF or CRLF)で区切る。')
7881

7982
parser.add_argument('--user_id',
8083
metavar='user_id',

examples/example_utils.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import annofabapi
44

5+
56
def read_lines(filepath: str) -> List[str]:
67
"""ファイルを行単位で読み込む。改行コードを除く"""
78
with open(filepath) as f:
89
lines = f.readlines()
910
return [e.rstrip('\r\n') for e in lines]
1011

12+
1113
class ExamplesWrapper:
1214
"""
1315
Exampleツール用のWrapperクラス
1416
Returns:
1517
1618
"""
19+
1720
def __init__(self, service: annofabapi.Resource):
1821
self.service = service
1922

20-
def get_account_id_last_annotation_phase(self, task_histories: List[Dict[str, Any]]):
23+
def get_account_id_last_annotation_phase(
24+
self, task_histories: List[Dict[str, Any]]):
2125
"""
2226
タスク履歴の最後のannotation phaseを担当したaccount_idを取得する. なければNoneを返す
2327
Args:
@@ -27,7 +31,9 @@ def get_account_id_last_annotation_phase(self, task_histories: List[Dict[str, An
2731
2832
2933
"""
30-
annotation_histories = [e for e in task_histories if e["phase"] == "annotation"]
34+
annotation_histories = [
35+
e for e in task_histories if e["phase"] == "annotation"
36+
]
3137
if len(annotation_histories) > 0:
3238
last_history = annotation_histories[-1]
3339
return last_history["account_id"]
@@ -48,8 +54,8 @@ def get_account_id_from_user_id(self, project_id: str, user_id: str):
4854
member, _ = self.service.api.get_project_member(project_id, user_id)
4955
return member['account_id']
5056

51-
52-
def change_operator_of_task(self, project_id: str, task_id: str, account_id: str) -> Dict[str, Any]:
57+
def change_operator_of_task(self, project_id: str, task_id: str,
58+
account_id: str) -> Dict[str, Any]:
5359
"""
5460
タスクの担当者を変更する
5561
Args:
@@ -68,11 +74,13 @@ def change_operator_of_task(self, project_id: str, task_id: str, account_id: str
6874
"status": "not_started",
6975
"account_id": account_id,
7076
"last_updated_datetime": task["updated_datetime"],
71-
7277
}
73-
return self.service.api.operate_task(project_id, task_id, request_body=req)[0]
78+
return self.service.api.operate_task(project_id,
79+
task_id,
80+
request_body=req)[0]
7481

75-
def change_to_working_phase(self, project_id: str, task_id: str, account_id: str) -> Dict[str, Any]:
82+
def change_to_working_phase(self, project_id: str, task_id: str,
83+
account_id: str) -> Dict[str, Any]:
7684
"""
7785
タスクを作業中に変更する
7886
Args:
@@ -91,10 +99,10 @@ def change_to_working_phase(self, project_id: str, task_id: str, account_id: str
9199
"status": "working",
92100
"account_id": account_id,
93101
"last_updated_datetime": task["updated_datetime"],
94-
95102
}
96-
return self.service.api.operate_task(project_id, task_id, request_body=req)[0]
97-
103+
return self.service.api.operate_task(project_id,
104+
task_id,
105+
request_body=req)[0]
98106

99107
def reject_task(self, project_id: str, task_id: str, account_id: str):
100108
"""
@@ -110,22 +118,22 @@ def reject_task(self, project_id: str, task_id: str, account_id: str):
110118

111119
# タスクを差し戻す
112120
task, _ = self.service.api.get_task(project_id, task_id)
113-
annotator_account_id = self.get_account_id_last_annotation_phase(task["histories_by_phase"])
121+
annotator_account_id = self.get_account_id_last_annotation_phase(
122+
task["histories_by_phase"])
114123

115124
req_reject = {
116125
"status": "rejected",
117126
"account_id": account_id,
118127
"last_updated_datetime": task["updated_datetime"],
119-
120128
}
121-
rejected_task, _ = self.service.api.operate_task(project_id, task_id, request_body=req_reject)
129+
rejected_task, _ = self.service.api.operate_task(
130+
project_id, task_id, request_body=req_reject)
122131

123132
req_change_operator = {
124133
"status": "not_started",
125134
"account_id": annotator_account_id,
126135
"last_updated_datetime": rejected_task["updated_datetime"],
127-
128136
}
129-
updated_task, _ = self.service.api.operate_task(project_id, task["task_id"], request_body=req_change_operator)
137+
updated_task, _ = self.service.api.operate_task(
138+
project_id, task["task_id"], request_body=req_change_operator)
130139
return updated_task
131-

examples/reject_tasks_with_adding_comment.py

+31-20
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import argparse
66
import logging
77
import time
8+
import uuid
89
from typing import Any, Dict, List, Optional, Tuple, Union
910

1011
import requests
11-
import uuid
12+
1213
import annofabapi
1314
import annofabapi.utils
14-
15-
from example_utils import read_lines, ExamplesWrapper
15+
from example_utils import ExamplesWrapper, read_lines
1616

1717
logging_formatter = '%(levelname)s : %(asctime)s : %(name)s : %(funcName)s : %(message)s'
1818
logging.basicConfig(format=logging_formatter)
@@ -21,7 +21,9 @@
2121
logger = logging.getLogger(__name__)
2222
logger.setLevel(level=logging.DEBUG)
2323

24-
def add_inspection_comment(project_id: str, task: Dict[str, Any], inspection_comment: str, commenter_account_id: str):
24+
25+
def add_inspection_comment(project_id: str, task: Dict[str, Any],
26+
inspection_comment: str, commenter_account_id: str):
2527
"""
2628
先頭画像の左上に検査コメントを付与する
2729
Args:
@@ -53,14 +55,17 @@ def add_inspection_comment(project_id: str, task: Dict[str, Any], inspection_com
5355
"created_datetime": annofabapi.utils.str_now()
5456
},
5557
"_type": "Put",
56-
5758
}]
5859

59-
return service.api.batch_update_inspections(project_id, task["task_id"], first_input_data_id, request_body=req_inspection)[0]
60-
60+
return service.api.batch_update_inspections(project_id,
61+
task["task_id"],
62+
first_input_data_id,
63+
request_body=req_inspection)[0]
6164

6265

63-
def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str], inspection_comment: str, commenter_user_id: str):
66+
def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str],
67+
inspection_comment: str,
68+
commenter_user_id: str):
6469
"""
6570
検査コメントを付与して、タスクを差し戻す
6671
Args:
@@ -70,21 +75,25 @@ def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str], i
7075
commenter_user_id: 検査コメントを付与して、タスクを差し戻すユーザのuser_id
7176
"""
7277

73-
commenter_account_id = examples_wrapper.get_account_id_from_user_id(project_id, commenter_user_id)
78+
commenter_account_id = examples_wrapper.get_account_id_from_user_id(
79+
project_id, commenter_user_id)
7480

7581
for task_id in task_id_list:
7682
task, _ = service.api.get_task(project_id, task_id)
7783
logger.debug(f"task_id = {task_id}, {task['status']}, {task['phase']}")
7884
if task["phase"] == "annotation":
79-
logger.warning(f"task_id = {task_id} はannofation phaseのため、差し戻しできません。")
85+
logger.warning(
86+
f"task_id = {task_id} はannofation phaseのため、差し戻しできません。")
8087
continue
8188

8289
try:
8390
# 担当者を変更して、作業中にする
84-
examples_wrapper.change_operator_of_task(project_id, task_id, commenter_account_id)
91+
examples_wrapper.change_operator_of_task(project_id, task_id,
92+
commenter_account_id)
8593
logger.debug(f"task_id = {task_id}, {commenter_user_id}に担当者変更 完了")
8694

87-
examples_wrapper.change_to_working_phase(project_id, task_id, commenter_account_id)
95+
examples_wrapper.change_to_working_phase(project_id, task_id,
96+
commenter_account_id)
8897
logger.debug(f"task_id = {task_id}, working statusに変更 完了")
8998
except requests.exceptions.HTTPError as e:
9099
logger.error(e)
@@ -95,7 +104,8 @@ def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str], i
95104
time.sleep(3)
96105
try:
97106
# 検査コメントを付与する
98-
add_inspection_comment(project_id, task, inspection_comment, commenter_account_id)
107+
add_inspection_comment(project_id, task, inspection_comment,
108+
commenter_account_id)
99109
logger.debug(f"task_id = {task_id}, 検査コメントの付与 完了")
100110
except requests.exceptions.HTTPError as e:
101111
logger.error(e)
@@ -104,7 +114,8 @@ def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str], i
104114

105115
try:
106116
# タスクを差し戻す
107-
rejected_task = examples_wrapper.reject_task(project_id, task_id, commenter_account_id)
117+
rejected_task = examples_wrapper.reject_task(
118+
project_id, task_id, commenter_account_id)
108119

109120
except requests.exceptions.HTTPError as e:
110121
logger.error(e)
@@ -115,17 +126,18 @@ def reject_tasks_with_adding_comment(project_id: str, task_id_list: List[str], i
115126
return
116127

117128

118-
119-
120129
def main(args):
121130
task_id_list = read_lines(args.task_id_file)
122131
user_id = service.api.login_user_id
123-
reject_tasks_with_adding_comment(args.project_id, task_id_list, args.comment, user_id)
132+
reject_tasks_with_adding_comment(args.project_id, task_id_list,
133+
args.comment, user_id)
124134

125135

126136
if __name__ == "__main__":
127137
parser = argparse.ArgumentParser(
128-
description="検査コメントを付与してタスクを差し戻します。検査コメントは先頭の画像の左上(0,0)に付与します。AnnoFab認証情報は`.netrc`に記載すること")
138+
description=
139+
"検査コメントを付与してタスクを差し戻します。検査コメントは先頭の画像の左上(0,0)に付与します。AnnoFab認証情報は`.netrc`に記載すること"
140+
)
129141
parser.add_argument('--project_id',
130142
metavar='project_id',
131143
type=str,
@@ -137,8 +149,7 @@ def main(args):
137149
metavar='file',
138150
type=str,
139151
required=True,
140-
help=
141-
'task_idの一覧が記載されたファイル。task_idは改行(LF or CRLF)で区切る。')
152+
help='task_idの一覧が記載されたファイル。task_idは改行(LF or CRLF)で区切る。')
142153

143154
parser.add_argument('--comment',
144155
metavar='comment',

tests/test_api.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import uuid
1515
from distutils.util import strtobool
1616

17-
from annofabapi import AnnofabApi, Wrapper
17+
import annofabapi
1818
from tests.utils_for_test import WrapperForTest, create_csv_for_task
1919

2020
# プロジェクトトップに移動する
@@ -27,9 +27,6 @@
2727
should_print_log_message: bool = strtobool(
2828
inifile.get('annofab', 'should_print_log_message'))
2929

30-
annofab_user_id = os.getenv('ANNOFAB_USER_ID')
31-
annofab_password = os.getenv('ANNOFAB_PASSWORD')
32-
3330
test_dir = './tests/data'
3431
out_dir = './tests/out'
3532

@@ -38,8 +35,9 @@
3835
logging.basicConfig(format=logging_formatter)
3936
logging.getLogger("annofabapi").setLevel(level=logging.DEBUG)
4037

41-
api = AnnofabApi(annofab_user_id, annofab_password)
42-
wrapper = Wrapper(api)
38+
service = annofabapi.build_from_netrc()
39+
api = service.api
40+
wrapper = service.wrapper
4341
test_wrapper = WrapperForTest(api)
4442

4543
my_account_id = api.get_my_account()[0]['account_id']

0 commit comments

Comments
 (0)