Mercurial > libervia-backend
comparison src/bridge/bridge_constructor/dbus_core_template.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 | 1fe00f0c9a91 |
children | 95758ef3faa8 |
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, |
192 def addSignal(self, name, int_suffix, signature, doc={}): | 194 def addSignal(self, name, int_suffix, signature, doc={}): |
193 """Dynamically add a signal to Dbus Bridge""" | 195 """Dynamically add a signal to Dbus Bridge""" |
194 attributes = ', '.join(self.__attributes(signature)) | 196 attributes = ', '.join(self.__attributes(signature)) |
195 #TODO: use doc parameter to name attributes | 197 #TODO: use doc parameter to name attributes |
196 | 198 |
197 #code = compile ('def '+name+' (self,'+attributes+'): debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the debug is too annoying with xmllog | 199 #code = compile ('def '+name+' (self,'+attributes+'): log.debug ("'+name+' signal")', '<DBus bridge>','exec') #XXX: the log.debug is too annoying with xmllog |
198 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec') | 200 code = compile('def ' + name + ' (self,' + attributes + '): pass', '<DBus bridge>', 'exec') |
199 exec (code) | 201 exec (code) |
200 signal = locals()[name] | 202 signal = locals()[name] |
201 setattr(DbusObject, name, dbus.service.signal( | 203 setattr(DbusObject, name, dbus.service.signal( |
202 const_INT_PREFIX + int_suffix, signature=signature)(signal)) | 204 const_INT_PREFIX + int_suffix, signature=signature)(signal)) |
207 | 209 |
208 class DBusBridge(Bridge): | 210 class DBusBridge(Bridge): |
209 def __init__(self): | 211 def __init__(self): |
210 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) | 212 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
211 Bridge.__init__(self) | 213 Bridge.__init__(self) |
212 info("Init DBus...") | 214 log.info("Init DBus...") |
213 try: | 215 try: |
214 self.session_bus = dbus.SessionBus() | 216 self.session_bus = dbus.SessionBus() |
215 except dbus.DBusException as e: | 217 except dbus.DBusException as e: |
216 if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': | 218 if e._dbus_error_name == 'org.freedesktop.DBus.Error.NotSupported': |
217 print u"D-Bus is not launched, please see README to see instructions on how to launch it" | 219 log.error(_(u"D-Bus is not launched, please see README to see instructions on how to launch it")) |
218 raise BridgeInitError | 220 raise BridgeInitError |
219 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) | 221 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) |
220 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) | 222 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) |
221 | 223 |
222 ##DIRECT_CALLS## | 224 ##DIRECT_CALLS## |
223 def register(self, name, callback): | 225 def register(self, name, callback): |
224 debug("registering DBus bridge method [%s]", name) | 226 log.debug("registering DBus bridge method [%s]" % name) |
225 self.dbus_bridge.register(name, callback) | 227 self.dbus_bridge.register(name, callback) |
226 | 228 |
227 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): | 229 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): |
228 """Dynamically add a method to Dbus Bridge""" | 230 """Dynamically add a method to Dbus Bridge""" |
229 #FIXME: doc parameter is kept only temporary, the time to remove it from calls | 231 #FIXME: doc parameter is kept only temporary, the time to remove it from calls |
230 print ("Adding method [%s] to DBus bridge" % name) | 232 log.debug("Adding method [%s] to DBus bridge" % name) |
231 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) | 233 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) |
232 self.register(name, method) | 234 self.register(name, method) |
233 | 235 |
234 def addSignal(self, name, int_suffix, signature, doc={}): | 236 def addSignal(self, name, int_suffix, signature, doc={}): |
235 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) | 237 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |