From 78b56d6ea0d00bfefd9e334bbfe5caf4248cad41 Mon Sep 17 00:00:00 2001 From: Yaroslav Chvanov Date: Thu, 2 Mar 2023 04:44:54 +0300 Subject: [PATCH] tools: make all exceptions types have Error as the sufix --- pyproject.toml | 3 +++ tools/ratbagc.py.in | 28 +++++++++++++------------- tools/ratbagctl.body.py.in | 14 ++++++------- tools/ratbagctl.devel.py.in | 4 ++-- tools/ratbagctl.test.py.in | 2 +- tools/ratbagd.py | 40 ++++++++++++++++++------------------- tools/toolbox.py | 4 ++-- 7 files changed, 49 insertions(+), 46 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index abf2e551..fe3e0ed0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -17,6 +17,9 @@ select = [ "B", # flake8-pie "PIE", + # pep8-naming + # `Error` suffix on exception names. + "N818", ] ignore = [ # Line too long. diff --git a/tools/ratbagc.py.in b/tools/ratbagc.py.in index 0e6ad8fe..b1383b4d 100644 --- a/tools/ratbagc.py.in +++ b/tools/ratbagc.py.in @@ -99,7 +99,7 @@ class RatbagDeviceType(IntEnum): KEYBOARD = 3 -class RatbagdUnavailable(Exception): +class RatbagdUnavailableError(Exception): """Signals DBus is unavailable or the ratbagd daemon is not available.""" @@ -107,33 +107,33 @@ class RatbagError(Exception): """A common base exception to catch any ratbag exception.""" -class RatbagErrorDevice(RatbagError): +class RatbagDeviceError(RatbagError): """An exception corresponding to RatbagErrorCode.DEVICE.""" -class RatbagErrorCapability(RatbagError): +class RatbagCapabilityError(RatbagError): """An exception corresponding to RatbagErrorCode.CAPABILITY.""" -class RatbagErrorValue(RatbagError): +class RatbagValueError(RatbagError): """An exception corresponding to RatbagErrorCode.VALUE.""" -class RatbagErrorSystem(RatbagError): +class RatbagSystemError(RatbagError): """An exception corresponding to RatbagErrorCode.SYSTEM.""" -class RatbagErrorImplementation(RatbagError): +class RatbagImplementationError(RatbagError): """An exception corresponding to RatbagErrorCode.IMPLEMENTATION.""" """A table mapping RatbagErrorCode values to RatbagError* exceptions.""" EXCEPTION_TABLE = { - RatbagErrorCode.DEVICE: RatbagErrorDevice, - RatbagErrorCode.CAPABILITY: RatbagErrorCapability, - RatbagErrorCode.VALUE: RatbagErrorValue, - RatbagErrorCode.SYSTEM: RatbagErrorSystem, - RatbagErrorCode.IMPLEMENTATION: RatbagErrorImplementation, + RatbagErrorCode.DEVICE: RatbagDeviceError, + RatbagErrorCode.CAPABILITY: RatbagCapabilityError, + RatbagErrorCode.VALUE: RatbagValueError, + RatbagErrorCode.SYSTEM: RatbagSystemError, + RatbagErrorCode.IMPLEMENTATION: RatbagImplementationError, } @@ -157,7 +157,7 @@ class Ratbagd: name = os.path.join("/dev/", event) try: dev = RatbagdDevice(self._ratbag, name) - except RatbagErrorDevice: + except RatbagDeviceError: pass else: self._devices[name] = dev @@ -210,7 +210,7 @@ class Ratbagd: if id not in self._devices.keys(): try: dev = RatbagdDevice(self._ratbag, id) - except RatbagErrorDevice: + except RatbagDeviceError: pass else: self._devices[id] = dev @@ -249,7 +249,7 @@ class RatbagdDevice(metaclass=MetaRatbag): self._ratbag = ratbag self._device = libratbag.ratbag_cmd_open_device(ratbag, path) if self._device is None: - raise RatbagErrorDevice("device not compatible") + raise RatbagDeviceError("device not compatible") self._profiles = [ RatbagdProfile(self._device, i) diff --git a/tools/ratbagctl.body.py.in b/tools/ratbagctl.body.py.in index e6b7953a..e9254223 100755 --- a/tools/ratbagctl.body.py.in +++ b/tools/ratbagctl.body.py.in @@ -30,7 +30,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple, Union # This must be on a single line, as we replace it using merge_ratbagd.py while building. # fmt: off -from ratbagd import Ratbagd, RatbagdDevice, RatbagdProfile, RatbagdMacro, RatbagdResolution, RatbagdButton, RatbagdLed, RatbagdUnavailable, RatbagError, RatbagErrorCode, RatbagErrorCapability, RatbagDeviceType # NOQA +from ratbagd import Ratbagd, RatbagdDevice, RatbagdProfile, RatbagdMacro, RatbagdResolution, RatbagdButton, RatbagdLed, RatbagdUnavailableError, RatbagError, RatbagErrorCode, RatbagCapabilityError, RatbagDeviceType # NOQA # fmt: on @@ -402,7 +402,7 @@ def func_button_get(ratbagd: Ratbagd, args: argparse.Namespace) -> None: def func_button_action_set_none(ratbagd: Ratbagd, args: argparse.Namespace) -> None: button, _profile, device = find_button(ratbagd, args) if button.ActionType.NONE not in button.action_types: - raise RatbagErrorCapability("disabling buttons is not supported on this device") + raise RatbagCapabilityError("disabling buttons is not supported on this device") button.disable() commit(device, args) @@ -428,7 +428,7 @@ def func_button_action_set_special(ratbagd: Ratbagd, args: argparse.Namespace) - def func_button_action_set_key(ratbagd: Ratbagd, args: argparse.Namespace) -> None: button, _profile, device = find_button(ratbagd, args) if button.ActionType.KEY not in button.action_types: - raise RatbagErrorCapability("assigning a key is not supported on this device") + raise RatbagCapabilityError("assigning a key is not supported on this device") try: key = args.target_key @@ -442,7 +442,7 @@ def func_button_action_set_key(ratbagd: Ratbagd, args: argparse.Namespace) -> No def func_button_action_set_macro(ratbagd: Ratbagd, args: argparse.Namespace) -> None: button, _profile, device = find_button(ratbagd, args) if button.ActionType.MACRO not in button.action_types: - raise RatbagErrorCapability("assigning a macro is not supported on this device") + raise RatbagCapabilityError("assigning a macro is not supported on this device") macro_keys = args.target_macro macro = RatbagdMacro() @@ -630,7 +630,7 @@ def func_profile_name_get(ratbagd: Ratbagd, args: argparse.Namespace) -> None: def func_profile_name_set(ratbagd: Ratbagd, args: argparse.Namespace) -> None: profile, device = find_profile(ratbagd, args) if not profile.name: - raise RatbagErrorCapability( + raise RatbagCapabilityError( "assigning a profile name is not supported on this profile" ) profile.name = args.name @@ -1957,7 +1957,7 @@ def open_ratbagd( except AttributeError: pass # the ratbag-command case - except RatbagdUnavailable as e: + except RatbagdUnavailableError as e: print(f"Unable to connect to ratbagd: {e}") return None @@ -1993,7 +1993,7 @@ def main(argv: List[str]) -> None: else: try: f(r, cmd) - except RatbagErrorCapability as e: + except RatbagCapabilityError as e: print(f"Error: {e}", file=sys.stderr) diff --git a/tools/ratbagctl.devel.py.in b/tools/ratbagctl.devel.py.in index bb40573e..831fbda2 100755 --- a/tools/ratbagctl.devel.py.in +++ b/tools/ratbagctl.devel.py.in @@ -29,7 +29,7 @@ import time import toolbox import signal import sys -from ratbagctl import RatbagErrorCapability # NOQA +from ratbagctl import RatbagCapabilityError # NOQA def main(argv, start_ratbagd=True): @@ -72,7 +72,7 @@ def main(argv, start_ratbagd=True): # give time for ratbagd to receive async requests before # we try to terminate it time.sleep(0.5) - except RatbagErrorCapability as e: + except RatbagCapabilityError as e: print(f"Error: {e}", file=sys.stderr) finally: try: diff --git a/tools/ratbagctl.test.py.in b/tools/ratbagctl.test.py.in index dccc0735..f31b9ac1 100755 --- a/tools/ratbagctl.test.py.in +++ b/tools/ratbagctl.test.py.in @@ -214,7 +214,7 @@ class TestRatbagCtlProfile(TestRatbagCtl): self.assertEqual(r, "banana") # profile 0 doesn't have the capability RATBAG_PROFILE_CAP_WRITABLE_NAME self.launch_fail_with_exception_test( - "test_device profile 0 name set kiwi", toolbox.RatbagErrorCapability + "test_device profile 0 name set kiwi", toolbox.RatbagCapabilityError ) # better be safe than sorry, checking that the previous actually failed :) r = self.launch_good_test("test_device profile 0 name get") diff --git a/tools/ratbagd.py b/tools/ratbagd.py index 6eb4c87f..6c94af3d 100644 --- a/tools/ratbagd.py +++ b/tools/ratbagd.py @@ -76,7 +76,7 @@ class RatbagDeviceType(IntEnum): KEYBOARD = 3 -class RatbagdIncompatible(Exception): +class RatbagdIncompatibleError(Exception): """ratbagd is incompatible with this client""" def __init__(self, ratbagd_version, required_version): @@ -91,11 +91,11 @@ class RatbagdIncompatible(Exception): return self.message -class RatbagdUnavailable(Exception): +class RatbagdUnavailableError(Exception): """Signals DBus is unavailable or the ratbagd daemon is not available.""" -class RatbagdDBusTimeout(Exception): +class RatbagdDBusTimeoutError(Exception): """Signals that a timeout occurred during a DBus method call.""" @@ -103,33 +103,33 @@ class RatbagError(Exception): """A common base exception to catch any ratbag exception.""" -class RatbagErrorDevice(RatbagError): +class RatbagDeviceError(RatbagError): """An exception corresponding to RatbagErrorCode.DEVICE.""" -class RatbagErrorCapability(RatbagError): +class RatbagCapabilityError(RatbagError): """An exception corresponding to RatbagErrorCode.CAPABILITY.""" -class RatbagErrorValue(RatbagError): +class RatbagValueError(RatbagError): """An exception corresponding to RatbagErrorCode.VALUE.""" -class RatbagErrorSystem(RatbagError): +class RatbagSystemError(RatbagError): """An exception corresponding to RatbagErrorCode.SYSTEM.""" -class RatbagErrorImplementation(RatbagError): +class RatbagImplementationError(RatbagError): """An exception corresponding to RatbagErrorCode.IMPLEMENTATION.""" """A table mapping RatbagErrorCode values to RatbagError* exceptions.""" EXCEPTION_TABLE = { - RatbagErrorCode.DEVICE: RatbagErrorDevice, - RatbagErrorCode.CAPABILITY: RatbagErrorCapability, - RatbagErrorCode.VALUE: RatbagErrorValue, - RatbagErrorCode.SYSTEM: RatbagErrorSystem, - RatbagErrorCode.IMPLEMENTATION: RatbagErrorImplementation, + RatbagErrorCode.DEVICE: RatbagDeviceError, + RatbagErrorCode.CAPABILITY: RatbagCapabilityError, + RatbagErrorCode.VALUE: RatbagValueError, + RatbagErrorCode.SYSTEM: RatbagSystemError, + RatbagErrorCode.IMPLEMENTATION: RatbagImplementationError, } @@ -143,7 +143,7 @@ class _RatbagdDBus(GObject.GObject): try: _RatbagdDBus._dbus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None) except GLib.Error as e: - raise RatbagdUnavailable(e.message) from e + raise RatbagdUnavailableError(e.message) from e ratbag1 = "org.freedesktop.ratbag1" if os.environ.get("RATBAG_TEST"): @@ -166,10 +166,10 @@ class _RatbagdDBus(GObject.GObject): None, ) except GLib.Error as e: - raise RatbagdUnavailable(e.message) from e + raise RatbagdUnavailableError(e.message) from e if self._proxy.get_name_owner() is None: - raise RatbagdUnavailable(f"No one currently owns {ratbag1}") + raise RatbagdUnavailableError(f"No one currently owns {ratbag1}") self._proxy.connect("g-properties-changed", self._on_properties_changed) self._proxy.connect("g-signal", self._on_signal_received) @@ -237,7 +237,7 @@ class _RatbagdDBus(GObject.GObject): return res.unpack()[0] # Result is always a tuple except GLib.Error as e: if e.code == Gio.IOErrorEnum.TIMED_OUT: - raise RatbagdDBusTimeout(e.message) from e + raise RatbagdDBusTimeoutError(e.message) from e else: # Unrecognized error code; print the message to stderr and raise # the GLib.Error. @@ -253,7 +253,7 @@ class Ratbagd(_RatbagdDBus): through ratbagd; actual interaction with the devices is via the RatbagdDevice, RatbagdProfile, RatbagdResolution and RatbagdButton objects. - Throws RatbagdUnavailable when the DBus service is not available. + Throws RatbagdUnavailableError when the DBus service is not available. """ __gsignals__ = { @@ -270,11 +270,11 @@ class Ratbagd(_RatbagdDBus): super().__init__("Manager", None) result = self._get_dbus_property("Devices") if result is None and not self._proxy.get_cached_property_names(): - raise RatbagdUnavailable( + raise RatbagdUnavailableError( "Make sure it is running and your user is in the required groups." ) if self.api_version != api_version: - raise RatbagdIncompatible(self.api_version or -1, api_version) + raise RatbagdIncompatibleError(self.api_version or -1, api_version) self._devices = [RatbagdDevice(objpath) for objpath in result or []] self._proxy.connect("notify::g-name-owner", self._on_name_owner_changed) diff --git a/tools/toolbox.py b/tools/toolbox.py index d53d5f8c..158709e3 100644 --- a/tools/toolbox.py +++ b/tools/toolbox.py @@ -117,7 +117,7 @@ ratbagctl = import_non_standard_path(RATBAGCTL_NAME, RATBAGCTL_PATH) from ratbagctl import ( # noqa: E402 RatbagError, - RatbagErrorCapability, + RatbagCapabilityError, get_parser, open_ratbagd, ) @@ -130,5 +130,5 @@ __all__ = [ open_ratbagd, get_parser, RatbagError, - RatbagErrorCapability, + RatbagCapabilityError, ]