Skip to content

Commit 32c4ec0

Browse files
committed
Pleasing ruff
1 parent 1806128 commit 32c4ec0

7 files changed

+65
-66
lines changed

pdfly/cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import pdfly.metadata
1717
import pdfly.pagemeta
1818
import pdfly.rm
19+
import pdfly.uncompress
1920
import pdfly.up2
2021
import pdfly.update_offsets
2122
import pdfly.x2pdf
22-
import pdfly.uncompress
2323

2424

2525
def version_callback(value: bool) -> None:

pdfly/uncompress.py

+52-52
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
"""Module for uncompressing PDF content streams."""
2-
3-
from pathlib import Path
4-
from typing import Optional
5-
import zlib
6-
7-
from pypdf import PdfReader, PdfWriter
8-
from pypdf.generic import IndirectObject, PdfObject
9-
10-
11-
def main(pdf: Path, output: Path) -> None:
12-
reader = PdfReader(pdf)
13-
writer = PdfWriter()
14-
15-
for page in reader.pages:
16-
if "/Contents" in page:
17-
contents: Optional[PdfObject] = page["/Contents"]
18-
if isinstance(contents, IndirectObject):
19-
contents = contents.get_object()
20-
if contents is not None:
21-
if isinstance(contents, list):
22-
for content in contents:
23-
if isinstance(content, IndirectObject):
24-
decompress_content_stream(content)
25-
elif isinstance(contents, IndirectObject):
26-
decompress_content_stream(contents)
27-
writer.add_page(page)
28-
29-
with open(output, "wb") as fp:
30-
writer.write(fp)
31-
32-
orig_size = pdf.stat().st_size
33-
uncomp_size = output.stat().st_size
34-
35-
print(f"Original Size : {orig_size:,}")
36-
print(
37-
f"Uncompressed Size: {uncomp_size:,} ({(uncomp_size / orig_size) * 100:.1f}% of original)"
38-
)
39-
40-
41-
def decompress_content_stream(content: IndirectObject) -> None:
42-
"""Decompress a content stream if it uses FlateDecode."""
43-
if content.get("/Filter") == "/FlateDecode":
44-
try:
45-
compressed_data = content.get_data()
46-
uncompressed_data = zlib.decompress(compressed_data)
47-
content.set_data(uncompressed_data)
48-
del content["/Filter"]
49-
except zlib.error as error:
50-
print(
51-
f"Some content stream with /FlateDecode failed to be decompressed: {error}"
52-
)
1+
"""Module for uncompressing PDF content streams."""
2+
3+
import zlib
4+
from pathlib import Path
5+
from typing import Optional
6+
7+
from pypdf import PdfReader, PdfWriter
8+
from pypdf.generic import IndirectObject, PdfObject
9+
10+
11+
def main(pdf: Path, output: Path) -> None:
12+
reader = PdfReader(pdf)
13+
writer = PdfWriter()
14+
15+
for page in reader.pages:
16+
if "/Contents" in page:
17+
contents: Optional[PdfObject] = page["/Contents"]
18+
if isinstance(contents, IndirectObject):
19+
contents = contents.get_object()
20+
if contents is not None:
21+
if isinstance(contents, list):
22+
for content in contents:
23+
if isinstance(content, IndirectObject):
24+
decompress_content_stream(content)
25+
elif isinstance(contents, IndirectObject):
26+
decompress_content_stream(contents)
27+
writer.add_page(page)
28+
29+
with open(output, "wb") as fp:
30+
writer.write(fp)
31+
32+
orig_size = pdf.stat().st_size
33+
uncomp_size = output.stat().st_size
34+
35+
print(f"Original Size : {orig_size:,}")
36+
print(
37+
f"Uncompressed Size: {uncomp_size:,} ({(uncomp_size / orig_size) * 100:.1f}% of original)"
38+
)
39+
40+
41+
def decompress_content_stream(content: IndirectObject) -> None:
42+
"""Decompress a content stream if it uses FlateDecode."""
43+
if content.get("/Filter") == "/FlateDecode":
44+
try:
45+
compressed_data = content.get_data()
46+
uncompressed_data = zlib.decompress(compressed_data)
47+
content.set_data(uncompressed_data)
48+
del content["/Filter"]
49+
except zlib.error as error:
50+
print(
51+
f"Some content stream with /FlateDecode failed to be decompressed: {error}"
52+
)

pyproject.toml

-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ ignore = [
138138
"SLF001", # Private member accessed
139139
"INP001", # File `docs/conf.py` is part of an implicit namespace package. Add an `__init__.py`.
140140
"FA100", # Missing `from __future__ import annotations`, but uses `typing.Optional`
141-
"I001" #Imports not at the top of the file.
142141
]
143142

144143
[tool.ruff.mccabe]

tests/conftest.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import os
44
from pathlib import Path
55

6-
from fpdf import FPDF
76
import pytest
7+
from fpdf import FPDF
8+
89
from pdfly.cli import entry_point
910

1011
try:
@@ -35,7 +36,7 @@ def run_cli(args):
3536
return error.code
3637

3738

38-
@pytest.fixture
39+
@pytest.fixture()
3940
def two_pages_pdf_filepath(tmp_path):
4041
"A PDF with 2 pages, and a different image on each page"
4142
# Note: prior to v2.7.9, fpdf2 produced incorrect /Resources dicts for each page (cf. fpdf2 PR #1133),
@@ -50,7 +51,7 @@ def two_pages_pdf_filepath(tmp_path):
5051
return pdf_filepath
5152

5253

53-
@pytest.fixture
54+
@pytest.fixture()
5455
def pdf_file_100(tmp_path):
5556
"""A PDF with 100 pages; each has only the page index on it."""
5657
pdf = FPDF()
@@ -65,7 +66,7 @@ def pdf_file_100(tmp_path):
6566
return pdf_filepath
6667

6768

68-
@pytest.fixture
69+
@pytest.fixture()
6970
def pdf_file_abc(tmp_path):
7071
"""A PDF with 100 pages; each has only the page index on it."""
7172
pdf = FPDF()

tests/test_extract_images.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import pytest
2-
31
from .conftest import RESOURCES_ROOT, chdir, run_cli
42

53

tests/test_uncompress.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Tests for the `uncompress` command."""
22

3-
import pytest
43
from pathlib import Path
5-
from pdfly.cli import entry_point
4+
5+
import pytest
6+
from pypdf import PdfReader
67
from typer.testing import CliRunner
78

9+
from pdfly.cli import entry_point
10+
811
runner = CliRunner()
912

1013

@@ -28,8 +31,6 @@ def test_uncompress_all_sample_files(
2831
output_pdf_filepath.exists()
2932
), f"Output PDF {output_pdf_filepath} does not exist."
3033

31-
from pypdf import PdfReader
32-
3334
reader = PdfReader(str(output_pdf_filepath))
3435
for page in reader.pages:
3536
contents = page.get("/Contents")

tests/test_update_offsets.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
Here should only be end-to-end tests.
55
"""
66

7+
import re
78
from pathlib import Path
89

910
import pytest
10-
import re
1111

12-
from .conftest import RESOURCES_ROOT, chdir, run_cli
12+
from .conftest import RESOURCES_ROOT, run_cli
1313

1414

1515
def test_update_offsets(capsys, tmp_path: Path) -> None:

0 commit comments

Comments
 (0)