|
1 | 1 | from contextlib import closing |
2 | 2 | from importlib import import_module |
3 | 3 | from socket import socket |
4 | | -from types import ModuleType |
5 | 4 | from typing import Any, List, Type |
6 | 5 |
|
7 | 6 |
|
8 | 7 | def find_builtin_server_type(type_name: str) -> Type[Any]: |
9 | 8 | """Find first installed server implementation""" |
10 | | - builtin_module_names = ["sanic", "flask", "tornado"] |
| 9 | + supported_packages = ["sanic", "flask", "tornado"] |
11 | 10 |
|
12 | | - installed_builtin_modules: List[ModuleType] = [] |
13 | | - for module_name in builtin_module_names: |
| 11 | + installed_builtins: List[str] = [] |
| 12 | + for name in supported_packages: |
14 | 13 | try: |
15 | | - installed_builtin_modules.append( |
16 | | - import_module(f"idom.server.{module_name}") |
17 | | - ) |
| 14 | + import_module(name) |
18 | 15 | except ImportError: # coverage: skip |
19 | | - pass |
20 | | - |
21 | | - if not installed_builtin_modules: # coverage: skip |
22 | | - raise RuntimeError( |
23 | | - f"Found none of the following builtin server implementations {builtin_module_names}" |
24 | | - ) |
25 | | - |
26 | | - for builtin_module in installed_builtin_modules: |
| 16 | + continue |
| 17 | + else: |
| 18 | + builtin_module = import_module(f"idom.server.{name}") |
| 19 | + installed_builtins.append(builtin_module.__name__) |
27 | 20 | try: |
28 | 21 | return getattr(builtin_module, type_name) # type: ignore |
29 | 22 | except AttributeError: # coverage: skip |
30 | 23 | pass |
31 | 24 | else: # coverage: skip |
32 | | - installed_names = [m.__name__ for m in installed_builtin_modules] |
33 | | - raise ImportError( |
34 | | - f"No server type {type_name!r} found in installed implementations {installed_names}" |
35 | | - ) |
| 25 | + if not installed_builtins: |
| 26 | + raise RuntimeError( |
| 27 | + f"Found none of the following builtin server implementations {supported_packages}" |
| 28 | + ) |
| 29 | + else: |
| 30 | + raise ImportError( |
| 31 | + f"No server type {type_name!r} found in installed implementations {installed_builtins}" |
| 32 | + ) |
36 | 33 |
|
37 | 34 |
|
38 | 35 | def find_available_port(host: str, port_min: int = 8000, port_max: int = 9000) -> int: |
|
0 commit comments