1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
# Copyright (C) 2020 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import Any
from typing import Optional
from typing import Callable
import weakref
import inspect
import logging
from enum import IntEnum
from functools import wraps
from gi.repository import GLib
from nbxmpp.simplexml import Node
from nbxmpp.errors import is_error
from nbxmpp.errors import CancelledError
from nbxmpp.errors import TimeoutStanzaError
from nbxmpp.http import HTTPRequest
from nbxmpp.modules.base import BaseModule
from nbxmpp.modules.util import make_func_arguments_string
log = logging.getLogger('nbxmpp.task')
class _ResultSet:
pass
ResultSet = _ResultSet()
class NoType:
pass
class TaskState(IntEnum):
INIT = 0
RUNNING = 1
FINISHED = 2
CANCELLED = 3
@property
def is_init(self):
return self == TaskState.INIT
@property
def is_running(self):
return self == TaskState.RUNNING
@property
def is_finished(self):
return self == TaskState.FINISHED
@property
def is_cancelled(self):
return self == TaskState.CANCELLED
def _setup_task(task: Task,
client: Any,
callback: Optional[Callable[..., Any]],
user_data: Any):
client.add_task(task)
task.set_finalize_func(client.remove_task)
task.set_user_data(user_data)
if callback is not None:
task.add_done_callback(callback)
task.start()
return task
def iq_request_task(func):
@wraps(func)
def func_wrapper(self: BaseModule,
*args,
timeout: Optional[int] = None,
callback: Optional[Callable[..., Any]] = None,
user_data: Any = None,
**kwargs):
if self._log.isEnabledFor(logging.INFO):
self._log.info(make_func_arguments_string(func, self, args, kwargs))
task = IqRequestTask(func(self, *args, **kwargs),
self._log,
self._client)
task.set_timeout(timeout)
return _setup_task(task, self._client, callback, user_data)
return func_wrapper
def http_request_task(func):
@wraps(func)
def func_wrapper(self, *args, callback=None, user_data=None, **kwargs):
task = HTTPRequestTask(func(self, *args, **kwargs),
self._log)
return _setup_task(task, self._client, callback, user_data)
return func_wrapper
def is_fatal_error(error):
if is_error(error):
return error.is_fatal
return isinstance(error, Exception)
class Task:
'''
Base class for wrapping a generator method.
It runs the generator depending on what the generator yields. If the
generator yields another generator a sub task is created. If it yields
a type defined in _process_types, _run_async() is called which needs to
be implemented by classes.
the implementation of _run_async() must be really async, means it should
not call _async_finished() in the same mainloop cycle. Otherwise sub tasks
may break. _async_finished() needs to call _next_step(result).
'''
_process_types = (NoType,)
def __init__(self, gen, logger: logging.Logger = log):
self._logger = logger
self._gen = gen
self._done_callbacks: list[Callable[..., Any]] = []
self._sub_task = None
self._result = None
self._error = None
self._user_data: Optional[Any] = None
self._timeout: Optional[int] = None
self._timeout_id: Optional[int] = None
self._finalize_func = None
self._finalize_context = None
self._state = TaskState.INIT
@property
def state(self) -> TaskState:
return self._state
def add_done_callback(self,
callback: Callable[..., Any],
weak: bool = True):
if self._state.is_finished or self._state.is_cancelled:
raise RuntimeError('Task is finished')
if weak:
if inspect.ismethod(callback):
callback = weakref.WeakMethod(callback)
elif inspect.isfunction(callback):
callback = weakref.ref(callback)
else:
ValueError('Unknown callback object')
self._done_callbacks.append(callback)
def set_timeout(self, timeout: Optional[int]):
self._timeout = timeout
def start(self):
if not self._state.is_init:
raise RuntimeError('Task already started')
if self._timeout is not None:
self._logger.info('Add timeout for task: %s s, task id: %s',
self._timeout, id(self))
self._timeout_id = GLib.timeout_add_seconds(
self._timeout, self._on_timeout)
self._state = TaskState.RUNNING
next(self._gen)
self._next_step(self)
def _run_async(self, data):
raise NotImplementedError
def _async_finished(self, *args, **kwargs):
raise NotImplementedError
def _sub_task_completed(self, task: Task):
self._sub_task = None
if not self._state.is_running:
return
result = task.get_result()
if is_fatal_error(result):
self._error = result
self._set_finished()
else:
self._next_step(result)
def _next_step(self, result):
try:
res = self._gen.send(result)
except StopIteration:
self._set_finished()
return
except Exception as error:
self._log_if_fatal(error)
self._error = error
self._set_finished()
return
if isinstance(res, self._process_types):
self._run_async(res)
elif isinstance(res, Task):
if self._sub_task is not None:
RuntimeError('Only one sub task can be active')
self._sub_task = res
self._sub_task.add_done_callback(self._sub_task_completed,
weak=False)
else:
if res is not ResultSet:
self._result = res
self._set_finished()
def _set_finished(self):
self._state = TaskState.FINISHED
self._invoke_callbacks()
self._finalize()
def _log_if_fatal(self, error):
if is_error(error):
if error.is_fatal:
self._logger.log(error.log_level, error)
elif isinstance(error, Exception):
self._logger.exception('Fatal Exception')
def _invoke_callbacks(self):
for callback in self._done_callbacks:
if isinstance(callback, (weakref.WeakMethod, weakref.ref)):
callback = callback()
if callback is None:
return
# Be conservative with catching exceptions here
# For example unittests raise Assertion errors
# which should not be catched here
try:
callback(self)
except CancelledError:
pass
def set_result(self, result):
self._result = result
return ResultSet
def get_result(self):
# if self._error is None, there was no error
# but None is a valid value for self._result
if self._error is not None:
return self._error
return self._result
def finish(self) -> Any:
if self._error is not None:
raise self._error # pylint: disable=raising-bad-type
return self._result
def set_user_data(self, user_data: Any):
self._user_data = user_data
def get_user_data(self) -> Any:
return self._user_data
def set_finalize_func(self,
func: Callable[..., Any],
context: Optional[Any] = None):
self._finalize_func = func
self._finalize_context = context
def _on_timeout(self) -> None:
self._logger.info('Timeout reached, task id: %s', id(self))
if not self._state.is_running:
return
self._timeout_id = None
if self._sub_task is not None:
self._sub_task.cancel(invoke_callbacks=False)
self._error = TimeoutStanzaError()
self._set_finished()
def cancel(self, invoke_callbacks: bool = True) -> None:
if not self._state.is_running:
return
self._state = TaskState.CANCELLED
if self._sub_task is not None:
self._sub_task.cancel(invoke_callbacks=False)
self._error = CancelledError()
if invoke_callbacks:
self._invoke_callbacks()
self._finalize()
def _finalize(self):
if self._timeout_id is not None:
GLib.source_remove(self._timeout_id)
self._timeout_id = None
self._done_callbacks.clear()
self._sub_task = None
self._error = None
self._result = None
self._user_data = None
self._gen.close()
if self._finalize_func is not None:
self._finalize_func(self, self._finalize_context)
class IqRequestTask(Task):
'''
A Task for running IQ requests
'''
_process_types = (Node,)
def __init__(self, gen, logger, client):
super().__init__(gen, logger)
self._client = client
self._iq_id = None
def _run_async(self, stanza: Node):
# pylint: disable=arguments-renamed
self._iq_id = self._client.send_stanza(stanza,
callback=self._async_finished,
timeout=self._timeout)
def _async_finished(self, _client, result, *args, **kwargs):
if self._state == TaskState.CANCELLED:
return
if result is None:
self._error = TimeoutStanzaError()
self._set_finished()
return
self._next_step(result)
def _finalize(self):
if self._iq_id is not None:
self._client._dispatcher.remove_iq_callback(self._iq_id)
self._client = None
super()._finalize()
class HTTPRequestTask(Task):
'''
A Task for running HTTP requests
'''
_process_types = (HTTPRequest,)
def __init__(self, gen, logger):
super().__init__(gen, logger)
def _run_async(self, request: HTTPRequest):
# pylint: disable=arguments-renamed
request.connect('finished', self._async_finished)
def _async_finished(self, request: HTTPRequest):
if self._state == TaskState.CANCELLED:
return
self._next_step(request)
def _finalize(self):
self._session = None
super()._finalize()
|