Skip to content

Commit

Permalink
MAINT: Use new PEP8 compliant names
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed May 22, 2022
1 parent 84a0366 commit db876fb
Show file tree
Hide file tree
Showing 23 changed files with 1,003 additions and 698 deletions.
66 changes: 33 additions & 33 deletions PyPDF2/_merger.py
Expand Up @@ -30,6 +30,7 @@

from ._page import PageObject
from ._reader import PdfFileReader
from ._utils import StrByteType, str_
from ._writer import PdfFileWriter
from .constants import PagesAttributes as PA
from .generic import (
Expand All @@ -55,7 +56,6 @@
ZoomArgsType,
ZoomArgType,
)
from .utils import StrByteType, str_

ERR_CLOSED_WRITER = "close() was called and thus the writer cannot be used anymore"

Expand Down Expand Up @@ -138,9 +138,9 @@ def merge(

# Find the range of pages to merge.
if pages is None:
pages = (0, reader.getNumPages())
pages = (0, len(reader.pages))
elif isinstance(pages, PageRange):
pages = pages.indices(reader.getNumPages())
pages = pages.indices(len(reader.pages))
elif not isinstance(pages, tuple):
raise TypeError('"pages" must be a tuple of (start, stop[, step])')

Expand All @@ -167,7 +167,7 @@ def merge(

# Gather all the pages that are going to be merged
for i in range(*pages):
pg = reader.getPage(i)
pg = reader.get_page(i)

id = self.id_count
self.id_count += 1
Expand Down Expand Up @@ -271,12 +271,12 @@ def write(self, fileobj: StrByteType) -> None:
# The commented out line below was replaced with the two lines below it
# to allow PdfFileMerger to work with PyPdf 1.13
for page in self.pages:
self.output.addPage(page.pagedata)
pages_obj = cast(Dict[str, Any], self.output._pages.getObject())
page.out_pagedata = self.output.getReference(
pages_obj[PA.KIDS][-1].getObject()
self.output.add_page(page.pagedata)
pages_obj = cast(Dict[str, Any], self.output._pages.get_object())
page.out_pagedata = self.output.get_reference(
pages_obj[PA.KIDS][-1].get_object()
)
# idnum = self.output._objects.index(self.output._pages.getObject()[PA.KIDS][-1].getObject()) + 1
# idnum = self.output._objects.index(self.output._pages.get_object()[PA.KIDS][-1].get_object()) + 1
# page.out_pagedata = IndirectObject(idnum, 0, self.output)

# Once all pages are added, create bookmarks to point at those pages
Expand All @@ -302,7 +302,7 @@ def close(self) -> None:
self.inputs = []
self.output = None

def addMetadata(self, infos: Dict[str, Any]) -> None:
def add_metadata(self, infos: Dict[str, Any]) -> None:
"""
Add custom metadata to the output.
Expand All @@ -312,9 +312,9 @@ def addMetadata(self, infos: Dict[str, Any]) -> None:
"""
if self.output is None:
raise RuntimeError(ERR_CLOSED_WRITER)
self.output.addMetadata(infos)
self.output.add_metadata(infos)

def setPageLayout(self, layout: LayoutType) -> None:
def set_page_layout(self, layout: LayoutType) -> None:
"""
Set the page layout
Expand All @@ -340,9 +340,9 @@ def setPageLayout(self, layout: LayoutType) -> None:
"""
if self.output is None:
raise RuntimeError(ERR_CLOSED_WRITER)
self.output.setPageLayout(layout)
self.output.set_page_layout(layout)

def setPageMode(self, mode: PagemodeType) -> None:
def set_page_mode(self, mode: PagemodeType) -> None:
"""
Set the page mode.
Expand All @@ -366,7 +366,7 @@ def setPageMode(self, mode: PagemodeType) -> None:
"""
if self.output is None:
raise RuntimeError(ERR_CLOSED_WRITER)
self.output.setPageMode(mode)
self.output.set_page_mode(mode)

def _trim_dests(
self,
Expand All @@ -381,8 +381,8 @@ def _trim_dests(
new_dests = []
for key, obj in dests.items():
for j in range(*pages):
if pdf.getPage(j).getObject() == obj["/Page"].getObject():
obj[NameObject("/Page")] = obj["/Page"].getObject()
if pdf.get_page(j).get_object() == obj["/Page"].get_object():
obj[NameObject("/Page")] = obj["/Page"].get_object()
assert str_(key) == str_(obj["/Title"])
new_dests.append(obj)
break
Expand Down Expand Up @@ -410,8 +410,8 @@ def _trim_outline(
else:
prev_header_added = False
for j in range(*pages):
if pdf.getPage(j).getObject() == o["/Page"].getObject():
o[NameObject("/Page")] = o["/Page"].getObject()
if pdf.get_page(j).get_object() == o["/Page"].get_object():
o[NameObject("/Page")] = o["/Page"].get_object()
new_outline.append(o)
prev_header_added = True
break
Expand All @@ -429,7 +429,7 @@ def _write_dests(self) -> None:
break

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

def _write_bookmarks(
self,
Expand All @@ -456,7 +456,7 @@ def _write_bookmarks(
break
if page_no is not None:
del bookmark["/Page"], bookmark["/Type"]
last_added = self.output.addBookmarkDict(bookmark, parent)
last_added = self.output.add_bookmark_dict(bookmark, parent)

def _write_bookmark_on_page(
self, bookmark: Union[Bookmark, Destination], page: _MergedPage
Expand Down Expand Up @@ -531,7 +531,7 @@ def _associate_dests_to_pages(self, pages: List[_MergedPage]) -> None:
continue

for p in pages:
if np.getObject() == p.pagedata.getObject():
if np.get_object() == p.pagedata.get_object():
pageno = p.id

if pageno is not None:
Expand Down Expand Up @@ -559,15 +559,15 @@ def _associate_bookmarks_to_pages(
continue

for p in pages:
if bp.getObject() == p.pagedata.getObject():
if bp.get_object() == p.pagedata.get_object():
pageno = p.id

if pageno is not None:
b[NameObject("/Page")] = NumberObject(pageno)
else:
raise ValueError("Unresolved bookmark '{}'".format(b["/Title"]))

def findBookmark(
def find_bookmark(
self,
bookmark: Dict[str, Any],
root: Optional[OutlinesType] = None,
Expand All @@ -579,7 +579,7 @@ def findBookmark(
if isinstance(b, list):
# b is still an inner node
# (OutlinesType, if recursive types were supported by mypy)
res = self.findBookmark(bookmark, b) # type: ignore
res = self.find_bookmark(bookmark, b) # type: ignore
if res:
return [i] + res
elif b == bookmark or b["/Title"] == bookmark:
Expand All @@ -588,7 +588,7 @@ def findBookmark(

return None

def addBookmark(
def add_bookmark(
self,
title: str,
pagenum: int,
Expand All @@ -597,7 +597,7 @@ def addBookmark(
bold: bool = False,
italic: bool = False,
fit: str = "/Fit",
*args: ZoomArgType
*args: ZoomArgType,
) -> IndirectObject:
"""
Add a bookmark to this PDF file.
Expand All @@ -615,7 +615,7 @@ def addBookmark(
"""
if self.output is None:
raise RuntimeError(ERR_CLOSED_WRITER)
out_pages = cast(Dict[str, Any], self.output.getObject(self.output._pages))
out_pages = cast(Dict[str, Any], self.output.get_object(self.output._pages))
if len(out_pages["/Kids"]) > 0:
page_ref = out_pages["/Kids"][pagenum]
else:
Expand All @@ -635,9 +635,9 @@ def addBookmark(
action.update(
{NameObject("/D"): dest_array, NameObject("/S"): NameObject("/GoTo")}
)
action_ref = self.output._addObject(action)
action_ref = self.output._add_object(action)

outline_ref = self.output.getOutlineRoot()
outline_ref = self.output.get_outline_root()

if parent is None:
parent = outline_ref
Expand All @@ -664,14 +664,14 @@ def addBookmark(
if format:
bookmark.update({NameObject("/F"): NumberObject(format)})

bookmark_ref = self.output._addObject(bookmark)
parent = cast(Bookmark, parent.getObject())
bookmark_ref = self.output._add_object(bookmark)
parent = cast(Bookmark, parent.get_object())
assert parent is not None, "hint for mypy"
parent.addChild(bookmark_ref, self.output)

return bookmark_ref

def addNamedDestination(self, title: str, pagenum: int) -> None:
def add_named_destination(self, title: str, pagenum: int) -> None:
"""
Add a destination to the output.
Expand Down

0 comments on commit db876fb

Please sign in to comment.