Fixed all pytest errors, enhanced book parser with context management and resource cleanup, update tests for proper parser closure

This commit is contained in:
Deniz Şafak
2026-01-10 00:09:04 +03:00
parent bd30939d27
commit 950eb317f0
6 changed files with 123 additions and 77 deletions
+17
View File
@@ -43,6 +43,18 @@ class BaseBookParser(ABC):
"""Load the book file."""
pass
def close(self):
"""Close any open file handles."""
pass
def __enter__(self):
# Already loaded in __init__, or lazily.
# Just ensure we have resources if needed, or do nothing.
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
@abstractmethod
def process_content(self, replace_single_newlines=True):
"""Process the book content to extract text and structure."""
@@ -110,6 +122,11 @@ class PdfParser(BaseBookParser):
logging.error(f"Error loading PDF {self.book_path}: {e}")
raise
def close(self):
if self.pdf_doc:
self.pdf_doc.close()
self.pdf_doc = None
def _extract_book_metadata(self):
# PDF metadata extraction can be added here if needed
# For now, base class metadata is empty dict