3
3
import os
4
4
import zipfile
5
5
from pathlib import Path
6
- from typing import Any , Dict , Iterator , List , Optional
6
+ from typing import Any , Callable , Dict , Iterator , List , Optional
7
7
8
8
from annofabapi .dataclass .annotation import FullAnnotation , SimpleAnnotation
9
9
from annofabapi .exceptions import AnnotationOuterFileNotFoundError
10
10
11
+ CONVERT_ANNOTATION_DETAIL_DATA_FUNC = Callable [[Dict [str , Any ]], Any ]
12
+
11
13
12
14
def _trim_extension (file_path : str ) -> str :
13
15
"""ファイルパスから拡張子を除去した文字列を返す"""
@@ -72,12 +74,25 @@ def open_outer_file(self, data_uri: str):
72
74
73
75
"""
74
76
75
- @abc .abstractmethod
76
- def parse (self ) -> SimpleAnnotation :
77
- """
78
- JSONファイルをパースする。
77
+ def parse (
78
+ self , convert_deitail_data_func : Optional [CONVERT_ANNOTATION_DETAIL_DATA_FUNC ] = None
79
+ ) -> SimpleAnnotation :
80
+ """JSONファイルをパースする
81
+
82
+ Args:
83
+ convert_deitail_data_func: SimpleAnnotationDetailクラスのdataプロパティを変換する関数を指定します。
84
+ dictからdataclassに変換する際に使います。
85
+
86
+ Returns:
87
+ SimpleAnnotationインスタンス
79
88
"""
80
89
90
+ simple_annotation = SimpleAnnotation .from_dict (self .load_json ()) # type: ignore
91
+ if convert_deitail_data_func is not None :
92
+ for detail in simple_annotation .details :
93
+ detail .data = convert_deitail_data_func (detail .data )
94
+ return simple_annotation
95
+
81
96
@abc .abstractmethod
82
97
def load_json (self ) -> Any :
83
98
"""
@@ -144,11 +159,28 @@ def open_outer_file(self, data_uri: str):
144
159
"""
145
160
146
161
@abc .abstractmethod
147
- def parse (self ) -> FullAnnotation :
162
+ def load_json (self ) -> Any :
148
163
"""
149
- JSONファイルをパースする 。
164
+ JSONファイルをloadします 。
150
165
"""
151
166
167
+ def parse (self , convert_deitail_data_func : Optional [CONVERT_ANNOTATION_DETAIL_DATA_FUNC ] = None ) -> FullAnnotation :
168
+ """JSONファイルをパースする
169
+
170
+ Args:
171
+ convert_deitail_data_func: FullAnnotationDetailクラスのdataプロパティを変換する関数を指定します。
172
+ dictからdataclassに変換する際に使います。
173
+
174
+ Returns:
175
+ FullAnnotationインスタンス
176
+ """
177
+
178
+ full_annotation = FullAnnotation .from_dict (self .load_json ()) # type: ignore
179
+ if convert_deitail_data_func is not None :
180
+ for detail in full_annotation .details :
181
+ detail .data = convert_deitail_data_func (detail .data )
182
+ return full_annotation
183
+
152
184
153
185
class SimpleAnnotationZipParser (SimpleAnnotationParser ):
154
186
"""
@@ -172,9 +204,6 @@ def __init__(self, zip_file: zipfile.ZipFile, json_file_path: str):
172
204
self .__zip_file = zip_file
173
205
super ().__init__ (json_file_path )
174
206
175
- def parse (self ) -> SimpleAnnotation :
176
- return SimpleAnnotation .from_dict (self .load_json ()) # type: ignore
177
-
178
207
def load_json (self ) -> Any :
179
208
with self .__zip_file .open (self .json_file_path ) as entry :
180
209
return json .load (entry )
@@ -208,9 +237,6 @@ class SimpleAnnotationDirParser(SimpleAnnotationParser):
208
237
def __init__ (self , json_file_path : Path ):
209
238
super ().__init__ (str (json_file_path ))
210
239
211
- def parse (self ) -> SimpleAnnotation :
212
- return SimpleAnnotation .from_dict (self .load_json ()) # type: ignore
213
-
214
240
def load_json (self ) -> Any :
215
241
with open (self .json_file_path , encoding = "utf-8" ) as f :
216
242
return json .load (f )
@@ -245,11 +271,9 @@ def __init__(self, zip_file: zipfile.ZipFile, json_file_path: str):
245
271
self .__zip_file = zip_file
246
272
super ().__init__ (json_file_path )
247
273
248
- def parse (self ) -> FullAnnotation :
274
+ def load_json (self ) -> Any :
249
275
with self .__zip_file .open (self .json_file_path ) as entry :
250
- anno_dict : dict = json .load (entry )
251
- # mypyの "has no attribute "from_dict" " をignore
252
- return FullAnnotation .from_dict (anno_dict ) # type: ignore
276
+ return json .load (entry )
253
277
254
278
def open_outer_file (self , data_uri : str ):
255
279
outer_file_path = _trim_extension (self .json_file_path ) + "/" + data_uri
@@ -281,10 +305,9 @@ class FullAnnotationDirParser(FullAnnotationParser):
281
305
def __init__ (self , json_file_path : Path ):
282
306
super ().__init__ (str (json_file_path ))
283
307
284
- def parse (self ) -> FullAnnotation :
308
+ def load_json (self ) -> Any :
285
309
with open (self .json_file_path , encoding = "utf-8" ) as f :
286
- anno_dict : dict = json .load (f )
287
- return FullAnnotation .from_dict (anno_dict ) # type: ignore
310
+ return json .load (f )
288
311
289
312
def open_outer_file (self , data_uri : str ):
290
313
outer_file_path = _trim_extension (self .json_file_path ) + "/" + data_uri
0 commit comments