comparison sat/tools/sat_defer.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 cd391ea847cb
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 # SàT: a XMPP client 4 # SàT: a XMPP 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
36 36
37 37
38 def stanza2NotFound(failure_): 38 def stanza2NotFound(failure_):
39 """Convert item-not-found StanzaError to exceptions.NotFound""" 39 """Convert item-not-found StanzaError to exceptions.NotFound"""
40 failure_.trap(jabber_error.StanzaError) 40 failure_.trap(jabber_error.StanzaError)
41 if failure_.value.condition == u'item-not-found': 41 if failure_.value.condition == 'item-not-found':
42 raise exceptions.NotFound(failure_.value.text or failure_.value.condition) 42 raise exceptions.NotFound(failure_.value.text or failure_.value.condition)
43 return failure_ 43 return failure_
44 44
45 45
46 class DelayedDeferred(object): 46 class DelayedDeferred(object):
105 data = {KEY_NEXT: defer.Deferred()} 105 data = {KEY_NEXT: defer.Deferred()}
106 session_id, session_data = super(RTDeferredSessions, self).newSession( 106 session_id, session_data = super(RTDeferredSessions, self).newSession(
107 data, profile=profile 107 data, profile=profile
108 ) 108 )
109 if isinstance(deferreds, dict): 109 if isinstance(deferreds, dict):
110 session_data[KEY_DEFERREDS] = deferreds.values() 110 session_data[KEY_DEFERREDS] = list(deferreds.values())
111 iterator = deferreds.iteritems() 111 iterator = iter(deferreds.items())
112 else: 112 else:
113 session_data[KEY_DEFERREDS] = deferreds 113 session_data[KEY_DEFERREDS] = deferreds
114 iterator = enumerate(deferreds) 114 iterator = enumerate(deferreds)
115 115
116 for idx, d in iterator: 116 for idx, d in iterator:
119 d.addCallback(self._callback, d, session_id, profile) 119 d.addCallback(self._callback, d, session_id, profile)
120 d.addErrback(self._errback, d, session_id, profile) 120 d.addErrback(self._errback, d, session_id, profile)
121 return session_id 121 return session_id
122 122
123 def _purgeSession( 123 def _purgeSession(
124 self, session_id, reason=u"timeout", no_warning=False, got_result=False 124 self, session_id, reason="timeout", no_warning=False, got_result=False
125 ): 125 ):
126 """Purge the session 126 """Purge the session
127 127
128 @param session_id(str): id of the session to purge 128 @param session_id(str): id of the session to purge
129 @param reason (unicode): human readable reason why the session is purged 129 @param reason (unicode): human readable reason why the session is purged
135 if not got_result: 135 if not got_result:
136 try: 136 try:
137 timer, session_data, profile = self._sessions[session_id] 137 timer, session_data, profile = self._sessions[session_id]
138 except ValueError: 138 except ValueError:
139 raise exceptions.InternalError( 139 raise exceptions.InternalError(
140 u"was expecting timer, session_data and profile; is profile set ?" 140 "was expecting timer, session_data and profile; is profile set ?"
141 ) 141 )
142 142
143 # next_defer must be called before deferreds, 143 # next_defer must be called before deferreds,
144 # else its callback will be called by _gotResult 144 # else its callback will be called by _gotResult
145 next_defer = session_data[KEY_NEXT] 145 next_defer = session_data[KEY_NEXT]
150 for d in deferreds: 150 for d in deferreds:
151 d.cancel() 151 d.cancel()
152 152
153 if not no_warning: 153 if not no_warning:
154 log.warning( 154 log.warning(
155 u"RTDeferredList cancelled: {} (profile {})".format(reason, profile) 155 "RTDeferredList cancelled: {} (profile {})".format(reason, profile)
156 ) 156 )
157 157
158 super(RTDeferredSessions, self)._purgeSession(session_id) 158 super(RTDeferredSessions, self)._purgeSession(session_id)
159 159
160 def _gotResult(self, session_id, profile): 160 def _gotResult(self, session_id, profile):
173 173
174 def _errback(self, failure, deferred, session_id, profile): 174 def _errback(self, failure, deferred, session_id, profile):
175 deferred._RTDeferred_return = (False, failure) 175 deferred._RTDeferred_return = (False, failure)
176 self._gotResult(session_id, profile) 176 self._gotResult(session_id, profile)
177 177
178 def cancel(self, session_id, reason=u"timeout", no_log=False): 178 def cancel(self, session_id, reason="timeout", no_log=False):
179 """Stop this RTDeferredList 179 """Stop this RTDeferredList
180 180
181 Cancel all remaining deferred, and call self.final_defer.errback 181 Cancel all remaining deferred, and call self.final_defer.errback
182 @param reason (unicode): reason of the cancellation 182 @param reason (unicode): reason of the cancellation
183 @param no_log(bool): if True, don't log the cancellation 183 @param no_log(bool): if True, don't log the cancellation