79 lines
2.6 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.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
配置管理器
===========
負責處理用戶配置的載入、保存和管理。
"""
import json
from pathlib import Path
from typing import Dict, Any
from ...debug import gui_debug_log as debug_log
class ConfigManager:
"""配置管理器"""
def __init__(self):
self._config_file = self._get_config_file_path()
self._config_cache = {}
self._load_config()
def _get_config_file_path(self) -> Path:
"""獲取配置文件路徑"""
config_dir = Path.home() / ".config" / "mcp-feedback-enhanced"
config_dir.mkdir(parents=True, exist_ok=True)
return config_dir / "ui_settings.json"
def _load_config(self) -> None:
"""載入配置"""
try:
if self._config_file.exists():
with open(self._config_file, 'r', encoding='utf-8') as f:
self._config_cache = json.load(f)
debug_log("配置文件載入成功")
else:
self._config_cache = {}
debug_log("配置文件不存在,使用預設配置")
except Exception as e:
debug_log(f"載入配置失敗: {e}")
self._config_cache = {}
def _save_config(self) -> None:
"""保存配置"""
try:
with open(self._config_file, 'w', encoding='utf-8') as f:
json.dump(self._config_cache, f, ensure_ascii=False, indent=2)
debug_log("配置文件保存成功")
except Exception as e:
debug_log(f"保存配置失敗: {e}")
def get(self, key: str, default: Any = None) -> Any:
"""獲取配置值"""
return self._config_cache.get(key, default)
def set(self, key: str, value: Any) -> None:
"""設置配置值"""
self._config_cache[key] = value
self._save_config()
def get_layout_mode(self) -> bool:
"""獲取佈局模式False=分離模式True=合併模式)"""
return self.get('combined_mode', False)
def set_layout_mode(self, combined_mode: bool) -> None:
"""設置佈局模式"""
self.set('combined_mode', combined_mode)
debug_log(f"佈局模式設置: {'合併模式' if combined_mode else '分離模式'}")
def get_language(self) -> str:
"""獲取語言設置"""
return self.get('language', 'zh-TW')
def set_language(self, language: str) -> None:
"""設置語言"""
self.set('language', language)
debug_log(f"語言設置: {language}")