Skip to content

Commit

Permalink
Safe guard dbus import
Browse files Browse the repository at this point in the history
  • Loading branch information
MattHag committed May 17, 2024
1 parent f8bcbe0 commit c7dcc73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from typing import Dict
from typing import Tuple

import dbus
import gi
import psutil

Expand Down Expand Up @@ -188,6 +187,8 @@ def gnome_dbus_interface_setup():
if _dbus_interface is not None:
return _dbus_interface
try:
import dbus

bus = dbus.SessionBus()
remote_object = bus.get_object("org.gnome.Shell", "/io/github/pwr_solaar/solaar")
_dbus_interface = dbus.Interface(remote_object, "io.github.pwr_solaar.solaar")
Expand Down
6 changes: 3 additions & 3 deletions lib/solaar/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
except Exception:
# Either the dbus library is not available or the system dbus is not running
logger.warning("failed to set up dbus")
pass
bus = None


_suspend_callback = None
Expand All @@ -55,7 +55,7 @@ def watch_suspend_resume(on_resume_callback=None, on_suspend_callback=None):
global _resume_callback, _suspend_callback
_suspend_callback = on_suspend_callback
_resume_callback = on_resume_callback
if on_resume_callback is not None or on_suspend_callback is not None:
if bus is not None and on_resume_callback is not None or on_suspend_callback is not None:
bus.add_signal_receiver(_suspend_or_resume, "PrepareForSleep", dbus_interface=_LOGIND_INTERFACE, path=_LOGIND_PATH)
if logger.isEnabledFor(logging.INFO):
logger.info("connected to system dbus, watching for suspend/resume events")
Expand All @@ -71,7 +71,7 @@ def watch_bluez_connect(serial, callback=None):
if _bluetooth_callbacks.get(serial):
_bluetooth_callbacks.get(serial).remove()
path = _BLUETOOTH_PATH_PREFIX + serial.replace(":", "_").upper()
if callback is not None:
if bus is not None and callback is not None:
_bluetooth_callbacks[serial] = bus.add_signal_receiver(
callback, "PropertiesChanged", path=path, dbus_interface=_BLUETOOTH_INTERFACE
)

0 comments on commit c7dcc73

Please sign in to comment.