0

为什么不导出对象?

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()
4

1 回答 1

2

您的错误在于设置主循环。您需要使用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()

当您只传入时,GLib.MainLoop它需要的是DBusGMainLoop

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()

这是设置事件循环的链接。

另外,我当时不确定您的错误消息是否有,但是,如果您提供更多关于您对您正在尝试做什么的理解的描述以及错误消息,您可能会得到更多的响应,如果可能的。

于 2014-11-11T00:01:12.870 回答