Mercurial > libervia-backend
comparison src/bridge/DBus.py @ 300:233e6fce0b49
DBus bridge: using new generated bridge
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 21 Feb 2011 01:35:39 +0100 |
parents | 15c8916317d0 |
children | 953536246d9d |
comparison
equal
deleted
inserted
replaced
299:e044d1dc37d1 | 300:233e6fce0b49 |
---|---|
294 def subscription(self, sub_type, entity, profile_key="@DEFAULT@"): | 294 def subscription(self, sub_type, entity, profile_key="@DEFAULT@"): |
295 debug ("subscription") | 295 debug ("subscription") |
296 return self.cb["subscription"](unicode(sub_type), unicode(entity), unicode(profile_key)) | 296 return self.cb["subscription"](unicode(sub_type), unicode(entity), unicode(profile_key)) |
297 | 297 |
298 | 298 |
299 def __attribute_string(self, in_sign): | 299 def __attributes(self, in_sign): |
300 """Return arguments to user given a in_sign | 300 """Return arguments to user given a in_sign |
301 @param in_sign: in_sign in the short form (using s,a,i,b etc) | 301 @param in_sign: in_sign in the short form (using s,a,i,b etc) |
302 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" | 302 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" |
303 i=0 | 303 i=0 |
304 idx=0 | 304 idx=0 |
305 attr_string="" | 305 attr=[] |
306 while i<len(in_sign): | 306 while i<len(in_sign): |
307 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: | 307 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: |
308 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) | 308 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) |
309 | 309 |
310 attr_string += ("" if idx==0 else ", ") + ("arg_%i" % idx) | 310 attr.append("arg_%i" % idx) |
311 idx+=1 | 311 idx+=1 |
312 | 312 |
313 if in_sign[i] == 'a': | 313 if in_sign[i] == 'a': |
314 i+=1 | 314 i+=1 |
315 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays | 315 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays |
328 if in_sign[i] == closing_car: | 328 if in_sign[i] == closing_car: |
329 opening_count-=1 | 329 opening_count-=1 |
330 if opening_count == 0: | 330 if opening_count == 0: |
331 break | 331 break |
332 i+=1 | 332 i+=1 |
333 return attr_string | 333 return attr |
334 | 334 |
335 def addMethod(self, name, int_suffix, in_sign, out_sign, doc={}): | 335 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}): |
336 """Dynamically add a method to Dbus Bridge""" | 336 """Dynamically add a method to Dbus Bridge""" |
337 #FIXME: Better way ??? | 337 _attributes = self.__attributes(in_sign) |
338 attributes = self.__attribute_string(in_sign) | 338 |
339 if async: | |
340 _attributes.extend(['callback','errback']) | |
341 | |
342 attributes = ', '.join(_attributes) | |
339 | 343 |
340 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') | 344 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') |
341 exec (code) | 345 exec (code) |
342 method = locals()[name] | 346 method = locals()[name] |
347 async_callbacks = ('callback', 'errback') if async else None | |
343 setattr(DbusObject, name, dbus.service.method( | 348 setattr(DbusObject, name, dbus.service.method( |
344 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) | 349 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign, |
350 async_callbacks=async_callbacks)(method)) | |
345 function = getattr(self, name) | 351 function = getattr(self, name) |
346 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface] | 352 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface] |
347 func_table[function.__name__] = function #Needed for introspection | 353 func_table[function.__name__] = function #Needed for introspection |
348 | 354 |
349 def addSignal(self, name, int_suffix, signature, doc={}): | 355 def addSignal(self, name, int_suffix, signature, doc={}): |
350 """Dynamically add a signal to Dbus Bridge""" | 356 """Dynamically add a signal to Dbus Bridge""" |
351 #FIXME: Better way ??? | 357 attributes = ', '.join(self.__attributes(signature)) |
352 attributes = self.__attribute_string(signature) | |
353 | 358 |
354 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') | 359 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') |
355 exec (code) | 360 exec (code) |
356 signal = locals()[name] | 361 signal = locals()[name] |
357 setattr(DbusObject, name, dbus.service.signal( | 362 setattr(DbusObject, name, dbus.service.signal( |
414 | 419 |
415 def register(self, name, callback): | 420 def register(self, name, callback): |
416 debug("registering DBus bridge method [%s]", name) | 421 debug("registering DBus bridge method [%s]", name) |
417 self.dbus_bridge.register(name, callback) | 422 self.dbus_bridge.register(name, callback) |
418 | 423 |
419 def addMethod(self, name, int_suffix, in_sign, out_sign, method, doc={}): | 424 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
420 """Dynamically add a method to Dbus Bridge""" | 425 """Dynamically add a method to Dbus Bridge""" |
421 print ("Adding method [%s] to DBus bridge" % name) | 426 print ("Adding method [%s] to DBus bridge" % name) |
422 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) | 427 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc) |
423 self.register(name, method) | 428 self.register(name, method) |
424 | 429 |
425 def addSignal(self, name, int_suffix, signature, doc={}): | 430 def addSignal(self, name, int_suffix, signature, doc={}): |
426 self.dbus_bridge.addSignal(name, int_suffix, signature) | 431 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |
427 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) | 432 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) |