Skip to content

Commit

Permalink
Use absolute path for tempdir (#63)
Browse files Browse the repository at this point in the history
This is my attempt to fix #37

I can't reproduce the issue so just making a best guess.
  • Loading branch information
tvhong authored Jan 13, 2025
1 parent ddc9f61 commit 0a4ed7c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions ir/importer/epub.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from ..util import Article


def nov_container_content_filename(filename):
def nov_container_content_filename(filename: str) -> str:
"""Return the content filename for CONTENT."""
query = "{*}rootfiles/{*}rootfile[@media-type='application/oebps-package+xml']"
doc = ET.parse(filename)
Expand All @@ -34,7 +34,7 @@ def nov_container_content_filename(filename):
return "OEBPS/Content.opf"


def nov_content_version(root):
def nov_content_version(root) -> float:
"""Return the EPUB version for ROOT."""
version = root.get("version") if root is not None else None
if not version:
Expand Down Expand Up @@ -151,7 +151,7 @@ def nov_toc_epub2_files(content_dir, root) -> List[Article]:
return files


def nov_toc_epub3_files(toc_file, root) -> List[Article]:
def nov_toc_epub3_files(toc_file: str, root) -> List[Article]:
toc_dir = os.path.dirname(toc_file)
query = ".//{*}nav//{*}ol/{*}li"
nav_points = root.findall(query)
Expand All @@ -170,23 +170,26 @@ def nov_toc_epub3_files(toc_file, root) -> List[Article]:
return files


def _get_extract_dir(filename):
def _get_extract_dir(filename: str) -> str:
"get extract directory by epub filename."

tempdir = tempfile.gettempdir()
tempdir = Path(tempdir).as_posix()

# Use absolute path as Windows sometimes returns a relative path without the drive letter
tempdir = Path(tempdir).absolute().as_posix()
basename = os.path.basename(filename)
nonextension = os.path.splitext(basename)[0]
return os.path.join(tempdir, nonextension)


def _unzip_epub(file_path):
def _unzip_epub(file_path: str) -> str:
extract_dir = _get_extract_dir(file_path)
with zipfile.ZipFile(file_path, "r") as zip_ref:
zip_ref.extractall(extract_dir)
return extract_dir


def getEpubToc(epub_file_path) -> List[Article]:
def getEpubToc(epub_file_path: str) -> List[Article]:
extract_dir = _unzip_epub(epub_file_path)
container_filename = os.path.join(extract_dir, "META-INF", "container.xml")
content_filename = nov_container_content_filename(container_filename)
Expand Down

0 comments on commit 0a4ed7c

Please sign in to comment.