From 6f93823fad0e752fd5a44da1426389b2611a2766 Mon Sep 17 00:00:00 2001 From: Volpeon Date: Sat, 27 Feb 2021 21:35:47 +0100 Subject: Switch from dbus-python to pydbus --- mpris_to_text.py | 68 ++++++++++++++++++++------------------------------------ 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/mpris_to_text.py b/mpris_to_text.py index c5d65d8..2f75350 100755 --- a/mpris_to_text.py +++ b/mpris_to_text.py @@ -6,16 +6,10 @@ import threading import argparse import signal -import dbus -import dbus.mainloop.glib from gi.repository import GLib - +from pydbus import SessionBus from blessed import Terminal - -dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) - - class MetaWriter: filename = "" output_format = "" @@ -65,7 +59,8 @@ class PlayerSelector(threading.Thread): def __init__(self, meta_writer, menu=None): super().__init__() - self.bus = dbus.SessionBus() + self.bus = SessionBus() + self.loop = GLib.MainLoop() self.meta_writer = meta_writer self.menu = menu @@ -76,26 +71,21 @@ class PlayerSelector(threading.Thread): self.players = {} self.players_indexes = [] - bus_path = self.bus.get_object( - "org.freedesktop.DBus", "/org/freedesktop/DBus") - bus_proxy = dbus.Interface(bus_path, "org.freedesktop.DBus") + bus_ctrl = self.bus.get(".DBus") - for service in bus_proxy.ListNames(): + for service in bus_ctrl.ListNames(): if self.service_regex.match(service): split_service = service.split(".") name_start_index = len( split_service[0]) + len(split_service[1]) + len(split_service[2]) + 3 - player_path = self.bus.get_object( - service, "/org/mpris/MediaPlayer2") - player_props = dbus.Interface( - player_path, "org.freedesktop.DBus.Properties") - player_name = service[name_start_index:] + player = self.bus.get(service, "/org/mpris/MediaPlayer2") + player_name = "" + try: - player_name = player_props.Get( - "org.mpris.MediaPlayer2.Player", "Identity") + player_name = player.Identity except dbus.exceptions.DBusException: - pass + player_name = service[name_start_index:] self.players[service] = player_name self.players_indexes.append(service) @@ -115,38 +105,28 @@ class PlayerSelector(threading.Thread): self.active_player = "" if self.signal_receiver is not None: - self.signal_receiver.remove() + self.signal_receiver.disconnect() if self.active_player != "": - self.signal_receiver = self.bus.add_signal_receiver( - handler_function=self.playing_song_changed, - bus_name=self.active_player, - dbus_interface="org.freedesktop.DBus.Properties", - signal_name="PropertiesChanged", - path="/org/mpris/MediaPlayer2" - ) + player = self.bus.get(self.active_player, "/org/mpris/MediaPlayer2") - player_path = self.bus.get_object( - self.active_player, "/org/mpris/MediaPlayer2") - player_props = dbus.Interface( - player_path, "org.freedesktop.DBus.Properties") + self.signal_receiver = self.bus.subscribe( + sender=self.active_player, + object="/org/mpris/MediaPlayer2", + signal="PropertiesChanged", + signal_fired=self.playing_song_changed + ) - self.meta_writer.write_meta(player_props.Get( - "org.mpris.MediaPlayer2.Player", "Metadata")) + self.meta_writer.write_meta(player.Metadata) else: self.meta_writer.write("") def run(self): - bus_path = self.bus.get_object( - "org.freedesktop.DBus", "/org/freedesktop/DBus") - bus_proxy = dbus.Interface(bus_path, "org.freedesktop.DBus") - bus_proxy.connect_to_signal( - "NameOwnerChanged", self.dbus_name_owner_changed) + bus_ctrl = self.bus.get(".DBus") + bus_ctrl.NameOwnerChanged.connect(self.dbus_name_owner_changed) self.get_players() self.menu.refresh() - - self.loop = GLib.MainLoop() self.loop.run() def quit(self): @@ -157,9 +137,9 @@ class PlayerSelector(threading.Thread): self.get_players() self.menu.refresh() - def playing_song_changed(self, player, changed_properties, invalidated_properties): - if "Metadata" in changed_properties: - self.meta_writer.write_meta(changed_properties["Metadata"]) + def playing_song_changed(self, sender, object, iface, signal, params): + if "Metadata" in params[1]: + self.meta_writer.write_meta(params[1]["Metadata"]) self.menu.refresh() -- cgit v1.2.3-70-g09d2