| 1 |
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license |
| 2 |
|
| 3 |
from typing import Any, Dict, List, Tuple |
| 4 |
|
| 5 |
import dns._features |
| 6 |
import dns.asyncbackend |
| 7 |
|
| 8 |
if dns._features.have("doq"): |
| 9 |
from dns._asyncbackend import NullContext |
| 10 |
from dns.quic._asyncio import AsyncioQuicConnection as AsyncioQuicConnection |
| 11 |
from dns.quic._asyncio import AsyncioQuicManager |
| 12 |
from dns.quic._asyncio import AsyncioQuicStream as AsyncioQuicStream |
| 13 |
from dns.quic._common import AsyncQuicConnection # pyright: ignore |
| 14 |
from dns.quic._common import AsyncQuicManager as AsyncQuicManager |
| 15 |
from dns.quic._sync import SyncQuicConnection # pyright: ignore |
| 16 |
from dns.quic._sync import SyncQuicStream # pyright: ignore |
| 17 |
from dns.quic._sync import SyncQuicManager as SyncQuicManager |
| 18 |
|
| 19 |
have_quic = True |
| 20 |
|
| 21 |
def null_factory( |
| 22 |
*args, # pylint: disable=unused-argument |
| 23 |
**kwargs, # pylint: disable=unused-argument |
| 24 |
): |
| 25 |
return NullContext(None) |
| 26 |
|
| 27 |
def _asyncio_manager_factory( |
| 28 |
context, *args, **kwargs # pylint: disable=unused-argument |
| 29 |
): |
| 30 |
return AsyncioQuicManager(*args, **kwargs) |
| 31 |
|
| 32 |
# We have a context factory and a manager factory as for trio we need to have |
| 33 |
# a nursery. |
| 34 |
|
| 35 |
_async_factories: Dict[str, Tuple[Any, Any]] = { |
| 36 |
"asyncio": (null_factory, _asyncio_manager_factory) |
| 37 |
} |
| 38 |
|
| 39 |
if dns._features.have("trio"): |
| 40 |
import trio |
| 41 |
|
| 42 |
# pylint: disable=ungrouped-imports |
| 43 |
from dns.quic._trio import TrioQuicConnection as TrioQuicConnection |
| 44 |
from dns.quic._trio import TrioQuicManager |
| 45 |
from dns.quic._trio import TrioQuicStream as TrioQuicStream |
| 46 |
|
| 47 |
def _trio_context_factory(): |
| 48 |
return trio.open_nursery() |
| 49 |
|
| 50 |
def _trio_manager_factory(context, *args, **kwargs): |
| 51 |
return TrioQuicManager(context, *args, **kwargs) |
| 52 |
|
| 53 |
_async_factories["trio"] = (_trio_context_factory, _trio_manager_factory) |
| 54 |
|
| 55 |
def factories_for_backend(backend=None): |
| 56 |
if backend is None: |
| 57 |
backend = dns.asyncbackend.get_default_backend() |
| 58 |
return _async_factories[backend.name()] |
| 59 |
|
| 60 |
else: # pragma: no cover |
| 61 |
have_quic = False |
| 62 |
|
| 63 |
class AsyncQuicStream: # type: ignore |
| 64 |
pass |
| 65 |
|
| 66 |
class AsyncQuicConnection: # type: ignore |
| 67 |
async def make_stream(self) -> Any: |
| 68 |
raise NotImplementedError |
| 69 |
|
| 70 |
class SyncQuicStream: # type: ignore |
| 71 |
pass |
| 72 |
|
| 73 |
class SyncQuicConnection: # type: ignore |
| 74 |
def make_stream(self) -> Any: |
| 75 |
raise NotImplementedError |
| 76 |
|
| 77 |
|
| 78 |
Headers = List[Tuple[bytes, bytes]] |