Files
allwatch/base/modulemanager.py

57 lines
2.1 KiB
Python

import logging
import importlib
import pkgutil
from pathlib import Path
from flask import Blueprint
from base.basemodule import BaseModule
class ModuleManager:
def __init__(self):
self.modules = []
def load(self, app: Flask, modules_package: str = "modules"):
# the module loading functionality was initially written by claude
"""
Scans the modules/ directory, imports each submodule's module.py,
instantiates its MODULE_CLASS, and returns the list of live instances.
"""
modules: list[BaseModule] = []
package = importlib.import_module(modules_package)
package_path = Path(package.__file__).parent
for _, name, is_pkg in pkgutil.iter_modules([str(package_path)]):
if not is_pkg:
logging.getLogger(__name__).warn(f"Stray file found: '{name}'")
continue # skip stray files, only real module folders count
mod_path = f"{modules_package}.{name}.module"
try:
mod = importlib.import_module(mod_path)
except ModuleNotFoundError as e:
if e.name == mod_path:
logging.getLogger(__name__).warning(f"Module Folder '{name}' without module found")
else:
logging.getLogger(__name__).error(f"Error importing '{mod_path}': {e}")
continue
cls = getattr(mod, "MODULE_CLASS", None)
if cls is None:
logging.getLogger(__name__).warn(f"Module was detected but no class specified with MODULE_CLASS!")
continue
instance = cls(app=app)
logging.getLogger(__name__).info(f"Loaded module '{instance.name}'")
modules.append(instance)
logging.getLogger(__name__).info(f"Finished loading modules. Registry: {[str(i.name) for i in modules]}")
self.modules = modules
def get_mod_list(self):
return [ {"name":mod.name, "icon":mod.faIcon} for mod in self.modules ]
def sync(self):
return