diff --git a/app/library/Utils.py b/app/library/Utils.py index 44a2cbc0..a70290d9 100644 --- a/app/library/Utils.py +++ b/app/library/Utils.py @@ -1201,3 +1201,30 @@ def init_class(cls: type[T], data: dict) -> T: from dataclasses import fields return cls(**{k: v for k, v in data.items() if k in {f.name for f in fields(cls)}}) + + +def load_modules(root_path: Path, directory: Path): + """ + Load all modules from a given directory relative to the root path. + + Args: + root_path (Path): The root path of the application. + directory (Path): The directory from which to load modules. + + """ + import importlib + import pkgutil + + package_name: str = str(directory.relative_to(root_path)).replace("/", ".") + + LOG.debug(f"Loading routes from '{directory}' with package name '{package_name}'.") + + for _, name, _ in pkgutil.iter_modules([directory]): + full_name: str = f"{package_name}.{name}" + if name.startswith("_"): + continue + try: + LOG.debug(f"Loading module '{full_name}'.") + importlib.import_module(full_name) + except ImportError as e: + LOG.error(f"Failed to import module '{full_name}': {e}") diff --git a/app/routes/_static.py b/app/routes/api/_static.py similarity index 100% rename from app/routes/_static.py rename to app/routes/api/_static.py diff --git a/app/routes/browser.py b/app/routes/api/browser.py similarity index 100% rename from app/routes/browser.py rename to app/routes/api/browser.py diff --git a/app/routes/conditions.py b/app/routes/api/conditions.py similarity index 100% rename from app/routes/conditions.py rename to app/routes/api/conditions.py diff --git a/app/routes/dev.py b/app/routes/api/dev.py similarity index 100% rename from app/routes/dev.py rename to app/routes/api/dev.py diff --git a/app/routes/history.py b/app/routes/api/history.py similarity index 100% rename from app/routes/history.py rename to app/routes/api/history.py diff --git a/app/routes/images.py b/app/routes/api/images.py similarity index 100% rename from app/routes/images.py rename to app/routes/api/images.py diff --git a/app/routes/logs.py b/app/routes/api/logs.py similarity index 100% rename from app/routes/logs.py rename to app/routes/api/logs.py diff --git a/app/routes/notifications.py b/app/routes/api/notifications.py similarity index 100% rename from app/routes/notifications.py rename to app/routes/api/notifications.py diff --git a/app/routes/ping.py b/app/routes/api/ping.py similarity index 100% rename from app/routes/ping.py rename to app/routes/api/ping.py diff --git a/app/routes/player.py b/app/routes/api/player.py similarity index 100% rename from app/routes/player.py rename to app/routes/api/player.py diff --git a/app/routes/presets.py b/app/routes/api/presets.py similarity index 100% rename from app/routes/presets.py rename to app/routes/api/presets.py diff --git a/app/routes/tasks.py b/app/routes/api/tasks.py similarity index 100% rename from app/routes/tasks.py rename to app/routes/api/tasks.py diff --git a/app/routes/yt_dlp.py b/app/routes/api/yt_dlp.py similarity index 100% rename from app/routes/yt_dlp.py rename to app/routes/api/yt_dlp.py