Reformat using black

This commit is contained in:
Deniz Şafak
2026-01-09 01:36:14 +03:00
parent 3c800df450
commit 5ae153f841
48 changed files with 1530 additions and 894 deletions
+35 -32
View File
@@ -5,10 +5,11 @@ import sys
from ebooklib import epub
# Ensure import path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from abogen.book_parser import get_book_parser
class TestEpubHtmlNavParsing(unittest.TestCase):
"""
Tests for EPUB 3 HTML5 Navigation Document parsing logic (_parse_html_nav_li).
@@ -32,12 +33,12 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
book = epub.EpubBook()
book.set_identifier("navtest123")
book.set_title("Nav Test Book")
# Add some content files
c1 = epub.EpubHtml(title="Chapter 1", file_name="chap1.xhtml", lang="en")
c1.content = "<h1>Chapter 1</h1><p>Text 1</p>"
book.add_item(c1)
c2 = epub.EpubHtml(title="Chapter 2", file_name="chap2.xhtml", lang="en")
c2.content = "<h1>Chapter 2</h1><p>Text 2</p>"
book.add_item(c2)
@@ -47,29 +48,29 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
nav = epub.EpubHtml(title="Nav", file_name="nav.xhtml")
nav.content = nav_html_content
book.add_item(nav)
# We must set spine manually
book.spine = [nav, c1, c2]
epub.write_epub(self.epub_path, book)
# Patch the OPF to remove toc="ncx" default which causes crash
# Patch the OPF to remove toc="ncx" default which causes crash
# because we intentionally excluded the legacy NCX file.
import zipfile
with zipfile.ZipFile(self.epub_path, 'r') as zin:
opf_content = zin.read('EPUB/content.opf').decode('utf-8')
opf_content = opf_content.replace('toc="ncx"', '')
with zipfile.ZipFile(self.epub_path, "r") as zin:
opf_content = zin.read("EPUB/content.opf").decode("utf-8")
opf_content = opf_content.replace('toc="ncx"', "")
# Repack
TEMP_EPUB = self.epub_path + ".temp"
with zipfile.ZipFile(TEMP_EPUB, 'w') as zout:
with zipfile.ZipFile(TEMP_EPUB, "w") as zout:
for item in zin.infolist():
if item.filename == 'EPUB/content.opf':
if item.filename == "EPUB/content.opf":
zout.writestr(item, opf_content)
else:
zout.writestr(item, zin.read(item.filename))
shutil.move(TEMP_EPUB, self.epub_path)
def test_basic_html_nav_parsing(self):
@@ -86,14 +87,14 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
</nav>
"""
self._create_epub_with_custom_nav(nav_html)
parser = get_book_parser(self.epub_path)
parser.process_content()
chapters = parser.get_chapters()
# Filter out "Nav" or "Introduction" prefix content found from the Nav file itself
chapters = [c for c in chapters if "Chapter" in c[1] or "Section" in c[1]]
self.assertEqual(len(chapters), 2)
self.assertEqual(chapters[0][1], "Chapter 1")
self.assertEqual(chapters[1][1], "Chapter 2")
@@ -116,11 +117,11 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
"""
# Note: In this test setup, chap2 is serving as "Section 1.1" effectively
self._create_epub_with_custom_nav(nav_html)
parser = get_book_parser(self.epub_path)
parser.process_content()
chapters = parser.get_chapters()
ids = [c[1] for c in chapters]
self.assertIn("Chapter 1", ids)
self.assertIn("Section 1.1", ids)
@@ -143,25 +144,26 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
</nav>
"""
self._create_epub_with_custom_nav(nav_html)
parser = get_book_parser(self.epub_path)
parser.process_content()
chapters = parser.get_chapters()
chapter_titles = [c[1] for c in chapters]
self.assertIn("Chapter 1", chapter_titles)
self.assertNotIn("Part I", chapter_titles)
self.assertNotIn("Part I", chapter_titles)
# Check internal structure
# Find the node named "Part I" in the processed structure
root_node = next(node for node in parser.processed_nav_structure if node['title'] == "Part I")
self.assertEqual(root_node['title'], "Part I")
self.assertFalse(root_node['has_content'])
self.assertEqual(len(root_node['children']), 1)
self.assertEqual(root_node['children'][0]['title'], "Chapter 1")
root_node = next(
node for node in parser.processed_nav_structure if node["title"] == "Part I"
)
self.assertEqual(root_node["title"], "Part I")
self.assertFalse(root_node["has_content"])
self.assertEqual(len(root_node["children"]), 1)
self.assertEqual(root_node["children"][0]["title"], "Chapter 1")
def test_identify_nav_item(self):
"""Test the _identify_nav_item method specifically."""
@@ -175,10 +177,11 @@ class TestEpubHtmlNavParsing(unittest.TestCase):
# But here we can call load directly if needed, or rely on normal flow up until navigation
parser.load()
nav_item, nav_type = parser._identify_nav_item()
self.assertEqual(nav_type, "html")
self.assertIsNotNone(nav_item)
self.assertTrue("nav.xhtml" in nav_item.get_name())
if __name__ == "__main__":
unittest.main()