comparison src/bridge/bridge_constructor/dbus_core_template.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
145 145
146 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): 146 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False):
147 """Dynamically add a method to Dbus Bridge""" 147 """Dynamically add a method to Dbus Bridge"""
148 inspect_args = inspect.getargspec(method) 148 inspect_args = inspect.getargspec(method)
149 149
150 _attributes = inspect_args.args 150 _arguments = inspect_args.args
151 _defaults = list(inspect_args.defaults or []) 151 _defaults = list(inspect_args.defaults or [])
152 152
153 if inspect.ismethod(method): 153 if inspect.ismethod(method):
154 #if we have a method, we don't want the first argument (usually 'self') 154 #if we have a method, we don't want the first argument (usually 'self')
155 del(_attributes[0]) 155 del(_arguments[0])
156 156
157 #first arguments are for the _callback method
158 arguments_callback = ', '.join([repr(name)] + ((_arguments + ['callback=callback','errback=errback']) if async else _arguments))
159
157 if async: 160 if async:
158 _attributes.extend(['callback','errback']) 161 _arguments.extend(['callback','errback'])
159 _defaults.extend(['callback', 'errback']) 162 _defaults.extend([None, None])
160 163
161 attributes = ', '.join(_attributes)
162 164
163 #now we create a second list with default values 165 #now we create a second list with default values
164 for i in range(1, len(_defaults)+1): 166 for i in range(1, len(_defaults)+1):
165 _attributes[-i] = "%s = %s" % (_attributes[-i], repr(_defaults[-i])) 167 _arguments[-i] = "%s = %s" % (_arguments[-i], repr(_defaults[-i]))
166 168
167 attributes_defaults = ', '.join(_attributes) 169 arguments_defaults = ', '.join(_arguments)
168 170
169 code = compile ('def %(name)s (self,%(attributes_defaults)s): return self.cb["%(name)s"](%(attributes)s)' % 171 code = compile ('def %(name)s (self,%(arguments_defaults)s): return self._callback(%(arguments_callback)s)' %
170 {'name':name, 'attributes_defaults':attributes_defaults, 'attributes':attributes}, '<DBus bridge>','exec') 172 {'name':name, 'arguments_defaults':arguments_defaults, 'arguments_callback':arguments_callback}, '<DBus bridge>','exec')
171 exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec 173 exec (code) #FIXME: to the same thing in a cleaner way, without compile/exec
172 method = locals()[name] 174 method = locals()[name]
173 async_callbacks = ('callback', 'errback') if async else None 175 async_callbacks = ('callback', 'errback') if async else None
174 setattr(DbusObject, name, dbus.service.method( 176 setattr(DbusObject, name, dbus.service.method(
175 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign, 177 const_INT_PREFIX+int_suffix, in_signature=in_sign, out_signature=out_sign,