-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpluginLaoder.py
More file actions
188 lines (177 loc) · 7.8 KB
/
pluginLaoder.py
File metadata and controls
188 lines (177 loc) · 7.8 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import json
import os
# import tkinter.filedialog as di
import shutil
import zipfile
import PyQt5
from PyQt5.QtWidgets import QMessageBox, QFileDialog
import sqlite
from Plugin import Plugin, Bot
Api = object
p_list = list()
ui: PyQt5.QtWidgets.QWidget
# respond_group_plugin = list()
# respond_private_plugin = list()
# respond_notice = list()
sql = sqlite.SqliteSDB('./data/data.db', check_same_thread=False)
sql.create_table('plugin_setting', 'pid int PRIMARY Key not null', 'name text', 'author text', 'ver int',
'is_enable int',
'complain text')
def loadplugin(_ui: object, BotApi):
global Api
Api = BotApi
global ui
ui = _ui
name = sql.selectAll('plugin_setting') # 获取所有插件设置
if not name: # 无插件
ui.s.sendmsg.emit({'type': 'info', 'sender': '框架', 'text': '未找到任何已安装插件'})
return 0
# 有插件
for i in name:
try:
p = __import__('plugins' + '.' + i[1] + '.' + i[1],
fromlist=i[1]) # todo
res = p.init(hash(p), Bot(Api, hash(p), can_group=True, name=p.name))
tmp = Plugin(name=res['name'],
version=res.get('ver'),
author=res.get('author'), setting=p.setting, pid=res['p_id'],
enable_func=p.enable, disable_func=p.disable, complain=res.get('complain'),
if_groupMsg=True,
if_notice=True,
if_privateMsg=True, respond_group_msg=res.get('respond_group_msg', False),
respond_private_msg=res.get('respond_private_msg', False),
respond_notice=res.get('respond_notice', False),
respond_request=res.get('respond_request'))
if tmp.respond_group_msg:
tmp.on_group_msg = p.on_group_msg
if tmp.respond_private_msg:
tmp.on_private_msg = p.on_private_msg
if tmp.respond_notice:
tmp.on_notice = p.on_notice
if tmp.respond_request:
tmp.on_request = p.on_request
if i[4] == 1:
tmp.enable = True
else:
tmp.enable = False
p_list.append(tmp)
ui.s.sendmsg.emit({'type': 'info', 'sender': '框架', 'text': '加载插件%s' % i[1]})
except ModuleNotFoundError:
ui.s.sendmsg.emit({'type': 'error', 'sender': '框架', 'text': '加载插件%s时出错,未找到插件本体。' % i[1]})
sql.delete('plugin_setting', name=i[1])
sql.commit()
continue
return
def add_plugins_to_ui() -> None:
if p_list == 0:
return
else:
for i in p_list:
if i.enable:
i.enable_func()
ui.s.add_plugin.emit(i)
else:
ui.s.add_plugin.emit(i)
def add_plugin_with_win(): # 添加插件
path, _ = QFileDialog.getOpenFileName()
# print(path)
# path = filedialog.askopenfilename() # todo
if path:
if not zipfile.is_zipfile(path): # 验证zip文件
QMessageBox.warning(
ui.mainwindow,
'文件错误',
'选择的文件不是有效的ZIP文件',
QMessageBox.Yes
)
return False
with zipfile.ZipFile(path, 'r') as f: # 检查info.json是否存在
if 'info.json' not in f.namelist():
QMessageBox.warning(
ui.mainwindow,
'插件包错误',
'插件包缺少info.json文件',
QMessageBox.Yes
)
return False
try:
info = json.loads(f.read('info.json'))
except json.JSONDecodeError:
QMessageBox.warning(
ui.mainwindow,
'插件包错误',
'info.json文件格式错误',
QMessageBox.Yes
)
return False
# 验证必要字段
required_fields = ['name', 'dist']
for field in required_fields:
if field not in info:
QMessageBox.warning(
ui.mainwindow,
'插件包错误',
f'info.json缺少必要字段: {field}',
QMessageBox.Yes
)
return False
plugin_name = info['name']
plugin_dir = f'./plugins/{plugin_name}'
# print(info)
ui.s.sendmsg.emit({'type': 'info', 'sender': '框架', 'text': '添加插件' + info['name']})
# print(1)
if os.path.exists(plugin_dir):
reply = QMessageBox.question(
ui.mainwindow,
'插件已存在',
f'插件 {plugin_name} 已存在,是否覆盖?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.No:
return False
shutil.rmtree(plugin_dir)
# 5. 创建插件目录
os.makedirs(plugin_dir, exist_ok=True)
else:
os.mkdir('./plugins/' + info['name'])
f.extractall(path='./plugins/' + info['name'])
if set(os.listdir('./plugins/' + info['name'])) == set(info['dist']):
module = __import__('plugins' + '.' + info['name'] + '.' + info['name'],
fromlist=info['name']) # todo
# print(module)
res: dict = module.init(p_id=hash(module), Api=Api)
# print('res', res)
pg = Plugin(name=res['name'],
version=res.get('ver', 0.0),
author=res.get('author', 'NNN'), setting=module.setting, pid=res['p_id'],
enable_func=module.enable, disable_func=module.disable, complain=res.get('complain'),
if_groupMsg=True,
if_notice=True,
if_privateMsg=True, respond_group_msg=res.get('respond_group_msg', False),
respond_private_msg=res.get('respond_private_msg', False),
respond_notice=res.get('respond_notice', False),
respond_request=res.get('respond_request', False))
# print('pg', pg)
if pg.respond_group_msg:
pg.on_group_msg = module.on_group_msg
if pg.respond_private_msg:
pg.on_private_msg = module.on_private_msg
if pg.respond_notice:
pg.on_notice = module.on_notice
if pg.respond_request:
pg.on_request = module.on_request
p_list.append(pg)
# print(p_list)
ui.s.add_plugin.emit(pg)
sql.insert('plugin_setting', hash(module), res['name'], res['author'], res['ver'], False,
res['complain'])
return True
else:
QMessageBox.warning(ui.mainwindow, '安装错误', info['name'] + '安装包不完整', QMessageBox.Yes)
# messagebox.showwarning('安装错误', info['name'] + '安装包不完整') # todo
shutil.rmtree('./plugins/' + info['name'])
return False
else:
ui.s.sendmsg.emit({'type': 'info', 'sender': '框架', 'text': '用户取消添加插件'})
return False