Skip to content

Commit

Permalink
STY: Apply black
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Apr 10, 2022
1 parent 984841c commit 93a6063
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 43 deletions.
1 change: 1 addition & 0 deletions PyPDF2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
from .merger import PdfFileMerger
from .pagerange import PageRange, parse_filename_page_ranges
from ._version import __version__

__all__ = ["pdf", "PdfFileMerger"]
2 changes: 1 addition & 1 deletion PyPDF2/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.27.2'
__version__ = "1.27.2"
21 changes: 12 additions & 9 deletions Scripts/booklet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

def main():
parser = argparse.ArgumentParser()
parser.add_argument('input', type=argparse.FileType('rb'))
parser.add_argument('output')
parser.add_argument("input", type=argparse.FileType("rb"))
parser.add_argument("output")
args = parser.parse_args()

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

pagesPerSheet = 4
virtualPages = (numPages + pagesPerSheet - 1) // pagesPerSheet * pagesPerSheet
Expand All @@ -31,24 +31,27 @@ def main():
mb = firstPage.mediaBox
pageWidth = 2 * mb.getWidth()
pageHeight = mb.getHeight()
print('Medium size:', '{}x{}'.format(pageWidth, pageHeight))
print("Medium size:", "{}x{}".format(pageWidth, pageHeight))

writer = PyPDF2.PdfFileWriter()

def scale(page):
return min(mb.getWidth() / page.mediaBox.getWidth(), mb.getHeight() / page.mediaBox.getHeight())
return min(
mb.getWidth() / page.mediaBox.getWidth(),
mb.getHeight() / page.mediaBox.getHeight(),
)

def mergePage(dst, src, xOffset):
pageScale = scale(src)
print('scaling by', pageScale)
print("scaling by", pageScale)
dx = (mb.getWidth() - pageScale * src.mediaBox.getWidth()) / 2
dy = (mb.getHeight() - pageScale * src.mediaBox.getHeight()) / 2
dst.mergeScaledTranslatedPage(src, scale(src), xOffset + dx, dy)

def mergePageByNumber(dstPage, pageNumber, xOffset):
if pageNumber >= numPages:
return
print('merging page', pageNumber, 'with offset', xOffset)
print("merging page", pageNumber, "with offset", xOffset)
page = reader.getPage(pageNumber)
mergePage(dstPage, page, xOffset)

Expand All @@ -60,8 +63,8 @@ def mergePageByNumber(dstPage, pageNumber, xOffset):
mergePageByNumber(page, i, offsets[0])
mergePageByNumber(page, virtualPages - i - 1, offsets[1])

writer.write(open(args.output, 'wb'))
writer.write(open(args.output, "wb"))


if __name__ == '__main__':
if __name__ == "__main__":
main()
55 changes: 29 additions & 26 deletions Tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,35 @@ def test_get_images(src, nb_images):
],
)
def test_get_images_raw(strict, with_prev_0, should_fail):
pdf_data = b"%%PDF-1.7\n" \
b"1 0 obj << /Count 1 /Kids [4 0 R] /Type /Pages >> endobj\n" \
b"2 0 obj << >> endobj\n" \
b"3 0 obj << >> endobj\n" \
b"4 0 obj << /Contents 3 0 R /CropBox [0.0 0.0 2550.0 3508.0]" \
b" /MediaBox [0.0 0.0 2550.0 3508.0] /Parent 1 0 R" \
b" /Resources << /Font << >> >>" \
b" /Rotate 0 /Type /Page >> endobj\n" \
b"5 0 obj << /Pages 1 0 R /Type /Catalog >> endobj\n" \
b"xref 1 5\n" \
b"%010d 00000 n\n" \
b"%010d 00000 n\n" \
b"%010d 00000 n\n" \
b"%010d 00000 n\n" \
b"%010d 00000 n\n" \
b"trailer << %s/Root 5 0 R /Size 6 >>\n" \
b"startxref %d\n" \
b"%%%%EOF"
pdf_data = pdf_data % (pdf_data.find(b"1 0 obj"),
pdf_data.find(b"2 0 obj"),
pdf_data.find(b"3 0 obj"),
pdf_data.find(b"4 0 obj"),
pdf_data.find(b"5 0 obj"),
b"/Prev 0 " if with_prev_0 else b"",
pdf_data.find(b"xref"),
)
pdf_data = (
b"%%PDF-1.7\n"
b"1 0 obj << /Count 1 /Kids [4 0 R] /Type /Pages >> endobj\n"
b"2 0 obj << >> endobj\n"
b"3 0 obj << >> endobj\n"
b"4 0 obj << /Contents 3 0 R /CropBox [0.0 0.0 2550.0 3508.0]"
b" /MediaBox [0.0 0.0 2550.0 3508.0] /Parent 1 0 R"
b" /Resources << /Font << >> >>"
b" /Rotate 0 /Type /Page >> endobj\n"
b"5 0 obj << /Pages 1 0 R /Type /Catalog >> endobj\n"
b"xref 1 5\n"
b"%010d 00000 n\n"
b"%010d 00000 n\n"
b"%010d 00000 n\n"
b"%010d 00000 n\n"
b"%010d 00000 n\n"
b"trailer << %s/Root 5 0 R /Size 6 >>\n"
b"startxref %d\n"
b"%%%%EOF"
)
pdf_data = pdf_data % (
pdf_data.find(b"1 0 obj"),
pdf_data.find(b"2 0 obj"),
pdf_data.find(b"3 0 obj"),
pdf_data.find(b"4 0 obj"),
pdf_data.find(b"5 0 obj"),
b"/Prev 0 " if with_prev_0 else b"",
pdf_data.find(b"xref"),
)
pdf_stream = io.BytesIO(pdf_data)
if should_fail:
with pytest.raises(PyPDF2.pdf.utils.PdfReadError):
Expand Down
15 changes: 8 additions & 7 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
# documentation root, use os.path.abspath to make it absolute, like shown here.
import os
import sys

sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath("../"))

# -- Project information -----------------------------------------------------

project = 'PyPDF2'
copyright = '2006 - 2008, Mathieu Fenniak'
author = 'Mathieu Fenniak'
project = "PyPDF2"
copyright = "2006 - 2008, Mathieu Fenniak"
author = "Mathieu Fenniak"


# -- General configuration ---------------------------------------------------
Expand All @@ -37,20 +38,20 @@
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand All @@ -73,4 +74,4 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

0 comments on commit 93a6063

Please sign in to comment.