Last active
May 10, 2023 07:47
-
-
Save ksvbka/f68deea30793802b78dc5a5d29a3afb1 to your computer and use it in GitHub Desktop.
Python Session bus demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# D-Bus Client V2 -- Session Bus | |
import dbus | |
bus = dbus.SessionBus() | |
session = bus.get_object("org.me.test_session", "/org/me/test_session") | |
interface1 = dbus.Interface(session, "org.me.test1") | |
interface2 = dbus.Interface(session, "org.me.test2") | |
# Call the methods using the interface | |
print(interface1.session_bus_message1()) | |
print(interface2.session_bus_message2()) | |
print(interface2.session_bus_strings("Hello", "world")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# D-Bus Client V1 -- Session Bus | |
import dbus | |
bus = dbus.SessionBus() | |
session = bus.get_object("org.me.test_session", "/org/me/test_session") | |
method_message1 = session.get_dbus_method('session_bus_message1', 'org.me.test1') | |
method_message2 = session.get_dbus_method('session_bus_message2', 'org.me.test2') | |
method_message3 = session.get_dbus_method('session_bus_strings', 'org.me.test2') | |
# Call the methods with their specific parameters | |
print(method_message1()) | |
print(method_message2()) | |
print(method_message3("Hello", "world")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import dbus | |
import os | |
import glib | |
import dbus.mainloop.glib | |
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | |
def finished(res): | |
print "finished: ", res | |
def progress(percent): | |
print "progress: ", percent | |
system_bus = dbus.SystemBus() | |
axi = dbus.Interface(system_bus.get_object("org.debian.AptXapianIndex","/"), | |
"org.debian.AptXapianIndex") | |
axi.connect_to_signal("UpdateFinished", finished) | |
axi.connect_to_signal("UpdateProgress", progress) | |
# force, update_only | |
axi.update_async(True, True) | |
glib.MainLoop().run() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3 | |
# D-Bus Server -- Session Bus | |
from gi.repository import GLib | |
import dbus | |
import dbus.service | |
from dbus.mainloop.glib import DBusGMainLoop | |
class Session_DBus(dbus.service.Object): | |
def __init__(self): | |
bus_name = dbus.service.BusName('org.me.test_session', bus=dbus.SessionBus()) | |
dbus.service.Object.__init__(self, bus_name, '/org/me/test_session') | |
# Interface and Method | |
@dbus.service.method('org.me.test1') | |
def session_bus_message1(self): | |
return "Session Bus 1" | |
# Different Interface and different Method | |
# The method must not have not the same name as the first | |
@dbus.service.method('org.me.test2') | |
def session_bus_message2(self): | |
return "Session Bus 2" | |
# Method with arguments | |
@dbus.service.method('org.me.test2') | |
def session_bus_strings(self, string1, string2): | |
return string1 + " " + string2 | |
DBusGMainLoop(set_as_default=True) | |
dbus_service = Session_DBus() | |
try: | |
GLib.MainLoop().run() | |
except KeyboardInterrupt: | |
print("\nThe MainLoop will close...") | |
GLib.MainLoop().quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment