This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import logging | ||
|
||
from urllib3.packages import six | ||
|
||
|
||
def load_trio_backend(): | ||
if not six.PY3: | ||
raise ValueError("trio backend requires Python 3") | ||
from .trio_backend import TrioBackend | ||
return TrioBackend | ||
|
||
|
||
def load_twisted_backend(): | ||
if not six.PY3: | ||
raise ValueError("twisted backend requires Python 3") | ||
from .twisted_backend import TwistedBackend | ||
return TwistedBackend | ||
|
||
|
||
def load_sync_backend(): | ||
from .sync_backend import SyncBackend | ||
return SyncBackend | ||
|
||
|
||
BACKENDS = { | ||
"sync": load_sync_backend, | ||
"trio": load_trio_backend, | ||
"twisted": load_twisted_backend, | ||
} | ||
|
||
|
||
def available_backends(): | ||
""" | ||
Returns a list of backend names that we are able to successfully import. | ||
""" | ||
output = [] | ||
for backend_name, load_backend_fn in sorted(BACKENDS.items()): | ||
try: | ||
load_backend_fn() | ||
output.append(backend_name) | ||
except: # noqa | ||
logging.exception("{} not available".format(backend_name)) | ||
return output | ||
|
||
|
||
def load_backend(backend_name, backend_args): | ||
_backend_args = backend_args or {} | ||
if backend_name is None: | ||
return load_sync_backend()(**_backend_args) | ||
elif backend_name in BACKENDS: | ||
return BACKENDS[backend_name]()(**_backend_args) | ||
else: | ||
valid_backends = ", ".join(sorted(BACKENDS.keys())) | ||
raise ValueError("Backend must be one of: {}".format(valid_backends)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters