changeset 337:4402ac630712

bridge: async callback managed in bridge_constructor + misc - the new flag async make the method asynchronous - new method asyncConnect (connect which return when the user is connected) - setMicroblogAccess added to DBus frontend
author Goffi <goffi@goffi.org>
date Thu, 26 May 2011 16:47:09 +0200
parents 953536246d9d
children 6dcdc4cf8622
files frontends/src/bridge/DBus.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_contructor.py src/bridge/bridge_constructor/bridge_template.ini src/bridge/bridge_constructor/dbus_core_template.py src/bridge/bridge_constructor/dbus_frontend_template.py
diffstat 6 files changed, 109 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Wed May 25 17:18:05 2011 +0200
+++ b/frontends/src/bridge/DBus.py	Thu May 26 16:47:09 2011 +0200
@@ -52,6 +52,9 @@
     def addContact(self, entity, profile_key="@DEFAULT@"):
         return self.db_comm_iface.addContact(entity, profile_key)
 
+    def asyncConnect(self, profile_key="@DEFAULT@", callback=None, errback=None):
+        return self.db_comm_iface.asyncConnect(profile_key, reply_handler=callback, error_handler=errback)
+
     def callMenu(self, category, name, menu_type, profile_key):
         return unicode(self.db_req_iface.callMenu(category, name, menu_type, profile_key))
 
@@ -199,3 +202,6 @@
     
     def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
         return self.db_comm_iface.sendPersonalEvent(event_type, data, profile_key)
+    
+    def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@', callback=None, errback=None):
+        return self.db_comm_iface.setMicroblogAccess(access, profile_key, reply_handler=callback, error_handler=errback)
--- a/src/bridge/DBus.py	Wed May 25 17:18:05 2011 +0200
+++ b/src/bridge/DBus.py	Thu May 26 16:47:09 2011 +0200
@@ -116,152 +116,188 @@
     ### methods ###    
     
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ss', out_signature='')
+                         in_signature='ss', out_signature='',
+                         async_callbacks=None)
     def addContact(self, entity, profile_key="@DEFAULT@"):
         return self.cb["addContact"](unicode(entity), unicode(profile_key))
 
+    @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
+                         in_signature='s', out_signature='',
+                         async_callbacks=('callback', 'errback'))
+    def asyncConnect(self, profile_key="@DEFAULT@", callback=None, errback=None):
+        return self.cb["asyncConnect"](unicode(profile_key), callback, errback)
+
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='ssss', out_signature='s')
+                         in_signature='ssss', out_signature='s',
+                         async_callbacks=None)
     def callMenu(self, category, name, menu_type, profile_key):
         return self.cb["callMenu"](unicode(category), unicode(name), unicode(menu_type), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='sba{ss}', out_signature='')
+                         in_signature='sba{ss}', out_signature='',
+                         async_callbacks=None)
     def confirmationAnswer(self, id, accepted, data):
         return self.cb["confirmationAnswer"](unicode(id), accepted, data)
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='')
+                         in_signature='s', out_signature='',
+                         async_callbacks=None)
     def connect(self, profile_key="@DEFAULT@"):
         return self.cb["connect"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='s', out_signature='i')
+                         in_signature='s', out_signature='i',
+                         async_callbacks=None)
     def createProfile(self, profile):
         return self.cb["createProfile"](unicode(profile))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ss', out_signature='')
+                         in_signature='ss', out_signature='',
+                         async_callbacks=None)
     def delContact(self, entity, profile_key="@DEFAULT@"):
         return self.cb["delContact"](unicode(entity), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='s', out_signature='i')
+                         in_signature='s', out_signature='i',
+                         async_callbacks=None)
     def deleteProfile(self, profile):
         return self.cb["deleteProfile"](unicode(profile))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='')
+                         in_signature='s', out_signature='',
+                         async_callbacks=None)
     def disconnect(self, profile_key="@DEFAULT@"):
         return self.cb["disconnect"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='a(sa{ss}as)')
+                         in_signature='s', out_signature='a(sa{ss}as)',
+                         async_callbacks=None)
     def getContacts(self, profile_key="@DEFAULT@"):
         return self.cb["getContacts"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ssi', out_signature='a{i(ss)}')
+                         in_signature='ssi', out_signature='a{i(ss)}',
+                         async_callbacks=None)
     def getHistory(self, from_jid, to_jid, size):
         return self.cb["getHistory"](unicode(from_jid), unicode(to_jid), size)
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='sss', out_signature='s')
+                         in_signature='sss', out_signature='s',
+                         async_callbacks=None)
     def getMenuHelp(self, category, name, menu_type):
         return self.cb["getMenuHelp"](unicode(category), unicode(name), unicode(menu_type))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='', out_signature='a(sss)')
+                         in_signature='', out_signature='a(sss)',
+                         async_callbacks=None)
     def getMenus(self, ):
         return self.cb["getMenus"]()
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ssss', out_signature='s')
+                         in_signature='ssss', out_signature='s',
+                         async_callbacks=None)
     def getParamA(self, name, category, attribute="value", profile_key="@DEFAULT@"):
         return self.cb["getParamA"](unicode(name), unicode(category), unicode(attribute), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='s')
+                         in_signature='s', out_signature='s',
+                         async_callbacks=None)
     def getParams(self, profile_key="@DEFAULT@"):
         return self.cb["getParams"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='', out_signature='as')
+                         in_signature='', out_signature='as',
+                         async_callbacks=None)
     def getParamsCategories(self, ):
         return self.cb["getParamsCategories"]()
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ss', out_signature='s')
+                         in_signature='ss', out_signature='s',
+                         async_callbacks=None)
     def getParamsForCategory(self, category, profile_key="@DEFAULT@"):
         return self.cb["getParamsForCategory"](unicode(category), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='s')
+                         in_signature='s', out_signature='s',
+                         async_callbacks=None)
     def getParamsUI(self, profile_key="@DEFAULT@"):
         return self.cb["getParamsUI"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='a{sa{s(sia{ss})}}')
+                         in_signature='s', out_signature='a{sa{s(sia{ss})}}',
+                         async_callbacks=None)
     def getPresenceStatus(self, profile_key="@DEFAULT@"):
         return self.cb["getPresenceStatus"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='s', out_signature='s')
+                         in_signature='s', out_signature='s',
+                         async_callbacks=None)
     def getProfileName(self, profile_key="@DEFAULT@"):
         return self.cb["getProfileName"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='', out_signature='as')
+                         in_signature='', out_signature='as',
+                         async_callbacks=None)
     def getProfilesList(self, ):
         return self.cb["getProfilesList"]()
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='s', out_signature='a{ss}')
+                         in_signature='s', out_signature='a{ss}',
+                         async_callbacks=None)
     def getProgress(self, id):
         return self.cb["getProgress"](unicode(id))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='', out_signature='s')
+                         in_signature='', out_signature='s',
+                         async_callbacks=None)
     def getVersion(self, ):
         return self.cb["getVersion"]()
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='a{ss}')
+                         in_signature='s', out_signature='a{ss}',
+                         async_callbacks=None)
     def getWaitingSub(self, profile_key="@DEFAULT@"):
         return self.cb["getWaitingSub"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='s', out_signature='b')
+                         in_signature='s', out_signature='b',
+                         async_callbacks=None)
     def isConnected(self, profile_key="@DEFAULT@"):
         return self.cb["isConnected"](unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_REQ_SUFFIX,
-                         in_signature='sa{ss}s', out_signature='s')
+                         in_signature='sa{ss}s', out_signature='s',
+                         async_callbacks=None)
     def launchAction(self, action_type, data, profile_key="@DEFAULT@"):
         return self.cb["launchAction"](unicode(action_type), data, unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ssssi', out_signature='s')
+                         in_signature='ssssi', out_signature='s',
+                         async_callbacks=None)
     def registerNewAccount(self, login, password, email, host, port=5222):
         return self.cb["registerNewAccount"](unicode(login), unicode(password), unicode(email), unicode(host), port)
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='sssss', out_signature='')
+                         in_signature='sssss', out_signature='',
+                         async_callbacks=None)
     def sendMessage(self, to_jid, message, subject='', mess_type="chat", profile_key="@DEFAULT@"):
         return self.cb["sendMessage"](unicode(to_jid), unicode(message), unicode(subject), unicode(mess_type), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ssss', out_signature='')
+                         in_signature='ssss', out_signature='',
+                         async_callbacks=None)
     def setParam(self, name, value, category, profile_key="@DEFAULT@"):
         return self.cb["setParam"](unicode(name), unicode(value), unicode(category), unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='ssia{ss}s', out_signature='')
+                         in_signature='ssia{ss}s', out_signature='',
+                         async_callbacks=None)
     def setPresence(self, to_jid='', show='', priority=0, statuses={}, profile_key="@DEFAULT@"):
         return self.cb["setPresence"](unicode(to_jid), unicode(show), priority, statuses, unicode(profile_key))
 
     @dbus.service.method(const_INT_PREFIX+const_COMM_SUFFIX,
-                         in_signature='sss', out_signature='')
+                         in_signature='sss', out_signature='',
+                         async_callbacks=None)
     def subscription(self, sub_type, entity, profile_key="@DEFAULT@"):
         return self.cb["subscription"](unicode(sub_type), unicode(entity), unicode(profile_key))
 
@@ -326,7 +362,8 @@
         """Dynamically add a signal to Dbus Bridge"""
         attributes = ', '.join(self.__attributes(signature))
 
-        code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
+        #code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the debug is too annoying with xmllog
+        code = compile ('def '+name+' (self,'+attributes+'): pass', '<DBus bridge>','exec')
         exec (code)
         signal = locals()[name]
         setattr(DbusObject, name, dbus.service.signal(
--- a/src/bridge/bridge_constructor/bridge_contructor.py	Wed May 25 17:18:05 2011 +0200
+++ b/src/bridge/bridge_constructor/bridge_contructor.py	Thu May 26 16:47:09 2011 +0200
@@ -37,7 +37,7 @@
 """
 MANAGED_PROTOCOLES=['dbus','mediawiki']
 DEFAULT_PROTOCOLE='dbus'
-FLAGS=['deprecated']
+FLAGS=['deprecated', 'async']
 
 import sys
 import os
@@ -252,6 +252,7 @@
             function = self.getValues(section)
             print ("Adding %s %s" % (section, function["type"]))
             default = self.getDefault(section)
+            async_msg = """<br />'''This method is asynchrone'''"""
             deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''"""
             signature_signal = \
 """\
@@ -274,6 +275,7 @@
                 'category':function['category'],
                 'name':section,
                 'doc':self.getDoc(section) or "FIXME: No description available",
+                'async':async_msg if "async" in self.getFlags(section) else "",
                 'deprecated':deprecated_msg if "deprecated" in self.getFlags(section) else "",
                 'parameters':self._wikiParameter(section, function['sig_in']),
                 'return':self._wikiReturn(section) if function['type'] == 'method' else ''
@@ -284,6 +286,7 @@
 == %(name)s ==
 ''%(doc)s''
 %(deprecated)s
+%(async)s
 {| class="wikitable" style="text-align:left; width:80%%;"
 ! scope=row | category
 | %(category)s 
@@ -335,6 +338,7 @@
             print ("Adding %s %s" % (section, function["type"]))
             default = self.getDefault(section)
             arg_doc = self.getArgumentsDoc(section)
+            async = "async" in self.getFlags(section)
             completion = {
         'sig_in':function['sig_in'] or '',
         'sig_out':function['sig_out'] or '',
@@ -359,11 +363,16 @@
             elif function["type"] == "method":
                 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
                 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc, unicode_protect=self.options.unicode)
+                completion['async_comma'] = ', ' if async and function['sig_in'] else ''
+                completion['async_args_def'] = 'callback=None, errback=None' if async else ''
+                completion['async_args_call'] = 'callback, errback' if async else ''
+                completion['async_callbacks'] = "('callback', 'errback')" if async else "None"
                 methods_part.append("""\
     @dbus.service.method(const_INT_PREFIX+const_%(category)s_SUFFIX,
-                         in_signature='%(sig_in)s', out_signature='%(sig_out)s')
-    def %(name)s(self, %(args)s):
-        %(debug)sreturn self.cb["%(name)s"](%(args_result)s)
+                         in_signature='%(sig_in)s', out_signature='%(sig_out)s',
+                         async_callbacks=%(async_callbacks)s)
+    def %(name)s(self, %(args)s%(async_comma)s%(async_args_def)s):
+        %(debug)sreturn self.cb["%(name)s"](%(args_result)s%(async_comma)s%(async_args_call)s)
 """ % completion)
 
         #at this point, signals_part, methods_part and direct_calls should be filled,
@@ -396,21 +405,25 @@
             print ("Adding %s %s" % (section, function["type"]))
             default = self.getDefault(section)
             arg_doc = self.getArgumentsDoc(section)
+            async = "async" in self.getFlags(section)
             completion = {
         'sig_in':function['sig_in'] or '',
         'sig_out':function['sig_out'] or '',
         'category':'req' if function['category'] == 'request' else 'comm',
         'name':section, 
-        'args':self.getArguments(function['sig_in'], name=arg_doc, default=default )
+        'args':self.getArguments(function['sig_in'], name=arg_doc, default=default)
         }
 
             if function["type"] == "method":
                 completion['debug'] = "" if not self.options.debug else 'debug ("%s")\n%s' % (section,8*' ')
                 completion['args_result'] = self.getArguments(function['sig_in'], name=arg_doc)
-                result = "self.db_%(category)s_iface.%(name)s(%(args_result)s)" % completion
+                completion['async_args'] = ', callback=None, errback=None' if async else ''
+                completion['async_comma'] = ', ' if async and function['sig_in'] else ''
+                completion['async_args_result'] = 'reply_handler=callback, error_handler=errback' if async else ''
+                result = "self.db_%(category)s_iface.%(name)s(%(args_result)s%(async_comma)s%(async_args_result)s)" % completion
                 completion['result'] = ("unicode(%s)" if self.options.unicode and function['sig_out'] == 's' else "%s") % result
                 methods_part.append("""\
-    def %(name)s(self, %(args)s):
+    def %(name)s(self, %(args)s%(async_args)s):
         %(debug)sreturn %(result)s
 """ % completion)
 
--- a/src/bridge/bridge_constructor/bridge_template.ini	Wed May 25 17:18:05 2011 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Thu May 26 16:47:09 2011 +0200
@@ -215,6 +215,16 @@
 doc=Connect a profile
 doc_param_0=%(doc_profile_key)s
 
+[asyncConnect]
+async=
+type=method
+category=communication
+sig_in=s
+sig_out=
+param_0_default="@DEFAULT@"
+doc=Connect a profile
+doc_param_0=%(doc_profile_key)s
+
 [disconnect]
 type=method
 category=communication
--- a/src/bridge/bridge_constructor/dbus_core_template.py	Wed May 25 17:18:05 2011 +0200
+++ b/src/bridge/bridge_constructor/dbus_core_template.py	Thu May 26 16:47:09 2011 +0200
@@ -108,7 +108,8 @@
         """Dynamically add a signal to Dbus Bridge"""
         attributes = ', '.join(self.__attributes(signature))
 
-        code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec')
+        #code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the debug is too annoying with xmllog
+        code = compile ('def '+name+' (self,'+attributes+'): pass', '<DBus bridge>','exec')
         exec (code)
         signal = locals()[name]
         setattr(DbusObject, name, dbus.service.signal(
--- a/src/bridge/bridge_constructor/dbus_frontend_template.py	Wed May 25 17:18:05 2011 +0200
+++ b/src/bridge/bridge_constructor/dbus_frontend_template.py	Thu May 26 16:47:09 2011 +0200
@@ -110,4 +110,7 @@
     
     def sendPersonalEvent(self, event_type, data, profile_key='@DEFAULT@'):
         return self.db_comm_iface.sendPersonalEvent(event_type, data, profile_key)
+    
+    def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@', callback=None, errback=None):
+        return self.db_comm_iface.setMicroblogAccess(access, profile_key, reply_handler=callback, error_handler=errback)