Skip to content

Commit 802267d

Browse files
authored
Merge pull request #136 from kurusugawa-computer/update-parser
parser.py: jsonをloadするメソッドを追加
2 parents ea428ef + 35860fc commit 802267d

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

annofabapi/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '0.27.8'
1+
__version__ = '0.27.9'

annofabapi/parser.py

+14-5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ def parse(self) -> SimpleAnnotation:
9494
JSONファイルをパースする。
9595
"""
9696

97+
@abc.abstractmethod
98+
def load_json(self) -> Any:
99+
"""
100+
JSONファイルをloadします。
101+
"""
102+
97103

98104
class FullAnnotationParser(abc.ABC):
99105
"""
@@ -181,10 +187,11 @@ def __init__(self, zip_file: zipfile.ZipFile, json_file_path: str):
181187
super().__init__(json_file_path)
182188

183189
def parse(self) -> SimpleAnnotation:
190+
return SimpleAnnotation.from_dict(self.load_json()) # type: ignore
191+
192+
def load_json(self) -> Any:
184193
with self.__zip_file.open(self.json_file_path) as entry:
185-
anno_dict: dict = json.load(entry)
186-
# mypyの "has no attribute "from_dict" " をignore
187-
return SimpleAnnotation.from_dict(anno_dict) # type: ignore
194+
return json.load(entry)
188195

189196
def open_outer_file(self, data_uri: str):
190197
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri
@@ -213,9 +220,11 @@ def __init__(self, json_file_path: Path):
213220
super().__init__(str(json_file_path))
214221

215222
def parse(self) -> SimpleAnnotation:
223+
return SimpleAnnotation.from_dict(self.load_json()) # type: ignore
224+
225+
def load_json(self) -> Any:
216226
with open(self.json_file_path, encoding="utf-8") as f:
217-
anno_dict: dict = json.load(f)
218-
return SimpleAnnotation.from_dict(anno_dict) # type: ignore
227+
return json.load(f)
219228

220229
def open_outer_file(self, data_uri: str):
221230
outer_file_path = _trim_extension(self.json_file_path) + "/" + data_uri

tests/test_local_parser.py

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def test_simple_annotation_zip(self):
7272
assert type(simple_annotation) == SimpleAnnotation
7373
index += 1
7474

75+
dict_simple_annotation = parser.load_json()
76+
assert type(dict_simple_annotation) == dict
77+
assert "details" in dict_simple_annotation
78+
7579
assert index == 4
7680

7781
with zipfile.ZipFile(zip_path) as zip_file:

0 commit comments

Comments
 (0)