comparison 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
comparison
equal deleted inserted replaced
298:15c8916317d0 299:e044d1dc37d1
46 46
47 ### methods ### 47 ### methods ###
48 48
49 ##METHODS_PART## 49 ##METHODS_PART##
50 50
51 def __attribute_string(self, in_sign): 51 def __attributes(self, in_sign):
52 """Return arguments to user given a in_sign 52 """Return arguments to user given a in_sign
53 @param in_sign: in_sign in the short form (using s,a,i,b etc) 53 @param in_sign: in_sign in the short form (using s,a,i,b etc)
54 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")""" 54 @return: list of arguments that correspond to a in_sign (e.g.: "sss" return "arg1, arg2, arg3")"""
55 i=0 55 i=0
56 idx=0 56 idx=0
57 attr_string="" 57 attr=[]
58 while i<len(in_sign): 58 while i<len(in_sign):
59 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']: 59 if in_sign[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
60 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i]) 60 raise ParseError("Unmanaged attribute type [%c]" % in_sign[i])
61 61
62 attr_string += ("" if idx==0 else ", ") + ("arg_%i" % idx) 62 attr.append("arg_%i" % idx)
63 idx+=1 63 idx+=1
64 64
65 if in_sign[i] == 'a': 65 if in_sign[i] == 'a':
66 i+=1 66 i+=1
67 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays 67 if in_sign[i]!='{' and in_sign[i]!='(': #FIXME: must manage tuples out of arrays
80 if in_sign[i] == closing_car: 80 if in_sign[i] == closing_car:
81 opening_count-=1 81 opening_count-=1
82 if opening_count == 0: 82 if opening_count == 0:
83 break 83 break
84 i+=1 84 i+=1
85 return attr_string 85 return attr
86 86
87 def addMethod(self, name, int_suffix, in_sign, out_sign, doc={}): 87 def addMethod(self, name, int_suffix, in_sign, out_sign, async=False, doc={}):
88 """Dynamically add a method to Dbus Bridge""" 88 """Dynamically add a method to Dbus Bridge"""
89 #FIXME: Better way ??? 89 _attributes = self.__attributes(in_sign)
90 attributes = self.__attribute_string(in_sign) 90
91 if async:
92 _attributes.extend(['callback','errback'])
93
94 attributes = ', '.join(_attributes)
91 95
92 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec') 96 code = compile ('def '+name+' (self,'+attributes+'): return self.cb["'+name+'"]('+attributes+')', '<DBus bridge>','exec')
93 exec (code) 97 exec (code)
94 method = locals()[name] 98 method = locals()[name]
99 async_callbacks = ('callback', 'errback') if async else None
95 setattr(DbusObject, name, dbus.service.method( 100 setattr(DbusObject, name, dbus.service.method(
96 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign)(method)) 101 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign,
102 async_callbacks=async_callbacks)(method))
97 function = getattr(self, name) 103 function = getattr(self, name)
98 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface] 104 func_table = self._dbus_class_table[self.__class__.__module__ + '.' + self.__class__.__name__][function._dbus_interface]
99 func_table[function.__name__] = function #Needed for introspection 105 func_table[function.__name__] = function #Needed for introspection
100 106
101 def addSignal(self, name, int_suffix, signature, doc={}): 107 def addSignal(self, name, int_suffix, signature, doc={}):
102 """Dynamically add a signal to Dbus Bridge""" 108 """Dynamically add a signal to Dbus Bridge"""
103 #FIXME: Better way ??? 109 attributes = ', '.join(self.__attributes(signature))
104 attributes = self.__attribute_string(signature)
105 110
106 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') 111 code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
107 exec (code) 112 exec (code)
108 signal = locals()[name] 113 signal = locals()[name]
109 setattr(DbusObject, name, dbus.service.signal( 114 setattr(DbusObject, name, dbus.service.signal(
125 130
126 def register(self, name, callback): 131 def register(self, name, callback):
127 debug("registering DBus bridge method [%s]", name) 132 debug("registering DBus bridge method [%s]", name)
128 self.dbus_bridge.register(name, callback) 133 self.dbus_bridge.register(name, callback)
129 134
130 def addMethod(self, name, int_suffix, in_sign, out_sign, method, doc={}): 135 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}):
131 """Dynamically add a method to Dbus Bridge""" 136 """Dynamically add a method to Dbus Bridge"""
132 print ("Adding method [%s] to DBus bridge" % name) 137 print ("Adding method [%s] to DBus bridge" % name)
133 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign) 138 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, async, doc)
134 self.register(name, method) 139 self.register(name, method)
135 140
136 def addSignal(self, name, int_suffix, signature, doc={}): 141 def addSignal(self, name, int_suffix, signature, doc={}):
137 self.dbus_bridge.addSignal(name, int_suffix, signature) 142 self.dbus_bridge.addSignal(name, int_suffix, signature, doc)
138 setattr(DBusBridge, name, getattr(self.dbus_bridge, name)) 143 setattr(DBusBridge, name, getattr(self.dbus_bridge, name))
139 144