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
This commit is contained in:
Goffi 2011-05-26 16:47:09 +02:00
commit 7130c89a7e
4 changed files with 109 additions and 39 deletions

View file

@ -37,7 +37,7 @@ This script construct a SàT bridge using the given protocol
"""
MANAGED_PROTOCOLES=['dbus','mediawiki']
DEFAULT_PROTOCOLE='dbus'
FLAGS=['deprecated']
FLAGS=['deprecated', 'async']
import sys
import os
@ -252,6 +252,7 @@ class MediawikiConstructor(Constructor):
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 @@ class MediawikiConstructor(Constructor):
'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 @@ class MediawikiConstructor(Constructor):
== %(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 @@ class DbusConstructor(Constructor):
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 @@ class DbusConstructor(Constructor):
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 @@ class DbusConstructor(Constructor):
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)

View file

@ -215,6 +215,16 @@ param_0_default="@DEFAULT@"
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

View file

@ -108,7 +108,8 @@ class DbusObject(dbus.service.Object):
"""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(

View file

@ -110,4 +110,7 @@ class DBusBridgeFrontend(BridgeFrontend):
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)