def dbus_sync_call_signal_wrapper(dbus_iface, func, handler_map, *args, **kwargs):
'''Run a D-BUS method call while receiving signals.
This function is an Ugly Hack™, since a normal synchronous dbus_iface.fn()
call does not cause signals to be received until the method returns. Thus
it calls func asynchronously and sets up a temporary main loop to receive
signals and call their handlers; these are assigned in handler_map (signal
name ? signal handler).
'''
if not hasattr(dbus_iface, 'connect_to_signal'):
# not a D-BUS object
return getattr(dbus_iface, func)(*args, **kwargs)
def _h_reply(*args, **kwargs):
"""protected method to send a reply"""
global _h_reply_result
_h_reply_result = args
loop.quit()
def _h_error(exception=None):
"""protected method to send an error"""
global _h_exception_exc
_h_exception_exc = exception
loop.quit()
loop = GLib.MainLoop()
global _h_reply_result, _h_exception_exc
_h_reply_result = None
_h_exception_exc = None
kwargs['reply_handler'] = _h_reply
kwargs['error_handler'] = _h_error
kwargs['timeout'] = 86400
for signame, sighandler in handler_map.items():
dbus_iface.connect_to_signal(signame, sighandler)
dbus_iface.get_dbus_method(func)(*args, **kwargs)
loop.run()
if _h_exception_exc:
raise _h_exception_exc
return _h_reply_result
评论列表
文章目录