Mercurial > libervia-backend
comparison sat/bridge/bridge_constructor/constructors/dbus/dbus_core_template.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 003b8b4b56a7 |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT: a jabber client | 4 # SAT: a jabber client |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
99 if not "errback" in kwargs: | 99 if not "errback" in kwargs: |
100 log.error("errback is missing in method call [%s]" % name) | 100 log.error("errback is missing in method call [%s]" % name) |
101 raise InternalError | 101 raise InternalError |
102 callback = kwargs.pop("callback") | 102 callback = kwargs.pop("callback") |
103 errback = kwargs.pop("errback") | 103 errback = kwargs.pop("errback") |
104 async = True | 104 async_ = True |
105 else: | 105 else: |
106 async = False | 106 async_ = False |
107 result = self.cb[name](*args, **kwargs) | 107 result = self.cb[name](*args, **kwargs) |
108 if async: | 108 if async_: |
109 if not isinstance(result, Deferred): | 109 if not isinstance(result, Deferred): |
110 log.error("Asynchronous method [%s] does not return a Deferred." % name) | 110 log.error("Asynchronous method [%s] does not return a Deferred." % name) |
111 raise AsyncNotDeferred | 111 raise AsyncNotDeferred |
112 result.addCallback( | 112 result.addCallback( |
113 lambda result: callback() if result is None else callback(result) | 113 lambda result: callback() if result is None else callback(result) |
168 if opening_count == 0: | 168 if opening_count == 0: |
169 break | 169 break |
170 i += 1 | 170 i += 1 |
171 return attr | 171 return attr |
172 | 172 |
173 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False): | 173 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async_=False): |
174 """Dynamically add a method to Dbus Bridge""" | 174 """Dynamically add a method to Dbus Bridge""" |
175 inspect_args = inspect.getargspec(method) | 175 inspect_args = inspect.getfullargspec(method) |
176 | 176 |
177 _arguments = inspect_args.args | 177 _arguments = inspect_args.args |
178 _defaults = list(inspect_args.defaults or []) | 178 _defaults = list(inspect_args.defaults or []) |
179 | 179 |
180 if inspect.ismethod(method): | 180 if inspect.ismethod(method): |
184 # first arguments are for the _callback method | 184 # first arguments are for the _callback method |
185 arguments_callback = ", ".join( | 185 arguments_callback = ", ".join( |
186 [repr(name)] | 186 [repr(name)] |
187 + ( | 187 + ( |
188 (_arguments + ["callback=callback", "errback=errback"]) | 188 (_arguments + ["callback=callback", "errback=errback"]) |
189 if async | 189 if async_ |
190 else _arguments | 190 else _arguments |
191 ) | 191 ) |
192 ) | 192 ) |
193 | 193 |
194 if async: | 194 if async_: |
195 _arguments.extend(["callback", "errback"]) | 195 _arguments.extend(["callback", "errback"]) |
196 _defaults.extend([None, None]) | 196 _defaults.extend([None, None]) |
197 | 197 |
198 # now we create a second list with default values | 198 # now we create a second list with default values |
199 for i in range(1, len(_defaults) + 1): | 199 for i in range(1, len(_defaults) + 1): |
211 "<DBus bridge>", | 211 "<DBus bridge>", |
212 "exec", | 212 "exec", |
213 ) | 213 ) |
214 exec(code) # FIXME: to the same thing in a cleaner way, without compile/exec | 214 exec(code) # FIXME: to the same thing in a cleaner way, without compile/exec |
215 method = locals()[name] | 215 method = locals()[name] |
216 async_callbacks = ("callback", "errback") if async else None | 216 async_callbacks = ("callback", "errback") if async_ else None |
217 setattr( | 217 setattr( |
218 DbusObject, | 218 DbusObject, |
219 name, | 219 name, |
220 dbus.service.method( | 220 dbus.service.method( |
221 const_INT_PREFIX + int_suffix, | 221 const_INT_PREFIX + int_suffix, |
263 self.session_bus = dbus.SessionBus() | 263 self.session_bus = dbus.SessionBus() |
264 except dbus.DBusException as e: | 264 except dbus.DBusException as e: |
265 if e._dbus_error_name == "org.freedesktop.DBus.Error.NotSupported": | 265 if e._dbus_error_name == "org.freedesktop.DBus.Error.NotSupported": |
266 log.error( | 266 log.error( |
267 _( | 267 _( |
268 u"D-Bus is not launched, please see README to see instructions on how to launch it" | 268 "D-Bus is not launched, please see README to see instructions on how to launch it" |
269 ) | 269 ) |
270 ) | 270 ) |
271 raise BridgeInitError | 271 raise BridgeInitError |
272 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) | 272 self.dbus_name = dbus.service.BusName(const_INT_PREFIX, self.session_bus) |
273 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) | 273 self.dbus_bridge = DbusObject(self.session_bus, const_OBJ_PATH) |
275 ##SIGNAL_DIRECT_CALLS_PART## | 275 ##SIGNAL_DIRECT_CALLS_PART## |
276 def register_method(self, name, callback): | 276 def register_method(self, name, callback): |
277 log.debug("registering DBus bridge method [%s]" % name) | 277 log.debug("registering DBus bridge method [%s]" % name) |
278 self.dbus_bridge.register_method(name, callback) | 278 self.dbus_bridge.register_method(name, callback) |
279 | 279 |
280 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async=False, doc={}): | 280 def addMethod(self, name, int_suffix, in_sign, out_sign, method, async_=False, doc={}): |
281 """Dynamically add a method to Dbus Bridge""" | 281 """Dynamically add a method to Dbus Bridge""" |
282 # FIXME: doc parameter is kept only temporary, the time to remove it from calls | 282 # FIXME: doc parameter is kept only temporary, the time to remove it from calls |
283 log.debug("Adding method [%s] to DBus bridge" % name) | 283 log.debug("Adding method [%s] to DBus bridge" % name) |
284 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async) | 284 self.dbus_bridge.addMethod(name, int_suffix, in_sign, out_sign, method, async_) |
285 self.register_method(name, method) | 285 self.register_method(name, method) |
286 | 286 |
287 def addSignal(self, name, int_suffix, signature, doc={}): | 287 def addSignal(self, name, int_suffix, signature, doc={}): |
288 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) | 288 self.dbus_bridge.addSignal(name, int_suffix, signature, doc) |
289 setattr(Bridge, name, getattr(self.dbus_bridge, name)) | 289 setattr(Bridge, name, getattr(self.dbus_bridge, name)) |