mirror of
https://github.com/kuitoi/kuitoi-Server.git
synced 2025-08-17 16:25:36 +00:00
Add PluginsLoader
This commit is contained in:
parent
8bf4ceb032
commit
2e1c3b5aa1
1
src/modules/PluginsLoader/__init__.py
Normal file
1
src/modules/PluginsLoader/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
from .plugins_loader import PluginsLoader
|
0
src/modules/PluginsLoader/plugins-builtins.pyi
Normal file
0
src/modules/PluginsLoader/plugins-builtins.pyi
Normal file
52
src/modules/PluginsLoader/plugins_loader.py
Normal file
52
src/modules/PluginsLoader/plugins_loader.py
Normal 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}")
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user