forked from py-pdf/pypdf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Explicitly export PdfFileReader, PdfFileWriter * Implicit string concatenation * Don't leave open file handles * Apply hints from flake8-simplify * Only import stuff that is used
- Loading branch information
1 parent
e45e66b
commit 38d5ec4
Showing
20 changed files
with
86 additions
and
90 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
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
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 |
---|---|---|
|
@@ -41,7 +41,6 @@ | |
__maintainer__ = "Phaseit, Inc." | ||
__maintainer_email = "[email protected]" | ||
|
||
import string | ||
import math | ||
import struct | ||
import sys | ||
|
@@ -57,7 +56,6 @@ | |
else: | ||
from io import BytesIO | ||
|
||
from . import filters | ||
from . import utils | ||
import warnings | ||
import codecs | ||
|
@@ -543,7 +541,6 @@ def _sweepIndirectReferences(self, externMap, data): | |
if debug: print((data, "TYPE", data.__class__.__name__)) | ||
if isinstance(data, DictionaryObject): | ||
for key, value in list(data.items()): | ||
origvalue = value | ||
value = self._sweepIndirectReferences(externMap, value) | ||
if isinstance(value, StreamObject): | ||
# a dictionary value is a stream. streams must be indirect | ||
|
@@ -794,6 +791,11 @@ def removeImages(self, ignoreByteStringObject=False): | |
to ignore ByteString Objects. | ||
""" | ||
pages = self.getObject(self._pages)['/Kids'] | ||
jump_operators = [ | ||
b_('cm'), b_('w'), b_('J'), b_('j'), b_('M'), b_('d'), b_('ri'), b_('i'), | ||
b_('gs'), b_('W'), b_('b'), b_('s'), b_('S'), b_('f'), b_('F'), b_('n'), b_('m'), b_('l'), | ||
b_('c'), b_('v'), b_('y'), b_('h'), b_('B'), b_('Do'), b_('sh') | ||
] | ||
for j in range(len(pages)): | ||
page = pages[j] | ||
pageRef = self.getObject(page) | ||
|
@@ -804,36 +806,29 @@ def removeImages(self, ignoreByteStringObject=False): | |
_operations = [] | ||
seq_graphics = False | ||
for operands, operator in content.operations: | ||
if operator == b_('Tj'): | ||
text = operands[0] | ||
if ignoreByteStringObject: | ||
if not isinstance(text, TextStringObject): | ||
operands[0] = TextStringObject() | ||
elif operator == b_("'"): | ||
if operator in [b_('Tj'), b_("'")]: | ||
text = operands[0] | ||
if ignoreByteStringObject: | ||
if not isinstance(text, TextStringObject): | ||
operands[0] = TextStringObject() | ||
elif operator == b_('"'): | ||
text = operands[2] | ||
if ignoreByteStringObject: | ||
if not isinstance(text, TextStringObject): | ||
operands[2] = TextStringObject() | ||
if ignoreByteStringObject and not isinstance(text, TextStringObject): | ||
operands[2] = TextStringObject() | ||
elif operator == b_("TJ"): | ||
for i in range(len(operands[0])): | ||
if ignoreByteStringObject: | ||
if not isinstance(operands[0][i], TextStringObject): | ||
operands[0][i] = TextStringObject() | ||
if ( | ||
ignoreByteStringObject | ||
and not isinstance(operands[0][i], TextStringObject) | ||
): | ||
operands[0][i] = TextStringObject() | ||
|
||
if operator == b_('q'): | ||
seq_graphics = True | ||
if operator == b_('Q'): | ||
seq_graphics = False | ||
if seq_graphics: | ||
if operator in [b_('cm'), b_('w'), b_('J'), b_('j'), b_('M'), b_('d'), b_('ri'), b_('i'), | ||
b_('gs'), b_('W'), b_('b'), b_('s'), b_('S'), b_('f'), b_('F'), b_('n'), b_('m'), b_('l'), | ||
b_('c'), b_('v'), b_('y'), b_('h'), b_('B'), b_('Do'), b_('sh')]: | ||
continue | ||
if seq_graphics and operator in jump_operators: | ||
continue | ||
if operator == b_('re'): | ||
continue | ||
_operations.append((operands, operator)) | ||
|
@@ -856,41 +851,29 @@ def removeText(self, ignoreByteStringObject=False): | |
if not isinstance(content, ContentStream): | ||
content = ContentStream(content, pageRef) | ||
for operands,operator in content.operations: | ||
if operator == b_('Tj'): | ||
if operator in [b_('Tj'), b_("'")]: | ||
text = operands[0] | ||
if not ignoreByteStringObject: | ||
if isinstance(text, TextStringObject): | ||
operands[0] = TextStringObject() | ||
else: | ||
if isinstance(text, TextStringObject) or \ | ||
isinstance(text, ByteStringObject): | ||
operands[0] = TextStringObject() | ||
elif operator == b_("'"): | ||
text = operands[0] | ||
if not ignoreByteStringObject: | ||
if isinstance(text, TextStringObject): | ||
operands[0] = TextStringObject() | ||
else: | ||
if isinstance(text, TextStringObject) or \ | ||
isinstance(text, ByteStringObject): | ||
if isinstance(text, (TextStringObject, ByteStringObject)): | ||
operands[0] = TextStringObject() | ||
elif operator == b_('"'): | ||
text = operands[2] | ||
if not ignoreByteStringObject: | ||
if isinstance(text, TextStringObject): | ||
operands[2] = TextStringObject() | ||
else: | ||
if isinstance(text, TextStringObject) or \ | ||
isinstance(text, ByteStringObject): | ||
if isinstance(text, (TextStringObject, ByteStringObject)): | ||
operands[2] = TextStringObject() | ||
elif operator == b_("TJ"): | ||
for i in range(len(operands[0])): | ||
if not ignoreByteStringObject: | ||
if isinstance(operands[0][i], TextStringObject): | ||
operands[0][i] = TextStringObject() | ||
else: | ||
if isinstance(operands[0][i], TextStringObject) or \ | ||
isinstance(operands[0][i], ByteStringObject): | ||
if isinstance(operands[0][i], (TextStringObject, ByteStringObject)): | ||
operands[0][i] = TextStringObject() | ||
|
||
pageRef.__setitem__(NameObject('/Contents'), content) | ||
|
@@ -1172,9 +1155,8 @@ def _showwarning(message, category, filename, lineno, file=warndest, line=None): | |
if hasattr(stream, 'mode') and 'b' not in stream.mode: | ||
warnings.warn("PdfFileReader stream/file object is not in binary mode. It may not be read correctly.", utils.PdfReadWarning) | ||
if isString(stream): | ||
fileobj = open(stream, 'rb') | ||
stream = BytesIO(b_(fileobj.read())) | ||
fileobj.close() | ||
with open(stream, 'rb') as fileobj: | ||
stream = BytesIO(b_(fileobj.read())) | ||
self.read(stream) | ||
self.stream = stream | ||
|
||
|
@@ -1729,7 +1711,7 @@ def getObject(self, indirectReference): | |
return retval | ||
|
||
def _decryptObject(self, obj, key): | ||
if isinstance(obj, ByteStringObject) or isinstance(obj, TextStringObject): | ||
if isinstance(obj, (ByteStringObject, TextStringObject)): | ||
obj = createStringObject(utils.RC4_encrypt(key, obj.original_bytes)) | ||
elif isinstance(obj, StreamObject): | ||
obj._data = utils.RC4_encrypt(key, obj._data) | ||
|
@@ -1752,7 +1734,10 @@ def readObjectHeader(self, stream): | |
idnum = readUntilWhitespace(stream) | ||
extra |= utils.skipOverWhitespace(stream); stream.seek(-1, 1) | ||
generation = readUntilWhitespace(stream) | ||
obj = stream.read(3) | ||
|
||
# although it's not used, it might still be necessary to read | ||
_obj = stream.read(3) # noqa: F841 | ||
|
||
readNonWhitespace(stream) | ||
stream.seek(-1, 1) | ||
if (extra and self.strict): | ||
|
@@ -1938,8 +1923,8 @@ def used_before(num, generation): | |
# The rest of the elements depend on the xref_type | ||
if xref_type == 0: | ||
# linked list of free objects | ||
next_free_object = getEntry(1) | ||
next_generation = getEntry(2) | ||
next_free_object = getEntry(1) # noqa: F841 | ||
next_generation = getEntry(2) # noqa: F841 | ||
elif xref_type == 1: | ||
# objects that are in use but are not compressed | ||
byte_offset = getEntry(1) | ||
|
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
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
Oops, something went wrong.