Skip to content

Commit ad0ed32

Browse files
committed
build: configure ruff and mypy, apply styling and typing fixes
1 parent b2e6dc6 commit ad0ed32

11 files changed

Lines changed: 199 additions & 136 deletions

File tree

pyproject.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,38 @@ where = ["src"]
2828

2929
[tool.setuptools.package-data]
3030
slicer_uri_bridge = ["resources/default_config.toml", "resources/macos-launcher.applescript"]
31+
32+
[tool.ruff]
33+
line-length = 120
34+
35+
[tool.ruff.lint]
36+
select = [
37+
"E", # pycodestyle errors
38+
"W", # pycodestyle warnings
39+
"F", # pyflakes
40+
"I", # isort
41+
"UP", # pyupgrade
42+
"B", # flake8-bugbear
43+
"C4", # flake8-comprehensions
44+
"SIM", # flake8-simplify
45+
]
46+
ignore = [
47+
"SIM102", # nested if statements
48+
"SIM108", # use ternary operator
49+
"SIM117", # nested with statements
50+
]
51+
52+
53+
[project.optional-dependencies]
54+
dev = [
55+
"mypy>=2.1.0",
56+
"ruff>=0.15.15",
57+
]
58+
59+
[tool.mypy]
60+
python_version = "3.11"
61+
check_untyped_defs = true
62+
warn_redundant_casts = true
63+
warn_unused_ignores = true
64+
warn_return_any = true
65+
files = ["src", "tests"]

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
from setuptools import setup
22

3-
43
setup()

src/slicer_uri_bridge/cli.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
import subprocess
77
import sys
88
import tomllib
9-
from importlib.metadata import PackageNotFoundError, version as package_version
9+
from importlib.metadata import PackageNotFoundError
10+
from importlib.metadata import version as package_version
1011
from pathlib import Path
1112

1213
from .config import config_matches_default, init_user_config, user_config_path
1314
from .manager import main as manager_main
1415

15-
1616
PACKAGE_NAME = "slicer-uri-bridge"
1717
YES_VALUES = {"y", "yes"}
1818
NO_VALUES = {"n", "no"}
@@ -124,7 +124,9 @@ def warn_if_bambu_target_missing(config_path: Path) -> None:
124124
eprint(f"Warning: Bambu Studio path from config was not found: {configured}")
125125
eprint(f"Edit {config_path} and update [bambu_studio].{key}.")
126126
if key != "windows":
127-
eprint("Fallback: if this path stays invalid, the bridge will try to open models with your default application.")
127+
eprint(
128+
"Fallback: if this path stays invalid, the bridge will try to open models with your default application."
129+
)
128130

129131

130132
def interactive_onboarding() -> int:

src/slicer_uri_bridge/config.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import os
44
import sys
5+
from collections.abc import Mapping
56
from importlib import resources
67
from pathlib import Path
7-
from typing import Mapping
88

99
CONFIG_DIR_NAME = "slicer-uri-bridge"
1010
CONFIG_FILE_NAME = "config.toml"
@@ -51,11 +51,7 @@ def user_log_path(
5151

5252

5353
def default_config_text() -> str:
54-
return (
55-
resources.files("slicer_uri_bridge")
56-
.joinpath("resources", "default_config.toml")
57-
.read_text(encoding="utf-8")
58-
)
54+
return resources.files("slicer_uri_bridge").joinpath("resources", "default_config.toml").read_text(encoding="utf-8")
5955

6056

6157
def init_user_config(*, force: bool = False) -> tuple[Path, bool]:

src/slicer_uri_bridge/handler.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
USER_AGENT = "OrcaSlicer/2.4.0-dev"
3232
IS_WINDOWS = sys.platform == "win32"
3333
IS_MACOS = sys.platform == "darwin"
34-
IS_LINUX = not IS_WINDOWS and not IS_MACOS
3534
MAX_REDIRECTS = 5
3635
MAX_DOWNLOAD_BYTES = 200 * 1024 * 1024
3736
BUFFER_SIZE = 81920
@@ -60,9 +59,7 @@ def return_response(self, req, fp, code, msg, headers):
6059

6160

6261
def parse_args(argv: list[str]) -> argparse.Namespace:
63-
parser = argparse.ArgumentParser(
64-
description="Open supported slicer-style URIs in local Bambu Studio."
65-
)
62+
parser = argparse.ArgumentParser(description="Open supported slicer-style URIs in local Bambu Studio.")
6663
parser.add_argument("uri", nargs="?")
6764
parser.add_argument("--uri-file", "-UriFile", dest="uri_file")
6865
return parser.parse_args(argv)
@@ -151,19 +148,18 @@ def load_config() -> dict:
151148
if not isinstance(extension, str) or not extension.strip():
152149
logger.warning(f"Ignoring invalid extension in security.allowed_extensions: {extension!r}")
153150
continue
154-
151+
155152
extension = extension.strip().lower()
156153
if not extension.startswith("."):
157154
extension = f".{extension}"
158155
valid_extensions.append(extension)
159156

160157
security["allowed_extensions"] = valid_extensions
161-
if not security["allowed_extensions"]:
158+
if not security["allowed_extensions"]:
162159
message = "Config value must be a list: security.allowed_extensions"
163160
logger.error(message)
164161
raise BridgeError(message)
165162

166-
167163
if not isinstance(config.get("bambu_studio"), dict):
168164
message = "Missing [bambu_studio] in config"
169165
logger.error(message)
@@ -187,10 +183,10 @@ def read_protocol_uri(uri_file: str) -> str:
187183

188184
def resolve_protocol_uri(args: argparse.Namespace) -> str:
189185
if args.uri:
190-
return args.uri.strip()
186+
return str(args.uri).strip()
191187

192188
if args.uri_file:
193-
return read_protocol_uri(args.uri_file).strip()
189+
return read_protocol_uri(str(args.uri_file)).strip()
194190

195191
raise BridgeError("Missing URI argument.")
196192

@@ -303,8 +299,7 @@ def assert_public_host(host: str) -> None:
303299
for address in addresses:
304300
if not ipaddress.ip_address(address).is_global:
305301
raise BridgeError(
306-
"Host resolves to a local/private/reserved address and is not allowed: "
307-
f"{host} -> {address}"
302+
f"Host resolves to a local/private/reserved address and is not allowed: {host} -> {address}"
308303
)
309304

310305

@@ -483,9 +478,7 @@ def download_model(
483478
while chunk := response.read(BUFFER_SIZE):
484479
total += len(chunk)
485480
if total > MAX_DOWNLOAD_BYTES:
486-
raise BridgeError(
487-
f"Download exceeded the size limit: {MAX_DOWNLOAD_BYTES} bytes"
488-
)
481+
raise BridgeError(f"Download exceeded the size limit: {MAX_DOWNLOAD_BYTES} bytes")
489482
output.write(chunk)
490483
except Exception:
491484
if output_created:
@@ -553,16 +546,9 @@ def post_process_message(path: Path, commands: list[str]) -> str:
553546
if len(commands) == 1:
554547
post_process = commands[0]
555548
else:
556-
post_process = "\n\n".join(
557-
f"[{index}]\n{command}" for index, command in enumerate(commands, start=1)
558-
)
549+
post_process = "\n\n".join(f"[{index}]\n{command}" for index, command in enumerate(commands, start=1))
559550

560-
return (
561-
"Downloaded 3MF file contains a post-processing script.\n\n"
562-
f"File: {path}\n\n"
563-
"post_process:\n"
564-
f"{post_process}"
565-
)
551+
return f"Downloaded 3MF file contains a post-processing script.\n\nFile: {path}\n\npost_process:\n{post_process}"
566552

567553

568554
def check_3mf_post_process(path: Path, action: str) -> None:
@@ -624,9 +610,7 @@ def resolve_bambu_command(config: dict) -> list[str]:
624610
if not resolved:
625611
if IS_WINDOWS:
626612
raise BridgeError(f"Bambu Studio executable not found on PATH: {configured_path}")
627-
return warn_and_resolve_default_open_command(
628-
f"Bambu Studio executable not found on PATH: {configured_path}"
629-
)
613+
return warn_and_resolve_default_open_command(f"Bambu Studio executable not found on PATH: {configured_path}")
630614
return [resolved]
631615

632616

0 commit comments

Comments
 (0)