-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: ontext managers for PDF #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,4 +56,4 @@ results/ | |
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db | ||
| Thumbs.db | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,17 +73,31 @@ def get_pdf_name(pdf_path: str | Path | BytesIO) -> str: | |
| return Path(pdf_path).name | ||
| elif isinstance(pdf_path, BytesIO): | ||
| pdf_reader = PyPDF2.PdfReader(pdf_path) | ||
| meta = pdf_reader.metadata | ||
| pdf_name = meta.title if meta and meta.title else "Untitled" | ||
| return sanitize_filename(pdf_name) | ||
| try: | ||
| meta = pdf_reader.metadata | ||
| pdf_name = meta.title if meta and meta.title else "Untitled" | ||
| return sanitize_filename(pdf_name) | ||
| finally: | ||
| if hasattr(pdf_reader, "stream") and hasattr(pdf_reader.stream, "close"): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing |
||
| pdf_reader.stream.close() | ||
| return "Unknown" | ||
|
|
||
|
|
||
| def get_pdf_title(pdf_path: str | Path) -> str: | ||
| """Extract PDF title from metadata.""" | ||
| pdf_reader = PyPDF2.PdfReader(str(pdf_path)) | ||
| meta = pdf_reader.metadata | ||
| return meta.title if meta and meta.title else "Untitled" | ||
| if isinstance(pdf_path, (str, Path)): | ||
| with open(pdf_path, "rb") as f: | ||
| pdf_reader = PyPDF2.PdfReader(f) | ||
| meta = pdf_reader.metadata | ||
| return meta.title if meta and meta.title else "Untitled" | ||
| else: | ||
| pdf_reader = PyPDF2.PdfReader(str(pdf_path)) | ||
| try: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| meta = pdf_reader.metadata | ||
| return meta.title if meta and meta.title else "Untitled" | ||
| finally: | ||
| if hasattr(pdf_reader, "stream") and hasattr(pdf_reader.stream, "close"): | ||
| pdf_reader.stream.close() | ||
|
|
||
|
|
||
| def convert_physical_index_to_int(data: Any) -> Any: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
hasattr(pdf_reader, "stream")pattern relies on PyPDF2 internals. Thewith open()approach you used on lines 86-89 is cleaner. Consider using that consistently.