import os import importlib import warnings from flask import current_app def get_app_directories() -> list: directories = [] src_dir = current_app.config['SRC_DIR'] excluded_dirs = current_app.config['EXCLUDED_DIRS'] for dirname in os.listdir(src_dir): path = src_dir / dirname if os.path.isdir(path) and dirname not in excluded_dirs: directories.append(dirname) return directories def import_models() -> None: directories = get_app_directories() models_module = "models" for dirname in directories: try: importlib.import_module(f"app.app.{dirname}.{models_module}") except ModuleNotFoundError: print(models_module, dirname) warnings.warn(f"Not found module {models_module}.py in package {dirname}")