small loader refactoring

This commit is contained in:
AAGaming
2024-02-14 17:49:27 -05:00
parent 9db3f3f20e
commit 091428f683
2 changed files with 8 additions and 12 deletions
+6 -7
View File
@@ -142,7 +142,12 @@ class Loader:
def import_plugin(self, file: str, plugin_directory: str, refresh: bool | None = False, batch: bool | None = False):
try:
plugin = PluginWrapper(file, plugin_directory, self.plugin_path)
async def plugin_emitted_event(event: str, data: Any):
self.logger.debug(f"PLUGIN EMITTED EVENT: {str(event)} {data}")
event_data = PluginEvent(plugin_name=plugin.name, event=event, data=data)
await self.ws.emit("plugin_event", event_data)
plugin = PluginWrapper(file, plugin_directory, self.plugin_path, plugin_emitted_event)
if plugin.name in self.plugins:
if not "debug" in plugin.flags and refresh:
self.logger.info(f"Plugin {plugin.name} is already loaded and has requested to not be re-loaded")
@@ -153,12 +158,6 @@ class Loader:
if plugin.passive:
self.logger.info(f"Plugin {plugin.name} is passive")
async def plugin_emitted_event(event: str, data: Any):
self.logger.debug(f"PLUGIN EMITTED EVENT: {str(event)} {data}")
event_data = PluginEvent(plugin_name=plugin.name, event=event, data=data)
await self.ws.emit("plugin_event", event_data)
self.plugins[plugin.name].set_emitted_event_callback(plugin_emitted_event)
self.plugins[plugin.name] = plugin.start()
self.logger.info(f"Loaded {plugin.name}")
if not batch:
+2 -5
View File
@@ -14,7 +14,7 @@ from typing import Any, Callable, Coroutine, Dict, List
EmittedEventCallbackType = Callable[[str, Any], Coroutine[Any, Any, Any]]
class PluginWrapper:
def __init__(self, file: str, plugin_directory: str, plugin_path: str) -> None:
def __init__(self, file: str, plugin_directory: str, plugin_path: str, emit_callback: EmittedEventCallbackType) -> None:
self.file = file
self.plugin_path = plugin_path
self.plugin_directory = plugin_directory
@@ -41,7 +41,7 @@ class PluginWrapper:
self._listener_task: Task[Any]
self._method_call_requests: Dict[str, MethodCallRequest] = {}
self.emitted_event_callback: EmittedEventCallbackType
self.emitted_event_callback: EmittedEventCallbackType = emit_callback
self.legacy_method_warning = False
@@ -61,9 +61,6 @@ class PluginWrapper:
except:
pass
def set_emitted_event_callback(self, callback: EmittedEventCallbackType):
self.emitted_event_callback = callback
async def execute_legacy_method(self, method_name: str, kwargs: Dict[Any, Any]):
if not self.legacy_method_warning:
self.legacy_method_warning = True