|
import importlib |
|
import os |
|
from typing import List |
|
|
|
|
|
def discover_models(target_file: str, directory: str = None) -> List[str]: |
|
""" |
|
Discover and import target files (e.g., models.py) in all directories and subdirectories. |
|
|
|
:param target_file: The filename to look for in subdirectories (e.g., "models.py"). |
|
:param directory: The root directory to start searching from. |
|
Defaults to the directory where this script is located. |
|
:return: A list of module paths as strings. |
|
""" |
|
if directory is None: |
|
directory = os.path.dirname( |
|
os.path.abspath(__file__) |
|
) |
|
|
|
model_modules = [] |
|
|
|
|
|
for root, _, files in os.walk(directory): |
|
if target_file in files: |
|
|
|
relative_path = os.path.relpath(root, directory) |
|
module_name = os.path.join(relative_path, target_file).replace( |
|
os.sep, "." |
|
) |
|
module_name = module_name[:-3] |
|
model_modules.append("App." + module_name) |
|
|
|
print("Discovered models:", model_modules) |
|
return model_modules |
|
|