comparison sat_bridge/DBus.py @ 73:9d113b5471e6

Dynamic signal addition in bridge - DBus: new addSignal method - plugins XEP-0045: roomJoined is now a dynamically added signal
author Goffi <goffi@goffi.org>
date Mon, 22 Mar 2010 14:21:57 +1100
parents f271fff3a713
children 6e3a06b4dd36
comparison
equal deleted inserted replaced
72:f271fff3a713 73:9d113b5471e6
65 65
66 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, 66 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
67 signature='ssia{ss}s') 67 signature='ssia{ss}s')
68 def presenceUpdate(self, entity, show, priority, statuses, profile): 68 def presenceUpdate(self, entity, show, priority, statuses, profile):
69 debug("presence update signal (from:%s show:%s priority:%d statuses:%s profile:%s) sended" , entity, show, priority, statuses, profile) 69 debug("presence update signal (from:%s show:%s priority:%d statuses:%s profile:%s) sended" , entity, show, priority, statuses, profile)
70
71 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
72 signature='ssasss')
73 def roomJoined(self, room_id, room_service, room_nicks, user_nick, profile):
74 debug("room joined signal: id:%(room_id)s service:%(room_service)s nicks:%(room_nicks)s user:%(user_nick)s profile=%(profile)s" % {'room_id':room_id, 'room_service':room_service, 'room_nicks':room_nicks, 'user_nick':user_nick, 'profile':profile})
75 70
76 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX, 71 @dbus.service.signal(const_INT_PREFIX+const_COMM_SUFFIX,
77 signature='sss') 72 signature='sss')
78 def subscribe(self, type, entity, profile): 73 def subscribe(self, type, entity, profile):
79 debug("subscribe (type: [%s] from:[%s] profile:[%s])" , type, entity, profile) 74 debug("subscribe (type: [%s] from:[%s] profile:[%s])" , type, entity, profile)
263 258
264 attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx) 259 attr_string += ("" if idx==0 else ",") + ("arg_%i" % idx)
265 idx+=1 260 idx+=1
266 261
267 if in_sign[i] == 'a': 262 if in_sign[i] == 'a':
268 while (True): 263 i+=1
264 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: gof: must manage tuples out of arrays
265 i+=1
266 continue #we have a simple type for the array
267 while (True): #we have a dict or a list of tuples
269 i+=1 268 i+=1
270 if i>=len(in_sign): 269 if i>=len(in_sign):
271 raise Exception #FIXME: create an exception here (the '}' is not presend) 270 raise Exception #FIXME: create an exception here (the '}' is not presend)
272 if in_sign[i] == '}' or in_sign[i] == ')': 271 if in_sign[i] == '}' or in_sign[i] == ')':
273 break 272 break
284 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') 283 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
285 exec (code) 284 exec (code)
286 method = locals()[name] 285 method = locals()[name]
287 setattr(DbusObject, name, dbus.service.method( 286 setattr(DbusObject, name, dbus.service.method(
288 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) 287 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method))
288
289 def addSignal(self, name, int_suffix, signature):
290 """Dynamically add a signal to Dbus Bridge"""
291 #FIXME: Better way ???
292 attributes = self.__attribute_string(signature)
293
294 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
295 exec (code)
296 signal = locals()[name]
297 setattr(DbusObject, name, dbus.service.signal(
298 const_INT_PREFIX+int_suffix, signature=signature)(signal))
289 299
290 class DBusBridge(Bridge): 300 class DBusBridge(Bridge):
291 def __init__(self): 301 def __init__(self):
292 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 302 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
293 Bridge.__init__(self) 303 Bridge.__init__(self)
348 """Dynamically add a method to Dbus Bridge""" 358 """Dynamically add a method to Dbus Bridge"""
349 print ("Adding method [%s] to DBus bridge" % name) 359 print ("Adding method [%s] to DBus bridge" % name)
350 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) 360 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign)
351 self.register(name, method) 361 self.register(name, method)
352 362
363 def addSignal(self, name, int_suffix, signature):
364 self.dbus_bridge.addSignal(name, int_suffix, signature)
365