Mercurial > libervia-backend
comparison src/bridge/DBus.py @ 993:301b342c697a
core: use of the new core.log module:
/!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 19 Apr 2014 19:19:19 +0200 |
parents | 723f28cd15c7 |
children | fee00f2e11c2 |
comparison
equal
deleted
inserted
replaced
992:f51a1895275c | 993:301b342c697a |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 17 # You should have received a copy of the GNU Affero General Public License |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | |
20 from bridge import Bridge | 21 from bridge import Bridge |
21 import dbus | 22 import dbus |
22 import dbus.service | 23 import dbus.service |
23 import dbus.mainloop.glib | 24 import dbus.mainloop.glib |
24 import inspect | 25 import inspect |
25 from logging import debug, info, error | 26 from sat.core.log import getLogger |
27 log = getLogger(__name__) | |
26 from twisted.internet.defer import Deferred | 28 from twisted.internet.defer import Deferred |
27 from sat.core.exceptions import BridgeInitError | 29 from sat.core.exceptions import BridgeInitError |
28 | 30 |
29 const_INT_PREFIX = "org.goffi.SAT" # Interface prefix | 31 const_INT_PREFIX = "org.goffi.SAT" # Interface prefix |
30 const_ERROR_PREFIX = const_INT_PREFIX + ".error" | 32 const_ERROR_PREFIX = const_INT_PREFIX + ".error" |
67 | 69 |
68 class DbusObject(dbus.service.Object): | 70 class DbusObject(dbus.service.Object): |
69 | 71 |
70 def __init__(self, bus, path): | 72 def __init__(self, bus, path): |
71 dbus.service.Object.__init__(self, bus, path) | 73 dbus.service.Object.__init__(self, bus, path) |
72 debug("Init DbusObject...") | 74 log.debug("Init DbusObject...") |
73 self.cb = {} | 75 self.cb = {} |
74 | 76 |
75 def register(self, name, cb): | 77 def register(self, name, cb): |
76 self.cb[name] = cb | 78 self.cb[name] = cb |
77 | 79 |
82 raise MethodNotRegistered | 84 raise MethodNotRegistered |
83 | 85 |
84 if "callback" in kwargs: | 86 if "callback" in kwargs: |
85 #we must have errback too | 87 #we must have errback too |
86 if not "errback" in kwargs: | 88 if not "errback" in kwargs: |
87 error("errback is missing in method call [%s]" % name) | 89 log.error("errback is missing in method call [%s]" % name) |
88 raise InternalError | 90 raise InternalError |
89 callback = kwargs.pop("callback") | 91 callback = kwargs.pop("callback") |
90 errback = kwargs.pop("errback") | 92 errback = kwargs.pop("errback") |
91 async = True | 93 async = True |
92 else: | 94 else: |
93 async = False | 95 async = False |
94 result = self.cb[name](*args, **kwargs) | 96 result = self.cb[name](*args, **kwargs) |
95 if async: | 97 if async: |
96 if not isinstance(result, Deferred): | 98 if not isinstance(result, Deferred): |
97 error("Asynchronous method [%s] does not return a Deferred." % name) | 99 log.error("Asynchronous method [%s] does not return a Deferred." % name) |
98 raise AsyncNotDeferred | 100 raise AsyncNotDeferred |
99 result.addCallback(lambda result: callback() if result is None else callback(result)) | 101 result.addCallback(lambda result: callback() if result is None else callback(result)) |
100 result.addErrback(lambda err: errback(GenericException(err))) | 102 result.addErrback(lambda err: errback(GenericException(err))) |
101 else: | 103 else: |
102 if isinstance(result, Deferred): | 104 if isinstance(result, Deferred): |
103 error("Synchronous method [%s] return a Deferred." % name) | 105 log.error("Synchronous method [%s] return a Deferred." % name) |
104 raise DeferredNotAsync | 106 raise DeferredNotAsync |
105 return result | 107 return result |
106 ### signals ### | 108 ### signals ### |
107 | 109 |
108 @dbus.service.signal(const_INT_PREFIX + const_PLUGIN_SUFFIX, | 110 @dbus.service.signal(const_INT_PREFIX + const_PLUGIN_SUFFIX, |
500 def addSignal(self, name, int_suffix, signature, doc={}): | 502 def addSignal(self, name, int_suffix, signature, doc={}): |
501 """Dynamically add a signal to Dbus Bridge""" | 503 """Dynamically add a signal to Dbus Bridge""" |
502 attributes = ', '.join(self.__attributes(signature)) | 504 attributes = ', '.join(self.__attributes(signature)) |
503 #TODO: use doc parameter to name attributes | 505 #TODO: use doc parameter to name attributes |
504 | 506 |
505 #code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the debug is too annoying with xmllog | 507 #code = compile ('def '+name+' (self,'+attributes+'): log.debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the log.debug is too annoying with xmllog |
506 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec') | 508 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec') |
507 exec (code) | 509 exec (code) |
508 signal = locals()[name] | 510 signal = locals()[name] |
509 setattr(DbusObject, name, dbus.service.signal( | 511 setattr(DbusObject, name, dbus.service.signal( |
510 const_INT_PREFIX + int_suffix, signature=signature)(signal)) | 512 const_INT_PREFIX + int_suffix, signature=signature)(signal)) |
515 | 517 |
516 class DBusBridge(Bridge): | 518 class DBusBridge(Bridge): |
517 def __init__(self): | 519 def __init__(self): |
518 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | 520 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
519 Bridge.__init__(self) | 521 Bridge.__init__(self) |
520 info("Init DBus...") | 522 log.info("Init DBus...") |
521 try: | 523 try: |
522 self.session_bus = dbus.SessionBus() | 524 self.session_bus = dbus.SessionBus() |
523 except dbus.DBusException as e: | 525 except dbus.DBusException as e: |
524 if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': | 526 if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': |
525 print u"D-Bus is not launched, please see README to see instructions on how to launch it" | 527 log.error(_(u"D-Bus is not launched, please see README to see instructions on how to launch it")) |
526 raise BridgeInitError | 528 raise BridgeInitError |
527 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) | 529 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) |
528 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) | 530 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) |
529 | 531 |
530 def actionResult(self, answer_type, id, data, profile): | 532 def actionResult(self, answer_type, id, data, profile): |
568 | 570 |
569 def subscribe(self, sub_type, entity_jid, profile): | 571 def subscribe(self, sub_type, entity_jid, profile): |
570 self.dbus_bridge.subscribe(sub_type, entity_jid, profile) | 572 self.dbus_bridge.subscribe(sub_type, entity_jid, profile) |
571 | 573 |
572 def register(self, name, callback): | 574 def register(self, name, callback): |
573 debug("registering DBus bridge method [%s]", name) | 575 log.debug("registering DBus bridge method [%s]" % name) |
574 self.dbus_bridge.register(name, callback) | 576 self.dbus_bridge.register(name, callback) |
575 | 577 |
576 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): | 578 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
577 """Dynamically add a method to Dbus Bridge""" | 579 """Dynamically add a method to Dbus Bridge""" |
578 #FIXME: doc parameter is kept only temporary, the time to remove it from calls | 580 #FIXME: doc parameter is kept only temporary, the time to remove it from calls |
579 print ("Adding method [%s] to DBus bridge" % name) | 581 log.debug("Adding method [%s] to DBus bridge" % name) |
580 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) | 582 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) |
581 self.register(name, method) | 583 self.register(name, method) |
582 | 584 |
583 def addSignal(self, name, int_suffix, signature, doc={}): | 585 def addSignal(self, name, int_suffix, signature, doc={}): |
584 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) | 586 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |