Skip to content

Commit 342b883

Browse files
committed
Pleasing ruff
1 parent e42efd5 commit 342b883

8 files changed

+52
-140
lines changed

pdfly/cat.py

+12-23
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
# All rights reserved. This software is available under a BSD license;
4343
# see https://github.com/py-pdf/pypdf/LICENSE
4444

45-
4645
import os
4746
import sys
4847
import traceback
@@ -59,9 +58,7 @@ def main(
5958
verbose: bool,
6059
inverted_page_selection: bool = False,
6160
) -> None:
62-
filename_page_ranges = parse_filepaths_and_pagerange_args(
63-
filename, fn_pgrgs
64-
)
61+
filename_page_ranges = parse_filepaths_and_pagerange_args(filename, fn_pgrgs)
6562
if output:
6663
output_fh = open(output, "wb")
6764
else:
@@ -71,22 +68,16 @@ def main(
7168
writer = PdfWriter()
7269
in_fs = {}
7370
try:
74-
for filename, page_range in filename_page_ranges: # type: ignore
71+
for filepath, page_range in filename_page_ranges: # type: ignore
7572
if verbose:
76-
print(filename, page_range, file=sys.stderr)
77-
if filename not in in_fs:
78-
in_fs[filename] = open(filename, "rb")
73+
print(filepath, page_range, file=sys.stderr)
74+
if filepath not in in_fs:
75+
in_fs[filepath] = open(filepath, "rb")
7976

80-
reader = PdfReader(in_fs[filename])
77+
reader = PdfReader(in_fs[filepath])
8178
num_pages = len(reader.pages)
8279
start, end, step = page_range.indices(num_pages)
83-
if (
84-
start < 0
85-
or end < 0
86-
or start >= num_pages
87-
or end > num_pages
88-
or start > end
89-
):
80+
if start < 0 or end < 0 or start >= num_pages or end > num_pages or start > end:
9081
print(
9182
f"WARNING: Page range {page_range} is out of bounds",
9283
file=sys.stderr,
@@ -111,17 +102,15 @@ def main(
111102
# Not closing the in_fs because this script exits now.
112103

113104

114-
def parse_filepaths_and_pagerange_args(
115-
filename: Path, fn_pgrgs: List[str]
116-
) -> List[Tuple[Path, PageRange]]:
105+
def parse_filepaths_and_pagerange_args(filename: Path, fn_pgrgs: List[str]) -> List[Tuple[Path, PageRange]]:
117106
fn_pgrgs_l = list(fn_pgrgs)
118107
fn_pgrgs_l.insert(0, str(filename))
119108
filename_page_ranges, invalid_filepaths = [], []
120-
for filename, page_range in parse_filename_page_ranges(fn_pgrgs_l): # type: ignore
121-
if Path(filename).is_file():
122-
filename_page_ranges.append((filename, page_range))
109+
for filepath, page_range in parse_filename_page_ranges(fn_pgrgs_l): # type: ignore
110+
if Path(filepath).is_file():
111+
filename_page_ranges.append((filepath, page_range))
123112
else:
124-
invalid_filepaths.append(str(filename))
113+
invalid_filepaths.append(str(filepath))
125114
if invalid_filepaths:
126115
print(
127116
f"Invalid file path or page range provided: {' '.join(invalid_filepaths)}",

pdfly/cli.py

+6-14
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def version_callback(value: bool) -> None:
3333

3434
entry_point = typer.Typer(
3535
add_completion=False,
36-
help=(
37-
"pdfly is a pure-python cli application for manipulating PDF files."
38-
),
36+
help=("pdfly is a pure-python cli application for manipulating PDF files."),
3937
rich_markup_mode="rich", # Allows to pretty-print commands documentation
4038
)
4139

@@ -57,7 +55,7 @@ def extract_images(
5755
exists=True,
5856
resolve_path=True,
5957
),
60-
]
58+
],
6159
) -> None:
6260
pdfly.extract_images.main(pdf)
6361

@@ -91,9 +89,7 @@ def cat(
9189
fn_pgrgs: List[str] = typer.Argument( # noqa
9290
..., help="filenames and/or page ranges"
9391
),
94-
verbose: bool = typer.Option(
95-
False, help="show page ranges as they are being read"
96-
),
92+
verbose: bool = typer.Option(False, help="show page ranges as they are being read"),
9793
) -> None:
9894
pdfly.cat.main(filename, fn_pgrgs, output, verbose)
9995

@@ -112,9 +108,7 @@ def rm(
112108
fn_pgrgs: List[str] = typer.Argument( # noqa
113109
..., help="filenames and/or page ranges"
114110
),
115-
verbose: bool = typer.Option(
116-
False, help="show page ranges as they are being read"
117-
),
111+
verbose: bool = typer.Option(False, help="show page ranges as they are being read"),
118112
) -> None:
119113
pdfly.rm.main(filename, fn_pgrgs, output, verbose)
120114

@@ -175,7 +169,7 @@ def extract_text(
175169
exists=True,
176170
resolve_path=True,
177171
),
178-
]
172+
],
179173
) -> None:
180174
"""Extract text from a PDF file."""
181175
from pypdf import PdfReader
@@ -240,9 +234,7 @@ def update_offsets(
240234
"ISO-8859-1",
241235
help="Encoding used to read and write the files, e.g. UTF-8.",
242236
),
243-
verbose: bool = typer.Option(
244-
False, help="Show progress while processing."
245-
),
237+
verbose: bool = typer.Option(False, help="Show progress while processing."),
246238
) -> None:
247239
pdfly.update_offsets.main(file_in, file_out, encoding, verbose)
248240

pdfly/metadata.py

+11-35
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ def main(pdf: Path, output: OutputOptions) -> None:
9494
creation_time=datetime.fromtimestamp(pdf_stat.st_ctime),
9595
modification_time=datetime.fromtimestamp(pdf_stat.st_mtime),
9696
access_time=datetime.fromtimestamp(pdf_stat.st_atime),
97-
images=[
98-
len(image.data)
99-
for page in reader.pages
100-
for image in page.images
101-
],
97+
images=[len(image.data) for page in reader.pages for image in page.images],
10298
)
10399
if info is not None:
104100
meta.author = info.author
@@ -117,9 +113,7 @@ def main(pdf: Path, output: OutputOptions) -> None:
117113
from rich.table import Table
118114

119115
table = Table(title="PDF Data")
120-
table.add_column(
121-
"Attribute", justify="right", style="cyan", no_wrap=True
122-
)
116+
table.add_column("Attribute", justify="right", style="cyan", no_wrap=True)
123117
table.add_column("Value", style="white")
124118

125119
if meta.title:
@@ -149,21 +143,13 @@ def main(pdf: Path, output: OutputOptions) -> None:
149143
emb, unemb = page._get_fonts()
150144
embedded_fonts = embedded_fonts.union(set(emb))
151145
unemedded_fonts = unemedded_fonts.union(set(unemb))
152-
table.add_row(
153-
"Fonts (unembedded)", ", ".join(sorted(unemedded_fonts))
154-
)
155-
table.add_row(
156-
"Fonts (embedded)", ", ".join(sorted(embedded_fonts))
157-
)
146+
table.add_row("Fonts (unembedded)", ", ".join(sorted(unemedded_fonts)))
147+
table.add_row("Fonts (embedded)", ", ".join(sorted(embedded_fonts)))
158148
table.add_row("Attachments", meta.attachments)
159-
table.add_row(
160-
"Images", f"{len(meta.images)} images ({sum(meta.images):,} bytes)"
161-
)
149+
table.add_row("Images", f"{len(meta.images)} images ({sum(meta.images):,} bytes)")
162150

163151
enc_table = Table(title="Encryption information")
164-
enc_table.add_column(
165-
"Attribute", justify="right", style="cyan", no_wrap=True
166-
)
152+
enc_table.add_column("Attribute", justify="right", style="cyan", no_wrap=True)
167153
enc_table.add_column("Value", style="white")
168154
if meta.encryption:
169155
enc_table.add_row(
@@ -173,28 +159,18 @@ def main(pdf: Path, output: OutputOptions) -> None:
173159
enc_table.add_row("V value", str(meta.encryption.v_value))
174160

175161
os_table = Table(title="Operating System Data")
176-
os_table.add_column(
177-
"Attribute", justify="right", style="cyan", no_wrap=True
178-
)
162+
os_table.add_column("Attribute", justify="right", style="cyan", no_wrap=True)
179163
os_table.add_column("Value", style="white")
180164
os_table.add_row("File Name", f"{pdf}")
181165
os_table.add_row("File Permissions", f"{meta.file_permissions}")
182166
os_table.add_row("File Size", f"{meta.file_size:,} bytes")
183-
os_table.add_row(
184-
"Creation Time", f"{meta.creation_time:%Y-%m-%d %H:%M:%S}"
185-
)
186-
os_table.add_row(
187-
"Modification Time", f"{meta.modification_time:%Y-%m-%d %H:%M:%S}"
188-
)
189-
os_table.add_row(
190-
"Access Time", f"{meta.access_time:%Y-%m-%d %H:%M:%S}"
191-
)
167+
os_table.add_row("Creation Time", f"{meta.creation_time:%Y-%m-%d %H:%M:%S}")
168+
os_table.add_row("Modification Time", f"{meta.modification_time:%Y-%m-%d %H:%M:%S}")
169+
os_table.add_row("Access Time", f"{meta.access_time:%Y-%m-%d %H:%M:%S}")
192170

193171
console = Console()
194172
console.print(os_table)
195173
console.print(table)
196174
if meta.encryption:
197175
console.print(enc_table)
198-
console.print(
199-
"Use the 'pagemeta' subcommand to get details about a single page"
200-
)
176+
console.print("Use the 'pagemeta' subcommand to get details about a single page")

pdfly/pagemeta.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ def main(pdf: Path, page_index: int, output: OutputOptions) -> None:
3838
console = Console()
3939

4040
table = Table(title=f"{pdf}, page index {page_index}")
41-
table.add_column(
42-
"Attribute", justify="right", style="cyan", no_wrap=True
43-
)
41+
table.add_column("Attribute", justify="right", style="cyan", no_wrap=True)
4442
table.add_column("Value", style="white")
4543

4644
table.add_row(
@@ -57,9 +55,7 @@ def main(pdf: Path, page_index: int, output: OutputOptions) -> None:
5755
)
5856
table.add_row(
5957
"artbox",
60-
f"{meta.artbox}: "
61-
f"with={meta.artbox[2] - meta.artbox[0]} "
62-
f"x height={meta.artbox[3] - meta.artbox[1]}",
58+
f"{meta.artbox}: with={meta.artbox[2] - meta.artbox[0]} x height={meta.artbox[3] - meta.artbox[1]}",
6359
)
6460
table.add_row(
6561
"bleedbox",

pdfly/rm.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,5 @@
4444
from pdfly.cat import main as cat_main
4545

4646

47-
def main(
48-
filename: Path, fn_pgrgs: List[str], output: Path, verbose: bool
49-
) -> None:
47+
def main(filename: Path, fn_pgrgs: List[str], output: Path, verbose: bool) -> None:
5048
cat_main(filename, fn_pgrgs, output, verbose, inverted_page_selection=True)

pdfly/uncompress.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ def main(pdf: Path, output: Path) -> None:
3333
uncomp_size = output.stat().st_size
3434

3535
print(f"Original Size : {orig_size:,}")
36-
print(
37-
f"Uncompressed Size: {uncomp_size:,} ({(uncomp_size / orig_size) * 100:.1f}% of original)"
38-
)
36+
print(f"Uncompressed Size: {uncomp_size:,} ({(uncomp_size / orig_size) * 100:.1f}% of original)")
3937

4038

4139
def decompress_content_stream(content: IndirectObject) -> None:
@@ -47,6 +45,4 @@ def decompress_content_stream(content: IndirectObject) -> None:
4745
content.set_data(uncompressed_data)
4846
del content["/Filter"]
4947
except zlib.error as error:
50-
print(
51-
f"Some content stream with /FlateDecode failed to be decompressed: {error}"
52-
)
48+
print(f"Some content stream with /FlateDecode failed to be decompressed: {error}")

0 commit comments

Comments
 (0)