Skip to content

Commit 7adf23e

Browse files
committed
Refactor
1 parent 892c191 commit 7adf23e

3 files changed

Lines changed: 27 additions & 34 deletions

File tree

src/slicer_uri_bridge/handler.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import argparse
5+
import contextlib
56
import ipaddress
67
import json
78
import logging
@@ -180,10 +181,8 @@ def read_protocol_uri(uri_file: str) -> str:
180181
return data.decode("utf-16").strip()
181182
return data.decode("utf-8-sig").strip()
182183
finally:
183-
try:
184+
with contextlib.suppress(OSError):
184185
path.unlink()
185-
except OSError:
186-
pass
187186

188187

189188
def resolve_protocol_uri(args: argparse.Namespace) -> str:
@@ -336,7 +335,7 @@ def validate_remote_url(
336335
if normalize_host(host) not in allowed_hosts:
337336
raise BridgeError(f"Download host is not allow-listed: {host}")
338337

339-
if not allow_local_resolved_hosts:
338+
if not allow_local_resolved_hosts or not check_allowlist:
340339
assert_public_host(host)
341340

342341

@@ -490,15 +489,11 @@ def download_model(
490489
output.write(chunk)
491490
except Exception:
492491
if output_created:
493-
try:
492+
with contextlib.suppress(OSError):
494493
destination.unlink()
495-
except OSError:
496-
pass
497494
if download_folder is None:
498-
try:
495+
with contextlib.suppress(OSError):
499496
destination.parent.rmdir()
500-
except OSError:
501-
pass
502497
raise
503498

504499
logger.info(f"Downloaded {total} bytes to {destination}")
@@ -678,16 +673,14 @@ def launch_bambu(command: list[str], model_path: Path) -> None:
678673

679674
def show_message(message: str, kind: str) -> None:
680675
print(message, file=sys.stderr)
681-
try:
676+
with contextlib.suppress(Exception):
682677
import tkinter
683678
from tkinter import messagebox
684679

685680
root = tkinter.Tk()
686681
root.withdraw()
687682
getattr(messagebox, kind)("Slicer URI Bridge", message)
688683
root.destroy()
689-
except Exception:
690-
pass
691684

692685

693686
def show_error(message: str) -> None:
@@ -755,15 +748,11 @@ def main(argv: list[str] | None = None) -> int:
755748
except Exception as exc:
756749
logger.error(f"Failed: {exc}")
757750
if local_path:
758-
try:
751+
with contextlib.suppress(OSError):
759752
local_path.unlink()
760-
except OSError:
761-
pass
762753
if download_folder is None:
763-
try:
754+
with contextlib.suppress(OSError):
764755
local_path.parent.rmdir()
765-
except OSError:
766-
pass
767756
show_error(str(exc))
768757
return 1
769758

src/slicer_uri_bridge/manager.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import argparse
5+
import contextlib
56
import importlib.resources
67
import importlib.util
78
import os
@@ -378,19 +379,15 @@ def refresh_desktop_database(self) -> None:
378379
command = shutil.which("update-desktop-database")
379380
if not command:
380381
return
381-
try:
382+
with contextlib.suppress(OSError):
382383
subprocess.run([command, str(self.applications_dir)], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
383-
except OSError:
384-
pass
385384

386385
def apply_xdg_default(self, mime: str) -> None:
387386
command = shutil.which("xdg-mime")
388387
if not command:
389388
return
390-
try:
389+
with contextlib.suppress(OSError):
391390
subprocess.run([command, "default", DESKTOP_ID, mime], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
392-
except OSError:
393-
pass
394391

395392
def write_bridge_files(self, definitions: Iterable[ProtocolDef]) -> None:
396393
self.check_expected_runtime()
@@ -686,10 +683,8 @@ def delete_handler_registration(self, definition: ProtocolDef) -> None:
686683
def delete_expected_key(self, root, path: str, root_path: str) -> None: # noqa: ANN001 - winreg root type is platform-only
687684
if not self.safe_delete_path(root, path, root_path):
688685
return
689-
try:
686+
with contextlib.suppress(OSError):
690687
self.winreg.DeleteKey(root, path)
691-
except OSError:
692-
pass
693688

694689
def safe_delete_path(self, root, path: str, root_path: str) -> bool: # noqa: ANN001 - winreg root type is platform-only
695690
if root != self.winreg.HKEY_CURRENT_USER:
@@ -949,19 +944,15 @@ def register_app_bundle(self) -> None:
949944
command = self.lsregister_command()
950945
if not command or not self.app_bundle.exists():
951946
return
952-
try:
947+
with contextlib.suppress(OSError):
953948
subprocess.run([command, "-f", str(self.app_bundle)], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
954-
except OSError:
955-
pass
956949

957950
def unregister_app_bundle(self) -> None:
958951
command = self.lsregister_command()
959952
if not command or not self.app_bundle.exists():
960953
return
961-
try:
954+
with contextlib.suppress(OSError):
962955
subprocess.run([command, "-u", str(self.app_bundle)], check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
963-
except OSError:
964-
pass
965956

966957
class _LaunchServices:
967958
ENCODING_UTF8 = 0x08000100

tests/test_handler.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ def test_validate_remote_url_can_allow_local_resolved_hosts(self) -> None:
266266

267267
assert_public_host.assert_not_called()
268268

269+
def test_validate_remote_url_still_checks_redirect_targets_when_local_hosts_are_allowed(self) -> None:
270+
with patch("slicer_uri_bridge.handler.assert_public_host") as assert_public_host:
271+
validate_remote_url(
272+
"https://127.0.0.1/model.3mf",
273+
allowed_hosts={"files.example"},
274+
allow_any_original_host=False,
275+
allow_plain_http=False,
276+
check_allowlist=False,
277+
allow_local_resolved_hosts=True,
278+
)
279+
280+
assert_public_host.assert_called_once_with("127.0.0.1")
281+
269282

270283
class FileValidationTests(unittest.TestCase):
271284
def test_validate_downloaded_file_accepts_non_empty_model_payload(self) -> None:

0 commit comments

Comments
 (0)