Skip to content

Commit 0d315d9

Browse files
committed
修复未设置密码时的无限递归问题,开始打包
1 parent f1fbef9 commit 0d315d9

4 files changed

Lines changed: 104 additions & 31 deletions

File tree

app/tools/path_utils.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# ==================================================
2020
# 导入模块
2121
# ==================================================
22+
import os
2223
import sys
2324
from pathlib import Path
2425
from typing import Union
@@ -65,16 +66,46 @@ def get_absolute_path(self, relative_path: Union[str, Path]) -> Path:
6566
Returns:
6667
Path: 绝对路径
6768
"""
68-
if isinstance(relative_path, str):
69-
relative_path = Path(relative_path)
69+
# 转换为字符串
70+
if isinstance(relative_path, Path):
71+
relative_path_str = str(relative_path)
72+
else:
73+
relative_path_str = relative_path
7074

71-
# 如果已经是绝对路径,直接返回
72-
if relative_path.is_absolute():
73-
return relative_path
75+
# 获取app_root的字符串表示
76+
app_root_str = str(self._app_root)
7477

75-
# 拼接为绝对路径
76-
absolute_path = self._app_root / relative_path
77-
return absolute_path
78+
# 使用字符串检查判断是否为绝对路径
79+
# Windows绝对路径:以驱动器号开头,如 C:\ 或 c:/
80+
# Linux绝对路径:以 / 开头
81+
if os.name == "nt":
82+
is_absolute = relative_path_str.startswith(("\\", "/")) or (
83+
len(relative_path_str) >= 2 and relative_path_str[1] == ":"
84+
)
85+
else:
86+
is_absolute = relative_path_str.startswith("/")
87+
88+
if is_absolute:
89+
# 直接返回Path对象
90+
return Path(relative_path_str)
91+
92+
# 使用字符串拼接构建绝对路径,避免使用Path的/运算符
93+
# 确保路径分隔符正确
94+
if os.name == "nt":
95+
# Windows使用\作为路径分隔符
96+
if relative_path_str.startswith("\\") or relative_path_str.startswith("/"):
97+
# 去掉相对路径开头的分隔符
98+
relative_path_str = relative_path_str[1:]
99+
absolute_path_str = rf"{app_root_str}\{relative_path_str}"
100+
else:
101+
# Linux使用/作为路径分隔符
102+
if relative_path_str.startswith("/"):
103+
# 去掉相对路径开头的分隔符
104+
relative_path_str = relative_path_str[1:]
105+
absolute_path_str = f"{app_root_str}/{relative_path_str}"
106+
107+
# 返回Path对象
108+
return Path(absolute_path_str)
78109

79110
def ensure_directory_exists(self, path: Union[str, Path]) -> Path:
80111
"""确保目录存在,如果不存在则创建

app/tools/secure_store.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ def _decrypt_payload(payload: bytes, key: bytes) -> bytes:
9494

9595
def read_secrets() -> dict:
9696
p = get_settings_path("secrets.json")
97+
# 不存在则创建空文件
98+
if not os.path.exists(p):
99+
ensure_dir(os.path.dirname(p))
100+
with open(p, "wb") as f:
101+
f.write(b"")
102+
_set_hidden(p)
103+
logger.debug(f"创建空安全配置文件:{p}")
104+
return {}
97105
if os.path.exists(p):
98106
try:
99107
with open(p, "rb") as f:

app/view/settings/safety_settings.py

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -418,11 +418,19 @@ def apply():
418418
def __on_setting_changed(self, first_level_key, second_level_key, value):
419419
if first_level_key != "basic_safety_settings":
420420
return
421-
if second_level_key == "safety_switch":
421+
# 只在状态不同时才更新,避免不必要的信号触发
422+
if (
423+
second_level_key == "safety_switch"
424+
and self.safety_switch.isChecked() != bool(value)
425+
):
422426
self.safety_switch.setChecked(bool(value))
423-
elif second_level_key == "totp_switch":
427+
elif second_level_key == "totp_switch" and self.totp_switch.isChecked() != bool(
428+
value
429+
):
424430
self.totp_switch.setChecked(bool(value))
425-
elif second_level_key == "usb_switch":
431+
elif second_level_key == "usb_switch" and self.usb_switch.isChecked() != bool(
432+
value
433+
):
426434
self.usb_switch.setChecked(bool(value))
427435
enabled = password_is_configured()
428436
self.totp_switch.setEnabled(enabled)
@@ -721,21 +729,45 @@ def __on_ops_setting_changed(self, first_level_key, second_level_key, value):
721729
if first_level_key != "basic_safety_settings":
722730
return
723731
self._ensure_ops_disabled_if_no_password()
724-
if second_level_key == "show_hide_floating_window_switch":
732+
# 只在状态不同时才更新,避免不必要的信号触发
733+
if (
734+
second_level_key == "show_hide_floating_window_switch"
735+
and self.show_hide_floating_window_switch.isChecked() != bool(value)
736+
):
725737
self.show_hide_floating_window_switch.setChecked(bool(value))
726-
elif second_level_key == "restart_switch":
738+
elif (
739+
second_level_key == "restart_switch"
740+
and self.restart_switch.isChecked() != bool(value)
741+
):
727742
self.restart_switch.setChecked(bool(value))
728-
elif second_level_key == "exit_switch":
743+
elif second_level_key == "exit_switch" and self.exit_switch.isChecked() != bool(
744+
value
745+
):
729746
self.exit_switch.setChecked(bool(value))
730-
elif second_level_key == "open_settings_switch":
747+
elif (
748+
second_level_key == "open_settings_switch"
749+
and self.open_settings_switch.isChecked() != bool(value)
750+
):
731751
self.open_settings_switch.setChecked(bool(value))
732-
elif second_level_key == "diagnostic_export_switch":
752+
elif (
753+
second_level_key == "diagnostic_export_switch"
754+
and self.diagnostic_export_switch.isChecked() != bool(value)
755+
):
733756
self.diagnostic_export_switch.setChecked(bool(value))
734-
elif second_level_key == "data_export_switch":
757+
elif (
758+
second_level_key == "data_export_switch"
759+
and self.data_export_switch.isChecked() != bool(value)
760+
):
735761
self.data_export_switch.setChecked(bool(value))
736-
elif second_level_key == "import_overwrite_switch":
762+
elif (
763+
second_level_key == "import_overwrite_switch"
764+
and self.import_overwrite_switch.isChecked() != bool(value)
765+
):
737766
self.import_overwrite_switch.setChecked(bool(value))
738-
elif second_level_key == "import_version_mismatch_switch":
767+
elif (
768+
second_level_key == "import_version_mismatch_switch"
769+
and self.import_version_mismatch_switch.isChecked() != bool(value)
770+
):
739771
self.import_version_mismatch_switch.setChecked(bool(value))
740772

741773
def _ensure_ops_disabled_if_no_password(self):
@@ -763,10 +795,12 @@ def _ensure_ops_disabled_if_no_password(self):
763795
("import_version_mismatch_switch", self.import_version_mismatch_switch),
764796
]:
765797
try:
766-
sw.blockSignals(True)
767-
sw.setChecked(False)
768-
sw.blockSignals(False)
769-
update_settings("basic_safety_settings", key, False)
798+
# 检查开关当前状态,如果已经是False,就不需要再次更新
799+
if sw.isChecked():
800+
sw.blockSignals(True)
801+
sw.setChecked(False)
802+
sw.blockSignals(False)
803+
update_settings("basic_safety_settings", key, False)
770804
except Exception:
771805
pass
772806

main.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,13 +367,6 @@ def show_float_window():
367367
# ==================================================
368368
def initialize_app():
369369
"""初始化应用程序"""
370-
program_dir = str(get_app_root())
371-
372-
# 更改当前工作目录
373-
if os.getcwd() != program_dir:
374-
os.chdir(program_dir)
375-
logger.debug(f"工作目录已设置为: {program_dir}")
376-
377370
# 管理设置文件,确保其存在且完整
378371
manage_settings_file()
379372

@@ -445,7 +438,14 @@ def main_async():
445438

446439

447440
if __name__ == "__main__":
448-
# 初始化日志记录器
441+
program_dir = str(get_app_root())
442+
443+
# 更改当前工作目录
444+
if os.getcwd() != program_dir:
445+
os.chdir(program_dir)
446+
logger.debug(f"工作目录已设置为: {program_dir}")
447+
448+
# 配置日志系统
449449
logger.remove()
450450
# 首先配置日志系统
451451
configure_logging()

0 commit comments

Comments
 (0)