4
4
from dataclasses import dataclass
5
5
from logging import getLogger
6
6
from pathlib import Path
7
- from typing import Iterator , List , Tuple , Union
7
+ from typing import Iterator , List , Union
8
8
9
9
from fastapi_cli .exceptions import FastAPICLIException
10
10
@@ -116,8 +116,6 @@ def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) ->
116
116
raise FastAPICLIException ("Could not find FastAPI app in module, try using --app" )
117
117
118
118
119
- # TODO: fix get_import_data vs get_import_string
120
-
121
119
@dataclass
122
120
class ImportData :
123
121
app_name : str
@@ -126,7 +124,7 @@ class ImportData:
126
124
127
125
128
126
def get_import_data (
129
- * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
127
+ * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
130
128
) -> ImportData :
131
129
if not path :
132
130
path = get_default_path ()
@@ -146,82 +144,13 @@ def get_import_data(
146
144
app_name = use_app_name , module_data = mod_data , import_string = import_string
147
145
)
148
146
149
- def get_import_string (
150
- * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
151
- ) -> str :
152
- if not path :
153
- path = get_default_path ()
154
-
155
- logger .debug (f"Using path [blue]{ path } [/blue]" )
156
- logger .debug (f"Resolved absolute path { path .resolve ()} " )
157
-
158
- if not path .exists ():
159
- raise FastAPICLIException (f"Path does not exist { path } " )
160
- mod_data = get_module_data_from_path (path )
161
- sys .path .insert (0 , str (mod_data .extra_sys_path ))
162
- use_app_name = get_app_name (mod_data = mod_data , app_name = app_name )
163
- import_example = Syntax (
164
- f"from { mod_data .module_import_str } import { use_app_name } " , "python"
165
- )
166
- import_panel = Padding (
167
- Panel (
168
- import_example ,
169
- title = "[b green]Importable FastAPI app[/b green]" ,
170
- expand = False ,
171
- padding = (1 , 2 ),
172
- ),
173
- 1 ,
174
- )
175
- logger .info ("Found importable FastAPI app" )
176
- print (import_panel )
177
- import_string = f"{ mod_data .module_import_str } :{ use_app_name } "
178
- logger .info (f"Using import string [b green]{ import_string } [/b green]" )
179
- return import_string
180
-
181
- def get_import_string_parts (
182
- * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
183
- ) -> Tuple [ModuleData , str ]:
184
- if not path :
185
- path = get_default_path ()
186
- logger .info (f"Using path [blue]{ path } [/blue]" )
187
- logger .info (f"Resolved absolute path { path .resolve ()} " )
188
- if not path .exists ():
189
- raise FastAPICLIException (f"Path does not exist { path } " )
190
- mod_data = get_module_data_from_path (path )
191
- sys .path .insert (0 , str (mod_data .extra_sys_path ))
192
- use_app_name = get_app_name (mod_data = mod_data , app_name = app_name )
193
-
194
- return mod_data , use_app_name
195
-
196
-
197
- def get_import_string (
198
- * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
199
- ) -> str :
200
- mod_data , use_app_name = get_import_string_parts (path = path , app_name = app_name )
201
- import_string = f"{ mod_data .module_import_str } :{ use_app_name } "
202
- import_example = Syntax (
203
- f"from { mod_data .module_import_str } import { use_app_name } " , "python"
204
- )
205
- import_panel = Padding (
206
- Panel (
207
- import_example ,
208
- title = "[b green]Importable FastAPI app[/b green]" ,
209
- expand = False ,
210
- padding = (1 , 2 ),
211
- ),
212
- 1 ,
213
- )
214
- logger .info ("Found importable FastAPI app" )
215
- print (import_panel )
216
-
217
- logger .info (f"Using import string [b green]{ import_string } [/b green]" )
218
- return import_string
219
-
220
147
221
148
def get_app (
222
149
* , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
223
150
) -> FastAPI :
224
- mod_data , use_app_name = get_import_string_parts (path = path , app_name = app_name )
151
+ """Get the FastAPI app instance from the given path and app name."""
152
+ import_data : ImportData = get_import_data (path = path , app_name = app_name )
153
+ mod_data , use_app_name = import_data .module_data , import_data .app_name
225
154
with mod_data .sys_path ():
226
155
mod = importlib .import_module (mod_data .module_import_str )
227
156
app = getattr (mod , use_app_name )
0 commit comments