-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathplugin_manager.py
More file actions
134 lines (109 loc) · 4.77 KB
/
plugin_manager.py
File metadata and controls
134 lines (109 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import importlib
from colorama import *
init(autoreset=True)
class EventManager:
"""Manages events and their associated plugins."""
def __init__(self):
self.events = {} # Dictionary to store callbacks for each event
def register_event(self, event_name, callback):
"""Registers a callback function to an event."""
if event_name not in self.events:
self.events[event_name] = []
self.events[event_name].append(callback)
def trigger_event(self, event_name, *args, **kwargs):
"""Triggers an event and calls all associated plugins."""
if event_name in self.events:
for callback in self.events[event_name]:
callback(*args, **kwargs)
else:
print(f"[!] No plugins registered for event '{event_name}'.")
import os
import importlib
import argparse
class PluginManager:
def __init__(self, plugin_folder="plugins"):
self.plugin_folder = plugin_folder
self.plugins = []
self.command_map = {}
def load_plugins(self):
self.plugins.clear()
self.command_map.clear()
if not os.path.exists(self.plugin_folder):
os.makedirs(self.plugin_folder)
return
for filename in os.listdir(self.plugin_folder):
if filename.endswith(".py") and filename not in ("__init__.py", "base.py"):
module_name = filename[:-3]
try:
mod = importlib.import_module(f"{self.plugin_folder}.{module_name}")
if hasattr(mod, "Plugin"):
plugin = mod.Plugin()
self.plugins.append(plugin)
self.command_map.update(plugin.commands)
except Exception as e:
print(f"[!] Plugin load error ({module_name}): {e}")
def get_plugin_names(self):
return [p.name for p in self.plugins]
def get_commands(self):
return self.command_map.keys()
def run_command(self, name, args):
if name in self.command_map:
return self.command_map[name]["func"](args)
else:
return "[!] Command not found."
def get_plugin_help(self):
help_lines = []
help_lines.append("\n[*] Available Plugin Commands:\n")
help_lines.append(f"{'Command':<20} {'Description':<50} Usage")
help_lines.append("-" * 90)
# Eğer command_map yoksa veya boşsa hata vermeden bilgi döndür
if not hasattr(self, 'command_map') or not isinstance(self.command_map, dict) or not self.command_map:
help_lines.append(" [!] No commands available or plugin not loaded.\n")
return "\n".join(help_lines)
for name, data in self.command_map.items():
try:
desc = str(data.get("desc", "No description provided."))
usage = str(data.get("usage", name))
except Exception:
desc = "No description provided."
usage = name
help_lines.append(f"{name:<20} {desc:<50} {usage}")
help_lines.append("")
return "\n".join(help_lines)
def load_command(self, plugin_name):
try:
mod = importlib.import_module(f"{self.plugin_folder}.{plugin_name}")
if hasattr(mod, "Plugin"):
plugin = mod.Plugin()
self.plugins.append(plugin)
self.command_map.update(plugin.commands)
return f"[+] Command '{plugin_name}' loaded."
else:
return f"[!] Class 'Plugin' not found in '{plugin_name}'."
except Exception as e:
return f"[!] Command load error ({plugin_name}): {e}"
def list_plugins(self):
if not self.plugins:
return "[!] No plugins loaded."
result = "[*] Loaded Plugins:\n"
for plugin in self.plugins:
result += f" - {plugin.name}\n"
return result.strip()
def load_plugin_module(self, plugin_name, path=None):
try:
if path:
plugin_path = os.path.abspath(path)
plugin_dir = os.path.dirname(plugin_path)
plugin_file = os.path.basename(plugin_path)
plugin_name = plugin_file[:-3] if plugin_file.endswith(".py") else plugin_file
if plugin_dir not in os.sys.path:
os.sys.path.insert(0, plugin_dir)
mod = importlib.import_module(plugin_name)
else:
mod = importlib.import_module(f"{self.plugin_folder}.{plugin_name}")
if hasattr(mod, "initialize"):
mod.initialize()
return f"[+] Plugin module '{plugin_name}' loaded."
except Exception as e:
return f"[!] Plugin module load error ({plugin_name}): {e}"