Skip to content

Commit

Permalink
dbus helper for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Oct 11, 2018
1 parent 068c67a commit a0b00f3
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
54 changes: 54 additions & 0 deletions py3status/dbus_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from threading import Thread


class DBus:

def __init__(self, py3_wrapper):
try:
from gi.repository import GObject
from pydbus import SystemBus, SessionBus
self.GObject = GObject
self.SessionBus = SessionBus
self.SystemBus = SystemBus
self.initialized = True
except ImportError:
# FIXME logging
self.initialized = False
pass
self.py3_wrapper = py3_wrapper
self.started = False
self.bus_system = None
self.bus_session = None

def get_bus(self, bus):
if bus == "system":
if self.bus_system is None:
self.bus_system = self.SystemBus()
return self.bus_system
if self.bus_session is None:
self.bus_session = self.SessionBus()
return self.bus_session

def start_main_loop(self):
self.GObject.threads_init()
loop = self.GObject.MainLoop()
t = Thread(target=loop.run)
t.daemon = True
t.start()
self.started = True

def subscribe(self, path, callback, event, bus):
if not self.started:
self.start_main_loop()
bus = self.get_bus(bus)
manager = bus.get(path)
setattr(manager, event, callback)

def module_update(self, module):
"""
Create a small helper function that will force a module to update
"""
def update(*args):
module.force_update()
return update

2 changes: 2 additions & 0 deletions py3status/modules/battery_level.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ def post_config_hook(self):
self.py3.log("Measurement mode: " + self.measurement_mode)
if self.measurement_mode != "acpi" and self.measurement_mode != "sys":
raise NameError("Invalid measurement mode")
# subscribe to dbus power notifications
self.py3.dbus_subscribe(".UPower", 'update')

def battery_level(self):
if not os.listdir(self.sys_battery_path):
Expand Down
16 changes: 16 additions & 0 deletions py3status/py3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from uuid import uuid4

from py3status import exceptions
from py3status.dbus_helper import DBus
from py3status.constants import COLOR_NAMES
from py3status.formatter import Formatter, Composite
from py3status.request import HttpResponse
Expand Down Expand Up @@ -92,6 +93,7 @@ class Py3:
"""Show as Warning"""

# Shared by all Py3 Instances
_dbus = None
_formatter = None
_gradients = Gradiants()
_none_color = NoneColor()
Expand Down Expand Up @@ -131,6 +133,9 @@ def __init__(self, module=None):
if not self._formatter:
self.__class__._formatter = Formatter(module._py3_wrapper)

if not self._dbus:
self.__class__._dbus = DBus(module._py3_wrapper)

def __getattr__(self, name):
"""
Py3 can provide COLOR constants
Expand Down Expand Up @@ -250,6 +255,17 @@ def _report_exception(self, msg, frame_skip=2):
msg, notify_user=False, error_frame=error_frame
)

def dbus_subscribe(
self, path, callback, event="onPropertiesChanged", bus="system"
):
"""
subscribe to dbus
"""
if callback == "update":
callback = self._dbus.module_update(self._module)
self.log(callback)
self._dbus.subscribe(path, callback, event=event, bus=bus)

def error(self, msg, timeout=None):
"""
Raise an error for the module.
Expand Down

0 comments on commit a0b00f3

Please sign in to comment.