home1018
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
import json
|
||||
import os
|
||||
from typing import Dict, List, Optional
|
||||
from logger_config import global_logger as logger
|
||||
|
||||
class ConfigManager:
|
||||
"""
|
||||
配置管理器,负责加载、保存和管理应用配置
|
||||
"""
|
||||
|
||||
def __init__(self, config_file: str = 'config.json'):
|
||||
"""
|
||||
初始化配置管理器
|
||||
|
||||
Args:
|
||||
config_file (str): 配置文件路径
|
||||
"""
|
||||
self.config_file = config_file
|
||||
self.default_config = {
|
||||
'sites': []
|
||||
}
|
||||
|
||||
def load_config(self) -> Dict:
|
||||
"""
|
||||
加载配置文件
|
||||
|
||||
Returns:
|
||||
dict: 配置数据
|
||||
"""
|
||||
try:
|
||||
if os.path.exists(self.config_file):
|
||||
with open(self.config_file, 'r', encoding='utf-8') as f:
|
||||
config = json.load(f)
|
||||
logger.info(f"成功加载配置文件: {self.config_file}")
|
||||
return config
|
||||
else:
|
||||
logger.warning(f"配置文件不存在: {self.config_file}")
|
||||
logger.info("使用默认配置")
|
||||
return self.default_config.copy()
|
||||
except json.JSONDecodeError as e:
|
||||
logger.error(f"配置文件格式错误: {e}")
|
||||
logger.info("使用默认配置")
|
||||
return self.default_config.copy()
|
||||
except Exception as e:
|
||||
logger.error(f"加载配置文件时发生错误: {e}")
|
||||
logger.info("使用默认配置")
|
||||
return self.default_config.copy()
|
||||
|
||||
def save_config(self, config: Dict) -> bool:
|
||||
"""
|
||||
保存配置到文件
|
||||
|
||||
Args:
|
||||
config (dict): 要保存的配置数据
|
||||
|
||||
Returns:
|
||||
bool: 保存是否成功
|
||||
"""
|
||||
try:
|
||||
# 确保配置中包含必要的键
|
||||
if 'sites' not in config:
|
||||
config['sites'] = []
|
||||
|
||||
with open(self.config_file, 'w', encoding='utf-8') as f:
|
||||
json.dump(config, f, ensure_ascii=False, indent=2)
|
||||
|
||||
logger.info(f"成功保存配置到文件: {self.config_file}")
|
||||
return True
|
||||
except Exception as e:
|
||||
logger.error(f"保存配置文件时发生错误: {e}")
|
||||
return False
|
||||
|
||||
def get_sites(self) -> List[Dict]:
|
||||
"""
|
||||
获取所有网站配置
|
||||
|
||||
Returns:
|
||||
list: 网站配置列表
|
||||
"""
|
||||
config = self.load_config()
|
||||
sites = config.get('sites', [])
|
||||
logger.info(f"获取到 {len(sites)} 个网站配置")
|
||||
return sites
|
||||
|
||||
def add_site(self, site_config: Dict) -> bool:
|
||||
"""
|
||||
添加网站配置
|
||||
|
||||
Args:
|
||||
site_config (dict): 网站配置
|
||||
|
||||
Returns:
|
||||
bool: 添加是否成功
|
||||
"""
|
||||
if not isinstance(site_config, dict) or 'name' not in site_config or 'url' not in site_config:
|
||||
logger.error("网站配置格式错误,必须包含name和url字段")
|
||||
return False
|
||||
|
||||
config = self.load_config()
|
||||
sites = config.get('sites', [])
|
||||
|
||||
# 检查是否已存在同名网站
|
||||
for site in sites:
|
||||
if site.get('name') == site_config.get('name'):
|
||||
logger.warning(f"网站 '{site_config.get('name')}' 已存在")
|
||||
return False
|
||||
|
||||
sites.append(site_config)
|
||||
config['sites'] = sites
|
||||
return self.save_config(config)
|
||||
|
||||
def update_site(self, site_name: str, updated_config: Dict) -> bool:
|
||||
"""
|
||||
更新网站配置
|
||||
|
||||
Args:
|
||||
site_name (str): 网站名称
|
||||
updated_config (dict): 更新后的配置
|
||||
|
||||
Returns:
|
||||
bool: 更新是否成功
|
||||
"""
|
||||
config = self.load_config()
|
||||
sites = config.get('sites', [])
|
||||
|
||||
for i, site in enumerate(sites):
|
||||
if site.get('name') == site_name:
|
||||
# 保留原有的name和url,只更新其他字段
|
||||
updated_site = site.copy()
|
||||
updated_site.update(updated_config)
|
||||
sites[i] = updated_site
|
||||
config['sites'] = sites
|
||||
return self.save_config(config)
|
||||
|
||||
logger.warning(f"找不到名称为 '{site_name}' 的网站配置")
|
||||
return False
|
||||
|
||||
def delete_site(self, site_name: str) -> bool:
|
||||
"""
|
||||
删除网站配置
|
||||
|
||||
Args:
|
||||
site_name (str): 网站名称
|
||||
|
||||
Returns:
|
||||
bool: 删除是否成功
|
||||
"""
|
||||
config = self.load_config()
|
||||
sites = config.get('sites', [])
|
||||
|
||||
new_sites = [site for site in sites if site.get('name') != site_name]
|
||||
|
||||
if len(new_sites) == len(sites):
|
||||
logger.warning(f"找不到名称为 '{site_name}' 的网站配置")
|
||||
return False
|
||||
|
||||
config['sites'] = new_sites
|
||||
return self.save_config(config)
|
||||
Reference in New Issue
Block a user