-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(epub, word): Update the documents cache whenever the file is modi…
…fied (#289) * Added failing test * Implemented modification detection
- Loading branch information
Showing
4 changed files
with
81 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Caching utilities""" | ||
from pathlib import Path | ||
|
||
from diskcache import Cache | ||
|
||
|
||
def is_document_modified(key: str, path: Path, cache: Cache) -> bool: | ||
""" | ||
Checks whether a particular document was modified | ||
We can currently afford to naively just stat() the file_path in order to determine it | ||
""" | ||
mtime = cache.get(f"{key}_meta") | ||
if not mtime: | ||
# No information for the book was found, so return True just to be safe | ||
# TODO: Is this acceptable? | ||
return True | ||
stat_mtime = path.stat().st_mtime | ||
return mtime != stat_mtime | ||
|
||
def set_document_modified_time(key: str, path: Path, cache: Cache) -> bool: | ||
key = f"{key}_meta" | ||
cache.set(key, path.stat().st_mtime) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,57 @@ | ||
from pathlib import Path | ||
|
||
from ebooklib import epub | ||
import pytest | ||
|
||
from bookworm.document.uri import DocumentUri | ||
from bookworm.document.formats.epub import EpubDocument | ||
|
||
def temp_book(title: str = 'Sample book') -> epub.EpubBook: | ||
book = epub.EpubBook() | ||
book.set_title("test book") | ||
book.set_language('en') | ||
c1 = epub.EpubHtml(title="Intro", file_name="chap_01.xhtml", lang="en") | ||
c1.content = ( | ||
"<h1>This is a test</h1>" | ||
) | ||
book.add_item(c1) | ||
book.toc = ( | ||
epub.Link("chap_01.xhtml", "Introduction", "intro"), | ||
(epub.Section("Simple book"), (c1,)), | ||
) | ||
|
||
book.add_item(epub.EpubNcx()) | ||
book.add_item(epub.EpubNav()) | ||
book.spine = ["nav", c1] | ||
return book | ||
|
||
|
||
def test_chapter_order_is_unchanged_with_roman_numbers(asset): | ||
epub = EpubDocument(DocumentUri.from_filename(asset('roman.epub'))) | ||
epub.read() | ||
spine = [x[0] for x in epub.epub.spine] | ||
items = [x.file_name.split('/')[-1] for x in epub.epub_html_items] | ||
print(items) | ||
print(spine) | ||
doc = EpubDocument(DocumentUri.from_filename(asset('roman.epub'))) | ||
doc.read() | ||
spine = [x[0] for x in doc.epub.spine] | ||
items = [x.file_name.split('/')[-1] for x in doc.epub_html_items] | ||
assert spine == items | ||
|
||
def test_modified_epub_modifies_cache(asset): | ||
book = temp_book() | ||
epub.write_epub(asset("test.epub"), book, {}) | ||
doc = EpubDocument(DocumentUri.from_filename(asset('test.epub'))) | ||
doc.read() | ||
content = doc.html_content | ||
|
||
# Let's now add a second chapter, and see whether the document read modifies its cache | ||
c2 = epub.EpubHtml(title="Second chapter", file_name="chap_02.xhtml", lang="en") | ||
c2.content = ( | ||
"<h1>This is another test</h1>" | ||
) | ||
book.add_item(c2) | ||
book.spine.append(c2) | ||
epub.write_epub(asset("test.epub"), book, {}) | ||
|
||
# read the book once more, and verify that the content is different | ||
doc = EpubDocument(DocumentUri.from_filename(asset('test.epub'))) | ||
doc.read() | ||
new_content = doc.html_content | ||
Path(asset("test.epub")).unlink() | ||
assert content != new_content |