Files
ComiPy/utils/config_validator.py
Kaku 8c4e5885c7 feat(file): 优化文件处理和缓存机制
- 重构文件处理逻辑,提高性能和可维护性
- 增加缓存机制,减少重复读取和处理
- 改进错误处理和日志记录
- 优化缩略图生成算法
- 添加性能监控和测试依赖
2025-07-11 00:21:57 +08:00

105 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
from typing import Dict, Any
import app_conf
def validate_config() -> Dict[str, Any]:
"""
验证配置文件的有效性
返回验证结果字典
"""
conf = app_conf.conf()
issues = []
warnings = []
# 检查必需的配置项
required_sections = {
'server': ['port', 'host'],
'user': ['username', 'password'],
'database': ['path'],
'file': ['inputdir', 'storedir', 'tmpdir'],
'img': ['encode', 'miniSize', 'fullSize']
}
for section, keys in required_sections.items():
if not conf.has_section(section):
issues.append(f"缺少配置节: [{section}]")
continue
for key in keys:
if not conf.has_option(section, key):
issues.append(f"缺少配置项: [{section}].{key}")
# 检查安全性问题
if conf.has_section('user'):
username = conf.get('user', 'username', fallback='')
password = conf.get('user', 'password', fallback='')
if username == 'admin' and password == 'admin':
warnings.append("使用默认用户名和密码不安全,建议修改")
if len(password) < 8:
warnings.append("密码过于简单建议使用8位以上的复杂密码")
# 检查端口配置
if conf.has_section('server'):
try:
port = conf.getint('server', 'port')
if port < 1024 or port > 65535:
warnings.append(f"端口号 {port} 可能不合适建议使用1024-65535范围内的端口")
except:
issues.append("服务器端口配置无效")
# 检查目录权限
if conf.has_section('file'):
directories = ['inputdir', 'storedir', 'tmpdir']
for dir_key in directories:
dir_path = conf.get('file', dir_key, fallback='')
if dir_path:
abs_path = os.path.abspath(dir_path)
parent_dir = os.path.dirname(abs_path)
if not os.path.exists(parent_dir):
issues.append(f"父目录不存在: {parent_dir} (配置: {dir_key})")
elif not os.access(parent_dir, os.W_OK):
issues.append(f"没有写入权限: {parent_dir} (配置: {dir_key})")
# 检查数据库路径
if conf.has_section('database'):
db_path = conf.get('database', 'path', fallback='')
if db_path:
db_dir = os.path.dirname(os.path.abspath(db_path))
if not os.path.exists(db_dir):
issues.append(f"数据库目录不存在: {db_dir}")
elif not os.access(db_dir, os.W_OK):
issues.append(f"数据库目录没有写入权限: {db_dir}")
return {
'valid': len(issues) == 0,
'issues': issues,
'warnings': warnings
}
def print_validation_results(results: Dict[str, Any]):
"""打印配置验证结果"""
if results['valid']:
print("✅ 配置验证通过")
else:
print("❌ 配置验证失败")
print("\n严重问题:")
for issue in results['issues']:
print(f"{issue}")
if results['warnings']:
print("\n⚠️ 警告:")
for warning in results['warnings']:
print(f"{warning}")
if __name__ == "__main__":
# 当直接运行此文件时,执行配置验证
results = validate_config()
print_validation_results(results)
if not results['valid']:
sys.exit(1)