Skip to content

Commit 718e60d

Browse files
authored
Merge pull request #145 from kurusugawa-computer/add-util-method
utils.pyを整理
2 parents 6b0d9d5 + ed19dcd commit 718e60d

File tree

6 files changed

+32
-24
lines changed

6 files changed

+32
-24
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,15 @@
1515
* 現在、APIは開発途上版です。予告なく互換性のない変更がある可能性をご了承ください。
1616
* put, post, delete系のメソッドを間違えて実行してしまわないよう、注意してください。特に「プロジェクト削除」や「アノテーション仕様更新」のAPIには十分注意してください。
1717

18-
## 廃止予定
18+
# 廃止予定
19+
20+
## 2020-05-01以降 utilsのいくつかのメソッドを非公開
21+
以下のメソッドを非公開にします。以下のメソッドは本来非公開用でして、外部で利用することを想定していなかったためです。
22+
* utils.raise_for_status
23+
* utils.log_error_response
24+
* utils.download
25+
26+
1927

2028
# Features
2129
cURLやPostmanなどよりも簡単にAnnoFab Web APIにアクセスできます。

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.29.2'
1+
__version__ = '0.29.3'

annofabapi/utils.py

+11-15
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import requests
1616
from requests.structures import CaseInsensitiveDict
1717

18-
from annofabapi.models import TaskHistory, TaskPhase
18+
from annofabapi.models import Task, TaskHistory, TaskPhase
1919

2020

2121
def raise_for_status(response: requests.Response):
@@ -222,21 +222,17 @@ def get_task_history_index_skipped_inspection(task_history_list: List[TaskHistor
222222
return index_list
223223

224224

225-
def first_true(iterable, default=None, pred=None):
225+
def can_put_annotation(task: Task, my_account_id: str) -> bool:
226226
"""
227-
Returns the first true value in the iterable.
227+
対象タスクが、`put_annotation` APIで、アノテーションを更新できる状態かどうか。
228+
過去に担当者が割り当たっている場合、または現在の担当者自分自身の場合は、アノテーションを更新できる。
228229
229-
If no true value is found, returns *default*
230-
231-
If *pred* is not None, returns the first item for which
232-
``pred(item) == True`` .
233-
234-
>>> first_true(range(10))
235-
1
236-
>>> first_true(range(10), pred=lambda x: x > 5)
237-
6
238-
>>> first_true(range(10), default='missing', pred=lambda x: x > 9)
239-
'missing'
230+
Args:
231+
task: 対象タスク
232+
my_account_id: 自分(ログインしているユーザ)のアカウントID
240233
234+
Returns:
235+
Trueならば、タスクの状態を変更せずに`put_annotation` APIを実行できる。
241236
"""
242-
return next(filter(pred, iterable), default)
237+
# ログインユーザはプロジェクトオーナであること前提
238+
return len(task["histories_by_phase"]) == 0 or task["account_id"] == my_account_id

annofabapi/wrapper.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from annofabapi.models import (AnnotationDataHoldingType, AnnotationSpecsV1, InputData, Inspection, InspectionStatus,
1717
Instruction, JobInfo, JobStatus, JobType, MyOrganization, Organization,
1818
OrganizationMember, Project, ProjectMember, SupplementaryData, Task)
19-
from annofabapi.utils import allow_404_error, first_true
19+
from annofabapi.utils import allow_404_error
2020

2121
logger = logging.getLogger(__name__)
2222

@@ -41,6 +41,10 @@ class AnnotationSpecsRelation:
4141
choice_id: Dict[ChoiceKey, ChoiceKey]
4242

4343

44+
def __first_true(iterable, default=None, pred=None):
45+
return next(filter(pred, iterable), default)
46+
47+
4448
class Wrapper:
4549
"""
4650
AnnofabApiのラッパー.
@@ -389,15 +393,15 @@ def get_annotation_specs_relation(self, src_project_id: str, dest_project_id: st
389393
dict_label_id: Dict[str, str] = {}
390394
for src_label in src_annotation_specs["labels"]:
391395
src_label_name_en = self.__get_label_name_en(src_label)
392-
dest_label = first_true(dest_labels, pred=lambda e, f=src_label_name_en: self.__get_label_name_en(e) == f)
396+
dest_label = __first_true(dest_labels, pred=lambda e, f=src_label_name_en: self.__get_label_name_en(e) == f)
393397
if dest_label is not None:
394398
dict_label_id[src_label["label_id"]] = dest_label["label_id"]
395399

396400
dict_additional_data_definition_id: Dict[str, str] = {}
397401
dict_choice_id: Dict[ChoiceKey, ChoiceKey] = {}
398402
for src_additional in src_annotation_specs["additionals"]:
399403
src_additional_name_en = self.__get_additional_data_definition_name_en(src_additional)
400-
dest_additional = first_true(
404+
dest_additional = __first_true(
401405
dest_additionals,
402406
pred=lambda e, f=src_additional_name_en: self.__get_additional_data_definition_name_en(e) == f)
403407
if dest_additional is None:
@@ -409,8 +413,8 @@ def get_annotation_specs_relation(self, src_project_id: str, dest_project_id: st
409413
dest_choices = dest_additional["choices"]
410414
for src_choice in src_additional["choices"]:
411415
src_choice_name_en = self.__get_choice_name_en(src_choice)
412-
dest_choice = first_true(dest_choices,
413-
pred=lambda e, f=src_choice_name_en: self.__get_choice_name_en(e) == f)
416+
dest_choice = __first_true(dest_choices,
417+
pred=lambda e, f=src_choice_name_en: self.__get_choice_name_en(e) == f)
414418
if dest_choice is not None:
415419
dict_choice_id[ChoiceKey(src_additional["additional_data_definition_id"],
416420
src_choice["choice_id"])] = ChoiceKey(

generate/swagger/swagger.v2.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ info:
7676
WebhookID | プロジェクト内で一意
7777
入力データセットID | 組織内で一意
7878
79-
version: 0.78.0
79+
version: 0.78.2
8080
title: AnnoFab Web API
8181
x-logo:
8282
url: "https://annofab.com/images/logo_landscape.png"

generate/swagger/swagger.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ info:
7676
WebhookID | プロジェクト内で一意
7777
入力データセットID | 組織内で一意
7878
79-
version: 0.78.0
79+
version: 0.78.2
8080
title: AnnoFab Web API
8181
x-logo:
8282
url: "https://annofab.com/images/logo_landscape.png"

0 commit comments

Comments
 (0)