Skip to content

Commit f1ccef0

Browse files
committed
Fix new Ruff issues
1 parent a402c7b commit f1ccef0

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

ci/schema-copy

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,24 @@
22

33
import json
44
import subprocess # nosec
5+
from pathlib import Path
56

67

78
def _main() -> None:
8-
with open("scan_to_paperless/config_schema.json", encoding="utf-8") as config_schema_file:
9+
with Path("scan_to_paperless/config_schema.json").open(encoding="utf-8") as config_schema_file:
910
config_schema = json.load(config_schema_file)
10-
with open("scan_to_paperless/process_schema.json", encoding="utf-8") as process_schema_file:
11+
with Path("scan_to_paperless/process_schema.json").open(encoding="utf-8") as process_schema_file:
1112
process_schema = json.load(process_schema_file)
1213

13-
for property in ("args", "auto_mask", "contour"):
14-
process_schema["definitions"][property]["properties"] = config_schema["definitions"][property][
14+
for property_ in ("args", "auto_mask", "contour"):
15+
process_schema["definitions"][property_]["properties"] = config_schema["definitions"][property_][
1516
"properties"
1617
]
17-
with open("scan_to_paperless/process_schema.json", "w", encoding="utf-8") as process_schema_file:
18+
with Path("scan_to_paperless/process_schema.json").open("w", encoding="utf-8") as process_schema_file:
1819
json.dump(process_schema, process_schema_file, indent=2)
1920

20-
subprocess.run( # nosec
21-
["pre-commit", "run", "--color=never", "--files=scan_to_paperless/process_schema.json", "prettier"],
21+
subprocess.run( # noqa: S603
22+
["pre-commit", "run", "--color=never", "--files=scan_to_paperless/process_schema.json", "prettier"], # noqa: S607
2223
check=False,
2324
)
2425

scan_to_paperless/add_code.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ def _get_bar_codes_with_open_cv(
149149
cv2.imwrite(
150150
str(dest_filename),
151151
decoded_image[
152-
int(math.floor(min(bbox_y))) : int(math.ceil(max(bbox_y))),
153-
int(math.floor(min(bbox_x))) : int(math.ceil(max(bbox_x))),
152+
math.floor(min(bbox_y)) : math.ceil(max(bbox_y)),
153+
math.floor(min(bbox_x)) : math.ceil(max(bbox_x)),
154154
],
155155
)
156156
founds: list[_FoundCode] = []
@@ -209,8 +209,8 @@ def _get_qr_codes_with_open_cv(
209209
cv2.imwrite(
210210
str(dest_filename),
211211
decoded_image[
212-
int(math.floor(min(bbox_y))) : int(math.ceil(max(bbox_y))),
213-
int(math.floor(min(bbox_x))) : int(math.ceil(max(bbox_x))),
212+
math.floor(min(bbox_y)) : math.ceil(max(bbox_y)),
213+
math.floor(min(bbox_x)) : math.ceil(max(bbox_x)),
214214
],
215215
)
216216

@@ -227,8 +227,8 @@ def _get_qr_codes_with_open_cv(
227227
)
228228
retvals = detector.detectAndDecode(
229229
decoded_image[
230-
int(math.floor(min(bbox_y))) : int(math.ceil(max(bbox_y))),
231-
int(math.floor(min(bbox_x))) : int(math.ceil(max(bbox_x))),
230+
(math.floor(min(bbox_y))) : (math.ceil(max(bbox_y))),
231+
(math.floor(min(bbox_x))) : (math.ceil(max(bbox_x))),
232232
],
233233
)
234234
founds.extend(

scan_to_paperless/process.py

+18-20
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ async def wrapper(context: process_utils.Context) -> None:
204204
context.image = new_image
205205
elapsed_time = time.perf_counter() - start_time
206206
if os.environ.get("TIME", "FALSE") == "TRUE":
207-
print(f"Elapsed time in {self.name}: {int(round(elapsed_time))}s.")
207+
print(f"Elapsed time in {self.name}: {round(elapsed_time)}s.")
208208

209209
if self.progress:
210210
context.save_progress_images(self.name)
@@ -340,8 +340,8 @@ async def _histogram(
340340
points.append(("max_level", max_, histogram_max / 10))
341341

342342
for label, value, pos in points:
343-
if int(round(value)) < len(histogram_data):
344-
hist_value = histogram_data[int(round(value))]
343+
if round(value) < len(histogram_data):
344+
hist_value = histogram_data[round(value)]
345345
axes.annotate(
346346
label,
347347
xy=(value, hist_value),
@@ -507,7 +507,7 @@ async def docrop(context: process_utils.Context) -> None:
507507
margin_vertical = context.get_px_value(
508508
crop_config.setdefault("margin_vertical", schema.MARGIN_VERTICAL_DEFAULT),
509509
)
510-
crop(context, int(round(margin_horizontal)), int(round(margin_vertical)))
510+
crop(context, round(margin_horizontal), round(margin_vertical))
511511

512512

513513
@Process("sharpen")
@@ -616,10 +616,10 @@ def draw_rectangle(image: NpNdarrayInt, contour: tuple[int, int, int, int], bord
616616
color = (0, 255, 0)
617617
opacity = 0.1
618618
x, y, width, height = contour
619-
x = int(round(x))
620-
y = int(round(y))
621-
width = int(round(width))
622-
height = int(round(height))
619+
x = round(x)
620+
y = round(y)
621+
width = round(width)
622+
height = round(height)
623623

624624
sub_img = image[y : y + height, x : x + width]
625625
mask_image = np.zeros(sub_img.shape, dtype=np.uint8)
@@ -689,20 +689,18 @@ def find_limit_contour(
689689
values = np.zeros(image_size)
690690
if vertical:
691691
for x, _, width, height in contours:
692-
x_int = int(round(x))
692+
x_int = round(x)
693693
for value in range(x_int, min(x_int + width, image_size)):
694694
values[value] += height
695695
else:
696696
for _, y, width, height in contours:
697-
y_int = int(round(y))
697+
y_int = round(y)
698698
for value in range(y_int, min(y_int + height, image_size)):
699699
values[value] += width
700700

701701
ranges = zero_ranges(values)
702702

703-
return [
704-
int(round(sum(ranges_) / 2)) for ranges_ in ranges if ranges_[0] != 0 and ranges_[1] != image_size
705-
]
703+
return [round(sum(ranges_) / 2) for ranges_ in ranges if ranges_[0] != 0 and ranges_[1] != image_size]
706704

707705

708706
def find_limits(
@@ -813,7 +811,7 @@ def _find_contours_thresh(
813811
config.setdefault("contour_kernel_size", schema.CONTOUR_KERNEL_SIZE_DEFAULT),
814812
)
815813

816-
kernel_size = int(round(kernel_size / 2))
814+
kernel_size = round(kernel_size / 2)
817815

818816
# Assign a rectangle kernel size
819817
kernel: NpNdarrayInt = np.ones((kernel_size, kernel_size), "uint8")
@@ -1722,7 +1720,7 @@ async def split(
17221720
)
17231721
context.image = cv2.imread(process_file.name)
17241722
if crop_config.setdefault("enabled", schema.CROP_ENABLED_DEFAULT):
1725-
crop(context, int(round(margin_horizontal)), int(round(margin_vertical)))
1723+
crop(context, round(margin_horizontal), round(margin_vertical))
17261724
process_file = tempfile.NamedTemporaryFile( # pylint: disable=consider-using-with
17271725
suffix=".png",
17281726
)
@@ -1959,12 +1957,12 @@ async def _process_code(name: str) -> bool:
19591957
await add_code.add_codes(
19601958
pdf_filename,
19611959
destination_filename,
1962-
dpi=float(os.environ.get("SCAN_CODES_DPI", 200)),
1963-
pdf_dpi=float(os.environ.get("SCAN_CODES_PDF_DPI", 72)),
1960+
dpi=float(os.environ.get("SCAN_CODES_DPI", "200")),
1961+
pdf_dpi=float(os.environ.get("SCAN_CODES_PDF_DPI", "72")),
19641962
font_name=os.environ.get("SCAN_CODES_FONT_NAME", "Helvetica-Bold"),
1965-
font_size=float(os.environ.get("SCAN_CODES_FONT_SIZE", 16)),
1966-
margin_top=float(os.environ.get("SCAN_CODES_MARGIN_TOP", 0)),
1967-
margin_left=float(os.environ.get("SCAN_CODES_MARGIN_LEFT", 2)),
1963+
font_size=float(os.environ.get("SCAN_CODES_FONT_SIZE", "16")),
1964+
margin_top=float(os.environ.get("SCAN_CODES_MARGIN_TOP", "0")),
1965+
margin_left=float(os.environ.get("SCAN_CODES_MARGIN_LEFT", "2")),
19681966
)
19691967
if destination_filename.exists():
19701968
# Remove the source file on success

scan_to_paperless/process_utils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import logging
44
import math
55
import os
6-
from collections.abc import Callable, Sequence
76
from pathlib import Path
87
from typing import TYPE_CHECKING, Any, cast
98

@@ -17,6 +16,8 @@
1716
from scan_to_paperless import process_schema as schema
1817

1918
if TYPE_CHECKING:
19+
from collections.abc import Callable, Sequence
20+
2021
NpNdarrayInt = np.ndarray[tuple[int, ...], np.dtype[np.integer[Any] | np.floating[Any]]]
2122
else:
2223
NpNdarrayInt = np.ndarray
@@ -40,7 +41,7 @@ def rotate_image(image: NpNdarrayInt, angle: float, background: int | tuple[int,
4041
cv2.warpAffine(
4142
image,
4243
rot_mat,
43-
(int(round(height)), int(round(width))),
44+
((round(height)), (round(width))),
4445
borderValue=cast("Sequence[float]", background),
4546
),
4647
)
@@ -58,7 +59,7 @@ def crop_image(
5859
matrix: NpNdarrayInt = np.array([[1.0, 0.0, -x], [0.0, 1.0, -y]])
5960
return cast(
6061
"NpNdarrayInt",
61-
cv2.warpAffine(image, matrix, (int(round(width)), int(round(height))), borderValue=background),
62+
cv2.warpAffine(image, matrix, ((round(width)), (round(height))), borderValue=background),
6263
)
6364

6465

scan_to_paperless/scan.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ def main() -> None:
182182

183183
if mode_config.get("auto_bash", mode_default.get("auto_bash", schema.AUTO_BASH_DEFAULT)):
184184
call([*scanimage, "--batch-start=1", "--batch-increment=2"])
185-
odd = os.listdir(root_folder)
185+
odd = os.iterdir(root_folder)
186186
input("Put your document in the automatic document feeder for the other side, and press enter.")
187187
call(
188188
[
@@ -193,7 +193,7 @@ def main() -> None:
193193
],
194194
)
195195
if mode_config.get("rotate_even", mode_default.get("rotate_even", schema.ROTATE_EVEN_DEFAULT)):
196-
for img in os.listdir(root_folder):
196+
for img in os.iterdir(root_folder):
197197
if img not in odd:
198198
path = root_folder / img
199199
with PIL.Image.open(path) as image:
@@ -211,7 +211,7 @@ def main() -> None:
211211
print(exception)
212212
sys.exit(1)
213213

214-
for img in os.listdir(root_folder):
214+
for img in os.iterdir(root_folder):
215215
if not img.startswith("image-"):
216216
continue
217217
if "dpi" not in args_:
@@ -224,7 +224,7 @@ def main() -> None:
224224
subprocess.call([config.get("viewer", VIEWER_DEFAULT), root_folder]) # noqa: S603
225225

226226
images = []
227-
for img in os.listdir(root_folder):
227+
for img in os.iterdir(root_folder):
228228
if not img.startswith("image-"):
229229
continue
230230
images.append(Path("source") / img)

0 commit comments

Comments
 (0)