-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ENH: "with" support for PdfMerger and PdfWriter #1193
Merged
Merged
Changes from 41 commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
a2b7b55
Add `with ... as ...` usage (#1108)
JianzhengLuo 2a88bd5
Update PyPDF2/_merger.py according to @MasterOdin's suggestions
JianzhengLuo b6d0351
Update PyPDF2/_merger.py according to @MasterOdin's suggestions
JianzhengLuo 80813aa
Update PyPDF2/_merger.py according to @MasterOdin's suggestions
JianzhengLuo e6ec1f6
Update PyPDF2/_merger.py according to @MasterOdin's suggestions
JianzhengLuo 386be5b
Update PyPDF2/_merger.py according to @MasterOdin's suggestions
JianzhengLuo 31786bb
Sorry, I forgot to run before committing, so didn't notice that `Trac…
JianzhengLuo ae1bec0
Modify the wrong closing place that cause.
JianzhengLuo 0b7c64e
Merge branch 'main' into add-with-as-usage-#1108
JianzhengLuo b695113
Modify PyPDF2/_writer.py according to @MasterOdin's suggestions
JianzhengLuo 58797c2
Modify PyPDF2/_writer.py according to @MasterOdin's suggestions
JianzhengLuo 1caa9ec
Modify PyPDF2/_writer.py according to @MasterOdin's suggestions
JianzhengLuo 4fbe3cc
Modify PyPDF2/_writer.py according to @MasterOdin's suggestions
JianzhengLuo d598e8a
Modify PyPDF2/_writer.py according to @MasterOdin's suggestions
JianzhengLuo 7ecf9ff
Merge branch 'add-with-as-usage-#1108' of https://github.com/Jianzhen…
JianzhengLuo 562ebc7
Merge branch 'main' into add-with-as-usage-#1108
519dad1
Fix accident
JianzhengLuo 336053a
Merge branch 'add-with-as-usage-#1108' of https://github.com/Jianzhen…
JianzhengLuo 90af68a
Fix error raising while using half traditional usage
JianzhengLuo 0f67658
Add a unit test (Problems still exist, please help.)
JianzhengLuo a6f973e
Update PyPDF2/_merger.py according to @MartinThoma's suggestions
JianzhengLuo 580ad8c
Modify PyPDF2/_merger.py according to flake8
JianzhengLuo bf264bc
Modify to compatible with the existing usage
JianzhengLuo c705300
Modify to compatible with the with .. as ... usage
JianzhengLuo 2940589
Removed a meaningless annotation
JianzhengLuo cda38ac
Fixed wrong test function names
JianzhengLuo 0b18198
Fixed those I can fix only.
JianzhengLuo 62f970f
Removed useless `else` section
JianzhengLuo d85c91b
Renamed argument name `fileobj` back to `stream` to keep the existing…
JianzhengLuo 5cd3f3e
Modified annoation for `PdfWriter().write()` and `PdfWriter().write_s…
JianzhengLuo a7346ef
Switched `fileobj` and `strict` in initializing `PdfWriter()` to keep…
JianzhengLuo d3e62ee
Modified annoation to make the it match the arguments
JianzhengLuo 59be94d
Modified undefined name `fileobj`
JianzhengLuo 092f209
Merge branch 'main' into add-with-as-usage-#1108
MartinThoma da7a239
Fixes
MartinThoma fa8880f
os.path.join
MartinThoma 626d064
Fix issues
MartinThoma 6af6969
Add type hint
MartinThoma 73c2ffb
Test one more line
MartinThoma 5e6f663
Update comments
MartinThoma 8ffda32
Add JianzhengLuo to contributors list
MartinThoma 2888353
Allow using Path
MartinThoma aa2f64a
Remove print
MartinThoma d731281
Apply suggestions from code review
MartinThoma 698821e
More tests
MartinThoma 5464bd0
Use tmp_path fixture
MartinThoma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -27,7 +27,18 @@ | |
|
||
from io import BytesIO, FileIO, IOBase | ||
from pathlib import Path | ||
from typing import Any, Dict, Iterable, List, Optional, Tuple, Union, cast | ||
from types import TracebackType | ||
from typing import ( | ||
Any, | ||
Dict, | ||
Iterable, | ||
List, | ||
Optional, | ||
Tuple, | ||
Type, | ||
Union, | ||
cast, | ||
) | ||
|
||
from ._encryption import Encryption | ||
from ._page import PageObject | ||
|
@@ -84,18 +95,38 @@ class PdfMerger: | |
:param bool strict: Determines whether user should be warned of all | ||
problems and also causes some correctable problems to be fatal. | ||
Defaults to ``False``. | ||
:param fileobj: Output file. Can be a filename or any kind of | ||
file-like object. | ||
""" | ||
|
||
@deprecate_bookmark(bookmarks="outline") | ||
def __init__(self, strict: bool = False) -> None: | ||
def __init__(self, strict: bool = False, fileobj: StrByteType = "") -> None: | ||
self.inputs: List[Tuple[Any, PdfReader, bool]] = [] | ||
self.pages: List[Any] = [] | ||
self.output: Optional[PdfWriter] = PdfWriter() | ||
self.outline: OutlineType = [] | ||
self.named_dests: List[Any] = [] | ||
self.id_count = 0 | ||
self.fileobj = fileobj | ||
self.strict = strict | ||
|
||
def __enter__(self) -> "PdfMerger": | ||
# There is nothing to do. | ||
return self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: Optional[Type[BaseException]], | ||
exc: Optional[BaseException], | ||
traceback: Optional[TracebackType], | ||
) -> None: | ||
"""Write to the fileobj and close the merger.""" | ||
if self.fileobj: | ||
print(f"write to {self.fileobj}") | ||
self.write(self.fileobj) | ||
print("nope") | ||
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. Remove these prints? 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. 😅 Should we add https://pypi.org/project/flake8-print/ ? |
||
self.close() | ||
|
||
@deprecate_bookmark(bookmark="outline_item", import_bookmarks="import_outline") | ||
def merge( | ||
self, | ||
|
@@ -263,10 +294,6 @@ def write(self, fileobj: StrByteType) -> None: | |
""" | ||
if self.output is None: | ||
raise RuntimeError(ERR_CLOSED_WRITER) | ||
my_file = False | ||
if isinstance(fileobj, str): | ||
fileobj = FileIO(fileobj, "wb") | ||
my_file = True | ||
|
||
# Add pages to the PdfWriter | ||
# The commented out line below was replaced with the two lines below it | ||
|
@@ -285,7 +312,7 @@ def write(self, fileobj: StrByteType) -> None: | |
self._write_outline() | ||
|
||
# Write the output to the file | ||
self.output.write(fileobj) | ||
my_file, fileobj = self.output.write(fileobj) | ||
|
||
if my_file: | ||
fileobj.close() | ||
MartinThoma marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should
fileobj
be allowed to be path-like following from #1190?Additionally, why not make the type
Optional[StrByteType]
and just have it default to None instead of""
?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.
Good idea to allow Path objects 👍
What is the advantage of using
None
?