20
20
def trigger_hooks (hook : mount .Hooks , server : PluginServerInterface , objects_dict : Dict [str , Any ] = None ):
21
21
if not cfg .config .automatically :
22
22
return
23
-
23
+
24
24
try :
25
25
if len (cfg .temp_config .hooks .get (hook .value )) != 0 :
26
26
_trigger_hooks (hook , server , objects_dict )
27
27
except Exception as e :
28
28
server .logger .exception (f'Unexpected exception when triggering hook { hook .value } ' , e )
29
29
30
30
31
- @new_thread ('hooks - trigger' )
32
- def _trigger_hooks (hook : mount .Hooks , server : PluginServerInterface , objects_dict : Dict [str , Any ] = None ):
33
- logger .debug (f'Triggering hooks { hook .value } ' , server )
34
-
31
+ def process_objects (objects_dict : Dict [str , Any ] = None ) -> Dict :
35
32
# 初始化最终变量字典
36
33
finally_var_dict = dict ()
37
-
34
+
38
35
if (objects_dict is not None ) and (len (objects_dict .keys ()) != 0 ):
39
36
# 遍历所有已知对象
40
37
for an_object_key in objects_dict .keys ():
@@ -46,14 +43,22 @@ def _trigger_hooks(hook: mount.Hooks, server: PluginServerInterface, objects_dic
46
43
if (not hasattr (var_inner_attr_dict , 'keys' )) or (var_inner_attr_dict is None ):
47
44
finally_var_dict [an_object_key ] = an_object_value
48
45
continue
49
-
46
+
50
47
# 正在遍历的对象的属性字典中的正在遍历的key
51
48
for var_inner_attr_key in var_inner_attr_dict .keys ():
52
49
# 正在遍历的对象的属性字典中的正在遍历的key的value
53
50
var_inner_attr_value : Any = var_inner_attr_dict .get (var_inner_attr_key )
54
51
# 整合进入finally_var_dict
55
52
finally_var_dict [an_object_key + '_' + var_inner_attr_key ] = var_inner_attr_value
56
-
53
+ return finally_var_dict
54
+
55
+
56
+ @new_thread ('hooks - trigger' )
57
+ def _trigger_hooks (hook : mount .Hooks , server : PluginServerInterface , objects_dict : Dict [str , Any ] = None ):
58
+ logger .debug (f'Triggering hooks { hook .value } ' , server )
59
+
60
+ finally_var_dict = process_objects (objects_dict )
61
+
57
62
# 遍历被挂载到此hook的task的key
58
63
for task in cfg .temp_config .hooks .get (hook .value ):
59
64
if cfg .temp_config .task .get (task ) is None :
@@ -74,12 +79,12 @@ def _trigger_hooks(hook: mount.Hooks, server: PluginServerInterface, objects_dic
74
79
@new_thread ('hooks - list' )
75
80
def list_task (src : CommandSource ):
76
81
rtext_list = RTextList ()
77
-
82
+
78
83
if len (cfg .temp_config .task .values ()) == 0 :
79
84
rtext_list .append (RText ('Nothing' , color = RColor .dark_gray , styles = RStyle .italic ))
80
85
src .reply (RTextMCDRTranslation ('hooks.list.task' , rtext_list ))
81
86
return
82
-
87
+
83
88
for t in cfg .temp_config .task .values ():
84
89
rtext_list .append (RTextList (
85
90
RText ('\n ' ),
@@ -92,32 +97,32 @@ def list_task(src: CommandSource):
92
97
@new_thread ('hooks - list' )
93
98
def list_mount (src : CommandSource ):
94
99
list_hooks : list = list ()
95
-
100
+
96
101
for hk in dict (mount .Hooks .__members__ ).keys ():
97
102
list_hooks .append (str (cfg .temp_config .hooks .get (str (hk ))))
98
-
103
+
99
104
src .reply (RTextMCDRTranslation ('hooks.list.mount' , * list_hooks ))
100
105
101
106
102
107
@new_thread ('hooks - list' )
103
108
def list_scripts (src : CommandSource ):
104
109
rtext_list = RTextList ()
105
-
110
+
106
111
for scr in cfg .temp_config .scripts_list .keys ():
107
112
rtext_list .append (RText (scr + ' ' , color = RColor .red ).h (cfg .temp_config .scripts_list .get (scr )))
108
-
113
+
109
114
if rtext_list .is_empty ():
110
115
rtext_list .append (RText ('Nothing' , color = RColor .dark_gray , styles = RStyle .italic ))
111
-
116
+
112
117
src .reply (RTextMCDRTranslation ('hooks.list.script' , rtext_list ))
113
118
114
119
115
120
def reload_config (src : CommandSource , server : PluginServerInterface ):
116
121
schedule_tasks .stop_all_schedule_daemon_threads (server )
117
-
122
+
118
123
cfg .temp_config = cfg .TempConfig ()
119
124
cfg .config = server .load_config_simple (target_class = cfg .Configuration )
120
-
125
+
121
126
load_scripts (server )
122
127
server .logger .info ('Config reloaded.' )
123
128
src .reply (RTextMCDRTranslation ('hooks.reload.success' ))
@@ -127,13 +132,13 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
127
132
if task not in cfg .temp_config .task .keys ():
128
133
src .reply (RTextMCDRTranslation ('hooks.man_run.task_not_exist' ))
129
134
return
130
-
135
+
131
136
try :
132
137
env_dict : Dict [str , str ] = dict (json .loads (env_str ))
133
138
except Exception as e :
134
139
src .reply (RTextMCDRTranslation ('hooks.man_run.illegal_env_json' , e ))
135
140
return
136
-
141
+
137
142
try :
138
143
cfg .temp_config .task .get (task ).execute_task (server , mount .Hooks .undefined .value , var_dict = env_dict ,
139
144
obj_dict = env_dict )
@@ -148,7 +153,7 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
148
153
def clear_tasks (server : PluginServerInterface , src : CommandSource ):
149
154
for tsk in cfg .temp_config .task .copy ().keys ():
150
155
tasks .delete_task (tsk , src , server )
151
-
156
+
152
157
153
158
@new_thread ('hooks - run_command' )
154
159
def run_command (command : str , task_type : str , server : PluginServerInterface , src : CommandSource ):
@@ -157,7 +162,9 @@ def run_command(command: str, task_type: str, server: PluginServerInterface, src
157
162
except ValueError :
158
163
src .reply (RTextMCDRTranslation ('hooks.create.task_type_wrong' , task_type ))
159
164
return
160
-
165
+
166
+ ## TODO
167
+
161
168
if task_type_var1 == tasks .TaskType .shell_command :
162
169
os .system (command )
163
170
elif task_type_var1 == tasks .TaskType .server_command :
@@ -170,30 +177,30 @@ def run_command(command: str, task_type: str, server: PluginServerInterface, src
170
177
171
178
def _parse_and_apply_scripts (script : str , server : PluginServerInterface ):
172
179
logger .debug (f'Prepare for apply script: { script } ' , server )
173
-
180
+
174
181
try :
175
182
# 读取
176
183
_yml = yaml .YAML ()
177
184
with open (cfg .temp_config .scripts_list .get (script ), 'r' ) as f :
178
185
content : Dict [str , Union [str , Union [list , dict ]]] = _yml .load (f ) # yaml.load(f.read(), Loader=yaml.Loader)
179
-
186
+
180
187
if content is not None :
181
188
if content .get ('tasks' ) is not None :
182
189
for task in content .get ('tasks' ):
183
190
use_cmd_file : bool = False
184
191
cmd_file_path : str = ''
185
-
192
+
186
193
if task .get ('command_file' ) is not None :
187
194
var1 = str (task .get ('command_file' )).replace ('{hooks_config_path}' , server .get_data_folder ())
188
-
195
+
189
196
if os .path .isfile (var1 ):
190
197
cmd_file_path = var1
191
198
use_cmd_file = True
192
199
else :
193
200
server .logger .warning (
194
201
f'Script path for task { task .get ("name" )} is invalid, use command instead! '
195
202
f'{ task .get ("command_file" )} ' )
196
-
203
+
197
204
if use_cmd_file :
198
205
# 读取
199
206
with open (cmd_file_path , 'r' ) as command_file :
@@ -207,33 +214,33 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
207
214
tasks .create_task (task .get ('task_type' ), task .get ('command' ), task .get ('name' ),
208
215
server .get_plugin_command_source (),
209
216
server , created_by = script )
210
-
217
+
211
218
if task .get ('hooks' ) is None :
212
219
continue
213
220
for hook in task .get ('hooks' ):
214
221
# 挂载
215
222
mount .mount_task (hook , task .get ('name' ), server .get_plugin_command_source (), server )
216
-
223
+
217
224
if content .get ('schedule_tasks' ) is not None :
218
225
for schedule in content .get ('schedule_tasks' ):
219
226
use_cmd_file : bool = False
220
227
cmd_file_path : str = ''
221
-
228
+
222
229
if int (schedule .get ('exec_interval' )) <= 0 :
223
230
server .logger .warning (f'Invalid exec_interval in schedule task { schedule .get ("name" )} !' )
224
-
231
+
225
232
if schedule .get ('command_file' ) is not None :
226
233
var1 = str (schedule .get ('command_file' )).replace ('{hooks_config_path}' ,
227
234
server .get_data_folder ())
228
-
235
+
229
236
if os .path .isfile (var1 ):
230
237
cmd_file_path = var1
231
238
use_cmd_file = True
232
239
else :
233
240
server .logger .warning (
234
241
f'Script path for task { schedule .get ("name" )} is invalid, use command instead! '
235
242
f'{ schedule .get ("command_file" )} ' )
236
-
243
+
237
244
if use_cmd_file :
238
245
with open (cmd_file_path , 'r' ) as command_file :
239
246
command_file_content = command_file .read ()
@@ -248,7 +255,7 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
248
255
server .get_plugin_command_source (),
249
256
server , created_by = script , is_schedule = True ,
250
257
exec_interval = schedule .get ('exec_interval' ))
251
-
258
+
252
259
if schedule .get ('hooks' ) is None :
253
260
continue
254
261
for hook in schedule .get ('hooks' ):
@@ -262,19 +269,19 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
262
269
263
270
def load_scripts (server : PluginServerInterface ):
264
271
logger .debug ('Loading scripts...' , server )
265
-
272
+
266
273
if not os .path .isdir (scripts_folder ):
267
274
# 创建脚本目录
268
275
os .makedirs (scripts_folder )
269
276
return
270
-
277
+
271
278
def list_all_files (root_dir ) -> List [str ]:
272
279
# 显示一个文件夹及子文件夹中的所有yaml文件
273
280
_files_in_a_folder : List [str ] = []
274
-
281
+
275
282
for file in os .listdir (root_dir ):
276
283
file_path = os .path .join (root_dir , file )
277
-
284
+
278
285
if os .path .isdir (file_path ):
279
286
if file_path .endswith ('_' ):
280
287
logger .debug ('Ignored folder ' + str (file_path ), server )
@@ -284,33 +291,33 @@ def list_all_files(root_dir) -> List[str]:
284
291
if os .path .isfile (file_path ) and (file_path .endswith ('.yaml' ) or file_path .endswith ('.yml' )):
285
292
# 添加文件路径
286
293
_files_in_a_folder .append (file_path )
287
-
294
+
288
295
return _files_in_a_folder
289
-
296
+
290
297
# 遍历所有yaml文件
291
298
for script_path in list_all_files (scripts_folder ):
292
299
# key:文件名 value:文件路径
293
300
cfg .temp_config .scripts_list [os .path .basename (script_path )] = script_path
294
-
301
+
295
302
# 遍历所有已成功注册的脚本
296
303
for script in cfg .temp_config .scripts_list .keys ():
297
304
_parse_and_apply_scripts (script , server )
298
305
299
306
300
307
def on_load (server : PluginServerInterface , old_module ):
301
308
global scripts_folder
302
-
309
+
303
310
cfg .temp_config = cfg .TempConfig ()
304
311
cfg .config = server .load_config_simple (target_class = cfg .Configuration )
305
-
312
+
306
313
scripts_folder = os .path .join (server .get_data_folder (), 'scripts' )
307
314
load_scripts (server )
308
-
315
+
309
316
if utils .is_windows ():
310
317
server .logger .warning ('!###################################################################################!' )
311
318
server .logger .warning ('Some features of hooks plugin cannot be run on Windows, you have already been warned.' )
312
319
server .logger .warning ('!###################################################################################!' )
313
-
320
+
314
321
server .register_command (
315
322
Literal ('!!hooks' )
316
323
.then (
@@ -437,16 +444,16 @@ def on_load(server: PluginServerInterface, old_module):
437
444
)
438
445
)
439
446
)
440
-
447
+
441
448
trigger_hooks (mount .Hooks .on_plugin_loaded , server ,
442
449
{'server' : process_arg_server (server ), 'old_module' : old_module })
443
450
444
451
445
452
def on_unload (server : PluginServerInterface ):
446
453
schedule_tasks .stop_all_schedule_daemon_threads (server )
447
-
454
+
448
455
trigger_hooks (mount .Hooks .on_plugin_unloaded , server , {'server' : process_arg_server (server )})
449
-
456
+
450
457
server .save_config_simple (cfg .config )
451
458
452
459
0 commit comments