ruff: replace asserts outside of tests with Exceptions

This commit is contained in:
Jörg Thalheim
2025-08-20 15:45:49 +02:00
parent 5be9b8383b
commit dc5485d9f1
23 changed files with 257 additions and 91 deletions

View File

@@ -16,6 +16,7 @@ from clan_cli import vms
from clan_cli.vms.inspect import inspect_vm
from clan_cli.vms.qemu import QMPWrapper
from clan_lib.dirs import vm_state_dir
from clan_lib.errors import ClanError
from clan_lib.machines.machines import Machine
from clan_vm_manager.clan_uri import ClanURI
@@ -158,14 +159,18 @@ class VMObject(GObject.Object):
name=self.data.flake.flake_attr,
flake=uri.flake,
)
assert self.machine is not None
if self.machine is None:
msg = "Machine object is not available"
raise ClanError(msg)
state_dir = vm_state_dir(
flake_url=self.machine.flake.identifier,
vm_name=self.machine.name,
)
self.qmp_wrap = QMPWrapper(state_dir)
assert self.machine is not None
if self.machine is None:
msg = "Machine object is not available"
raise ClanError(msg)
yield self.machine
self.machine = None
@@ -332,7 +337,9 @@ class VMObject(GObject.Object):
# Try to shutdown the VM gracefully using QMP
try:
assert self.qmp_wrap is not None
if self.qmp_wrap is None:
msg = "QMP wrapper is not available"
raise ClanError(msg)
with self.qmp_wrap.qmp_ctx() as qmp:
qmp.command("system_powerdown")
except Exception as ex:

View File

@@ -4,6 +4,7 @@ from collections.abc import Callable
from typing import Any, ClassVar, cast
import gi
from clan_lib.errors import ClanError
from clan_lib.machines.machines import Machine
from clan_vm_manager.clan_uri import ClanURI
@@ -109,7 +110,9 @@ class JoinList:
def _on_join_finished(self, source: JoinValue) -> None:
log.info(f"Join finished: {source.url}")
self.discard(source)
assert source.entry is not None
if source.entry is None:
msg = "Join entry is not available"
raise ClanError(msg)
ClanStore.use().push_history_entry(source.entry)
def discard(self, value: JoinValue) -> None:

View File

@@ -64,7 +64,9 @@ class ClanList(Gtk.Box):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
app = Gio.Application.get_default()
assert app is not None
if app is None:
msg = "Application is not available"
raise ClanError(msg)
app.connect("join_request", self.on_join_request)
self.log_label: Gtk.Label = Gtk.Label()
@@ -306,7 +308,9 @@ class ClanList(Gtk.Box):
# Can't do this here because clan store is empty at this point
if vm is not None:
sub = row.get_subtitle()
assert sub is not None
if sub is None:
msg = "Subtitle is not available"
raise ClanError(msg)
ToastOverlay.use().add_toast_unique(
WarningToast(

View File

@@ -1,6 +1,7 @@
import logging
import gi
from clan_lib.errors import ClanError
gi.require_version("Adw", "1")
from gi.repository import Adw, Gio, Gtk
@@ -19,7 +20,9 @@ class Logs(Gtk.Box):
super().__init__(orientation=Gtk.Orientation.VERTICAL)
app = Gio.Application.get_default()
assert app is not None
if app is None:
msg = "Application is not available"
raise ClanError(msg)
self.banner = Adw.Banner.new("")
self.banner.set_use_markup(True)

View File

@@ -2,6 +2,7 @@ import logging
import threading
import gi
from clan_lib.errors import ClanError
from clan_vm_manager.components.interfaces import ClanConfig
from clan_vm_manager.history import list_history
@@ -37,7 +38,9 @@ class MainWindow(Adw.ApplicationWindow):
view.add_top_bar(header)
app = Gio.Application.get_default()
assert app is not None
if app is None:
msg = "Application is not available"
raise ClanError(msg)
self.tray_icon: TrayIcon = TrayIcon(app)
# Initialize all ClanStore