cli (loop): Remove GLib and Twisted related code:

Now that D-Bus has been fully reimplemented with txdbus, GLib is not needed anymore for
D-Bus bridge.
As `dbus` and `pb` (and future other bridges) both support "AIOBridge", we don't have to
use a Twisted specific version for anything.
This commit is contained in:
Goffi 2026-03-13 11:54:27 +01:00
commit b69c7e4565

View file

@ -22,7 +22,6 @@ import logging as log
from libervia.backend.core.exceptions import InternalError
from libervia.backend.core.i18n import _
from libervia.cli.constants import Const as C
from libervia.frontends.tools import aio
log.basicConfig(level=log.WARNING, format="[%(name)s] %(message)s")
@ -63,7 +62,7 @@ class LiberviaCLILoop:
# exit code must be set when using quit, so if it's not set something got wrong
# and we must report it
self._exit_code = C.EXIT_INTERNAL_ERROR
self._is_dbus = False
self._is_asyncio = True
def install_hooks(self, bridge_name):
"""Install signal handlers and event loop hooks.
@ -78,16 +77,6 @@ class LiberviaCLILoop:
import signal
if "dbus" in bridge_name:
self._is_dbus = True
# Install GLib integration for dbus
aio.install_glib_asyncio_iteration()
elif bridge_name == "embedded":
raise NotImplementedError
else:
self._is_dbus = False
# Twisted reactor has already been installed in __init__
signal.signal(signal.SIGINT, self._on_sigint)
def run(self, libervia_cli):
@ -101,42 +90,22 @@ class LiberviaCLILoop:
" install_hooks(bridge_name) is called after bridge initialization."
)
if self._is_dbus:
self._run_dbus()
else:
self._run_twisted(libervia_cli)
self._run_asyncio()
def _run_dbus(self):
def _run_asyncio(self):
"""Run the dbus-based loop."""
loop = self.loop
loop.run_forever()
def _run_twisted(self, libervia_cli):
"""Run the Twisted-based loop."""
self.libervia_cli = libervia_cli
try:
self.reactor.run(installSignalHandlers=False)
except SystemExit as e:
self._exit_code = e.code
sys.exit(self._exit_code)
def quit(self, exit_code):
"""Stop the loop and exit.
@param exit_code: Exit code to return
"""
self._exit_code = exit_code
if self._is_dbus:
loop = asyncio.get_event_loop()
loop.stop()
sys.exit(exit_code)
else:
# Only stop the reactor if it's running
if self.reactor.running:
self.reactor.stop()
else:
# Reactor not running (e.g., --help case), just exit
sys.exit(exit_code)
loop = asyncio.get_event_loop()
loop.stop()
sys.exit(exit_code)
def call_later(self, delay, callback, *args):
"""Call a callback after a delay.
@ -145,24 +114,8 @@ class LiberviaCLILoop:
@param callback: Method to call
@param args: Arguments to pass to callback
"""
if self._is_dbus:
loop = asyncio.get_event_loop()
loop.call_later(delay, callback, *args)
else:
self.reactor.callLater(delay, self._timeout_cb, args, callback, delay)
def _timeout_cb(self, args, callback, delay):
"""Callback wrapper for Twisted-based delayed calls."""
try:
ret = callback(*args)
# FIXME: temporary hack to avoid traceback when using XMLUI
# to be removed once create_task is not used anymore in
# xmlui_manager (i.e. once libervia.frontends.tools.xmlui fully supports
# async syntax)
except QuitException:
return
if ret:
self.reactor.callLater(delay, self._timeout_cb, args, callback, delay)
loop = asyncio.get_event_loop()
loop.call_later(delay, callback, *args)
def _on_sigint(self, sig_number, stack_frame):
"""Called on keyboard interruption
@ -171,7 +124,4 @@ class LiberviaCLILoop:
"""
print("\r" + USER_INTER_MSG)
self._exit_code = C.EXIT_USER_CANCELLED
if self._is_dbus:
self.quit(self._exit_code)
else:
self.reactor.callFromThread(self.reactor.stop)
self.quit(self._exit_code)