diff src/bridge/bridge_constructor/dbus_core_template.py @ 299:e044d1dc37d1

dbus bridge: asynchrone methods management
author Goffi <goffi@goffi.org>
date Sun, 20 Feb 2011 23:59:43 +0100
parents 15c8916317d0
children 4402ac630712
line wrap: on
line diff
--- a/src/bridge/bridge_constructor/dbus_core_template.py	Sun Feb 20 17:21:03 2011 +0100
+++ b/src/bridge/bridge_constructor/dbus_core_template.py	Sun Feb 20 23:59:43 2011 +0100
@@ -48,18 +48,18 @@
     
 ##METHODS_PART##
     
-    def __attribute_string(self, in_sign):
+    def __attributes(self, in_sign):
         """Return arguments to user given a in_sign
         @param in_sign: in_sign in the short form (using s,a,i,b etc)
         @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" 
         i=0
         idx=0
-        attr_string=""
+        attr=[]
         while i<len(in_sign):
             if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
                 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
 
-            attr_string += ("" if idx==0 else ", ") + ("arg_%i" % idx)
+            attr.append("arg_%i" % idx)
             idx+=1
 
             if in_sign[i] == 'a':
@@ -82,26 +82,31 @@
                         if opening_count == 0:
                             break
             i+=1
-        return attr_string
+        return attr
 
-    def addMethod(self, name, int_suffix, in_sign, out_sign, doc={}):
+    def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}):
         """Dynamically add a method to Dbus Bridge"""
-        #FIXME: Better way ???
-        attributes = self.__attribute_string(in_sign)
+        _attributes = self.__attributes(in_sign)
+
+        if async:
+            _attributes.extend(['callback','errback'])
+        
+        attributes = ', '.join(_attributes)
 
         code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
         exec (code)
         method = locals()[name]
+        async_callbacks = ('callback', 'errback') if async else None
         setattr(DbusObject, name, dbus.service.method(
-            const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method))
+            const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign,
+            async_callbacks=async_callbacks)(method))
         function = getattr(self, name)
         func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
         func_table[function.__name__] = function #Needed for introspection
     
     def addSignal(self, name, int_suffix, signature, doc={}):
         """Dynamically add a signal to Dbus Bridge"""
-        #FIXME: Better way ???
-        attributes = self.__attribute_string(signature)
+        attributes = ', '.join(self.__attributes(signature))
 
         code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
         exec (code)
@@ -127,13 +132,13 @@
         debug("registering DBus bridge method [%s]", name)
         self.dbus_bridge.register(name, callback)
 
-    def addMethod(self, name, int_suffix, in_sign, out_sign, method, doc={}):
+    def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
         """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.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc)
         self.register(name, method)
 
     def addSignal(self, name, int_suffix, signature, doc={}):
-        self.dbus_bridge.addSignal(name, int_suffix, signature)
+        self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
         setattr(DBusBridge, name, getattr(self.dbus_bridge, name))