remove legacy code
Omer Akram
4 years ago
31 | 31 | |
32 | 32 | # This is the API |
33 | 33 | # see tx.py for Twisted implementation |
34 | # see aio.py for asyncio/trollius implementation | |
34 | # see aio.py for asyncio implementation | |
35 | 35 | |
36 | 36 | |
37 | 37 | class _Config(object): |
0 | ############################################################################### | |
1 | # | |
2 | # The MIT License (MIT) | |
3 | # | |
4 | # Copyright (c) Crossbar.io Technologies GmbH | |
5 | # | |
6 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | # of this software and associated documentation files (the "Software"), to deal | |
8 | # in the Software without restriction, including without limitation the rights | |
9 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | # copies of the Software, and to permit persons to whom the Software is | |
11 | # furnished to do so, subject to the following conditions: | |
12 | # | |
13 | # The above copyright notice and this permission notice shall be included in | |
14 | # all copies or substantial portions of the Software. | |
15 | # | |
16 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | # THE SOFTWARE. | |
23 | # | |
24 | ############################################################################### | |
0 | 25 | |
1 | 26 | import math |
2 | 27 | from txaio.interfaces import IBatchedTimer |
23 | 23 | # |
24 | 24 | ############################################################################### |
25 | 25 | |
26 | import asyncio | |
27 | from asyncio import iscoroutine | |
28 | from asyncio import Future | |
29 | from types import AsyncGeneratorType | |
26 | 30 | import io |
27 | 31 | import os |
28 | 32 | import sys |
40 | 44 | from txaio._common import _BatchedTimer |
41 | 45 | from txaio import _util |
42 | 46 | from txaio import _Config |
43 | ||
44 | ||
45 | try: | |
46 | import asyncio | |
47 | from asyncio import iscoroutine | |
48 | from asyncio import Future | |
49 | ||
50 | except ImportError: | |
51 | # Trollius >= 0.3 was renamed | |
52 | # noinspection PyUnresolvedReferences | |
53 | import trollius as asyncio | |
54 | from trollius import iscoroutine | |
55 | from trollius import Future | |
56 | ||
57 | try: | |
58 | from types import AsyncGeneratorType # python 3.5+ | |
59 | except ImportError: | |
60 | class AsyncGeneratorType(object): | |
61 | pass | |
62 | ||
63 | ||
64 | def _create_future_of_loop(loop): | |
65 | return loop.create_future() | |
66 | ||
67 | ||
68 | def _create_future_directly(loop=None): | |
69 | return Future(loop=loop) | |
70 | ||
71 | ||
72 | def _create_task_of_loop(res, loop): | |
73 | return loop.create_task(res) | |
74 | ||
75 | ||
76 | def _create_task_directly(res, loop=None): | |
77 | return asyncio.Task(res, loop=loop) | |
78 | ||
79 | ||
80 | if sys.version_info >= (3, 4, 2): | |
81 | _create_task = _create_task_of_loop | |
82 | if sys.version_info >= (3, 5, 2): | |
83 | _create_future = _create_future_of_loop | |
84 | else: | |
85 | _create_future = _create_future_directly | |
86 | else: | |
87 | _create_task = _create_task_directly | |
88 | _create_future = _create_future_directly | |
89 | 47 | |
90 | 48 | |
91 | 49 | config = _Config() |
391 | 349 | if result is not _unspecified and error is not _unspecified: |
392 | 350 | raise ValueError("Cannot have both result and error.") |
393 | 351 | |
394 | f = _create_future(loop=self._config.loop) | |
352 | f = self._config.loop.create_future() | |
395 | 353 | if result is not _unspecified: |
396 | 354 | resolve(f, result) |
397 | 355 | elif error is not _unspecified: |
428 | 386 | if isinstance(res, Future): |
429 | 387 | return res |
430 | 388 | elif iscoroutine(res): |
431 | return _create_task(res, loop=self._config.loop) | |
389 | return self._config.loop.create_task(res) | |
432 | 390 | elif isinstance(res, AsyncGeneratorType): |
433 | 391 | raise RuntimeError( |
434 | 392 | "as_future() received an async generator function; does " |
22 | 22 | # THE SOFTWARE. |
23 | 23 | # |
24 | 24 | ############################################################################### |
25 | ||
26 | 25 | |
27 | 26 | import txaio |
28 | 27 | from contextlib import contextmanager |
45 | 45 | from txaio import _util |
46 | 46 | |
47 | 47 | |
48 | PY3_CORO = False | |
49 | try: | |
50 | from twisted.internet.defer import ensureDeferred | |
51 | from asyncio import iscoroutinefunction | |
52 | PY3_CORO = True | |
53 | except ImportError: | |
54 | pass | |
48 | from twisted.internet.defer import ensureDeferred | |
49 | from asyncio import iscoroutinefunction | |
55 | 50 | |
56 | 51 | using_twisted = True |
57 | 52 | using_asyncio = False |
421 | 416 | |
422 | 417 | def as_future(self, fun, *args, **kwargs): |
423 | 418 | # Twisted doesn't automagically deal with coroutines on Py3 |
424 | if PY3_CORO and iscoroutinefunction(fun): | |
419 | if iscoroutinefunction(fun): | |
425 | 420 | return ensureDeferred(fun(*args, **kwargs)) |
426 | 421 | return maybeDeferred(fun, *args, **kwargs) |
427 | 422 |