wds 37a5d8d804 feat: V1.0.0
2025-10-21
-  add the shell history feature
-  add the logging feature
-  refactor the codebase for better maintainability
2025-10-21 19:36:27 +08:00

37 lines
1.3 KiB
Python

import os
import json
from typing import Dict, Optional, Any
from autoterminal.utils.logger import logger
class ConfigLoader:
"""配置加载器,支持从文件加载配置"""
def __init__(self, config_file: str = None):
if config_file is None:
# 从用户主目录下的.autoterminal目录中加载配置文件
home_dir = os.path.expanduser("~")
config_dir = os.path.join(home_dir, ".autoterminal")
self.config_file = os.path.join(config_dir, "config.json")
else:
self.config_file = config_file
def load_from_file(self) -> Dict:
"""从配置文件加载配置"""
if os.path.exists(self.config_file):
try:
logger.debug(f"从文件加载配置: {self.config_file}")
with open(self.config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
logger.info("配置文件加载成功")
return config
except Exception as e:
logger.error(f"无法读取配置文件 {self.config_file}: {e}")
else:
logger.debug(f"配置文件不存在: {self.config_file}")
return {}
def get_config(self) -> Dict:
"""获取配置"""
return self.load_from_file()