41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
配置管理器
|
||
|
|
"""
|
||
|
|
|
||
|
|
import yaml
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
class ConfigManager:
|
||
|
|
"""配置管理类"""
|
||
|
|
|
||
|
|
# 默认配置文件路径
|
||
|
|
DEFAULT_CONFIG_FILE = Path(__file__).parent.parent / "default_config.yaml"
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def load_config(filename):
|
||
|
|
"""加载配置文件"""
|
||
|
|
with open(filename, 'r', encoding='utf-8') as f:
|
||
|
|
config = yaml.safe_load(f)
|
||
|
|
return config
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def save_config(config, filename):
|
||
|
|
"""保存配置文件"""
|
||
|
|
# 确保目录存在
|
||
|
|
Path(filename).parent.mkdir(parents=True, exist_ok=True)
|
||
|
|
with open(filename, 'w', encoding='utf-8') as f:
|
||
|
|
yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def load_default_config():
|
||
|
|
"""加载默认配置文件"""
|
||
|
|
if ConfigManager.DEFAULT_CONFIG_FILE.exists():
|
||
|
|
return ConfigManager.load_config(ConfigManager.DEFAULT_CONFIG_FILE)
|
||
|
|
return None
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def save_default_config(config):
|
||
|
|
"""保存默认配置文件"""
|
||
|
|
ConfigManager.save_config(config, ConfigManager.DEFAULT_CONFIG_FILE)
|