diff 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
line wrap: on
line diff
--- a/src/bridge/DBus.py	Fri Mar 30 09:23:23 2012 +0200
+++ b/src/bridge/DBus.py	Sun Apr 01 19:45:35 2012 +0200
@@ -431,27 +431,29 @@
         """Dynamically add a method to Dbus Bridge"""
         inspect_args = inspect.getargspec(method)
 
-        _attributes = inspect_args.args
+        _arguments = inspect_args.args
         _defaults = list(inspect_args.defaults or [])
         
         if inspect.ismethod(method):
             #if we have a method, we don't want the first argument (usually 'self')
-            del(_attributes[0])
+            del(_arguments[0])
 
+        #first arguments are for the _callback method
+        arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback','errback=errback']) if async else _arguments))
+        
         if async:
-            _attributes.extend(['callback','errback'])
-            _defaults.extend(['callback', 'errback'])
+            _arguments.extend(['callback','errback'])
+            _defaults.extend([None, None])
         
-        attributes = ', '.join(_attributes)
         
         #now we create a second list with default values
         for i in range(1, len(_defaults)+1):
-            _attributes[-i] = "%s = %s" % (_attributes[-i], repr(_defaults[-i]))
+            _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i]))
 
-        attributes_defaults =  ', '.join(_attributes)
+        arguments_defaults =  ', '.join(_arguments)
 
-        code = compile ('def %(name)s (self,%(attributes_defaults)s): return self.cb["%(name)s"](%(attributes)s)' %
-                        {'name':name, 'attributes_defaults':attributes_defaults, 'attributes':attributes}, '<DBus bridge>','exec')
+        code = compile ('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' %
+                        {'name':name, 'arguments_defaults':arguments_defaults, 'arguments_callback':arguments_callback}, '<DBus bridge>','exec')
         exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec
         method = locals()[name]
         async_callbacks = ('callback', 'errback') if async else None