格式化代码

This commit is contained in:
liuwei
2025-06-04 17:52:41 +08:00
parent cd2f8b9679
commit 94e0c16e7b
9 changed files with 148 additions and 110 deletions
+38 -32
View File
@@ -18,6 +18,7 @@ LOG = logger
def robot_management():
return render_template('plugins_manage.html')
@plugin_routes.route('/api/plugins', methods=['GET'])
@login_required
def get_plugins():
@@ -26,7 +27,7 @@ def get_plugins():
server = current_app.dashboard_server
# 获取插件注册表
plugins = server.plugin_registry.get_all_plugins()
# 转换为前端需要的格式
plugin_list = []
for name, plugin in plugins.items():
@@ -35,7 +36,7 @@ def get_plugins():
module_name = plugin.__class__.__module__.split('.')[-2]
except (IndexError, AttributeError):
module_name = "unknown"
plugin_info = {
"name": plugin.name,
"module_name": module_name,
@@ -45,12 +46,13 @@ def get_plugins():
"status": plugin.status.name if hasattr(plugin, 'status') else 'UNKNOWN'
}
plugin_list.append(plugin_info)
return jsonify({"success": True, "data": plugin_list})
except Exception as e:
LOG.error(f"获取插件列表失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"获取插件列表失败: {str(e)}"})
@plugin_routes.route('/api/plugins/info', methods=['GET'])
@login_required
def get_plugin_info():
@@ -60,19 +62,19 @@ def get_plugin_info():
plugin_name = request.args.get('plugin_name')
if not plugin_name:
return jsonify({"success": False, "message": "缺少插件名称参数"})
# 获取插件管理器
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
if not plugin:
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
# 获取插件模块名
try:
module_name = plugin.__class__.__module__.split('.')[-2]
except (IndexError, AttributeError):
module_name = "unknown"
# 构建详细信息
plugin_info = {
"name": plugin.name,
@@ -85,12 +87,13 @@ def get_plugin_info():
"commands": getattr(plugin, 'commands', []),
"config": getattr(plugin, '_config', {})
}
return jsonify({"success": True, "data": plugin_info})
except Exception as e:
LOG.error(f"获取插件详情失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"获取插件详情失败: {str(e)}"})
@plugin_routes.route('/api/plugins/enable', methods=['POST'])
@login_required
def enable_plugin():
@@ -101,7 +104,7 @@ def enable_plugin():
plugin_name = data.get('plugin_name')
if not plugin_name:
return jsonify({"success": False, "message": "缺少插件名称参数"})
# 获取插件管理器
# 启用插件
if server.plugin_manager.start_plugin(plugin_name):
@@ -112,6 +115,7 @@ def enable_plugin():
LOG.error(f"启用插件失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"启用插件失败: {str(e)}"})
@plugin_routes.route('/api/plugins/disable', methods=['POST'])
@login_required
def disable_plugin():
@@ -122,7 +126,7 @@ def disable_plugin():
plugin_name = data.get('plugin_name')
if not plugin_name:
return jsonify({"success": False, "message": "缺少插件名称参数"})
# 禁用插件
if server.plugin_manager.stop_plugin(plugin_name):
return jsonify({"success": True, "message": f"插件 {plugin_name} 禁用成功"})
@@ -132,6 +136,7 @@ def disable_plugin():
LOG.error(f"禁用插件失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"禁用插件失败: {str(e)}"})
@plugin_routes.route('/api/plugins/reload', methods=['POST'])
@login_required
def reload_plugin():
@@ -142,10 +147,10 @@ def reload_plugin():
plugin_name = data.get('plugin_name')
if not plugin_name:
return jsonify({"success": False, "message": "缺少插件名称参数"})
# 重载插件
reloaded_plugin = server.plugin_manager.reload_plugin(plugin_name)
if reloaded_plugin:
return jsonify({"success": True, "message": f"插件 {plugin_name} 重载成功"})
else:
@@ -164,34 +169,34 @@ def get_raw_plugin_config():
plugin_name = request.args.get('plugin_name')
if not plugin_name:
return jsonify({"success": False, "message": "缺少插件名称参数"})
# 获取插件管理器
# 查找插件
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
if not plugin:
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
# 获取配置文件路径
config_path = plugin.get_config_path()
if not os.path.exists(config_path):
return jsonify({"success": False, "message": f"配置文件不存在: {config_path}"})
# 读取配置文件内容
with open(config_path, 'r', encoding='utf-8') as f:
config_text = f.read()
# 确定配置文件格式
format_type = 'toml'
if config_path.endswith('.json'):
format_type = 'json'
elif config_path.endswith('.yaml') or config_path.endswith('.yml'):
format_type = 'yaml'
return jsonify({
"success": True,
"success": True,
"data": config_text,
"format": format_type
})
@@ -199,6 +204,7 @@ def get_raw_plugin_config():
LOG.error(f"获取插件配置文件失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"获取配置文件失败: {str(e)}"})
@plugin_routes.route('/api/plugins/config/update', methods=['POST'])
@login_required
def update_plugin_config():
@@ -209,27 +215,27 @@ def update_plugin_config():
plugin_name = data.get('plugin_name')
config_text = data.get('config_text')
format_type = data.get('format', 'toml')
if not plugin_name or config_text is None:
return jsonify({"success": False, "message": "缺少必要参数"})
# 查找插件
# 获取插件管理器
display_name, plugin = server.plugin_manager.find_plugin_by_name(plugin_name)
if not plugin:
return jsonify({"success": False, "message": f"未找到插件: {plugin_name}"})
# 获取配置文件路径
config_path = plugin.get_config_path()
# 确保配置目录存在
os.makedirs(os.path.dirname(config_path), exist_ok=True)
# 写入配置文件
with open(config_path, 'w', encoding='utf-8') as f:
f.write(config_text)
# 解析配置并更新插件内部配置
try:
if format_type == 'toml':
@@ -238,15 +244,15 @@ def update_plugin_config():
config_obj = json.loads(config_text)
else:
return jsonify({"success": False, "message": f"不支持的配置格式: {format_type}"})
# 更新插件内部配置
plugin._config = config_obj
return jsonify({"success": True, "message": "配置已保存"})
except Exception as e:
LOG.error(f"解析配置失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"配置已保存,但解析失败: {str(e)}"})
except Exception as e:
LOG.error(f"更新插件配置失败: {str(e)}", exc_info=True)
return jsonify({"success": False, "message": f"更新配置失败: {str(e)}"})
return jsonify({"success": False, "message": f"更新配置失败: {str(e)}"})