Add PluginsLoader

This commit is contained in:
Maxim Khomutov 2023-07-05 17:17:33 +03:00
parent 8bf4ceb032
commit 2e1c3b5aa1
4 changed files with 55 additions and 1 deletions

View File

@ -0,0 +1 @@
from .plugins_loader import PluginsLoader

View File

@ -0,0 +1,52 @@
import os
import types
from core import get_logger
class BEAMP:
def __init__(self, name=None):
if name is None:
raise Exception("BEAMP: Name is required")
self.log = get_logger(f"BEAMP({name})")
self.name = name
def set_name(self, name):
self.name = name
@staticmethod
def register_event(event_name, event_func):
ev.register_event(event_name, event_func)
@staticmethod
def call_event(event_name, *data):
ev.call_event(event_name, *data)
class PluginsLoader:
def __init__(self, plugins_dir):
self.__plugins = {}
self.__plugins_dir = plugins_dir
self.log = get_logger("PluginsLoader")
def load_plugins(self):
self.log.debug("Loading plugins...")
files = os.listdir(self.__plugins_dir)
for file in files:
if file.endswith(".py"):
try:
self.log.debug(f"Loading plugin: {file}")
plugin = types.ModuleType('plugin')
plugin.BEAMP = BEAMP
file = os.path.join(self.__plugins_dir, file)
with open(f'{file}', 'r') as f:
code = f.read().replace("import BEAMP\n", "")
exec(code, plugin.__dict__)
plugin.load()
self.__plugins.update({file[:-3]: plugin})
self.log.debug(f"Plugin loaded: {file}")
except Exception as e:
self.log.error(f"Error loading plugin: {file}; Error: {e}")

View File

@ -7,6 +7,7 @@
# Licence: FPA
# (c) kuitoi.su 2023
from .console import Console
from .config_provider import ConfigProvider, Config
from .ConfigProvider import ConfigProvider, Config
from .i18n import MultiLanguage
from .EventsSystem import EventsSystem
from .PluginsLoader import PluginsLoader