File size: 1,119 Bytes
3dabe4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import importlib.util
import sys
from scripts.iib.tool import cwd

def load_plugins(plugin_dir):
    plugins = []
    for filename in os.listdir(plugin_dir):
        main_module_path = os.path.join(plugin_dir, filename, 'main.py')
        if not os.path.exists(main_module_path):
            continue
        spec = importlib.util.spec_from_file_location('main', main_module_path)
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        plugins.append(module)
    return plugins

plugin_insts = []
plugin_inst_map = {}
# 使用插件
try:
    plugin_dir = os.path.normpath(os.path.join(cwd, 'plugins'))
    plugins = load_plugins(plugin_dir)
    sys.path.append(plugin_dir)
    for plugin in plugins:
        try:
            res = plugin.Main()
            plugin_insts.append(res)
            plugin_inst_map[res.source_identifier] = res
            print(f'IIB loaded plugin: {res.name}')
        except Exception as e:
            print(f'Error running plugin {plugin.__class__.__name__}: {e}')
except Exception as e:
    print(f'Error loading plugins: {e}')