feat: Add session bus option for local testing and development

Introduced a `--use-session-bus` flag to optionally use the D-Bus
session bus instead of the system bus. This allows better flexibility
for local testing and development scenarios without requiring
system-level changes. Updated related classes and functions to respect
the new flag.
This commit is contained in:
2025-04-19 09:33:54 +02:00
parent a4ccb9081f
commit bde3920fc1
4 changed files with 19 additions and 7 deletions

View File

@@ -5,8 +5,9 @@ from threading import Thread
from time import sleep from time import sleep
from gi.repository import GLib from gi.repository import GLib
from pydbus import SystemBus from pydbus import SessionBus, SystemBus
from .. import proginit as pi
from ..dbus_middleware1 import REVPI_DBUS_BASE_PATH from ..dbus_middleware1 import REVPI_DBUS_BASE_PATH
from ..dbus_middleware1 import REVPI_DBUS_NAME from ..dbus_middleware1 import REVPI_DBUS_NAME
@@ -34,7 +35,7 @@ def simple_call(method: str, *args, interface: str, object_path=REVPI_DBUS_BASE_
Any Any
The result of the method invocation on the targeted D-Bus interface. The result of the method invocation on the targeted D-Bus interface.
""" """
bus = SystemBus() bus = SessionBus() if pi.pargs.use_session_bus else SystemBus()
revpi = bus.get(REVPI_DBUS_NAME, object_path) revpi = bus.get(REVPI_DBUS_NAME, object_path)
iface = revpi[interface] iface = revpi[interface]
return getattr(iface, method)(*args) return getattr(iface, method)(*args)
@@ -55,7 +56,7 @@ def await_signal(signal_name: str, timeout: int, interface: str, object_path=REV
detected_signal = True detected_signal = True
loop.quit() loop.quit()
bus = SystemBus() bus = SessionBus() if pi.pargs.use_session_bus else SystemBus()
revpi = bus.get(REVPI_DBUS_NAME, object_path) revpi = bus.get(REVPI_DBUS_NAME, object_path)
iface = revpi[interface] iface = revpi[interface]

View File

@@ -42,7 +42,7 @@ class MiddlewareDaemon:
if self.bus_provider and self.bus_provider.is_alive(): if self.bus_provider and self.bus_provider.is_alive():
return return
self.bus_provider = BusProvider() self.bus_provider = BusProvider(use_system_bus=not pi.pargs.use_session_bus)
self.bus_provider.start() self.bus_provider.start()
log.debug("leave MiddlewareDaemon.dbus_start") log.debug("leave MiddlewareDaemon.dbus_start")

View File

@@ -6,7 +6,7 @@ from logging import getLogger
from threading import Thread from threading import Thread
from gi.repository import GLib from gi.repository import GLib
from pydbus import SystemBus from pydbus import SessionBus, SystemBus
from . import REVPI_DBUS_NAME from . import REVPI_DBUS_NAME
from .process_image import InterfacePiControl from .process_image import InterfacePiControl
@@ -20,11 +20,12 @@ class BusProvider(Thread):
self, self,
picontrol_device="/dev/piControl0", picontrol_device="/dev/piControl0",
config_rsc="/etc/revpi/config.rsc", config_rsc="/etc/revpi/config.rsc",
use_system_bus=True,
): ):
log.debug("enter BusProvider.__init__") log.debug("enter BusProvider.__init__")
super().__init__() super().__init__()
self._bus = SystemBus() self._bus = SystemBus() if use_system_bus else SessionBus()
self._loop = GLib.MainLoop() self._loop = GLib.MainLoop()
self.picontrol_device = picontrol_device self.picontrol_device = picontrol_device

View File

@@ -9,7 +9,7 @@ __version__ = "1.4.0"
import logging import logging
import sys import sys
from argparse import ArgumentParser, Namespace from argparse import ArgumentParser, Namespace, SUPPRESS
from configparser import ConfigParser from configparser import ConfigParser
from enum import Enum from enum import Enum
from os import R_OK, W_OK, access, environ, getpid, remove from os import R_OK, W_OK, access, environ, getpid, remove
@@ -262,6 +262,16 @@ parser = ArgumentParser(
prog=programname, prog=programname,
description="Program description", description="Program description",
) )
# Use session bus of D-Bus for local testing and development proposes (hidden)
parser.add_argument(
"--use-session-bus",
dest="use_session_bus",
action="store_true",
default=False,
help=SUPPRESS,
)
parser.add_argument("--version", action="version", version=f"%(prog)s {program_version}") parser.add_argument("--version", action="version", version=f"%(prog)s {program_version}")
parser.add_argument( parser.add_argument(
"-f", "-f",