为什么不导出对象?
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()