1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
"""Tests for file text extraction."""
import tempfile
import unittest
from pathlib import Path
from converter.extractors import extract_text
def _ebooklib_usable() -> bool:
"""True when ebooklib's EPUB reader imports (it needs a working lxml)."""
try:
from ebooklib import epub # noqa: F401
except Exception:
return False
return True
# The managed env can end up with compiled wheels that cannot load on this
# platform (e.g. glibc lxml under a musl interpreter) — the tool repairs or
# degrades at runtime, and these tests must degrade with it instead of
# failing. Relaunch audiobook.py once (or delete app/envs/tts) to rebuild.
requires_epub = unittest.skipUnless(
_ebooklib_usable(),
"ebooklib is unusable in this environment "
"(its compiled dependency failed to import)")
class TxtExtractionTests(unittest.TestCase):
def _extract(self, data: bytes) -> str:
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.txt"
path.write_bytes(data)
return extract_text(path)
def test_utf8(self):
self.assertEqual(self._extract("héllo wörld".encode("utf-8")), "héllo wörld")
def test_utf16_with_bom(self):
self.assertEqual(self._extract("héllo".encode("utf-16")), "héllo")
def test_cp1252(self):
self.assertEqual(self._extract("“quotes”".encode("cp1252")), "“quotes”")
def test_latin1_fallback(self):
# 0x81 is undefined in cp1252, forcing the latin-1 catch-all
self.assertEqual(self._extract(b"caf\x81"), "caf\x81")
def test_unsupported_format(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.xyz"
path.write_bytes(b"data")
with self.assertRaises(ValueError):
extract_text(path)
def test_utf16_without_bom_detected(self):
self.assertEqual(self._extract("chapter one".encode("utf-16-le")),
"chapter one")
def test_lone_nul_does_not_flip_to_utf16(self):
# A single stray NUL byte in an otherwise-ASCII UTF-8 file must not
# switch the whole book to a UTF-16 decode (mojibake): the text
# comes back readable instead.
self.assertEqual(self._extract(b"hello world\x00rest"),
"hello world\x00rest")
def test_standalone_page_numbers_removed_but_years_kept(self):
from converter.extractors import clean_text
cleaned = clean_text("Chapter 1\n\n42\n\nIt was 1984.")
self.assertNotIn("42", cleaned)
self.assertIn("1984", cleaned)
kept = clean_text("It was the year\n\n1984\n\nwhen it began.")
self.assertIn("1984", kept)
class EpubZipfileFallbackTests(unittest.TestCase):
"""The no-ebooklib EPUB fallback: spine order, no TOC narration."""
@staticmethod
def _write_epub(path: Path):
import zipfile
container = ("<?xml version=\"1.0\"?>"
"<container><rootfiles>"
"<rootfile full-path=\"OEBPS/content.opf\"/>"
"</rootfiles></container>")
opf = ("<?xml version=\"1.0\"?>"
"<package xmlns=\"http://www.idpf.org/2007/opf\">"
"<manifest>"
"<item id=\"nav\" href=\"nav.xhtml\" properties=\"nav\"/>"
"<item id=\"c2\" href=\"text/chapterB.xhtml\"/>"
"<item id=\"c1\" href=\"text/chapterA.xhtml\"/>"
"</manifest>"
"<spine><itemref idref=\"nav\"/>"
"<itemref idref=\"c2\"/><itemref idref=\"c1\"/></spine>"
"</package>")
with zipfile.ZipFile(path, "w") as zf:
zf.writestr("mimetype", "application/epub+zip")
zf.writestr("META-INF/container.xml", container)
zf.writestr("OEBPS/content.opf", opf)
zf.writestr("OEBPS/nav.xhtml",
"<html><body><p>Contents</p></body></html>")
zf.writestr("OEBPS/text/chapterA.xhtml",
"<html><body><p>Alpha text.</p></body></html>")
zf.writestr("OEBPS/text/chapterB.xhtml",
"<html><body><p>Beta text.</p></body></html>")
def test_spine_order_and_no_nav(self):
from converter.extractors import _read_epub_zipfile
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
self._write_epub(path)
items = _read_epub_zipfile(path)
titles = [title for title, _ in items]
self.assertNotIn("nav", titles)
# Spine order (B before A) beats filename sort (A before B).
self.assertEqual(titles, ["chapterB", "chapterA"])
def test_unparsable_opf_falls_back_to_filename_order(self):
import zipfile
from converter.extractors import _read_epub_zipfile
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
with zipfile.ZipFile(path, "w") as zf:
zf.writestr("a.xhtml", "<html><body><p>A</p></body></html>")
zf.writestr("b.xhtml", "<html><body><p>B</p></body></html>")
items = _read_epub_zipfile(path)
self.assertEqual([title for title, _ in items], ["a", "b"])
def _build_test_epub(path: Path, chapters=(("One", "First chapter text."),
("Two", "Second chapter text."))) -> None:
from ebooklib import epub
book = epub.EpubBook()
book.set_identifier("test-id")
book.set_title("Test Book")
book.set_language("en")
book.add_author("Test Author")
items = []
for index, (title, text) in enumerate(chapters, 1):
chapter = epub.EpubHtml(title=title, file_name=f"chap{index}.xhtml", lang="en")
chapter.content = f"<html><body><p>{text}</p></body></html>"
book.add_item(chapter)
items.append(chapter)
book.toc = tuple(items)
book.spine = ["nav", *items]
book.add_item(epub.EpubNcx())
book.add_item(epub.EpubNav())
epub.write_epub(str(path), book)
class EpubExtractionTests(unittest.TestCase):
def setUp(self):
try:
import ebooklib # noqa: F401
except ImportError:
self.skipTest("ebooklib not installed")
@requires_epub
def test_ebooklib_extraction(self):
# Regression test: the ebooklib path used to silently return "" due to
# isinstance(item, ebooklib.ITEM_DOCUMENT) (an int, not a class).
from converter.extractors import _read_epub_ebooklib
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
_build_test_epub(path)
items = _read_epub_ebooklib(path)
html = "\n".join(content for _, content in items)
self.assertIn("First chapter text.", html)
self.assertIn("Second chapter text.", html)
@requires_epub
def test_epub_extraction_follows_spine_order(self):
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
_build_test_epub(path)
text = extract_text(path)
self.assertIn("First chapter text.", text)
self.assertIn("Second chapter text.", text)
self.assertLess(text.index("First chapter text."),
text.index("Second chapter text."))
class ExtractSectionsTests(unittest.TestCase):
def setUp(self):
try:
import ebooklib # noqa: F401
except ImportError:
self.skipTest("ebooklib not installed")
@requires_epub
def test_epub_sections_split_on_chapters(self):
from converter.extractors import extract_sections
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
_build_test_epub(path)
sections = extract_sections(path)
self.assertEqual(len(sections), 2)
self.assertEqual(sections[0].title, "One")
self.assertEqual(sections[1].title, "Two")
self.assertIn("First chapter text.", sections[0].text)
self.assertIn("Second chapter text.", sections[1].text)
def test_txt_is_single_section(self):
from converter.extractors import extract_sections
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.txt"
path.write_text("Hello world.", encoding="utf-8")
sections = extract_sections(path)
self.assertEqual(len(sections), 1)
self.assertEqual(sections[0].title, "book")
self.assertEqual(sections[0].text, "Hello world.")
@requires_epub
def test_single_chapter_epub_keeps_chapter_title(self):
from converter.extractors import extract_sections
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
_build_test_epub(path, chapters=(("Only", "Just one chapter."),))
sections = extract_sections(path)
self.assertEqual(len(sections), 1)
self.assertEqual(sections[0].title, "Only")
self.assertIn("Just one chapter.", sections[0].text)
class ExtractBookTests(unittest.TestCase):
def test_txt_falls_back_to_stem_and_blank_author(self):
from converter.extractors import extract_book
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "mybook.txt"
path.write_text("Hello world.", encoding="utf-8")
book = extract_book(path)
self.assertEqual(book.title, "mybook")
self.assertEqual(book.author, "")
self.assertEqual(len(book.sections), 1)
@requires_epub
def test_epub_metadata_harvested(self):
try:
import ebooklib # noqa: F401
except ImportError:
self.skipTest("ebooklib not installed")
from converter.extractors import extract_book
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.epub"
_build_test_epub(path)
book = extract_book(path)
self.assertEqual(book.title, "Test Book")
self.assertEqual(book.author, "Test Author")
self.assertEqual([s.title for s in book.sections], ["One", "Two"])
def test_pdf_metadata_harvested(self):
from converter.extractors import extract_book
try:
from pypdf import PdfWriter
except ImportError:
self.skipTest("pypdf not installed")
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "book.pdf"
writer = PdfWriter()
writer.add_metadata({"/Title": "PDF Title", "/Author": "PDF Author"})
writer.add_blank_page(width=612, height=792)
with open(path, "wb") as handle:
writer.write(handle)
book = extract_book(path)
self.assertEqual(book.title, "PDF Title")
self.assertEqual(book.author, "PDF Author")
self.assertEqual(len(book.sections), 1)
def test_pdf_without_metadata_falls_back(self):
from converter.extractors import extract_book
try:
from pypdf import PdfWriter
except ImportError:
self.skipTest("pypdf not installed")
with tempfile.TemporaryDirectory() as tmp:
path = Path(tmp) / "plain.pdf"
writer = PdfWriter()
writer.add_blank_page(width=612, height=792)
with open(path, "wb") as handle:
writer.write(handle)
book = extract_book(path)
self.assertEqual(book.title, "plain")
self.assertEqual(book.author, "")
if __name__ == "__main__":
unittest.main()
|