-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.py
52 lines (38 loc) · 1.5 KB
/
Plugin.py
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
import importlib
import os
import sys
class Plugins:
def __init__(self) -> None:
pass
class PluginLoader:
def __init__(self) -> None:
# Get Plugins
self.PluginPath = './RyhBotPythonSDK/Plugins'
self.Plugins = os.listdir(self.PluginPath)
# Add Import Path
if self.PluginPath not in sys.path:
sys.path.append(self.PluginPath)
# Load Plugin
for PluginName in self.Plugins:
if PluginName.startswith('plugin_'):
# Add Module Improt Path
ModulePath = self.PluginPath + '/' + PluginName
if ModulePath not in sys.path:
sys.path.append(ModulePath)
print(f'Load: {PluginName}')
# Get Plugin Info
module_install = importlib.import_module(f'{PluginName}.install')
func_name = module_install.funcname
attr_dict = module_install.attrs
# Add Attr To Class
attrs = list(attr_dict.keys())
attr_list = {}
for attr_name in attrs:
attr_value_name = attr_dict[attr_name]
attr_value = importlib.import_module(f'{PluginName}.{attr_value_name}')
attr_list[attr_name] = attr_value
attr_class = type(func_name, (object,), attr_list)
# Add Class To Plugin
setattr(Plugins, func_name, attr_class)
PluginLoader()
Plugin = Plugins()