2020def trigger_hooks (hook : mount .Hooks , server : PluginServerInterface , objects_dict : Dict [str , Any ] = None ):
2121 if not cfg .config .automatically :
2222 return
23-
23+
2424 try :
2525 if len (cfg .temp_config .hooks .get (hook .value )) != 0 :
2626 _trigger_hooks (hook , server , objects_dict )
2727 except Exception as e :
2828 server .logger .exception (f'Unexpected exception when triggering hook { hook .value } ' , e )
2929
3030
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 :
3532 # 初始化最终变量字典
3633 finally_var_dict = dict ()
37-
34+
3835 if (objects_dict is not None ) and (len (objects_dict .keys ()) != 0 ):
3936 # 遍历所有已知对象
4037 for an_object_key in objects_dict .keys ():
@@ -46,14 +43,22 @@ def _trigger_hooks(hook: mount.Hooks, server: PluginServerInterface, objects_dic
4643 if (not hasattr (var_inner_attr_dict , 'keys' )) or (var_inner_attr_dict is None ):
4744 finally_var_dict [an_object_key ] = an_object_value
4845 continue
49-
46+
5047 # 正在遍历的对象的属性字典中的正在遍历的key
5148 for var_inner_attr_key in var_inner_attr_dict .keys ():
5249 # 正在遍历的对象的属性字典中的正在遍历的key的value
5350 var_inner_attr_value : Any = var_inner_attr_dict .get (var_inner_attr_key )
5451 # 整合进入finally_var_dict
5552 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+
5762 # 遍历被挂载到此hook的task的key
5863 for task in cfg .temp_config .hooks .get (hook .value ):
5964 if cfg .temp_config .task .get (task ) is None :
@@ -74,12 +79,12 @@ def _trigger_hooks(hook: mount.Hooks, server: PluginServerInterface, objects_dic
7479@new_thread ('hooks - list' )
7580def list_task (src : CommandSource ):
7681 rtext_list = RTextList ()
77-
82+
7883 if len (cfg .temp_config .task .values ()) == 0 :
7984 rtext_list .append (RText ('Nothing' , color = RColor .dark_gray , styles = RStyle .italic ))
8085 src .reply (RTextMCDRTranslation ('hooks.list.task' , rtext_list ))
8186 return
82-
87+
8388 for t in cfg .temp_config .task .values ():
8489 rtext_list .append (RTextList (
8590 RText ('\n ' ),
@@ -92,32 +97,32 @@ def list_task(src: CommandSource):
9297@new_thread ('hooks - list' )
9398def list_mount (src : CommandSource ):
9499 list_hooks : list = list ()
95-
100+
96101 for hk in dict (mount .Hooks .__members__ ).keys ():
97102 list_hooks .append (str (cfg .temp_config .hooks .get (str (hk ))))
98-
103+
99104 src .reply (RTextMCDRTranslation ('hooks.list.mount' , * list_hooks ))
100105
101106
102107@new_thread ('hooks - list' )
103108def list_scripts (src : CommandSource ):
104109 rtext_list = RTextList ()
105-
110+
106111 for scr in cfg .temp_config .scripts_list .keys ():
107112 rtext_list .append (RText (scr + ' ' , color = RColor .red ).h (cfg .temp_config .scripts_list .get (scr )))
108-
113+
109114 if rtext_list .is_empty ():
110115 rtext_list .append (RText ('Nothing' , color = RColor .dark_gray , styles = RStyle .italic ))
111-
116+
112117 src .reply (RTextMCDRTranslation ('hooks.list.script' , rtext_list ))
113118
114119
115120def reload_config (src : CommandSource , server : PluginServerInterface ):
116121 schedule_tasks .stop_all_schedule_daemon_threads (server )
117-
122+
118123 cfg .temp_config = cfg .TempConfig ()
119124 cfg .config = server .load_config_simple (target_class = cfg .Configuration )
120-
125+
121126 load_scripts (server )
122127 server .logger .info ('Config reloaded.' )
123128 src .reply (RTextMCDRTranslation ('hooks.reload.success' ))
@@ -127,13 +132,13 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
127132 if task not in cfg .temp_config .task .keys ():
128133 src .reply (RTextMCDRTranslation ('hooks.man_run.task_not_exist' ))
129134 return
130-
135+
131136 try :
132137 env_dict : Dict [str , str ] = dict (json .loads (env_str ))
133138 except Exception as e :
134139 src .reply (RTextMCDRTranslation ('hooks.man_run.illegal_env_json' , e ))
135140 return
136-
141+
137142 try :
138143 cfg .temp_config .task .get (task ).execute_task (server , mount .Hooks .undefined .value , var_dict = env_dict ,
139144 obj_dict = env_dict )
@@ -148,7 +153,7 @@ def man_run_task(task: str, env_str: str, src: CommandSource, server: PluginServ
148153def clear_tasks (server : PluginServerInterface , src : CommandSource ):
149154 for tsk in cfg .temp_config .task .copy ().keys ():
150155 tasks .delete_task (tsk , src , server )
151-
156+
152157
153158@new_thread ('hooks - run_command' )
154159def 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
157162 except ValueError :
158163 src .reply (RTextMCDRTranslation ('hooks.create.task_type_wrong' , task_type ))
159164 return
160-
165+
166+ ## TODO
167+
161168 if task_type_var1 == tasks .TaskType .shell_command :
162169 os .system (command )
163170 elif task_type_var1 == tasks .TaskType .server_command :
@@ -170,30 +177,30 @@ def run_command(command: str, task_type: str, server: PluginServerInterface, src
170177
171178def _parse_and_apply_scripts (script : str , server : PluginServerInterface ):
172179 logger .debug (f'Prepare for apply script: { script } ' , server )
173-
180+
174181 try :
175182 # 读取
176183 _yml = yaml .YAML ()
177184 with open (cfg .temp_config .scripts_list .get (script ), 'r' ) as f :
178185 content : Dict [str , Union [str , Union [list , dict ]]] = _yml .load (f ) # yaml.load(f.read(), Loader=yaml.Loader)
179-
186+
180187 if content is not None :
181188 if content .get ('tasks' ) is not None :
182189 for task in content .get ('tasks' ):
183190 use_cmd_file : bool = False
184191 cmd_file_path : str = ''
185-
192+
186193 if task .get ('command_file' ) is not None :
187194 var1 = str (task .get ('command_file' )).replace ('{hooks_config_path}' , server .get_data_folder ())
188-
195+
189196 if os .path .isfile (var1 ):
190197 cmd_file_path = var1
191198 use_cmd_file = True
192199 else :
193200 server .logger .warning (
194201 f'Script path for task { task .get ("name" )} is invalid, use command instead! '
195202 f'{ task .get ("command_file" )} ' )
196-
203+
197204 if use_cmd_file :
198205 # 读取
199206 with open (cmd_file_path , 'r' ) as command_file :
@@ -207,33 +214,33 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
207214 tasks .create_task (task .get ('task_type' ), task .get ('command' ), task .get ('name' ),
208215 server .get_plugin_command_source (),
209216 server , created_by = script )
210-
217+
211218 if task .get ('hooks' ) is None :
212219 continue
213220 for hook in task .get ('hooks' ):
214221 # 挂载
215222 mount .mount_task (hook , task .get ('name' ), server .get_plugin_command_source (), server )
216-
223+
217224 if content .get ('schedule_tasks' ) is not None :
218225 for schedule in content .get ('schedule_tasks' ):
219226 use_cmd_file : bool = False
220227 cmd_file_path : str = ''
221-
228+
222229 if int (schedule .get ('exec_interval' )) <= 0 :
223230 server .logger .warning (f'Invalid exec_interval in schedule task { schedule .get ("name" )} !' )
224-
231+
225232 if schedule .get ('command_file' ) is not None :
226233 var1 = str (schedule .get ('command_file' )).replace ('{hooks_config_path}' ,
227234 server .get_data_folder ())
228-
235+
229236 if os .path .isfile (var1 ):
230237 cmd_file_path = var1
231238 use_cmd_file = True
232239 else :
233240 server .logger .warning (
234241 f'Script path for task { schedule .get ("name" )} is invalid, use command instead! '
235242 f'{ schedule .get ("command_file" )} ' )
236-
243+
237244 if use_cmd_file :
238245 with open (cmd_file_path , 'r' ) as command_file :
239246 command_file_content = command_file .read ()
@@ -248,7 +255,7 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
248255 server .get_plugin_command_source (),
249256 server , created_by = script , is_schedule = True ,
250257 exec_interval = schedule .get ('exec_interval' ))
251-
258+
252259 if schedule .get ('hooks' ) is None :
253260 continue
254261 for hook in schedule .get ('hooks' ):
@@ -262,19 +269,19 @@ def _parse_and_apply_scripts(script: str, server: PluginServerInterface):
262269
263270def load_scripts (server : PluginServerInterface ):
264271 logger .debug ('Loading scripts...' , server )
265-
272+
266273 if not os .path .isdir (scripts_folder ):
267274 # 创建脚本目录
268275 os .makedirs (scripts_folder )
269276 return
270-
277+
271278 def list_all_files (root_dir ) -> List [str ]:
272279 # 显示一个文件夹及子文件夹中的所有yaml文件
273280 _files_in_a_folder : List [str ] = []
274-
281+
275282 for file in os .listdir (root_dir ):
276283 file_path = os .path .join (root_dir , file )
277-
284+
278285 if os .path .isdir (file_path ):
279286 if file_path .endswith ('_' ):
280287 logger .debug ('Ignored folder ' + str (file_path ), server )
@@ -284,33 +291,33 @@ def list_all_files(root_dir) -> List[str]:
284291 if os .path .isfile (file_path ) and (file_path .endswith ('.yaml' ) or file_path .endswith ('.yml' )):
285292 # 添加文件路径
286293 _files_in_a_folder .append (file_path )
287-
294+
288295 return _files_in_a_folder
289-
296+
290297 # 遍历所有yaml文件
291298 for script_path in list_all_files (scripts_folder ):
292299 # key:文件名 value:文件路径
293300 cfg .temp_config .scripts_list [os .path .basename (script_path )] = script_path
294-
301+
295302 # 遍历所有已成功注册的脚本
296303 for script in cfg .temp_config .scripts_list .keys ():
297304 _parse_and_apply_scripts (script , server )
298305
299306
300307def on_load (server : PluginServerInterface , old_module ):
301308 global scripts_folder
302-
309+
303310 cfg .temp_config = cfg .TempConfig ()
304311 cfg .config = server .load_config_simple (target_class = cfg .Configuration )
305-
312+
306313 scripts_folder = os .path .join (server .get_data_folder (), 'scripts' )
307314 load_scripts (server )
308-
315+
309316 if utils .is_windows ():
310317 server .logger .warning ('!###################################################################################!' )
311318 server .logger .warning ('Some features of hooks plugin cannot be run on Windows, you have already been warned.' )
312319 server .logger .warning ('!###################################################################################!' )
313-
320+
314321 server .register_command (
315322 Literal ('!!hooks' )
316323 .then (
@@ -437,16 +444,16 @@ def on_load(server: PluginServerInterface, old_module):
437444 )
438445 )
439446 )
440-
447+
441448 trigger_hooks (mount .Hooks .on_plugin_loaded , server ,
442449 {'server' : process_arg_server (server ), 'old_module' : old_module })
443450
444451
445452def on_unload (server : PluginServerInterface ):
446453 schedule_tasks .stop_all_schedule_daemon_threads (server )
447-
454+
448455 trigger_hooks (mount .Hooks .on_plugin_unloaded , server , {'server' : process_arg_server (server )})
449-
456+
450457 server .save_config_simple (cfg .config )
451458
452459
0 commit comments