44from dataclasses import dataclass
55from logging import getLogger
66from pathlib import Path
7- from typing import Iterator , List , Tuple , Union
7+ from typing import Iterator , List , Union
88
99from fastapi_cli .exceptions import FastAPICLIException
1010
@@ -116,8 +116,6 @@ def get_app_name(*, mod_data: ModuleData, app_name: Union[str, None] = None) ->
116116 raise FastAPICLIException ("Could not find FastAPI app in module, try using --app" )
117117
118118
119- # TODO: fix get_import_data vs get_import_string
120-
121119@dataclass
122120class ImportData :
123121 app_name : str
@@ -126,7 +124,7 @@ class ImportData:
126124
127125
128126def 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
130128) -> ImportData :
131129 if not path :
132130 path = get_default_path ()
@@ -146,82 +144,13 @@ def get_import_data(
146144 app_name = use_app_name , module_data = mod_data , import_string = import_string
147145 )
148146
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-
220147
221148def get_app (
222149 * , path : Union [Path , None ] = None , app_name : Union [str , None ] = None
223150) -> 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
225154 with mod_data .sys_path ():
226155 mod = importlib .import_module (mod_data .module_import_str )
227156 app = getattr (mod , use_app_name )
0 commit comments