diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2022-05-04 15:04:46 +0200 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2022-05-04 15:04:46 +0200 |
commit | 1ec64ccb178a4164ac204925c20c559d96508a9b (patch) | |
tree | a11105d202afa7fb54273ab9d74985bfe7098faa /labbot/bot.py | |
parent | 27b8241380d5f02fdcf00a832c281767ef258d9f (diff) | |
download | lab-bot-1ec64ccb178a4164ac204925c20c559d96508a9b.tar.gz lab-bot-1ec64ccb178a4164ac204925c20c559d96508a9b.zip |
add logging, offload addons into config, allow external addons
Diffstat (limited to 'labbot/bot.py')
-rw-r--r-- | labbot/bot.py | 42 |
1 files changed, 40 insertions, 2 deletions
diff --git a/labbot/bot.py b/labbot/bot.py index 3ea1929..1361656 100644 --- a/labbot/bot.py +++ b/labbot/bot.py @@ -1,13 +1,51 @@ +import os.path +import sys from gidgetlab.aiohttp import GitLabBot +from importlib import import_module +import logging + +import labbot +import labbot.config + +log = logging.getLogger(__name__) class Bot: def __init__(self, *args, **kwargs): - self.instance = GitLabBot("lab-bot", **kwargs) - pass + self.name = kwargs.get("name", "lab-bot") + self.secret = kwargs.get("secret", "") + self.config = kwargs.get("config", labbot.config.read_instance_config(self.name)) + + self.addons = self.config.get("addons", []) + self.addon_paths = [] + + self.core_addon_path = os.path.join(os.path.dirname(labbot.__file__), "addons") + self.addon_paths.append(self.core_addon_path) + if "addon_path" in self.config: + self.addon_paths.append(self.config.get("addon_path")) + + self.instance = GitLabBot(self.name, **kwargs) + + for path in self.addon_paths: + sys.path.insert(0, path) + + for addon in self.addons: + self.load_addon(addon) + + for path in self.addon_paths: + sys.path.remove(path) + + + def load_addon(self, addon: str): + try: + import_module(f"{addon}").setup(self) + log.info(f"Loaded {addon}") + except ModuleNotFoundError: + log.error(f"No addon named `{addon}`") def register(self, func, *args, **kwargs): return self.instance.router.register(*args, **kwargs)(func) def run(self, *args, **kwargs): + log.info(f"Started {self.name}") self.instance.run(*args, **kwargs)
\ No newline at end of file |