-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
28 lines (19 loc) · 717 Bytes
/
loader.py
File metadata and controls
28 lines (19 loc) · 717 Bytes
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
import importlib
from fastapi import FastAPI
class AppLoadError(Exception):
pass
def load_app(app_path: str) -> FastAPI:
module_path, app_name = app_path.split(":")
try:
module = importlib.import_module(module_path)
except ModuleNotFoundError as e:
raise AppLoadError(f"Could not import module '{module_path}': {e}") from e
try:
app = getattr(module, app_name)
except AttributeError as e:
raise AppLoadError(f"Module '{module_path}' has no attribute '{app_name}'") from e
if not isinstance(app, FastAPI):
raise AppLoadError(
f"Attribute '{app_name}' in module '{module_path}' is not a FastAPI instance"
)
return app