Skip to content
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

STY: Fix spelling / names #1682

Merged
merged 1 commit into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ coverage.xml
# Docs
docs/_build/

.cspell/

# Files generated by some of the scripts
dont_commit_*.pdf
pypdf-output.pdf
Expand Down
4 changes: 2 additions & 2 deletions pypdf/_cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def build_char_map(
space_width = _default_fonts_space_width[cast(str, ft["/BaseFont"])]
except Exception:
pass
# I conside the space_code is available on one byte
# I consider the space_code is available on one byte
if isinstance(space_code, str):
try: # one byte
sp = space_code.encode("charmap")[0]
Expand Down Expand Up @@ -140,7 +140,7 @@ def parse_encoding(
enc: Union(str, DictionaryObject) = ft["/Encoding"].get_object() # type: ignore
if isinstance(enc, str):
try:
# allready done : enc = NameObject.unnumber(enc.encode()).decode()
# already done : enc = NameObject.unnumber(enc.encode()).decode()
# for #xx decoding
if enc in charset_encoding:
encoding = charset_encoding[enc].copy()
Expand Down
6 changes: 3 additions & 3 deletions pypdf/_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ def decrypt_object(self, obj: PdfObject) -> PdfObject:
elif isinstance(obj, StreamObject):
obj._data = self.stmCrypt.decrypt(obj._data)
elif isinstance(obj, DictionaryObject):
for dictkey, value in list(obj.items()):
obj[dictkey] = self.decrypt_object(value)
for key, value in obj.items():
obj[key] = self.decrypt_object(value)
elif isinstance(obj, ArrayObject):
for i in range(len(obj)):
obj[i] = self.decrypt_object(obj[i])
Expand Down Expand Up @@ -484,7 +484,7 @@ def verify_user_password(
to decrypt the document.

Args:
user_password: The user passwort as a bytes stream
user_password: The user password as a bytes stream
rev: The encryption revision (see PDF standard)
key_size: The size of the key in bytes
o_entry: The owner entry
Expand Down
28 changes: 14 additions & 14 deletions pypdf/_merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def merge(
)

if page_number is None: # deprecated
# The paremter is only marked as Optional as long as
# The parameter is only marked as Optional as long as
# position is not fully deprecated
raise ValueError("page_number may not be None")
if fileobj is None: # deprecated
Expand Down Expand Up @@ -271,8 +271,8 @@ def _create_stream(
fileobj.stream.seek(orig_tell)
elif hasattr(fileobj, "seek") and hasattr(fileobj, "read"):
fileobj.seek(0)
filecontent = fileobj.read()
stream = BytesIO(filecontent)
file_content = fileobj.read()
stream = BytesIO(file_content)
else:
raise NotImplementedError(
"PdfMerger.merge requires an object that PdfReader can parse. "
Expand Down Expand Up @@ -521,14 +521,14 @@ def _write_dests(self) -> None:
if self.output is None:
raise RuntimeError(ERR_CLOSED_WRITER)
for named_dest in self.named_dests:
pageno = None
page_index = None
if "/Page" in named_dest:
for pageno, page in enumerate(self.pages): # noqa: B007
for page_index, page in enumerate(self.pages): # noqa: B007
if page.id == named_dest["/Page"]:
named_dest[NameObject("/Page")] = page.out_pagedata
break

if pageno is not None:
if page_index is not None:
self.output.add_named_destination_object(named_dest)

@deprecation_bookmark(bookmarks="outline")
Expand Down Expand Up @@ -596,21 +596,21 @@ def _write_outline_item_on_page(

def _associate_dests_to_pages(self, pages: List[_MergedPage]) -> None:
for named_dest in self.named_dests:
pageno = None
page_index = None
np = named_dest["/Page"]

if isinstance(np, NumberObject):
continue

for page in pages:
if np.get_object() == page.pagedata.get_object():
pageno = page.id
page_index = page.id

if pageno is None:
if page_index is None:
raise ValueError(
f"Unresolved named destination '{named_dest['/Title']}'"
)
named_dest[NameObject("/Page")] = NumberObject(pageno)
named_dest[NameObject("/Page")] = NumberObject(page_index)

@deprecation_bookmark(bookmarks="outline")
def _associate_outline_items_to_pages(
Expand All @@ -624,18 +624,18 @@ def _associate_outline_items_to_pages(
self._associate_outline_items_to_pages(pages, outline_item)
continue

pageno = None
page_index = None
outline_item_page = outline_item["/Page"]

if isinstance(outline_item_page, NumberObject):
continue

for p in pages:
if outline_item_page.get_object() == p.pagedata.get_object():
pageno = p.id
page_index = p.id

if pageno is not None:
outline_item[NameObject("/Page")] = NumberObject(pageno)
if page_index is not None:
outline_item[NameObject("/Page")] = NumberObject(page_index)

@deprecation_bookmark(bookmark="outline_item")
def find_outline_item(
Expand Down
14 changes: 7 additions & 7 deletions pypdf/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,7 @@ def read(self, stream: StreamType) -> None:
continue
xref_k = sorted(
xref_entry.keys()
) # must ensure ascendant to prevent damange
) # must ensure ascendant to prevent damage
for id in xref_k:
stream.seek(xref_entry[id], 0)
try:
Expand Down Expand Up @@ -1600,10 +1600,10 @@ def _read_standard_xref_table(self, stream: StreamType) -> None:
raise PdfReadError("xref table read error")
read_non_whitespace(stream)
stream.seek(-1, 1)
firsttime = True # check if the first time looking at the xref table
first_time = True # check if the first time looking at the xref table
while True:
num = cast(int, read_object(stream, self))
if firsttime and num != 0:
if first_time and num != 0:
self.xref_index = num
if self.strict:
logger_warning(
Expand All @@ -1612,7 +1612,7 @@ def _read_standard_xref_table(self, stream: StreamType) -> None:
)
# if table not zero indexed, could be due to error from when PDF was created
# which will lead to mismatched indices later on, only warned and corrected if self.strict==True
firsttime = False
first_time = False
read_non_whitespace(stream)
stream.seek(-1, 1)
size = cast(int, read_object(stream, self))
Expand Down Expand Up @@ -1647,7 +1647,7 @@ def _read_standard_xref_table(self, stream: StreamType) -> None:

offset, generation = int(offset_b), int(generation_b)
except Exception:
# if something wrong occured
# if something wrong occurred
if hasattr(stream, "getbuffer"):
buf = bytes(stream.getbuffer()) # type: ignore
else:
Expand Down Expand Up @@ -1695,8 +1695,8 @@ def _read_standard_xref_table(self, stream: StreamType) -> None:
num += 1
read_non_whitespace(stream)
stream.seek(-1, 1)
trailertag = stream.read(7)
if trailertag != b"trailer":
trailer_tag = stream.read(7)
if trailer_tag != b"trailer":
# more xrefs!
stream.seek(-7, 1)
else:
Expand Down
32 changes: 16 additions & 16 deletions pypdf/_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
def _alg32(
password: str,
rev: Literal[2, 3, 4],
keylen: int,
key_length: int,
owner_entry: ByteStringObject,
p_entry: int,
id1_entry: ByteStringObject,
Expand All @@ -68,7 +68,7 @@ def _alg32(
Args:
password: The encryption secret as a bytes-string
rev: The encryption revision (see PDF standard)
keylen:
key_length:
owner_entry:
p_entry: A set of flags specifying which operations shall be permitted
when the document is opened with user access. If bit 2 is set to 1, all other
Expand All @@ -79,7 +79,7 @@ def _alg32(
metadata_encrypt: (Default value = True)

Returns:
An MD5 hash of keylen characters.
An MD5 hash of key_length characters.
"""
# 1. Pad or truncate the password string to exactly 32 bytes. If the
# password string is more than 32 bytes long, use only its first 32 bytes;
Expand Down Expand Up @@ -113,16 +113,16 @@ def _alg32(
# /Length entry.
if rev >= 3:
for _ in range(50):
md5_hash = md5(md5_hash[:keylen]).digest()
md5_hash = md5(md5_hash[:key_length]).digest()
# 9. Set the encryption key to the first n bytes of the output from the
# final MD5 hash, where n is always 5 for revision 2 but, for revision 3 or
# greater, depends on the value of the encryption dictionary's /Length
# entry.
return md5_hash[:keylen]
return md5_hash[:key_length]


def _alg33(
owner_password: str, user_password: str, rev: Literal[2, 3, 4], keylen: int
owner_password: str, user_password: str, rev: Literal[2, 3, 4], key_length: int
) -> bytes:
"""
Implementation of algorithm 3.3 of the PDF standard security handler,
Expand All @@ -132,13 +132,13 @@ def _alg33(
owner_password:
user_password:
rev: The encryption revision (see PDF standard)
keylen:
key_length:

Returns:
A transformed version of the owner and the user password
"""
# steps 1 - 4
key = _alg33_1(owner_password, rev, keylen)
key = _alg33_1(owner_password, rev, key_length)
# 5. Pad or truncate the user password string as described in step 1 of
# algorithm 3.2.
user_password_bytes = b_((user_password + str_(_encryption_padding))[:32])
Expand All @@ -160,14 +160,14 @@ def _alg33(
return val


def _alg33_1(password: str, rev: Literal[2, 3, 4], keylen: int) -> bytes:
def _alg33_1(password: str, rev: Literal[2, 3, 4], key_length: int) -> bytes:
"""
Steps 1-4 of algorithm 3.3.

Args:
password:
rev: The encryption revision (see PDF standard)
keylen:
key_length:

Returns:
A transformed version of the password
Expand All @@ -189,7 +189,7 @@ def _alg33_1(password: str, rev: Literal[2, 3, 4], keylen: int) -> bytes:
# from the final MD5 hash, where n is always 5 for revision 2 but, for
# revision 3 or greater, depends on the value of the encryption
# dictionary's /Length entry.
key = md5_hash[:keylen]
key = md5_hash[:key_length]
return key


Expand Down Expand Up @@ -220,8 +220,8 @@ def _alg34(
# 1. Create an encryption key based on the user password string, as
# described in algorithm 3.2.
rev: Literal[2] = 2
keylen = 5
key = _alg32(password, rev, keylen, owner_entry, p_entry, id1_entry)
key_length = 5
key = _alg32(password, rev, key_length, owner_entry, p_entry, id1_entry)
# 2. Encrypt the 32-byte padding string shown in step 1 of algorithm 3.2,
# using an RC4 encryption function with the encryption key from the
# preceding step.
Expand All @@ -234,7 +234,7 @@ def _alg34(
def _alg35(
password: str,
rev: Literal[2, 3, 4],
keylen: int,
key_length: int,
owner_entry: ByteStringObject,
p_entry: int,
id1_entry: ByteStringObject,
Expand All @@ -248,7 +248,7 @@ def _alg35(
Args:
password:
rev: The encryption revision (see PDF standard)
keylen:
key_length:
owner_entry:
p_entry: A set of flags specifying which operations shall be permitted
when the document is opened with user access. If bit 2 is set to 1, all other
Expand All @@ -263,7 +263,7 @@ def _alg35(
"""
# 1. Create an encryption key based on the user password string, as
# described in Algorithm 3.2.
key = _alg32(password, rev, keylen, owner_entry, p_entry, id1_entry)
key = _alg32(password, rev, key_length, owner_entry, p_entry, id1_entry)
# 2. Initialize the MD5 hash function and pass the 32-byte padding string
# shown in step 1 of Algorithm 3.2 as input to this function.
m = md5()
Expand Down
2 changes: 1 addition & 1 deletion pypdf/generic/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def unnumber(sin: bytes) -> bytes:
sin = sin[:i] + unhexlify(sin[i + 1 : i + 3]) + sin[i + 3 :]
i = sin.find(b"#", i + 1)
except ValueError:
# if the 2 characters after # can not be converted to hexa
# if the 2 characters after # can not be converted to hex
# we change nothing and carry on
i = i + 1
return sin
Expand Down
Loading