diff --git a/Makefile b/Makefile index ce2c184..3024816 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: test clean build test_deploy deploy test: - python -m pytest tests/* + pytest tests/* clean: rm -rf build dist *.egg-info diff --git a/README.md b/README.md index f8c368e..965c3dd 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ pip install manga109api import manga109api from pprint import pprint -# Instantiate a parser with the root directory of Manga109 +# (0) Instantiate a parser with the root directory of Manga109 manga109_root_dir = "YOUR_DIR/Manga109_2017_09_28" p = manga109api.Parser(root_dir=manga109_root_dir) diff --git a/manga109api/manga109api.py b/manga109api/manga109api.py index b4f3fb9..922aad8 100644 --- a/manga109api/manga109api.py +++ b/manga109api/manga109api.py @@ -14,7 +14,7 @@ def __init__(self, root_dir): self.root_dir = pathlib.Path(root_dir) self.books = [] # book titles - with (self.root_dir / "books.txt").open("rt", encoding= 'utf-8') as f: + with (self.root_dir / "books.txt").open("rt", encoding='utf-8') as f: self.books = [line.rstrip() for line in f] def get_annotation(self, book): @@ -82,6 +82,9 @@ def _format_annotation(annotation): def _format_page_dict_style(page): """ Format page annotation data. Make page data have the same key, and align the style of dict. + For example, + in: [{'body': [123], 'face': 123, 'frame': []}] + out: [{'body': [123], 'face': [123], 'frame': [], 'text': []}] Args: page (dict): Annotation data for all pages including info such as frame, text, etc. diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_func.py b/tests/test_func.py new file mode 100644 index 0000000..3eb76e3 --- /dev/null +++ b/tests/test_func.py @@ -0,0 +1,49 @@ +import manga109api +from pathlib import Path + + +def test_img_path(): + manga109_root_dir = "tests/data_dummy/" + p = manga109api.Parser(root_dir=manga109_root_dir) + + img1 = Path(p.img_path(book="TitleA", index=0)).absolute() + img2 = Path("tests/data_dummy/images/TitleA/000.jpg").absolute() + assert(img1 == img2) + + +def test_format_annotation(): + annotation = { + 'book': { + '@title': 'AAA', + 'characters': {'character': [ + {'id': '123', 'name': 'yyy'} + ]}, + 'pages': {'page': [ + {'index': 234, 'width': 345, 'height': 456, + 'frame': [{'id': '567', 'xmin': 11, 'ymin': 22, 'xmax': 33, 'ymax': 44}]} + ]} + } + } + gt = { + 'title': 'AAA', + 'character': [{'id': '123', 'name': 'yyy'}], + 'page': [ + {'index': 234, 'width': 345, 'height': 456, 'face': [], 'body': [], 'text': [], + 'frame': [{'id': '567', 'xmin': 11, 'ymin': 22, 'xmax': 33, 'ymax': 44}]} + ] + } + + ret = manga109api.manga109api._format_annotation(annotation) + assert(gt == ret) + +def test_format_page_dict_style(): + page = [{'body': [123], 'face': 123, 'frame': []}] + gt = [{'body': [123], 'face': [123], 'frame': [], 'text': []}] + manga109api.manga109api._format_page_dict_style(page) + assert(gt == page) + +def test_convert_str_to_int_recursively(): + annotation = [{'@xmax': '234', 'id': '0007a8be'}] + gt = [{'@xmax': 234, 'id': '0007a8be'}] + manga109api.manga109api._convert_str_to_int_recursively(annotation) + assert(gt == annotation)