feat: store packaged user data under data directory
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
import os
|
||||
import sys
|
||||
|
||||
from .. import appconfig
|
||||
from . import widgets as _widgets
|
||||
from .widgets import *
|
||||
|
||||
@@ -46,6 +47,17 @@ def main() -> int:
|
||||
return 1
|
||||
_ensure_offscreen_for_headless_tests()
|
||||
app = QApplication.instance() or QApplication(sys.argv)
|
||||
try:
|
||||
appconfig.prepare_data_dir()
|
||||
except appconfig.DataMigrationConflictError as exc:
|
||||
QMessageBox.critical(None, "数据迁移冲突", str(exc))
|
||||
return 1
|
||||
except appconfig.DataDirectoryWriteError as exc:
|
||||
QMessageBox.critical(None, "数据目录不可写", str(exc))
|
||||
return 1
|
||||
except appconfig.ConfigError as exc:
|
||||
QMessageBox.critical(None, "启动配置错误", str(exc))
|
||||
return 1
|
||||
window = MainWindow()
|
||||
window.show()
|
||||
return app.exec()
|
||||
|
||||
@@ -84,7 +84,7 @@ class MainWindow(QMainWindow):
|
||||
self.ai_models_path = (
|
||||
ai_models_path
|
||||
or self.config.get("ai_models_path")
|
||||
or appconfig.AI_MODELS_PATH
|
||||
or appconfig.ai_models_config_path(self.config)
|
||||
)
|
||||
self.setWindowTitle("蝦皮圈優化助手")
|
||||
_fit_and_center_window(self)
|
||||
@@ -123,6 +123,8 @@ class MainWindow(QMainWindow):
|
||||
config=self.config,
|
||||
config_path=self.config_path,
|
||||
status_callback=self.statusBar().showMessage,
|
||||
title_prompt_path=appconfig.title_prompt_path(self.config),
|
||||
cover_prompts_dir=appconfig.cover_prompts_dir(self.config),
|
||||
open_accounts_callback=lambda: self.open_accounts_tab(),
|
||||
)
|
||||
if title == "③ 更新shopee":
|
||||
|
||||
@@ -48,8 +48,8 @@ class GenerateTab(QWidget):
|
||||
self.db_path = _database_path(db_path, self.config)
|
||||
self.status_callback = status_callback
|
||||
self.open_accounts_callback = open_accounts_callback
|
||||
self.title_prompt_path = title_prompt_path or prompts.TITLE_PROMPT_PATH
|
||||
self.cover_prompts_dir = cover_prompts_dir or prompts.COVER_PROMPTS_DIR
|
||||
self.title_prompt_path = title_prompt_path or appconfig.title_prompt_path(self.config)
|
||||
self.cover_prompts_dir = cover_prompts_dir or appconfig.cover_prompts_dir(self.config)
|
||||
self.current_cover_template = None
|
||||
self.generate_worker = None
|
||||
self.generate_thread = None
|
||||
@@ -288,7 +288,7 @@ class GenerateTab(QWidget):
|
||||
payload = {
|
||||
key: value
|
||||
for key, value in self.config.items()
|
||||
if key not in {"config_path", "ai_models_path", "cmhub_config_path"}
|
||||
if key not in {"config_path", "ai_models_path", "cmhub_config_path", "data_dir"}
|
||||
}
|
||||
payload["ai"] = ai_settings
|
||||
try:
|
||||
@@ -299,7 +299,7 @@ class GenerateTab(QWidget):
|
||||
internal = {
|
||||
key: value
|
||||
for key, value in self.config.items()
|
||||
if key in {"config_path", "ai_models_path", "cmhub_config_path"}
|
||||
if key in {"config_path", "ai_models_path", "cmhub_config_path", "data_dir"}
|
||||
}
|
||||
self.config.clear()
|
||||
self.config.update(saved)
|
||||
|
||||
+12
-19
@@ -10,7 +10,7 @@ from ..workers import CMHubSettingsWorker as _RealCMHubSettingsWorker
|
||||
|
||||
|
||||
PLAINTEXT_CMHUB_API_KEY_WARNING = (
|
||||
"cmhub API Key 会以本地明文保存到 config/cmhub.json,仅供本机调用 cmhub 网关使用。"
|
||||
"cmhub API Key 会以本地明文保存到 data/config/cmhub.json,仅供本机调用 cmhub 网关使用。"
|
||||
"该文件已 gitignore,UI 打码显示,日志/导出不记录明文。"
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ def CMHubSettingsWorker(*args, **kwargs):
|
||||
return _call_package_attr("CMHubSettingsWorker", _RealCMHubSettingsWorker, *args, **kwargs)
|
||||
|
||||
class SettingsTab(QWidget):
|
||||
"""Tab 5: AI model definitions stored in config/ai_models.json."""
|
||||
"""Tab 5: AI model definitions stored in data/config/ai_models.json."""
|
||||
|
||||
BACKEND_ITEMS = [("直连模型", "direct"), ("cmhub 网关", "cmhub")]
|
||||
CATEGORY_ITEMS = [("文本", "text"), ("图像", "image")]
|
||||
@@ -48,11 +48,11 @@ class SettingsTab(QWidget):
|
||||
self.ai_models_path = (
|
||||
ai_models_path
|
||||
or self.config.get("ai_models_path")
|
||||
or appconfig.AI_MODELS_PATH
|
||||
or appconfig.ai_models_config_path(self.config)
|
||||
)
|
||||
self.cmhub_config_path = (
|
||||
self.config.get("cmhub_config_path")
|
||||
or self._default_cmhub_config_path(self.config_path)
|
||||
or appconfig.cmhub_config_file_path(self.config)
|
||||
)
|
||||
self.status_callback = status_callback
|
||||
self.models = []
|
||||
@@ -546,15 +546,6 @@ class SettingsTab(QWidget):
|
||||
self._cmhub_auto_refresh_done = True
|
||||
self.refresh_cmhub_models()
|
||||
|
||||
def _default_cmhub_config_path(self, config_path):
|
||||
if config_path and config_path != appconfig.CONFIG_PATH:
|
||||
return os.path.join(
|
||||
os.path.dirname(os.path.abspath(config_path)),
|
||||
"config",
|
||||
"cmhub.json",
|
||||
)
|
||||
return appconfig.CMHUB_CONFIG_PATH
|
||||
|
||||
def _on_backend_changed(self, index=None):
|
||||
self.backend_combo.setVisible(False)
|
||||
self.model_picker_panel.setVisible(False)
|
||||
@@ -752,7 +743,7 @@ class SettingsTab(QWidget):
|
||||
settings = {
|
||||
key: value
|
||||
for key, value in self.config.items()
|
||||
if key not in {"config_path", "ai_models_path", "cmhub_config_path"}
|
||||
if key not in {"config_path", "ai_models_path", "cmhub_config_path", "data_dir"}
|
||||
}
|
||||
settings.update(
|
||||
{
|
||||
@@ -799,6 +790,8 @@ class SettingsTab(QWidget):
|
||||
internal["ai_models_path"] = self.ai_models_path
|
||||
if self.cmhub_config_path != appconfig.CMHUB_CONFIG_PATH:
|
||||
internal["cmhub_config_path"] = self.cmhub_config_path
|
||||
if self.config.get("data_dir"):
|
||||
internal["data_dir"] = self.config.get("data_dir")
|
||||
self.config.clear()
|
||||
self.config.update(saved)
|
||||
self.config.update(internal)
|
||||
@@ -847,9 +840,11 @@ class SettingsTab(QWidget):
|
||||
)
|
||||
self.jpg_quality_spin.setValue(int(ai_cfg.get("jpg_quality", 90) or 90))
|
||||
self.chrome_path_edit.setText(appconfig.chrome_path(self.config))
|
||||
self.user_data_root_edit.setText(appconfig.user_data_root(self.config))
|
||||
self.image_dir_edit.setText(appconfig.image_dir(self.config))
|
||||
self.db_path_edit.setText(appconfig.db_path(self.config))
|
||||
self.user_data_root_edit.setText(
|
||||
str(self.config.get("user_data_root", "chrome_user_data_dir") or "")
|
||||
)
|
||||
self.image_dir_edit.setText(str(self.config.get("image_dir", "images") or ""))
|
||||
self.db_path_edit.setText(str(self.config.get("db_path", "cmshopee.db") or ""))
|
||||
self.default_debug_port_spin.setValue(
|
||||
int(appconfig.default_debug_port(self.config))
|
||||
)
|
||||
@@ -1318,5 +1313,3 @@ class SettingsTab(QWidget):
|
||||
|
||||
def _category_label(self, category):
|
||||
return {"text": "文本", "image": "图像"}.get(category, category)
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -170,7 +170,7 @@ def run_worker(*args, **kwargs):
|
||||
|
||||
PLAINTEXT_SECRET_TITLE = "本地明文保存提示"
|
||||
PLAINTEXT_API_KEY_WARNING = (
|
||||
"API Key 会以本地明文保存到 config/ai_models.json,仅供本机调用 AI 使用。"
|
||||
"API Key 会以本地明文保存到 data/config/ai_models.json,仅供本机调用 AI 使用。"
|
||||
"该文件已 gitignore,UI 打码显示,日志/导出不记录明文。"
|
||||
)
|
||||
PLAINTEXT_PASSWORD_WARNING = (
|
||||
|
||||
Reference in New Issue
Block a user