Skip to content

Commit 9097bde

Browse files
committed
Fix new Ruff issues
1 parent eea9462 commit 9097bde

File tree

6 files changed

+38
-37
lines changed

6 files changed

+38
-37
lines changed

scan_to_paperless/process.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ async def transform(
14631463
images_path.append(img2)
14641464
process_count = context.process_count
14651465

1466-
from scan_to_paperless import jupyter # pylint: disable=import-outside-toplevel
1466+
from scan_to_paperless import jupyter # noqa: PLC0415, RUF100
14671467

14681468
jupyter.create_transform_notebook(root_folder, context, step)
14691469

@@ -1683,7 +1683,7 @@ async def split(
16831683
else:
16841684
vertical_value = width
16851685
vertical_margin = 0
1686-
process_file = tempfile.NamedTemporaryFile( # pylint: disable=consider-using-with
1686+
process_file = tempfile.NamedTemporaryFile( # noqa: SIM115
16871687
suffix=".png",
16881688
)
16891689
await call(
@@ -1721,7 +1721,7 @@ async def split(
17211721
context.image = cv2.imread(process_file.name)
17221722
if crop_config.setdefault("enabled", schema.CROP_ENABLED_DEFAULT):
17231723
crop(context, round(margin_horizontal), round(margin_vertical))
1724-
process_file = tempfile.NamedTemporaryFile( # pylint: disable=consider-using-with
1724+
process_file = tempfile.NamedTemporaryFile( # noqa: SIM115
17251725
suffix=".png",
17261726
)
17271727
cv2.imwrite(process_file.name, context.image) # type: ignore[arg-type]
@@ -1952,7 +1952,7 @@ async def _process_code(name: str) -> bool:
19521952
try:
19531953
if pdf_filename.exists():
19541954
_LOG.info("Processing codes for %s", pdf_filename)
1955-
from scan_to_paperless import add_code # pylint: disable=import-outside-toplevel
1955+
from scan_to_paperless import add_code # noqa: PLC0415, RUF100
19561956

19571957
await add_code.add_codes(
19581958
pdf_filename,

scan_to_paperless/process_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ def save_progress_images(
307307
if image is None:
308308
return None
309309

310-
from IPython.display import ( # pylint: disable=import-outside-toplevel
310+
from IPython.display import ( # noqa: PLC0415, RUF100
311311
display,
312312
)
313313

@@ -349,7 +349,7 @@ def save_progress_images(
349349
def display_image(self, image: NpNdarrayInt) -> None:
350350
"""Display the image."""
351351
if scan_to_paperless.jupyter_utils.is_ipython():
352-
from IPython.display import ( # pylint: disable=import-outside-toplevel
352+
from IPython.display import ( # noqa: PLC0415, RUF100
353353
display,
354354
)
355355

scan_to_paperless/scan_sane.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def _all_options(device: sane.SaneDev) -> None:
9292
value = getattr(device, name)
9393
if value is not None:
9494
print(f"Default value: {value}")
95-
except: # pylint: disable=bare-except
95+
except: # noqa: S110
9696
pass
9797
print()
9898

scan_to_paperless/status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def write(self) -> None:
435435
if self.no_write:
436436
return
437437

438-
import natsort # pylint: disable=import-outside-toplevel
438+
import natsort # noqa: PLC0415, RUF100
439439

440440
with self._file.open("w", encoding="utf-8") as status_file:
441441
env = jinja2.Environment(

tests/test_process.py

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import os.path
1+
import os
22
import re
33
import shutil
44
import subprocess
@@ -29,7 +29,7 @@ def test_should_not_commit() -> None:
2929

3030
# @pytest.mark.skip(reason="for test")
3131
def test_find_lines() -> None:
32-
lines = process.find_lines(load_image("limit-lines-1.png"), True, {})
32+
lines = process.find_lines(load_image("limit-lines-1.png"), vertical=True, config={})
3333
assert 1821 in [line[0] for line in lines]
3434

3535

@@ -38,7 +38,7 @@ def test_find_limit_contour() -> None:
3838
context = process_utils.Context({"args": {}}, {})
3939
context.image = load_image("limit-contour-1.png")
4040
contours = process.find_contours(context.image, context, "limit", {})
41-
limits = process.find_limit_contour(context.image, True, contours)
41+
limits = process.find_limit_contour(context.image, vertical=True, contours=contours)
4242
assert limits == [1589]
4343

4444

@@ -199,7 +199,7 @@ async def test_assisted_split_full(type_, limit, better_value, cut_white) -> Non
199199
assert step["name"] == "split"
200200
images = step["sources"]
201201
assert len(images) == 1
202-
assert os.path.basename(images[0]) == config["assisted_split"][0]["image"]
202+
assert Path(images[0]).name == Path(config["assisted_split"][0]["image"]).name
203203
print(f"Compare '{images[0]}' with expected image 'assisted-split-{type_}-1.expected.png'.")
204204
check_image_file(
205205
str(root_folder),
@@ -290,11 +290,11 @@ async def test_assisted_split_join_full() -> None:
290290
"sources": ["image-1.png", "image-2.png"],
291291
}
292292
config_file_name = root_folder / "config.yaml"
293-
step = await process.transform(config, step, config_file_name, root_folder)
293+
step = await process.transform(config, step, str(config_file_name), root_folder)
294294
assert step["name"] == "split"
295295
images = step["sources"]
296-
assert os.path.basename(images[0]) == config["assisted_split"][0]["image"]
297296
assert len(images) == 2
297+
assert Path(images[0]).name == Path(config["assisted_split"][0]["image"]).name
298298
for number, elements in enumerate(
299299
[
300300
({"value": 738, "vertical": True, "margin": 0}, ["-", "1.2"]),
@@ -432,7 +432,7 @@ async def test_full(progress) -> None:
432432
"images": [str(Path(__file__).parent / "all-1.png")],
433433
}
434434
step = {"sources": [str(Path(__file__).parent / "all-1.png")]}
435-
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder)
435+
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder) # noqa: S108
436436
assert len(step["sources"]) == 1
437437
print(f"Compare '{step['sources'][0]}' with expected image 'all-1.expected.png'.")
438438
check_image_file(
@@ -508,7 +508,7 @@ async def test_credit_card_full() -> None:
508508
str(Path(__file__).parent / "credit-card-2.png"),
509509
],
510510
}
511-
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder)
511+
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder) # noqa: S108
512512
assert len(step["sources"]) == 2
513513
assert step["name"] == "finalize"
514514
await process.finalize(config, step, root_folder)
@@ -558,7 +558,7 @@ async def test_empty() -> None:
558558
str(Path(__file__).parent / "empty.png"),
559559
],
560560
}
561-
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder)
561+
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder) # noqa: S108
562562
assert len(step["sources"]) == 0
563563
assert step["name"] == "finalize"
564564
shutil.rmtree(root_folder)
@@ -577,7 +577,7 @@ async def test_custom_process(test: str, args: dict[str, Any]) -> None:
577577
root_folder.mkdir(parents=True, exist_ok=True)
578578
config = {"args": args}
579579
step = {"sources": [str(Path(__file__).parent / f"{test}.png")]}
580-
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder)
580+
step = await process.transform(config, step, "/tmp/test-config.yaml", root_folder) # noqa: S108
581581
assert len(step["sources"]) == 1
582582
try:
583583
print(f"Compare '{step['sources'][0]}' with expected image '{test}.expected.png'.")
@@ -607,8 +607,8 @@ async def test_qr_code(name) -> None:
607607
await add_code.add_codes(Path(__file__).parent / f"{name}.pdf", Path(f"/results/{name}.pdf"))
608608
root_folder = Path("/results/qrcode")
609609
for page in range(2):
610-
subprocess.run(
611-
[
610+
subprocess.run( # noqa: S603, ASYNC221
611+
[ # noqa: S607
612612
"gm",
613613
"convert",
614614
"-density",
@@ -696,8 +696,8 @@ async def test_multi_code() -> None:
696696
)
697697
root_folder = Path("/results/qrcode")
698698
for page in range(3):
699-
subprocess.run(
700-
[
699+
subprocess.run( # noqa: S603, ASYNC221
700+
[ # noqa: S607
701701
"gm",
702702
"convert",
703703
"-density",
@@ -743,9 +743,9 @@ async def test_tiff_jupyter() -> None:
743743
config_file_name = str(root_folder / "config.yaml")
744744
step = await process.transform(config, step, config_file_name, root_folder)
745745
assert step["sources"] == ["/results/tiff/image-1.png"]
746-
assert [p for p in root_folder.rglob("*.tiff")] == [root_folder / "source/image-1.tiff"]
746+
assert list(root_folder.rglob("*.tiff")) == [root_folder / "source/image-1.tiff"]
747747

748-
with open("/results/tiff/jupyter/jupyter.ipynb") as f:
748+
with Path("/results/tiff/jupyter/jupyter.ipynb").open() as f: # noqa: ASYNC230
749749
nb = nbformat.read(f, as_version=4)
750750
ep = ExecutePreprocessor(timeout=600, kernel_name="python3")
751751
ep.preprocess(nb, {"metadata": {"path": "/results/tiff/jupyter/"}})
@@ -809,7 +809,7 @@ def test_auto_mask_combine() -> None:
809809
check_image(
810810
"/results/",
811811
cv2.cvtColor(context.mask, cv2.COLOR_BGR2RGB),
812-
os.path.join(os.path.dirname(__file__), "auto_mask_combine.expected.png"),
812+
str(Path(__file__).parent / "auto_mask_combine.expected.png"),
813813
generate_expected_image=REGENERATE,
814814
)
815815

@@ -818,12 +818,12 @@ def test_auto_mask_combine() -> None:
818818
def test_auto_cut() -> None:
819819
init_test()
820820
context = process_utils.Context({"args": {"cut": {}, "background_color": [255, 0, 0]}}, {})
821-
context.image = cv2.imread(os.path.join(os.path.dirname(__file__), "auto-mask-source.png"))
821+
context.image = cv2.imread(str(Path(__file__).parent / "auto-mask-source.png"))
822822
context.do_initial_cut()
823823
check_image(
824824
"/results/",
825825
cv2.cvtColor(context.image, cv2.COLOR_BGR2RGB),
826-
os.path.join(os.path.dirname(__file__), "auto_cut.expected.png"),
826+
str(Path(__file__).parent / "auto_cut.expected.png"),
827827
generate_expected_image=REGENERATE,
828828
)
829829

@@ -833,13 +833,13 @@ def test_auto_cut() -> None:
833833
async def test_color_cut() -> None:
834834
init_test()
835835
context = process_utils.Context({"args": {"cut_white": 200}}, {})
836-
context.image = cv2.imread(os.path.join(os.path.dirname(__file__), "white-cut.png"))
836+
context.image = cv2.imread(str(Path(__file__).parent / "white-cut.png"))
837837
await process.color_cut(context)
838838
cv2.imwrite("/results/white-cut.actual.png", context.image)
839839
check_image(
840840
"/results/",
841841
cv2.cvtColor(context.image, cv2.COLOR_BGR2RGB),
842-
os.path.join(os.path.dirname(__file__), "white-cut.expected.png"),
842+
str(Path(__file__).parent / "white-cut.expected.png"),
843843
generate_expected_image=REGENERATE,
844844
)
845845

@@ -858,21 +858,21 @@ async def test_histogram() -> None:
858858
},
859859
{},
860860
)
861-
context.image = cv2.imread(os.path.join(os.path.dirname(__file__), "limit-contour-all-1.png"))
861+
context.image = cv2.imread(str(Path(__file__).parent / "limit-contour-all-1.png"))
862862
context.image_name = "histogram.png"
863-
context.root_folder = Path("/tmp")
863+
context.root_folder = Path("/tmp") # noqa: S108
864864
await process.histogram(context)
865865
print("Compare '/results/histogram/histogram.png' with expected image 'histogram.expected.png'.")
866866
check_image_file(
867867
"/results/histogram/",
868-
"/tmp/histogram/histogram.png",
869-
os.path.join(os.path.dirname(__file__), "histogram.expected.png"),
868+
str(Path("/tmp") / "histogram" / "histogram.png"), # noqa: S108
869+
str(Path(__file__).parent / "histogram.expected.png"),
870870
generate_expected_image=REGENERATE,
871871
)
872872
print("Compare '/results/histogram/log-histogram.png' with expected image 'histogram-log.expected.png'.")
873873
check_image_file(
874874
"/results/histogram/",
875-
"/tmp/histogram/log-histogram.png",
876-
os.path.join(os.path.dirname(__file__), "histogram-log.expected.png"),
875+
str(Path("/tmp") / "histogram" / "log-histogram.png"), # noqa: S108
876+
str(Path(__file__).parent / "histogram-log.expected.png"),
877877
generate_expected_image=REGENERATE,
878878
)

tests/test_status.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ def test_status() -> None:
1212
os.environ["SCAN_CODES_FOLDER"] = "./codes"
1313
os.environ["SCAN_FINAL_FOLDER"] = "./consume"
1414
os.environ["SCAN_SOURCE_FOLDER"] = "./scan"
15-
old_cwd = os.getcwd()
16-
os.chdir(os.path.join(os.path.dirname(__file__), "status"))
15+
old_cwd = Path.cwd()
16+
status_dir = Path(__file__).parent / "status"
17+
os.chdir(status_dir)
1718
status_instance = status.Status()
1819
status_instance.set_current_folder(Path("7"))
1920
status_instance.write()

0 commit comments

Comments
 (0)