Mercurial > libervia-backend
view 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 source
#!/usr/bin/python #-*- coding: utf-8 -*- """ SAT: a jabber client Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. """ from bridge import Bridge import dbus import dbus.service import dbus.mainloop.glib 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): dbus.service.Object.__init__(self, bus, path) debug("Init DbusObject...") self.cb={} def register(self, name, cb): self.cb[name]=cb ### signals ### @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(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(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(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(CONST_INT_PREFIX+".communication", signature='s') def contactDeleted(self, jid): debug("contact deleted signal: %s", jid) @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) ### methods ### @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='') def connect(self): info ("Connection asked") return self.cb["connect"]() @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='') def disconnect(self): info ("Disconnection asked") return self.cb["disconnect"]() @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(CONST_INT_PREFIX+".communication", in_signature='', out_signature='a(ssssi)') def getPresenceStatus(self): debug("getPresenceStatus...") return self.cb["getPresenceStatus"]() @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(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(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(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(CONST_INT_PREFIX+".communication", in_signature='s', out_signature='a(sss)') def getParams(self, namespace): return self.cb["getParams"](namespace) @dbus.service.method(CONST_INT_PREFIX+".communication", in_signature='', out_signature='as') def getParamsCategories(self): return self.cb["getParamsCategories"]() @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(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(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(CONST_INT_PREFIX+".communication", in_signature='', out_signature='b') def isConnected(self): debug("Connection status requested") return self.cb["isConnected"]() @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(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(CONST_INT_PREFIX, self.session_bus) self.dbus_bridge = DbusObject(self.session_bus, '/org/goffi/SAT/bridge') def newContact(self, contact, attributes, groups): self.dbus_bridge.newContact(contact, attributes, groups) def newMessage(self,from_jid,msg,type='chat', to=''): debug("sending message...") self.dbus_bridge.newMessage(from_jid, msg, type, to) def presenceUpdate(self, jid, type, show, status, priority): debug("updating presence for %s",jid) self.dbus_bridge.presenceUpdate(jid, type, show, status, priority) def paramUpdate(self, name, value, namespace): debug("updating param [%s] %s ", namespace, name) self.dbus_bridge.paramUpdate(name, value, namespace) def contactDeleted(self, jid): debug("sending contact deleted signal %s ", jid) self.dbus_bridge.contactDeleted(jid) def askConfirmation(self, type, id, data): self.dbus_bridge.askConfirmation(type, id, data) def register(self, name, callback): 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)