Mercurial > libervia-backend
diff sat_bridge/DBus.py @ 7:c14a3a7018a5
added dynamic exportation of Dbus bridge method (usefull for plugins)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 24 Oct 2009 01:05:17 +0200 |
parents | a06a151fc31f |
children | 14d7861ca59e |
line wrap: on
line diff
--- a/sat_bridge/DBus.py Fri Oct 23 17:04:22 2009 +0200 +++ b/sat_bridge/DBus.py Sat Oct 24 01:05:17 2009 +0200 @@ -27,6 +27,8 @@ import pdb from logging import debug, info, error +CONST_INT_PREFIX = "org.goffi.SAT" #Interface prefix + class DbusObject(dbus.service.Object): def __init__(self, bus, path): @@ -39,32 +41,32 @@ ### signals ### - @dbus.service.signal("org.goffi.SAT.communication", + @dbus.service.signal(CONST_INT_PREFIX+".communication", signature='sa{ss}as') def newContact(self, contact, attributes, groups): debug("new contact signal (%s) sended", contact) - @dbus.service.signal("org.goffi.SAT.communication", + @dbus.service.signal(CONST_INT_PREFIX+".communication", signature='ssss') def newMessage(self, from_jid, msg, type='chat', to=''): debug("new message signal (from:%s msg:%s type:%s to:%s) sended", from_jid, msg, type, to) - @dbus.service.signal("org.goffi.SAT.communication", + @dbus.service.signal(CONST_INT_PREFIX+".communication", signature='ssssi') def presenceUpdate(self, jid, type, show, status, priority): debug("presence update signal (from:%s type: %s show:%s status:\"%s\" priority:%d) sended" , jid, type, show, status, priority) - @dbus.service.signal("org.goffi.SAT.communication", + @dbus.service.signal(CONST_INT_PREFIX+".communication", signature='sss') def paramUpdate(self, name, value, namespace): debug("param update signal: %s=%s in namespace %s", name, value, namespace) - @dbus.service.signal("org.goffi.SAT.communication", + @dbus.service.signal(CONST_INT_PREFIX+".communication", signature='s') def contactDeleted(self, jid): debug("contact deleted signal: %s", jid) - @dbus.service.signal("org.goffi.SAT.request", + @dbus.service.signal(CONST_INT_PREFIX+".request", signature='ssa{ss}') def askConfirmation(self, type, id, data): debug("asking for confirmation: id = [%s] type = %s data = %s", id, type, data) @@ -73,111 +75,138 @@ ### methods ### - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='') def connect(self): info ("Connection asked") return self.cb["connect"]() - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='') def disconnect(self): info ("Disconnection asked") return self.cb["disconnect"]() - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='a(sa{ss}as)') def getContacts(self): debug("getContacts...") return self.cb["getContacts"]() - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='a(ssssi)') def getPresenceStatus(self): debug("getPresenceStatus...") return self.cb["getPresenceStatus"]() - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='ss', out_signature='') def sendMessage(self, to, message): debug("sendMessage...") self.cb["sendMessage"](to, message) - @dbus.service.method("org.goffi.SAT.communication", - in_signature='ss', out_signature='s') - def sendFile(self, to, path): - debug("sendFile...") - return self.cb["sendFile"](to, path) - - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='ssssi', out_signature='') def setPresence(self, to="", type="", show="", status="", priority=0): self.cb["setPresence"](to, type, show, status, priority) - - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='sss', out_signature='') def setParam(self, name, value, namespace="default"): self.cb["setParam"](name, str(value), namespace) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='ss', out_signature='(ss)') def getParam(self, name, namespace="default"): return self.cb["getParam"](name, namespace) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='s', out_signature='a(sss)') def getParams(self, namespace): return self.cb["getParams"](namespace) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='as') def getParamsCategories(self): return self.cb["getParamsCategories"]() - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='ssi', out_signature='a{i(ss)}') def getHistory(self, from_jid, to_jid, size): debug("History asked for %s", to_jid) return self.cb["getHistory"](from_jid, to_jid, size) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='s', out_signature='') def addContact(self, jid): debug("Subscription asked for %s", jid) return self.cb["addContact"](jid) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='s', out_signature='') def delContact(self, jid): debug("Unsubscription asked for %s", jid) return self.cb["delContact"](jid) - @dbus.service.method("org.goffi.SAT.communication", + @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='b') def isConnected(self): debug("Connection status requested") return self.cb["isConnected"]() - @dbus.service.method("org.goffi.SAT.request", + @dbus.service.method(CONST_INT_PREFIX+".request", in_signature='sba{ss}', out_signature='') def confirmationAnswer(self, id, accepted, data): debug("Answer for confirmation [%s]: %s", id, "Accepted" if accepted else "Refused") return self.cb["confirmationAnswer"](id, accepted, data) - @dbus.service.method("org.goffi.SAT.request", + @dbus.service.method(CONST_INT_PREFIX+".request", in_signature='s', out_signature='a{ss}') def getProgress(self, id): #debug("Progress asked for %s", id) return self.cb["getProgress"](id) + def _attribute_string(self, in_sign): + i=0 + idx=0 + attr_string="" + while i<len(in_sign): + if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: + raise Exception #FIXME: create an exception here (unmanaged attribute type) + + attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx) + idx+=1 + + if in_sign[i] == 'a': + while (True): + i+=1 + if i>=len(in_sign): + raise Exception #FIXME: create an exception here (the '}' is not presend) + if in_sign[i] == '}': + break + i+=1 + return attr_string + + + + def addMethod(self, name, int_suffix, in_sign, out_sign): + """Dynamically add a method to Dbus Bridge""" + #FIXME: Better way ??? + attributes = self._attribute_string(in_sign) + + code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') + exec (code) + method = locals()[name] + setattr(DbusObject, name, dbus.service.method( + CONST_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) + class DBusBridge(Bridge): def __init__(self): dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) Bridge.__init__(self) info ("Init DBus...") self.session_bus = dbus.SessionBus() - self.dbus_name = dbus.service.BusName("org.goffi.SAT", self.session_bus) + self.dbus_name = dbus.service.BusName(CONST_INT_PREFIX, self.session_bus) self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') def newContact(self, contact, attributes, groups): @@ -203,6 +232,12 @@ self.dbus_bridge.askConfirmation(type, id, data) def register(self, name, callback): - debug("enregistrement de %s",name) + debug("registering DBus bridge method [%s]",name) self.dbus_bridge.register(name, callback) + def addMethod(self, name, int_suffix, in_sign, out_sign, method): + """Dynamically add a method to Dbus Bridge""" + print ("Adding method [%s] to DBus bridge" % name) + self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) + self.register(name, method) +