Skip to content

Commit

Permalink
Use properties more
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 28, 2022
1 parent 5dc9628 commit 9315c98
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 16 deletions.
12 changes: 7 additions & 5 deletions PyPDF2/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1422,11 +1422,13 @@ def getPage(self, pageNumber):
self._flatten()
return self.flattenedPages[pageNumber]

namedDestinations = property(lambda self: self.getNamedDestinations(), None, None)
"""
Read-only property that accesses the
:meth:`getNamedDestinations()<PdfFileReader.getNamedDestinations>` function.
"""
@property
def namedDestinations(self):
"""
Read-only property that accesses the
:meth:`getNamedDestinations()<PdfFileReader.getNamedDestinations>` function.
"""
return self.getNamedDestinations()

# A select group of relevant field attributes. For the complete list,
# see section 8.6.2 of the PDF 1.7 reference.
Expand Down
2 changes: 1 addition & 1 deletion Scripts/2-up.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main():
print("2-up input " + sys.argv[1])
reader = PdfFileReader(sys.argv[1])
writer = PdfFileWriter()
for iter in range(0, reader.getNumPages() - 1, 2):
for iter in range(0, reader.numPages - 1, 2):
lhs = reader.getPage(iter)
rhs = reader.getPage(iter + 1)
lhs.mergeTranslatedPage(rhs, lhs.mediaBox.getUpperRight_x(), 0, True)
Expand Down
2 changes: 1 addition & 1 deletion Scripts/booklet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():
args = parser.parse_args()

reader = PyPDF2.PdfFileReader(args.input)
numPages = reader.getNumPages()
numPages = reader.numPages
print("Pages in file:", numPages)

pagesPerSheet = 4
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_basic_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_basic_features():
reader = PdfFileReader(pdf_path)
writer = PdfFileWriter()

reader.getNumPages()
assert reader.numPages == 1

# add page 1 from input1 to output document, unchanged
writer.addPage(reader.getPage(0))
Expand Down
12 changes: 6 additions & 6 deletions Tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
def test_get_num_pages(src, num_pages):
src = os.path.join(RESOURCE_ROOT, src)
reader = PdfFileReader(src)
assert reader.getNumPages() == num_pages
assert reader.numPages == num_pages


@pytest.mark.parametrize(
Expand Down Expand Up @@ -71,7 +71,7 @@ def test_get_num_pages(src, num_pages):
def test_read_metadata(pdf_path, expected):
with open(pdf_path, "rb") as inputfile:
reader = PdfFileReader(inputfile)
docinfo = reader.getDocumentInfo()
docinfo = reader.documentInfo
metadict = dict(docinfo)
assert metadict == expected
docinfo.title
Expand Down Expand Up @@ -117,7 +117,7 @@ def test_get_attachments(src):
reader = PdfFileReader(src)

attachments = {}
for i in range(reader.getNumPages()):
for i in range(reader.numPages):
page = reader.getPage(i)
if PG.ANNOTS in page:
for annotation in page[PG.ANNOTS]:
Expand Down Expand Up @@ -485,20 +485,20 @@ def test_read_unknown_zero_pages():
pdf_stream = io.BytesIO(pdf_data)
with pytest.raises(PdfReadError) as exc:
reader = PdfFileReader(pdf_stream, strict=True)
reader.getNumPages()
reader.numPages

assert exc.value.args[0] == "Could not find object."
reader = PdfFileReader(pdf_stream, strict=False)
with pytest.raises(AttributeError) as exc:
reader.getNumPages()
reader.numPages
assert exc.value.args[0] == "'NoneType' object has no attribute 'getObject'"


def test_read_encrypted_without_decryption():
src = os.path.join(RESOURCE_ROOT, "libreoffice-writer-password.pdf")
reader = PdfFileReader(src)
with pytest.raises(PdfReadError) as exc:
reader.getNumPages()
reader.numPages
assert exc.value.args[0] == "File has not been decrypted"


Expand Down
2 changes: 1 addition & 1 deletion Tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def test_io_streams():

# Read from bytes stream
reader = PdfFileReader(bytes_stream)
assert reader.getNumPages() == 4
assert reader.numPages == 4

# Write to bytes stream
writer = PdfFileWriter()
Expand Down
2 changes: 1 addition & 1 deletion Tests/test_xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
)
def test_read_xmp(src, has_xmp):
reader = PdfFileReader(src)
xmp = reader.getXmpMetadata()
xmp = reader.xmpMetadata
assert (xmp is None) == (not has_xmp)
if has_xmp:
for el in xmp.getElement(
Expand Down

0 comments on commit 9315c98

Please sign in to comment.