Python 导出Dbus对象

Python 导出Dbus对象,python,dbus,libnotify,Python,Dbus,Libnotify,为什么对象不能导出 from dbus.mainloop.glib import DBusGMainLoop as glib_main from gi.repository import GLib import dbus.service as Service import dbus as DBus import pymongo as Mgo class Emitter(object): __signals__ = {} def sig(self, sig, *args):

为什么对象不能导出

from dbus.mainloop.glib import DBusGMainLoop as glib_main
from gi.repository import GLib
import dbus.service as Service
import dbus as DBus
import pymongo as Mgo

class Emitter(object):
    __signals__ = {}

    def sig(self, sig, *args):
        self.__signals__[sig](*args)

    def on(self, sig, f):
        self.__signals__[sig] = f

class Notifier(Service.Object, Emitter):
    __signals__ = {"notified" : None, "update" : None}
    def __init__(self, conn, bus):
        Service.Object.__init__(self, object_path = "/org/freedesktop/Notifications", bus_name = Service.BusName(bus, conn))
        Emitter.__init__(self)
        self.client = Mgo.MongoClient()
        self.lot = self.client["metroid"]["notifications"]
        self.server_info = {"name" : "bang-notifier/metroid", "vendor" : "christopherlramsey@gmx.us", "version" : "v0.1", "spec-version" : "v1.2"}
        self.connect = self.on
        self.emit = self.sig
        self.connect("update", self.update)

    def update(self):
        self.emit("notified", None, self.lot.find())

    def delete(self, id):
        self.CloseNotification(id)

    @Service.method("org.freedesktop.Notifications", out_signature="as")
    def GetCapabilities(self):
        return ["actions", "action-icons", "body", "body-markup", "icon-static", "persistence"]

    @Service.method("org.freedesktop.Notifications", in_signature="susssasa{sv}i", out_signature="u")
    def Notify(self, appName, replacesId, appIcon, summary, body, actions, hints, expireTimeout):
        message = {"id" : replacesId, "icon" : appIcon, "summary" : summary, "body" : body, "actions" : actions, "hints" : hints, "expires" : expireTimeout}
        if message["id"] == 0:
            message["id"] = self.lot.count() 
            if message["id"] == 0: message["id"] = 1
            self.lot.insert(message)
        else:
            self.lot.update({"id" : replacesId}, {"$set", message})
        self.emit("notified", message, None)
        return message["id"]

    @Service.method("org.freedesktop.Notifications", in_signature="u")
    def CloseNotification(self, id):
        self.lot.remove({"id" : id})
        self.NotificationClosed(id)

    @Service.method("org.freedesktop.Notifications", out_signature="ssss")
    def GetServerInformation(self):
        return self.server_info.values()

    @Service.signal("org.freedesktop.Notifications", signature="u")
    def NotificationClosed(self, id):
        pass

    @Service.signal("org.freedesktop.Notifications", signature="us")
    def ActionInvoked(self, id, actionKey):
        pass

    def close(self):
        self.client.close()


if __name__ == '__main__':
    from gi.repository import GLib
    loop = GLib.MainLoop()
    glib_main(set_as_default = True)
    conn = DBus.SessionBus(mainloop = loop)
    Notifier(conn, "org.freedesktop.Notifications")
    loop.run()

您的错误在于设置主循环。您需要使用
DBusGMainLoop
作为其工作主循环来创建总线连接:

if __name__ == '__main__':
    from gi.repository import GLib
    loop = GLib.MainLoop()
    glib_main(set_as_default = True)
    conn = DBus.SessionBus(mainloop = loop) # <**offending line**>
    Notifier(conn, "org.freedesktop.Notifications")
    loop.run()
这里是一个链接

此外,我不确定您当时是否有错误消息,但如果您对您试图执行的操作和错误消息的理解(如果可能)提供多一点描述,您可能会得到更多的响应

if __name__ == '__main__':
    from gi.repository import GLib
    loop = GLib.MainLoop()
    glib_main(set_as_default = True)
    conn = DBus.SessionBus(mainloop=DBusGMainLoop()) <-- change to -->
    # conn = DBus.SessionBus(mainloop = glib_main()) <-- as you have it imported -->
    Notifier(conn, "org.freedesktop.Notifications")
    loop.run()