1
0
forked from drath/libratbag

tools: make all exceptions types have Error as the sufix

This commit is contained in:
Yaroslav Chvanov
2023-03-02 04:44:54 +03:00
parent e84a56c424
commit 78b56d6ea0
7 changed files with 49 additions and 46 deletions

View File

@@ -17,6 +17,9 @@ select = [
"B",
# flake8-pie
"PIE",
# pep8-naming
# `Error` suffix on exception names.
"N818",
]
ignore = [
# Line too long.

View File

@@ -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)

View File

@@ -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)

View File

@@ -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:

View File

@@ -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")

View File

@@ -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)

View File

@@ -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,
]