feat: 新增股票数据波形图和截图功能
refactor: 重构数据库和LLM分析器逻辑 fix: 修复爬虫解析和UI显示问题 docs: 更新配置文件和注释 style: 优化代码格式和日志输出
This commit is contained in:
@@ -5,6 +5,7 @@ import json
|
||||
import os
|
||||
from typing import Any, Dict
|
||||
from pathlib import Path
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class ConfigManager:
|
||||
@@ -12,10 +13,10 @@ class ConfigManager:
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"llm_api": {
|
||||
"base_url": "https://api.openai.com/v1",
|
||||
"base_url": "https://integrate.api.nvidia.com/v1",
|
||||
"api_key": "",
|
||||
"model": "gpt-3.5-turbo",
|
||||
"timeout": 30,
|
||||
"model": "deepseek-ai/deepseek-r1",
|
||||
"timeout": 120,
|
||||
"retry_times": 3
|
||||
},
|
||||
"spider": {
|
||||
@@ -46,19 +47,24 @@ class ConfigManager:
|
||||
def __init__(self, config_path: str = "config.json"):
|
||||
self.config_path = Path(config_path)
|
||||
self.config = self._load_config()
|
||||
logger.info(f"配置管理器初始化完成,配置文件: {config_path}")
|
||||
|
||||
def _load_config(self) -> Dict[str, Any]:
|
||||
"""加载配置文件"""
|
||||
if self.config_path.exists():
|
||||
try:
|
||||
logger.info(f"从文件加载配置: {self.config_path}")
|
||||
with open(self.config_path, 'r', encoding='utf-8') as f:
|
||||
loaded_config = json.load(f)
|
||||
# 合并默认配置,确保所有键都存在
|
||||
return self._merge_config(self.DEFAULT_CONFIG, loaded_config)
|
||||
merged = self._merge_config(self.DEFAULT_CONFIG, loaded_config)
|
||||
logger.info("配置加载成功")
|
||||
return merged
|
||||
except (json.JSONDecodeError, IOError) as e:
|
||||
print(f"配置文件加载失败,使用默认配置: {e}")
|
||||
logger.error(f"配置文件加载失败,使用默认配置: {e}")
|
||||
return self.DEFAULT_CONFIG.copy()
|
||||
else:
|
||||
logger.warning(f"配置文件不存在: {self.config_path},使用默认配置")
|
||||
return self.DEFAULT_CONFIG.copy()
|
||||
|
||||
def _merge_config(self, default: Dict, loaded: Dict) -> Dict:
|
||||
@@ -74,11 +80,13 @@ class ConfigManager:
|
||||
def save_config(self) -> bool:
|
||||
"""保存配置到文件"""
|
||||
try:
|
||||
logger.debug(f"保存配置到文件: {self.config_path}")
|
||||
with open(self.config_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(self.config, f, ensure_ascii=False, indent=4)
|
||||
logger.info("配置保存成功")
|
||||
return True
|
||||
except IOError as e:
|
||||
print(f"配置保存失败: {e}")
|
||||
logger.error(f"配置保存失败: {e}")
|
||||
return False
|
||||
|
||||
def get(self, *keys: str, default: Any = None) -> Any:
|
||||
@@ -118,6 +126,7 @@ class ConfigManager:
|
||||
self.config["llm_api"]["timeout"] = timeout
|
||||
if retry_times:
|
||||
self.config["llm_api"]["retry_times"] = retry_times
|
||||
logger.info("LLM API配置已更新")
|
||||
self.save_config()
|
||||
|
||||
def update_spider(self, target_url: str = None, xpath: str = None,
|
||||
@@ -136,6 +145,7 @@ class ConfigManager:
|
||||
self.config["spider"]["retry_times"] = retry_times
|
||||
if retry_interval:
|
||||
self.config["spider"]["retry_interval"] = retry_interval
|
||||
logger.info("爬虫配置已更新")
|
||||
self.save_config()
|
||||
|
||||
def update_ui(self, opacity: float = None, is_on_top: bool = None,
|
||||
@@ -149,6 +159,7 @@ class ConfigManager:
|
||||
self.config["ui"]["thresholds"]["cold"] = cold_threshold
|
||||
if warm_threshold is not None:
|
||||
self.config["ui"]["thresholds"]["warm"] = warm_threshold
|
||||
logger.info("UI配置已更新")
|
||||
self.save_config()
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user