From d5b0c262894c66cc5858ccc930f0c007b8948c70 Mon Sep 17 00:00:00 2001 From: Rat Date: Wed, 8 Jul 2026 11:04:19 +0800 Subject: [PATCH] fix(path): pass plugin name to StarTools.get_data_dir() to avoid inspect.stack() crash (#193) * fix: pass plugin name to StarTools.get_data_dir to avoid inspect-stack crash * refactor: reuse plugin name constant for data dir --------- Co-authored-by: Rat0323 Co-authored-by: Rat0323 <261020116+Rat0323@users.noreply.github.com> --- main.py | 36 ++++--------------- .../analysis/analyzers/base_analyzer.py | 14 ++------ src/infrastructure/analysis/llm_analyzer.py | 3 +- src/infrastructure/config/config_manager.py | 20 +++-------- src/infrastructure/reporting/dispatcher.py | 22 ++++-------- 5 files changed, 21 insertions(+), 74 deletions(-) diff --git a/main.py b/main.py index 3506eef..86c0a07 100644 --- a/main.py +++ b/main.py @@ -45,6 +45,7 @@ from .src.infrastructure.platform.template_preview import ( ) from .src.infrastructure.reporting.generators import ReportGenerator from .src.infrastructure.scheduler.auto_scheduler import AutoScheduler +from .src.shared.constants import PLUGIN_NAME from .src.shared.trace_context import TraceContext, TraceLogFilter from .src.utils.logger import logger from .src.utils.resilience import GlobalRateLimiter @@ -77,10 +78,6 @@ class GroupDailyAnalysis(Star): super().__init__(context) self.config = config - from pathlib import Path - - from astrbot.core.utils.astrbot_path import get_astrbot_data_path - # 1. 基础设施层 self.config_manager = ConfigManager(config) self.bot_manager = BotManager(self.config_manager) @@ -88,15 +85,7 @@ class GroupDailyAnalysis(Star): self.bot_manager.set_plugin_instance(self) self.history_manager = HistoryManager(self) - try: - plugin_data_dir = StarTools.get_data_dir() - except Exception: - # 回退逻辑:手动构造满足规范的路径 - plugin_data_dir = ( - Path(get_astrbot_data_path()) - / "plugin_data" - / "astrbot_plugin_qq_group_daily_analysis" - ) + plugin_data_dir = StarTools.get_data_dir(PLUGIN_NAME) self.report_generator = ReportGenerator(self.config_manager, plugin_data_dir) @@ -640,23 +629,12 @@ class GroupDailyAnalysis(Star): # 若用户配置为空,使用默认目录 if not html_output_dir: - try: - from astrbot.api.star import StarTools + from astrbot.api.star import StarTools - html_output_dir = os.path.join( - StarTools.get_data_dir(), "self_hosted_html_reports" - ) - except Exception: - from astrbot.core.utils.astrbot_path import ( - get_astrbot_data_path, - ) - - html_output_dir = os.path.join( - get_astrbot_data_path(), - "plugin_data", - "astrbot_plugin_qq_group_daily_analysis", - "self_hosted_html_reports", - ) + html_output_dir = os.path.join( + StarTools.get_data_dir(PLUGIN_NAME), + "self_hosted_html_reports", + ) # 计算相对路径并转换为URL rel_path = os.path.relpath(html_path, html_output_dir) diff --git a/src/infrastructure/analysis/analyzers/base_analyzer.py b/src/infrastructure/analysis/analyzers/base_analyzer.py index b9f73a7..c5fa5a9 100644 --- a/src/infrastructure/analysis/analyzers/base_analyzer.py +++ b/src/infrastructure/analysis/analyzers/base_analyzer.py @@ -8,6 +8,7 @@ from collections.abc import Sized from typing import Generic, TypeVar from ....domain.models.data_models import TokenUsage +from ....shared.constants import PLUGIN_NAME from ....utils.logger import logger from ..utils.json_utils import parse_json_response from ..utils.llm_utils import ( @@ -280,20 +281,9 @@ class BaseAnalyzer(ABC, Generic[TDataObject, TInputData]): session_id: 会话ID """ try: - from pathlib import Path - from astrbot.api.star import StarTools - from astrbot.core.utils.astrbot_path import get_astrbot_data_path - try: - data_path = StarTools.get_data_dir() / "debug_data" - except Exception: - data_path = ( - Path(get_astrbot_data_path()) - / "plugin_data" - / "astrbot_plugin_qq_group_daily_analysis" - / "debug_data" - ) + data_path = StarTools.get_data_dir(PLUGIN_NAME) / "debug_data" data_path.mkdir(parents=True, exist_ok=True) diff --git a/src/infrastructure/analysis/llm_analyzer.py b/src/infrastructure/analysis/llm_analyzer.py index cbc73c5..a4eb48b 100644 --- a/src/infrastructure/analysis/llm_analyzer.py +++ b/src/infrastructure/analysis/llm_analyzer.py @@ -13,6 +13,7 @@ from ...domain.models.data_models import ( UserTitle, ) from ...domain.repositories.analysis_repository import IAnalysisProvider +from ...shared.constants import PLUGIN_NAME from ...utils.logger import logger from .analyzers.chat_quality_analyzer import ChatQualityAnalyzer from .analyzers.golden_quote_analyzer import GoldenQuoteAnalyzer @@ -464,7 +465,7 @@ class LLMAnalyzer(IAnalysisProvider): from astrbot.api.star import StarTools - debug_dir = StarTools.get_data_dir() / "debug_data" + debug_dir = StarTools.get_data_dir(PLUGIN_NAME) / "debug_data" debug_dir.mkdir(parents=True, exist_ok=True) msg_file_path = debug_dir / f"{session_id}_messages.json" diff --git a/src/infrastructure/config/config_manager.py b/src/infrastructure/config/config_manager.py index 5f65382..145ae7a 100644 --- a/src/infrastructure/config/config_manager.py +++ b/src/infrastructure/config/config_manager.py @@ -6,6 +6,7 @@ from astrbot.api import AstrBotConfig from astrbot.api.star import StarTools +from ...shared.constants import PLUGIN_NAME from ...utils.logger import logger from ..utils.template_utils import upgrade_str_format_template @@ -297,23 +298,10 @@ class ConfigManager: def get_html_output_dir(self) -> str: """获取HTML输出目录""" - from pathlib import Path - from astrbot.core.utils.astrbot_path import get_astrbot_data_path - - try: - default_path = StarTools.get_data_dir() / "self_hosted_html_reports" - val = self._get_group("html").get("html_output_dir") - return val if val else str(default_path) - except Exception: - val = self._get_group("html").get("html_output_dir") - fallback_path = ( - Path(get_astrbot_data_path()) - / "plugin_data" - / "astrbot_plugin_qq_group_daily_analysis" - / "self_hosted_html_reports" - ) - return val if val else str(fallback_path) + default_path = StarTools.get_data_dir(PLUGIN_NAME) / "self_hosted_html_reports" + val = self._get_group("html").get("html_output_dir") + return val if val else str(default_path) def get_html_base_url(self) -> str: """获取HTML外链Base URL""" diff --git a/src/infrastructure/reporting/dispatcher.py b/src/infrastructure/reporting/dispatcher.py index d543eba..2ad0088 100644 --- a/src/infrastructure/reporting/dispatcher.py +++ b/src/infrastructure/reporting/dispatcher.py @@ -5,6 +5,7 @@ from collections.abc import Callable from datetime import datetime from typing import Any +from ...shared.constants import PLUGIN_NAME from ...shared.trace_context import TraceContext from ...utils.logger import logger @@ -149,23 +150,12 @@ class ReportDispatcher: # 若用户配置为空,使用默认目录 if not html_output_dir: - try: - from astrbot.api.star import StarTools + from astrbot.api.star import StarTools - html_output_dir = os.path.join( - StarTools.get_data_dir(), "self_hosted_html_reports" - ) - except Exception: - from astrbot.core.utils.astrbot_path import ( - get_astrbot_data_path, - ) - - html_output_dir = os.path.join( - get_astrbot_data_path(), - "plugin_data", - "astrbot_plugin_qq_group_daily_analysis", - "self_hosted_html_reports", - ) + html_output_dir = os.path.join( + StarTools.get_data_dir(PLUGIN_NAME), + "self_hosted_html_reports", + ) # 计算相对路径并转换为URL rel_path = os.path.relpath(html_path, html_output_dir)