You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
2.0 KiB
47 lines
2.0 KiB
# Zxy导入日志模块
|
|
import logging
|
|
|
|
# Zxy获取日志记录器
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Zxy定义全局钩子字典
|
|
_hooks = {}
|
|
|
|
# Zxy注册钩子回调
|
|
def register(hook_name: str, callback: callable):
|
|
"""
|
|
注册一个钩子回调。
|
|
"""
|
|
if hook_name not in _hooks: # Zxy检查钩子是否已存在
|
|
_hooks[hook_name] = [] # Zxy初始化钩子列表
|
|
_hooks[hook_name].append(callback) # Zxy添加回调到钩子列表
|
|
logger.debug(f"Registered hook '{hook_name}' with callback '{callback.__name__}'") # Zxy记录注册日志
|
|
|
|
# Zxy执行Action Hook
|
|
def run_action(hook_name: str, *args, **kwargs):
|
|
"""
|
|
执行一个 Action Hook。
|
|
它会按顺序执行所有注册到该钩子上的回调函数。
|
|
"""
|
|
if hook_name in _hooks: # Zxy检查钩子是否存在
|
|
logger.debug(f"Running action hook '{hook_name}'") # Zxy记录执行日志
|
|
for callback in _hooks[hook_name]: # Zxy遍历钩子回调
|
|
try:
|
|
callback(*args, **kwargs) # Zxy执行回调
|
|
except Exception as e:
|
|
logger.error(f"Error running action hook '{hook_name}' callback '{callback.__name__}': {e}", exc_info=True) # Zxy记录错误日志
|
|
|
|
# Zxy执行Filter Hook
|
|
def apply_filters(hook_name: str, value, *args, **kwargs):
|
|
"""
|
|
执行一个 Filter Hook。
|
|
它会把 value 依次传递给所有注册的回调函数进行处理。
|
|
"""
|
|
if hook_name in _hooks: # Zxy检查钩子是否存在
|
|
logger.debug(f"Applying filter hook '{hook_name}'") # Zxy记录执行日志
|
|
for callback in _hooks[hook_name]: # Zxy遍历钩子回调
|
|
try:
|
|
value = callback(value, *args, **kwargs) # Zxy调用回调处理值
|
|
except Exception as e:
|
|
logger.error(f"Error applying filter hook '{hook_name}' callback '{callback.__name__}': {e}", exc_info=True) # Zxy记录错误日志
|
|
return value # Zxy返回处理后的值 |