Skip to content

Commit 573278e

Browse files
author
Cimon Lucas (LCM)
committed
Pleasing mypy & typing imports under Python 3.8
1 parent c3a6c88 commit 573278e

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

pdfly/update_offsets.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424
update-offsets --verbose --encoding ISO-8859-1 issue-297.pdf issue-297.out.pdf
2525
"""
2626

27-
from collections.abc import Iterable
27+
if sys.version_info >= (3, 9):
28+
List = list
29+
else:
30+
from typing import List
2831
from pathlib import Path
2932
from rich.console import Console
3033
import re
@@ -41,13 +44,13 @@
4144

4245

4346
def update_lines(
44-
lines_in: Iterable[str], encoding: str, console: Console, verbose: bool
45-
) -> Iterable[str]:
47+
lines_in: List[str], encoding: str, console: Console, verbose: bool
48+
) -> List[str]:
4649
"""Iterates over the lines of a pdf-files and updates offsets.
4750
4851
The input is expected to be a pdf without binary-sections.
4952
50-
:param lines_in: An Iterable over the lines including line-breaks.
53+
:param lines_in: A list over the lines including line-breaks.
5154
:param encoding: The encoding, e.g. "iso-8859-1" or "UTF-8".
5255
:param console: Console used to print messages.
5356
:param verbose: True to activate logging of info-messages.
@@ -184,7 +187,12 @@ def update_lines(
184187

185188
for curr_obj, stream_len in map_stream_len.items():
186189
if curr_obj in map_obj_length_line:
187-
m_length = RE_LENGTH.match(map_obj_length_line[curr_obj])
190+
line = map_obj_length_line[curr_obj]
191+
m_length = RE_LENGTH.match(line)
192+
if m_length is None:
193+
raise RuntimeError(
194+
f"Invalid PDF file: line '{line}' does not contain a valid /Length."
195+
)
188196
prev_length = m_length.group(2)
189197
len_digits = len(prev_length)
190198
len_format = "%%0%dd" % len_digits
@@ -229,16 +237,16 @@ def update_lines(
229237
return lines_out
230238

231239

232-
def read_binary_file(file_path: str, encoding: str) -> Iterable[str]:
240+
def read_binary_file(file_path: Path, encoding: str) -> List[str]:
233241
"""Reads a binary file line by line and returns these lines as a list of strings in the given encoding.
234242
Encoding utf-8 can't be used to read random binary data.
235243
236244
:param file_path: file to be read line by line
237245
:param encoding: encoding to be used (e.g. "iso-8859-1")
238246
:return lines including line-breaks
239247
"""
240-
chunks = []
241-
with open(file_path, "rb") as file:
248+
chunks: List[str] = []
249+
with file_path.open("rb") as file:
242250
buffer = bytearray()
243251
while True:
244252
chunk = file.read(4096) # Read in chunks of 4096 bytes
@@ -253,7 +261,7 @@ def read_binary_file(file_path: str, encoding: str) -> Iterable[str]:
253261
if not match:
254262
break # No more line breaks found, process the remaining buffer
255263

256-
start, end = match.start(), match.end()
264+
end = match.end()
257265
chunk_str = buffer[:end].decode(encoding, errors="strict")
258266
buffer = buffer[end:]
259267

@@ -277,4 +285,4 @@ def main(file_in: Path, file_out: Path, encoding: str, verbose: bool) -> None:
277285
for line in lines_out:
278286
f.write(line.encode(encoding))
279287

280-
console.print(f"Wrote {file_out}")
288+
console.print(f"Wrote {file_out}", soft_wrap=True)

tests/conftest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Utilities and fixtures that are available automatically for all tests."""
22

3-
import io, os
3+
import os
44
from pathlib import Path
55

66
from fpdf import FPDF
@@ -58,7 +58,7 @@ def pdf_file_100(tmp_path):
5858
for i in range(100):
5959
pdf.add_page()
6060
pdf.set_font("helvetica", size=12)
61-
pdf.cell(200, 10, txt=f"{i}", ln=True, align="C")
61+
pdf.cell(200, 10, text=f"{i}", ln=True, align="C")
6262

6363
pdf_filepath = tmp_path / "pdf_file_100.pdf"
6464
pdf.output(pdf_filepath)
@@ -73,7 +73,7 @@ def pdf_file_abc(tmp_path):
7373
for char in [chr(i) for i in range(ord("a"), ord("z") + 1)]:
7474
pdf.add_page()
7575
pdf.set_font("helvetica", size=12)
76-
pdf.cell(200, 10, txt=f"{char}", ln=True, align="C")
76+
pdf.cell(200, 10, text=f"{char}", ln=True, align="C")
7777

7878
pdf_filepath = tmp_path / "abc.pdf"
7979
pdf.output(pdf_filepath)

0 commit comments

Comments
 (0)