From f18771364cfc6ddafad60ee9924ee4f44cbcd9f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= Date: Tue, 3 Sep 2024 18:07:36 +0200 Subject: [PATCH] get rid of ValueError --- docs/nix/render_options/__init__.py | 11 ++++++----- nixosModules/clanCore/zerotier/generate.py | 4 ++-- pkgs/clan-app/clan_app/api/__init__.py | 7 ++++--- pkgs/clan-cli/clan_cli/api/__init__.py | 4 ++-- pkgs/clan-cli/clan_cli/api/modules.py | 2 +- pkgs/clan-cli/clan_cli/nix/__init__.py | 2 +- pkgs/clan-cli/tests/helpers/validator.py | 10 +++++++--- .../clan_vm_manager/components/gkvstore.py | 3 ++- pkgs/clan-vm-manager/clan_vm_manager/views/list.py | 5 +++-- .../moonlight_sunshine_accept/errors.py | 2 ++ .../moonlight_sunshine_accept/moonlight/uri.py | 6 ++++-- 11 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/errors.py diff --git a/docs/nix/render_options/__init__.py b/docs/nix/render_options/__init__.py index 3abcadd5a..fb0a5b337 100644 --- a/docs/nix/render_options/__init__.py +++ b/docs/nix/render_options/__init__.py @@ -29,6 +29,7 @@ from pathlib import Path from typing import Any from clan_cli.api.modules import Frontmatter, extract_frontmatter, get_roles +from clan_cli.errors import ClanError # Get environment variables CLAN_CORE_PATH = Path(os.environ["CLAN_CORE_PATH"]) @@ -153,11 +154,11 @@ options_head = "\n## Module Options\n" def produce_clan_core_docs() -> None: if not CLAN_CORE_DOCS: msg = f"Environment variables are not set correctly: $CLAN_CORE_DOCS={CLAN_CORE_DOCS}" - raise ValueError(msg) + raise ClanError(msg) if not OUT: msg = f"Environment variables are not set correctly: $out={OUT}" - raise ValueError(msg) + raise ClanError(msg) # A mapping of output file to content core_outputs: dict[str, str] = {} @@ -228,15 +229,15 @@ clan_modules_descr = """Clan modules are [NixOS modules](https://wiki.nixos.org/ def produce_clan_modules_docs() -> None: if not CLAN_MODULES: msg = f"Environment variables are not set correctly: $out={CLAN_MODULES}" - raise ValueError(msg) + raise ClanError(msg) if not CLAN_CORE_PATH: msg = f"Environment variables are not set correctly: $CLAN_CORE_PATH={CLAN_CORE_PATH}" - raise ValueError(msg) + raise ClanError(msg) if not OUT: msg = f"Environment variables are not set correctly: $out={OUT}" - raise ValueError(msg) + raise ClanError(msg) with Path(CLAN_MODULES).open() as f: links: dict[str, str] = json.load(f) diff --git a/nixosModules/clanCore/zerotier/generate.py b/nixosModules/clanCore/zerotier/generate.py index 110ddcfcf..75d969472 100644 --- a/nixosModules/clanCore/zerotier/generate.py +++ b/nixosModules/clanCore/zerotier/generate.py @@ -217,7 +217,7 @@ def main() -> None: case "network": if args.network_id is None: msg = "network_id parameter is required" - raise ValueError(msg) + raise ClanError(msg) controller = create_network_controller() identity = controller.identity network_id = controller.networkid @@ -227,7 +227,7 @@ def main() -> None: network_id = args.network_id case _: msg = f"unknown mode {args.mode}" - raise ValueError(msg) + raise ClanError(msg) ip = compute_zerotier_ip(network_id, identity) args.identity_secret.write_text(identity.private) diff --git a/pkgs/clan-app/clan_app/api/__init__.py b/pkgs/clan-app/clan_app/api/__init__.py index c8cda46f5..aaebbe004 100644 --- a/pkgs/clan-app/clan_app/api/__init__.py +++ b/pkgs/clan-app/clan_app/api/__init__.py @@ -11,6 +11,7 @@ from typing import ( cast, ) +from clan_cli.errors import ClanError from gi.repository import GLib, GObject log = logging.getLogger(__name__) @@ -94,7 +95,7 @@ class GObjApi: if fn_name in self._obj_registry: msg = f"Function '{fn_name}' already registered" - raise ValueError(msg) + raise ClanError(msg) self._obj_registry[fn_name] = obj def check_signature(self, fn_signatures: dict[str, inspect.Signature]) -> None: @@ -121,7 +122,7 @@ class GObjApi: log.error(f"Expected signature: {exp_signature}") log.error(f"Actual signature: {got_signature}") msg = f"Overwritten method '{m_name}' has different signature than the implementation" - raise ValueError(msg) + raise ClanError(msg) def get_obj(self, fn_name: str) -> type[ImplFunc]: result = self._obj_registry.get(fn_name, None) @@ -131,7 +132,7 @@ class GObjApi: plain_fn = self._methods.get(fn_name, None) if plain_fn is None: msg = f"Method '{fn_name}' not found in Api" - raise ValueError(msg) + raise ClanError(msg) class GenericFnRuntime(ImplFunc[..., Any]): def __init__(self) -> None: diff --git a/pkgs/clan-cli/clan_cli/api/__init__.py b/pkgs/clan-cli/clan_cli/api/__init__.py index 03be06d4c..25d3c37b8 100644 --- a/pkgs/clan-cli/clan_cli/api/__init__.py +++ b/pkgs/clan-cli/clan_cli/api/__init__.py @@ -110,10 +110,10 @@ API.register(open_file) def register(self, fn: Callable[..., T]) -> Callable[..., T]: if fn.__name__ in self._registry: msg = f"Function {fn.__name__} already registered" - raise ValueError(msg) + raise ClanError(msg) if fn.__name__ in self._orig_signature: msg = f"Function {fn.__name__} already registered" - raise ValueError(msg) + raise ClanError(msg) # make copy of original function self._orig_signature[fn.__name__] = signature(fn) diff --git a/pkgs/clan-cli/clan_cli/api/modules.py b/pkgs/clan-cli/clan_cli/api/modules.py index b5e363db5..0decdde15 100644 --- a/pkgs/clan-cli/clan_cli/api/modules.py +++ b/pkgs/clan-cli/clan_cli/api/modules.py @@ -176,7 +176,7 @@ def set_service_instance( if module_name not in service_keys: msg = f"{module_name} is not a valid Service attribute. Expected one of {', '.join(service_keys)}." - raise ValueError(msg) + raise ClanError(msg) inventory = load_inventory_json(base_path) target_type = get_args(get_type_hints(Service)[module_name])[1] diff --git a/pkgs/clan-cli/clan_cli/nix/__init__.py b/pkgs/clan-cli/clan_cli/nix/__init__.py index bd1298fd1..a1346eee5 100644 --- a/pkgs/clan-cli/clan_cli/nix/__init__.py +++ b/pkgs/clan-cli/clan_cli/nix/__init__.py @@ -146,7 +146,7 @@ def run_cmd(programs: list[str], cmd: list[str]) -> list[str]: for program in programs: if not Programs.is_allowed(program): msg = f"Program not allowed: {program}" - raise ValueError(msg) + raise ClanError(msg) if os.environ.get("IN_NIX_SANDBOX"): return cmd missing_packages = [ diff --git a/pkgs/clan-cli/tests/helpers/validator.py b/pkgs/clan-cli/tests/helpers/validator.py index 559b55ec2..18d12ae08 100644 --- a/pkgs/clan-cli/tests/helpers/validator.py +++ b/pkgs/clan-cli/tests/helpers/validator.py @@ -2,6 +2,10 @@ import subprocess import tempfile +class Error(Exception): + pass + + def is_valid_age_key(secret_key: str) -> bool: # Run the age-keygen command with the -y flag to check the key format result = subprocess.run( @@ -11,7 +15,7 @@ def is_valid_age_key(secret_key: str) -> bool: if result.returncode == 0: return True msg = f"Invalid age key: {secret_key}" - raise ValueError(msg) + raise Error(msg) def is_valid_ssh_key(secret_key: str, ssh_pub: str) -> bool: @@ -27,7 +31,7 @@ def is_valid_ssh_key(secret_key: str, ssh_pub: str) -> bool: if result.returncode == 0: if result.stdout != ssh_pub: msg = f"Expected '{ssh_pub}' got '{result.stdout}' for ssh key: {secret_key}" - raise ValueError(msg) + raise Error(msg) return True msg = f"Invalid ssh key: {secret_key}" - raise ValueError(msg) + raise Error(msg) diff --git a/pkgs/clan-vm-manager/clan_vm_manager/components/gkvstore.py b/pkgs/clan-vm-manager/clan_vm_manager/components/gkvstore.py index 551a79ed5..33fa9a617 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/components/gkvstore.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/components/gkvstore.py @@ -5,6 +5,7 @@ from typing import Any, Generic, TypeVar import gi gi.require_version("Gio", "2.0") +from clan_cli.errors import ClanError from gi.repository import Gio, GObject log = logging.getLogger(__name__) @@ -77,7 +78,7 @@ class GKVStore(GObject.GObject, Gio.ListModel, Generic[K, V]): key = self.key_gen(item) if key in self._items: msg = "Key already exists in the dictionary" - raise ValueError(msg) + raise ClanError(msg) if position < 0 or position > len(self._items): msg = "Index out of range" raise IndexError(msg) diff --git a/pkgs/clan-vm-manager/clan_vm_manager/views/list.py b/pkgs/clan-vm-manager/clan_vm_manager/views/list.py index 20f402325..9f3a2d9ef 100644 --- a/pkgs/clan-vm-manager/clan_vm_manager/views/list.py +++ b/pkgs/clan-vm-manager/clan_vm_manager/views/list.py @@ -6,6 +6,7 @@ from typing import Any, TypeVar import gi from clan_cli.clan_uri import ClanURI +from clan_cli.errors import ClanError from clan_vm_manager.components.gkvstore import GKVStore from clan_vm_manager.components.interfaces import ClanConfig @@ -259,7 +260,7 @@ class ClanList(Gtk.Box): vm = ClanStore.use().set_logging_vm(target) if vm is None: msg = f"VM {target} not found" - raise ValueError(msg) + raise ClanError(msg) views = ViewStack.use().view # Reset the logs view @@ -267,7 +268,7 @@ class ClanList(Gtk.Box): if logs is None: msg = "Logs view not found" - raise ValueError(msg) + raise ClanError(msg) name = vm.machine.name if vm.machine else "Unknown" diff --git a/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/errors.py b/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/errors.py new file mode 100644 index 000000000..9c69c25f8 --- /dev/null +++ b/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/errors.py @@ -0,0 +1,2 @@ +class Error(Exception): + pass diff --git a/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/moonlight/uri.py b/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/moonlight/uri.py index 0b4e4aac3..339cb5e37 100644 --- a/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/moonlight/uri.py +++ b/pkgs/moonlight-sunshine-accept/moonlight_sunshine_accept/moonlight/uri.py @@ -1,5 +1,7 @@ from urllib.parse import urlparse +from moonlight_sunshine_accept.errors import Error + def parse_moonlight_uri(uri: str) -> tuple[str, int | None]: print(uri) @@ -11,10 +13,10 @@ def parse_moonlight_uri(uri: str) -> tuple[str, int | None]: parsed = urlparse(uri) if parsed.scheme != "moonlight": msg = f"Invalid moonlight URI: {uri}" - raise ValueError(msg) + raise Error(msg) hostname = parsed.hostname if hostname is None: msg = f"Invalid moonlight URI: {uri}" - raise ValueError + raise Error(msg) port = parsed.port return (hostname, port)