comparison src/bridge/DBus.py @ 469:db4c2b82bab6

D-Bus bridge: fixed getRoomsSubjects call + fixed dynamic addition of methods + added getLastGroupBlogs method
author Goffi <goffi@goffi.org>
date Sun, 01 Apr 2012 19:45:35 +0200
parents c97640c90a94
children 031b0e0aaab8
comparison
equal deleted inserted replaced
468:c97640c90a94 469:db4c2b82bab6
429 429
430 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): 430 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
431 """Dynamically add a method to Dbus Bridge""" 431 """Dynamically add a method to Dbus Bridge"""
432 inspect_args = inspect.getargspec(method) 432 inspect_args = inspect.getargspec(method)
433 433
434 _attributes = inspect_args.args 434 _arguments = inspect_args.args
435 _defaults = list(inspect_args.defaults or []) 435 _defaults = list(inspect_args.defaults or [])
436 436
437 if inspect.ismethod(method): 437 if inspect.ismethod(method):
438 #if we have a method, we don't want the first argument (usually 'self') 438 #if we have a method, we don't want the first argument (usually 'self')
439 del(_attributes[0]) 439 del(_arguments[0])
440 440
441 #first arguments are for the _callback method
442 arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback','errback=errback']) if async else _arguments))
443
441 if async: 444 if async:
442 _attributes.extend(['callback','errback']) 445 _arguments.extend(['callback','errback'])
443 _defaults.extend(['callback', 'errback']) 446 _defaults.extend([None, None])
444 447
445 attributes = ', '.join(_attributes)
446 448
447 #now we create a second list with default values 449 #now we create a second list with default values
448 for i in range(1, len(_defaults)+1): 450 for i in range(1, len(_defaults)+1):
449 _attributes[-i] = "%s = %s" % (_attributes[-i], repr(_defaults[-i])) 451 _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i]))
450 452
451 attributes_defaults = ', '.join(_attributes) 453 arguments_defaults = ', '.join(_arguments)
452 454
453 code = compile ('def %(name)s (self,%(attributes_defaults)s): return self.cb["%(name)s"](%(attributes)s)' % 455 code = compile ('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' %
454 {'name':name, 'attributes_defaults':attributes_defaults, 'attributes':attributes}, '<DBus bridge>','exec') 456 {'name':name, 'arguments_defaults':arguments_defaults, 'arguments_callback':arguments_callback}, '<DBus bridge>','exec')
455 exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec 457 exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec
456 method = locals()[name] 458 method = locals()[name]
457 async_callbacks = ('callback', 'errback') if async else None 459 async_callbacks = ('callback', 'errback') if async else None
458 setattr(DbusObject, name, dbus.service.method( 460 setattr(DbusObject, name, dbus.service.method(
459 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign, 461 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign,