Skip to content

Commit

Permalink
STY: Apply fixes suggested by pylint
Browse files Browse the repository at this point in the history
* Use f-strings: They are more readable and faster than other
                 string formatting options
  • Loading branch information
MartinThoma committed Jun 16, 2022
1 parent 6ce36f7 commit 141c787
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
5 changes: 0 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ repos:
hooks:
- id: black
args: [--target-version, py36]
# - repo: https://github.com/asottile/pyupgrade
# rev: v2.31.1
# hooks:
# - id: pyupgrade
# args: [--py36-plus]
- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
hooks:
Expand Down
9 changes: 9 additions & 0 deletions PyPDF2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
"""
PyPDF2 is a free and open-source pure-python PDF library capable of splitting,
merging, cropping, and transforming the pages of PDF files. It can also add
custom data, viewing options, and passwords to PDF files. PyPDF2 can retrieve
text and metadata from PDFs as well.
You can read the full docs at https://pypdf2.readthedocs.io/.
"""

from ._merger import PdfFileMerger, PdfMerger
from ._page import PageObject, Transformation
from ._reader import DocumentInformation, PdfFileReader, PdfReader
Expand Down
6 changes: 3 additions & 3 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ def read(self, stream: StreamType) -> None:
for id in self.xref[gen]:
stream.seek(self.xref[gen][id], 0)
try:
pid, pgen = self.read_object_header(stream)
pid, _pgen = self.read_object_header(stream)
except ValueError:
break
if pid == id - self.xref_index:
Expand Down Expand Up @@ -1678,8 +1678,8 @@ def _decrypt(self, password: Union[str, bytes]) -> int:
val = real_O
for i in range(19, -1, -1):
new_key = b""
for l in range(len(key)):
new_key += b_(chr(ord_(key[l]) ^ i))
for key_char in key:
new_key += b_(chr(ord_(key_char) ^ i))
val = RC4_encrypt(new_key, val)
userpass = val
owner_password, key = self._authenticate_user_password(userpass)
Expand Down
8 changes: 4 additions & 4 deletions PyPDF2/_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ def _alg33(owner_pwd: str, user_pwd: str, rev: int, keylen: int) -> bytes:
if rev >= 3:
for i in range(1, 20):
new_key = ""
for l in range(len(key)):
new_key += chr(ord_(key[l]) ^ i)
for key_char in key:
new_key += chr(ord_(key_char) ^ i)
val = RC4_encrypt(new_key, val)
# 8. Store the output from the final invocation of the RC4 as the value of
# the /O entry in the encryption dictionary.
Expand Down Expand Up @@ -233,10 +233,10 @@ def RC4_encrypt(key: Union[str, bytes], plaintext: bytes) -> bytes:
S[i], S[j] = S[j], S[i]
i, j = 0, 0
retval = []
for x in range(len(plaintext)):
for plaintext_char in plaintext:
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
t = S[(S[i] + S[j]) % 256]
retval.append(b_(chr(ord_(plaintext[x]) ^ t)))
retval.append(b_(chr(ord_(plaintext_char) ^ t)))
return b"".join(retval)
21 changes: 8 additions & 13 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def get_page(self, pageNumber: int) -> PageObject: # TODO: PEP8
:rtype: :class:`PageObject<PyPDF2._page.PageObject>`
"""
pages = cast(Dict[str, Any], self.get_object(self._pages))
# XXX: crude hack
# TODO: crude hack
return pages[PA.KIDS][pageNumber].get_object()

def getPage(self, pageNumber: int) -> PageObject: # pragma: no cover
Expand Down Expand Up @@ -693,10 +693,8 @@ def write(self, stream: StreamType) -> None:
"""
if hasattr(stream, "mode") and "b" not in stream.mode:
warnings.warn(
(
"File <{}> to write to is not in binary mode. " # type: ignore
"It may not be written to correctly."
).format(stream.name)
f"File <{stream.name}> to write to is not in binary mode. "
"It may not be written to correctly."
)

if not self._root:
Expand All @@ -712,8 +710,7 @@ def write(self, stream: StreamType) -> None:
# we sweep for indirect references. This forces self-page-referencing
# trees to reference the correct new object location, rather than
# copying in a new copy of the page object.
for obj_index in range(len(self._objects)):
obj = self._objects[obj_index]
for obj_index, obj in enumerate(self._objects):
if isinstance(obj, PageObject) and obj.indirect_ref is not None:
data = obj.indirect_ref
if data.pdf not in external_reference_map:
Expand All @@ -731,13 +728,13 @@ def write(self, stream: StreamType) -> None:
object_positions = self._write_header(stream)
xref_location = self._write_xref_table(stream, object_positions)
self._write_trailer(stream)
stream.write(b_("\nstartxref\n%s\n%%%%EOF\n" % (xref_location))) # eof
stream.write(b_(f"\nstartxref\n{xref_location}\n%%EOF\n")) # eof

def _write_header(self, stream: StreamType) -> List[int]:
object_positions = []
stream.write(self._header + b"\n")
stream.write(b"%\xE2\xE3\xCF\xD3\n")
for i in range(len(self._objects)):
for i, obj in enumerate(self._objects):
obj = self._objects[i]
# If the obj is None we can't write anything
if obj is not None:
Expand Down Expand Up @@ -1236,8 +1233,7 @@ def remove_images(self, ignore_byte_string_object: bool = False) -> None:
b"Do",
b"sh",
)
for j in range(len(pages)):
page = pages[j]
for page in pages:
page_ref = cast(DictionaryObject, self.get_object(page))
content = page_ref["/Contents"].get_object()
if not isinstance(content, ContentStream):
Expand Down Expand Up @@ -1298,8 +1294,7 @@ def remove_text(self, ignore_byte_string_object: bool = False) -> None:
"""
pg_dict = cast(DictionaryObject, self.get_object(self._pages))
pages = cast(List[IndirectObject], pg_dict[PA.KIDS])
for j in range(len(pages)):
page = pages[j]
for page in pages:
page_ref = cast(Dict[str, Any], self.get_object(page))
content = page_ref["/Contents"].get_object()
if not isinstance(content, ContentStream):
Expand Down
3 changes: 1 addition & 2 deletions PyPDF2/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ def next_code(self) -> int:
return -1
nextbits = ord_(self.data[self.bytepos])
bitsfromhere = 8 - self.bitpos
if bitsfromhere > fillbits:
bitsfromhere = fillbits
bitsfromhere = min(bitsfromhere, fillbits)
value |= (
(nextbits >> (8 - self.bitpos - bitsfromhere))
& (0xFF >> (8 - bitsfromhere))
Expand Down
3 changes: 1 addition & 2 deletions PyPDF2/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1887,8 +1887,7 @@ def createStringObject(
elif isinstance(forced_encoding, str):
if forced_encoding == "bytes":
return ByteStringObject(string)
else:
return TextStringObject(string.decode(forced_encoding))
return TextStringObject(string.decode(forced_encoding))
else:
try:
if string.startswith(codecs.BOM_UTF16_BE):
Expand Down

0 comments on commit 141c787

Please sign in to comment.