调整toml格式的内容显示与编辑
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
||||
import toml
|
||||
from flask import Blueprint, request, jsonify, render_template, current_app
|
||||
|
||||
from admin.dashboard.blueprints.auth import login_required
|
||||
@@ -148,4 +152,101 @@ def reload_plugin():
|
||||
return jsonify({"success": False, "message": f"插件 {plugin_name} 重载失败"})
|
||||
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)}"})
|
||||
|
||||
|
||||
@plugin_routes.route('/api/plugins/config/raw', methods=['GET'])
|
||||
@login_required
|
||||
def get_raw_plugin_config():
|
||||
"""获取插件原始配置文件内容"""
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
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,
|
||||
"data": config_text,
|
||||
"format": format_type
|
||||
})
|
||||
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/config/update', methods=['POST'])
|
||||
@login_required
|
||||
def update_plugin_config():
|
||||
"""更新插件配置"""
|
||||
try:
|
||||
server = current_app.dashboard_server
|
||||
data = request.json
|
||||
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':
|
||||
config_obj = toml.loads(config_text)
|
||||
elif format_type == 'json':
|
||||
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)}"})
|
||||
@@ -17,6 +17,8 @@
|
||||
<script src="/static/js/element-ui/index.min.js"></script>
|
||||
<!-- Axios -->
|
||||
<script src="/static/js/axios.min.js"></script>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/@iarna/toml@2.2.5/toml.js"></script>
|
||||
<style>
|
||||
body {
|
||||
margin: 0;
|
||||
|
||||
@@ -80,7 +80,30 @@
|
||||
</el-descriptions-item>
|
||||
<el-descriptions-item label="配置信息" :span="2" v-if="selectedPlugin.config">
|
||||
<div class="config-container">
|
||||
<pre>{% raw %}{{ JSON.stringify(selectedPlugin.config, null, 2) }}{% endraw %}</pre>
|
||||
<div class="config-actions">
|
||||
<el-button type="primary" size="mini" @click="editConfig" :disabled="isEditingConfig">
|
||||
编辑配置
|
||||
</el-button>
|
||||
<el-button type="success" size="mini" @click="saveConfig" v-if="isEditingConfig">
|
||||
保存配置
|
||||
</el-button>
|
||||
<el-button type="info" size="mini" @click="cancelEditConfig" v-if="isEditingConfig">
|
||||
取消
|
||||
</el-button>
|
||||
</div>
|
||||
<div v-if="!isEditingConfig">
|
||||
<pre>{% raw %}{{ selectedPlugin.configText }}{% endraw %}</pre>
|
||||
</div>
|
||||
<div v-else>
|
||||
<el-input
|
||||
type="textarea"
|
||||
v-model="editedConfig"
|
||||
:rows="10"
|
||||
placeholder="请输入TOML格式的配置"
|
||||
class="config-editor"
|
||||
></el-input>
|
||||
<div class="config-error" v-if="configError">{% raw %}{{ configError }}{% endraw %}</div>
|
||||
</div>
|
||||
</div>
|
||||
</el-descriptions-item>
|
||||
</el-descriptions>
|
||||
@@ -99,7 +122,11 @@
|
||||
plugins: [],
|
||||
selectedPlugin: null,
|
||||
pluginInfoVisible: false,
|
||||
loading: false
|
||||
loading: false,
|
||||
isEditingConfig: false,
|
||||
editedConfig: '',
|
||||
configError: '',
|
||||
configFormat: 'toml' // 默认为toml格式
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
@@ -182,13 +209,106 @@
|
||||
this.$message.info('已取消操作');
|
||||
});
|
||||
},
|
||||
// 编辑配置
|
||||
editConfig() {
|
||||
this.isEditingConfig = true;
|
||||
this.editedConfig = this.selectedPlugin.configText || '';
|
||||
this.configError = '';
|
||||
},
|
||||
|
||||
// 取消编辑配置
|
||||
cancelEditConfig() {
|
||||
this.isEditingConfig = false;
|
||||
this.editedConfig = '';
|
||||
this.configError = '';
|
||||
},
|
||||
|
||||
// 保存配置
|
||||
saveConfig() {
|
||||
try {
|
||||
// 验证TOML格式
|
||||
let configObj;
|
||||
try {
|
||||
configObj = toml.parse(this.editedConfig);
|
||||
} catch (e) {
|
||||
this.configError = 'TOML格式错误: ' + e.message;
|
||||
return;
|
||||
}
|
||||
|
||||
// 发送到后端保存
|
||||
axios.post('/api/plugins/config/update', {
|
||||
plugin_name: this.selectedPlugin.module_name,
|
||||
config_text: this.editedConfig,
|
||||
format: 'toml'
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data.success) {
|
||||
this.$message.success('配置保存成功');
|
||||
this.isEditingConfig = false;
|
||||
|
||||
// 更新本地配置显示
|
||||
this.selectedPlugin.configText = this.editedConfig;
|
||||
this.selectedPlugin.config = configObj;
|
||||
|
||||
// 询问是否要重载插件以应用新配置
|
||||
this.$confirm('配置已保存,是否要重载插件以应用新配置?', '提示', {
|
||||
confirmButtonText: '重载插件',
|
||||
cancelButtonText: '稍后手动重载',
|
||||
type: 'info'
|
||||
}).then(() => {
|
||||
this.reloadPlugin(this.selectedPlugin);
|
||||
}).catch(() => {
|
||||
this.$message.info('您可以稍后手动重载插件以应用新配置');
|
||||
});
|
||||
} else {
|
||||
this.configError = response.data.message || '保存配置失败';
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('保存配置出错:', error);
|
||||
this.configError = '保存配置出错: ' + (error.response?.data?.message || error.message);
|
||||
});
|
||||
} catch (e) {
|
||||
this.configError = '处理配置时出错: ' + e.message;
|
||||
}
|
||||
},
|
||||
|
||||
// 修改现有的showPluginInfo方法,获取原始配置文本
|
||||
showPluginInfo(plugin) {
|
||||
// 获取插件详细信息
|
||||
axios.get(`/api/plugins/info?plugin_name=${plugin.module_name}`)
|
||||
.then(response => {
|
||||
if (response.data.success) {
|
||||
this.selectedPlugin = response.data.data;
|
||||
this.pluginInfoVisible = true;
|
||||
// 如果有配置文本,直接使用
|
||||
if (this.selectedPlugin.configText) {
|
||||
this.pluginInfoVisible = true;
|
||||
this.isEditingConfig = false;
|
||||
this.editedConfig = '';
|
||||
this.configError = '';
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果没有配置文本,获取原始配置文件内容
|
||||
axios.get(`/api/plugins/config/raw?plugin_name=${plugin.module_name}`)
|
||||
.then(configResponse => {
|
||||
if (configResponse.data.success) {
|
||||
this.selectedPlugin.configText = configResponse.data.data;
|
||||
this.configFormat = configResponse.data.format || 'toml';
|
||||
} else {
|
||||
this.selectedPlugin.configText = '# 无法获取原始配置文件';
|
||||
console.error('获取配置文件失败:', configResponse.data.message);
|
||||
}
|
||||
this.pluginInfoVisible = true;
|
||||
this.isEditingConfig = false;
|
||||
this.editedConfig = '';
|
||||
this.configError = '';
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('获取配置文件出错:', error);
|
||||
this.selectedPlugin.configText = '# 获取配置文件时出错';
|
||||
this.pluginInfoVisible = true;
|
||||
});
|
||||
} else {
|
||||
this.$message.error(response.data.message || '获取插件详情失败');
|
||||
}
|
||||
@@ -248,5 +368,24 @@
|
||||
.config-container::-webkit-scrollbar-track {
|
||||
background: #f5f7fa;
|
||||
}
|
||||
|
||||
/* ... 现有样式保持不变 ... */
|
||||
|
||||
.config-actions {
|
||||
margin-bottom: 10px;
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.config-editor {
|
||||
font-family: monospace;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.config-error {
|
||||
color: #f56c6c;
|
||||
font-size: 12px;
|
||||
margin-top: 5px;
|
||||
}
|
||||
</style>
|
||||
{% endblock %}
|
||||
@@ -170,7 +170,8 @@ class PluginInterface(ABC):
|
||||
"""
|
||||
return True
|
||||
|
||||
# ... 其他现有方法 ...
|
||||
def get_config_path(self) -> str:
|
||||
return os.path.join(self._plugin_path, 'config.toml')
|
||||
|
||||
def can_process(self, data: Any) -> bool:
|
||||
"""检查插件是否可以处理给定的数据
|
||||
|
||||
Reference in New Issue
Block a user