@@ -45,27 +45,34 @@ class ModuleData:
45
45
def get_module_data_from_path (path : Path ) -> ModuleData :
46
46
use_path = path .resolve ()
47
47
module_path = use_path
48
+
48
49
if use_path .is_file () and use_path .stem == "__init__" :
49
50
module_path = use_path .parent
51
+
50
52
module_paths = [module_path ]
51
53
extra_sys_path = module_path .parent
54
+
52
55
for parent in module_path .parents :
53
56
init_path = parent / "__init__.py"
57
+
54
58
if init_path .is_file ():
55
59
module_paths .insert (0 , parent )
56
60
extra_sys_path = parent .parent
57
61
else :
58
62
break
59
63
60
64
module_str = "." .join (p .stem for p in module_paths )
65
+
61
66
return ModuleData (
62
67
module_import_str = module_str ,
63
68
extra_sys_path = extra_sys_path .resolve (),
64
69
module_paths = module_paths ,
65
70
)
66
71
67
72
68
- def get_app_name (* , mod_data : ModuleData , app_name : Union [str , None ] = None ) -> str :
73
+ def get_app_name (
74
+ * , mod_data : ModuleData , app_name : Union [str , None ] = None
75
+ ) -> tuple [str , FastAPI ]:
69
76
try :
70
77
mod = importlib .import_module (mod_data .module_import_str )
71
78
except (ImportError , ValueError ) as e :
@@ -74,32 +81,41 @@ def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) ->
74
81
"Ensure all the package directories have an [blue]__init__.py[/blue] file"
75
82
)
76
83
raise
84
+
77
85
if not FastAPI : # type: ignore[truthy-function]
78
86
raise FastAPICLIException (
79
87
"Could not import FastAPI, try running 'pip install fastapi'"
80
88
) from None
89
+
81
90
object_names = dir (mod )
82
91
object_names_set = set (object_names )
92
+
83
93
if app_name :
84
94
if app_name not in object_names_set :
85
95
raise FastAPICLIException (
86
96
f"Could not find app name { app_name } in { mod_data .module_import_str } "
87
97
)
98
+
88
99
app = getattr (mod , app_name )
100
+
89
101
if not isinstance (app , FastAPI ):
90
102
raise FastAPICLIException (
91
103
f"The app name { app_name } in { mod_data .module_import_str } doesn't seem to be a FastAPI app"
92
104
)
93
- return app_name
105
+
106
+ return app_name , app
107
+
94
108
for preferred_name in ["app" , "api" ]:
95
109
if preferred_name in object_names_set :
96
110
obj = getattr (mod , preferred_name )
97
111
if isinstance (obj , FastAPI ):
98
- return preferred_name
112
+ return preferred_name , obj
113
+
99
114
for name in object_names :
100
115
obj = getattr (mod , name )
101
116
if isinstance (obj , FastAPI ):
102
- return name
117
+ return name , obj
118
+
103
119
raise FastAPICLIException ("Could not find FastAPI app in module, try using --app" )
104
120
105
121
@@ -108,6 +124,7 @@ class ImportData:
108
124
app_name : str
109
125
module_data : ModuleData
110
126
import_string : str
127
+ fastapi_app : FastAPI
111
128
112
129
113
130
def get_import_data (
@@ -121,12 +138,16 @@ def get_import_data(
121
138
122
139
if not path .exists ():
123
140
raise FastAPICLIException (f"Path does not exist { path } " )
141
+
124
142
mod_data = get_module_data_from_path (path )
125
143
sys .path .insert (0 , str (mod_data .extra_sys_path ))
126
- use_app_name = get_app_name (mod_data = mod_data , app_name = app_name )
144
+ use_app_name , app = get_app_name (mod_data = mod_data , app_name = app_name )
127
145
128
146
import_string = f"{ mod_data .module_import_str } :{ use_app_name } "
129
147
130
148
return ImportData (
131
- app_name = use_app_name , module_data = mod_data , import_string = import_string
149
+ app_name = use_app_name ,
150
+ module_data = mod_data ,
151
+ import_string = import_string ,
152
+ fastapi_app = app ,
132
153
)
0 commit comments