feat(cli): Add D-Bus helper functions for CLI commands.
This introduces `simple_call`, a utility function to interact with D-Bus interfaces within the RevPi system. It facilitates method invocation on specific interfaces and paths, improving modularity and code reuse in middleware operations.
This commit is contained in:
36
src/revpi_middleware/cli_commands/dbus_helper.py
Normal file
36
src/revpi_middleware/cli_commands/dbus_helper.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# SPDX-FileCopyrightText: 2025 KUNBUS GmbH
|
||||
# SPDX-License-Identifier: GPL-2.0-or-later
|
||||
"""D-Bus helper functions for cli commands."""
|
||||
from pydbus import SystemBus
|
||||
|
||||
from ..dbus_middleware1 import REVPI_DBUS_BASE_PATH
|
||||
from ..dbus_middleware1 import REVPI_DBUS_NAME
|
||||
|
||||
PICONTROL_INTERFACE = "com.revolutionpi.middleware1.picontrol"
|
||||
RESET_DRIVER_METHOD = "ResetDriver"
|
||||
|
||||
|
||||
def simple_call(method: str, *args, interface: str, object_path=REVPI_DBUS_BASE_PATH):
|
||||
"""
|
||||
Executes a method on a specific D-Bus object interface within the RevPi system. This function
|
||||
connects to the system bus, retrieves the desired interface and object path, and invokes
|
||||
the specified method with provided arguments.
|
||||
|
||||
Parameters:
|
||||
method: str
|
||||
The name of the method to be invoked on the targeted interface.
|
||||
*args: tuple
|
||||
Positional arguments to be passed to the method being invoked.
|
||||
interface: str
|
||||
The name of the D-Bus interface providing the required functionality.
|
||||
object_path: str, optional
|
||||
The D-Bus object path of the RevPi interface. Defaults to REVPI_DBUS_BASE_PATH.
|
||||
|
||||
Returns:
|
||||
Any
|
||||
The result of the method invocation on the targeted D-Bus interface.
|
||||
"""
|
||||
bus = SystemBus()
|
||||
revpi = bus.get(REVPI_DBUS_NAME, object_path)
|
||||
iface = revpi[interface]
|
||||
return getattr(iface, method)(*args)
|
||||
Reference in New Issue
Block a user