Skip to content

Commit 10fdcf0

Browse files
committed
一些代码格式化
1 parent 4864184 commit 10fdcf0

4 files changed

Lines changed: 73 additions & 41 deletions

File tree

app/tools/secure_store.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import ctypes
77
import uuid
88
from loguru import logger
9-
from app.tools.path_utils import get_settings_path, get_config_path, ensure_dir
9+
from app.tools.path_utils import get_settings_path, ensure_dir
10+
1011
try:
1112
from Cryptodome.Cipher import AES
1213
except Exception:
@@ -15,40 +16,44 @@
1516
except Exception:
1617
AES = None
1718

19+
1820
def _set_hidden(path: str) -> None:
1921
try:
2022
if platform.system() == "Windows":
2123
# Windows 平台:设置文件属性为隐藏和系统文件
2224
FILE_ATTRIBUTE_HIDDEN = 0x2
2325
FILE_ATTRIBUTE_SYSTEM = 0x4
24-
ctypes.windll.kernel32.SetFileAttributesW(path, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM)
26+
ctypes.windll.kernel32.SetFileAttributesW(
27+
path, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM
28+
)
2529
else: # Linux 平台:使用点前缀隐藏文件
2630
import os
31+
2732
dirname = os.path.dirname(path)
2833
basename = os.path.basename(path)
29-
3034
# 如果文件名已经以点开头,则不需要处理
3135
if basename.startswith("."):
3236
return
33-
3437
# 将文件重命名为以点开头的文件名
3538
hidden_path = os.path.join(dirname, f".{basename}")
36-
3739
# 如果隐藏文件已存在,先删除它
3840
if os.path.exists(hidden_path):
3941
os.remove(hidden_path)
40-
4142
# 重命名原文件为隐藏文件
4243
os.rename(path, hidden_path)
4344
except Exception as e:
4445
logger.warning(f"隐藏文件失败: {e}")
4546
pass
4647

48+
4749
def _get_machine_guid() -> str:
4850
if platform.system() == "Windows":
4951
try:
5052
import winreg
51-
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Cryptography")
53+
54+
key = winreg.OpenKey(
55+
winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\Cryptography"
56+
)
5257
val, _ = winreg.QueryValueEx(key, "MachineGuid")
5358
winreg.CloseKey(key)
5459
return str(val)
@@ -59,10 +64,12 @@ def _get_machine_guid() -> str:
5964
except Exception:
6065
return "SecRandom"
6166

67+
6268
def _platform_key() -> bytes:
6369
base = _get_machine_guid() + str(get_settings_path(""))
6470
return hashlib.sha256(base.encode("utf-8")).digest()[:16]
6571

72+
6673
def _encrypt_payload(data: bytes, key: bytes) -> bytes:
6774
if AES:
6875
nonce = os.urandom(16)
@@ -73,6 +80,7 @@ def _encrypt_payload(data: bytes, key: bytes) -> bytes:
7380
obf = bytes(b ^ stream[i % len(stream)] for i, b in enumerate(data))
7481
return obf
7582

83+
7684
def _decrypt_payload(payload: bytes, key: bytes) -> bytes:
7785
if AES:
7886
nonce = payload[:16]
@@ -83,6 +91,7 @@ def _decrypt_payload(payload: bytes, key: bytes) -> bytes:
8391
data = bytes(b ^ stream[i % len(stream)] for i, b in enumerate(payload))
8492
return data
8593

94+
8695
def read_secrets() -> dict:
8796
p = get_settings_path("secrets.json")
8897
if os.path.exists(p):
@@ -112,6 +121,7 @@ def read_secrets() -> dict:
112121

113122
return {}
114123

124+
115125
def write_secrets(d: dict) -> None:
116126
p = get_settings_path("secrets.json")
117127
ensure_dir(os.path.dirname(p))

app/view/another_window/bind_usb.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
from app.Language.obtain_language import *
88
from app.tools.personalised import *
9-
from app.common.safety.usb import list_removable_drives, list_usb_drive_letters_wmi, get_volume_serial, get_volume_label, bind_with_options, remove_key_file
9+
from app.common.safety.usb import (
10+
list_removable_drives,
11+
list_usb_drive_letters_wmi,
12+
get_volume_serial,
13+
get_volume_label,
14+
bind_with_options,
15+
remove_key_file,
16+
)
1017

1118

1219
class BindUsbWindow(QWidget):
@@ -22,20 +29,30 @@ def init_ui(self):
2229
self.main_layout.setContentsMargins(20, 20, 20, 20)
2330
self.main_layout.setSpacing(15)
2431

25-
self.title_label = TitleLabel(get_content_name_async("basic_safety_settings", "bind_usb"))
32+
self.title_label = TitleLabel(
33+
get_content_name_async("basic_safety_settings", "bind_usb")
34+
)
2635
self.main_layout.addWidget(self.title_label)
2736

28-
self.description_label = BodyLabel(get_content_description_async("basic_safety_settings", "bind_usb"))
37+
self.description_label = BodyLabel(
38+
get_content_description_async("basic_safety_settings", "bind_usb")
39+
)
2940
self.description_label.setWordWrap(True)
3041
self.main_layout.addWidget(self.description_label)
3142

3243
card = CardWidget()
3344
layout = QVBoxLayout(card)
3445

3546
self.drive_combo = ComboBox()
36-
self.refresh_button = PushButton(get_content_name_async("basic_safety_settings","usb_refresh"))
37-
self.require_key_checkbox = CheckBox(get_content_name_async("basic_safety_settings","usb_require_key_file"))
38-
self.bind_button = PrimaryPushButton(get_content_name_async("basic_safety_settings","usb_bind"))
47+
self.refresh_button = PushButton(
48+
get_content_name_async("basic_safety_settings", "usb_refresh")
49+
)
50+
self.require_key_checkbox = CheckBox(
51+
get_content_name_async("basic_safety_settings", "usb_require_key_file")
52+
)
53+
self.bind_button = PrimaryPushButton(
54+
get_content_name_async("basic_safety_settings", "usb_bind")
55+
)
3956

4057
layout.addWidget(self.drive_combo)
4158
layout.addWidget(self.refresh_button)
@@ -46,7 +63,9 @@ def init_ui(self):
4663

4764
btns = QHBoxLayout()
4865
btns.addStretch(1)
49-
self.close_button = PushButton(get_content_name_async("basic_safety_settings", "cancel_button"))
66+
self.close_button = PushButton(
67+
get_content_name_async("basic_safety_settings", "cancel_button")
68+
)
5069
btns.addWidget(self.close_button)
5170
self.main_layout.addLayout(btns)
5271
self.main_layout.addStretch(1)
@@ -61,7 +80,7 @@ def __connect_signals(self):
6180
def _notify_error(self, text: str, duration: int = 3000):
6281
try:
6382
InfoBar.error(
64-
title=get_content_name_async("basic_safety_settings","title"),
83+
title=get_content_name_async("basic_safety_settings", "title"),
6584
content=text,
6685
position=InfoBarPosition.TOP,
6786
duration=duration,
@@ -73,7 +92,7 @@ def _notify_error(self, text: str, duration: int = 3000):
7392
def _notify_success(self, text: str, duration: int = 3000):
7493
try:
7594
InfoBar.success(
76-
title=get_content_name_async("basic_safety_settings","title"),
95+
title=get_content_name_async("basic_safety_settings", "title"),
7796
content=text,
7897
position=InfoBarPosition.TOP,
7998
duration=duration,
@@ -86,13 +105,13 @@ def __refresh(self):
86105
self.drive_combo.clear()
87106
try:
88107
import platform
108+
89109
# 优先使用WMI获取USB设备的逻辑盘符(可覆盖固定盘类型的外置硬盘)
90110
letters = list_usb_drive_letters_wmi()
91111
if not letters:
92112
# 回退到驱动类型为可移动盘的枚举
93113
letters = list_removable_drives()
94114
logger.debug(f"刷新可绑定设备,数量:{len(letters)}")
95-
96115
for device in letters:
97116
if platform.system() == "Windows":
98117
# Windows 平台:device 是盘符(如 "E")
@@ -105,15 +124,16 @@ def __refresh(self):
105124
# 在Linux上,name 可能就是挂载点的目录名,所以直接使用 device 作为显示文本
106125
text = device
107126
self.drive_combo.addItem(text, device)
108-
109127
if not letters:
110128
self.drive_combo.setCurrentIndex(-1)
111129
# 使占位文本可见
112130
try:
113131
self.drive_combo.setEditable(True)
114132
self.drive_combo.lineEdit().setReadOnly(True)
115133
self.drive_combo.lineEdit().setPlaceholderText(
116-
get_content_name_async("basic_safety_settings", "usb_no_removable")
134+
get_content_name_async(
135+
"basic_safety_settings", "usb_no_removable"
136+
)
117137
)
118138
self.bind_button.setEnabled(False)
119139
except Exception:
@@ -130,47 +150,58 @@ def __refresh(self):
130150
def __bind(self):
131151
idx = self.drive_combo.currentIndex()
132152
if idx < 0:
133-
self._notify_error(get_content_name_async("basic_safety_settings","usb_no_removable"))
153+
self._notify_error(
154+
get_content_name_async("basic_safety_settings", "usb_no_removable")
155+
)
134156
return
135157
text = self.drive_combo.currentText()
136158
device = self.drive_combo.currentData()
137-
138159
import platform
160+
139161
try:
140162
# 允许来自USB设备的逻辑盘(包括部分显示为固定盘的外置硬盘)
141163
serial = get_volume_serial(device)
142164
if not serial or serial == "00000000":
143-
raise RuntimeError(get_content_name_async("basic_safety_settings","usb_no_removable"))
144-
165+
raise RuntimeError(
166+
get_content_name_async("basic_safety_settings", "usb_no_removable")
167+
)
145168
require_key = bool(self.require_key_checkbox.isChecked())
146169
key_value = None
147170
if require_key:
148171
try:
149172
from app.common.safety.usb import write_key_file
173+
150174
try:
151175
remove_key_file(device)
152176
except Exception:
153177
pass
154178
key_value = os.urandom(16).hex()
155179
ok = write_key_file(device, key_value)
156180
if not ok:
157-
raise RuntimeError(get_content_name_async("basic_safety_settings","usb_no_removable"))
181+
raise RuntimeError(
182+
get_content_name_async(
183+
"basic_safety_settings", "usb_no_removable"
184+
)
185+
)
158186
except Exception as e:
159-
raise RuntimeError(str(e))
160-
187+
raise RuntimeError(str(e)) from e
161188
display_name = get_volume_label(device)
162-
bind_with_options(serial, require_key_file=require_key, key_value=key_value, name=display_name)
163-
189+
bind_with_options(
190+
serial,
191+
require_key_file=require_key,
192+
key_value=key_value,
193+
name=display_name,
194+
)
164195
if platform.system() == "Windows":
165196
logger.debug(f"绑定设备成功:{device}:")
166197
else:
167198
logger.debug(f"绑定设备成功:{device}")
168-
169-
self._notify_success(f"{get_content_name_async('basic_safety_settings','usb_bind_success')}: {text}")
199+
self._notify_success(
200+
f"{get_content_name_async('basic_safety_settings', 'usb_bind_success')}: {text}"
201+
)
170202
except Exception as e:
171203
self._notify_error(str(e))
172204

173-
174205
def __cancel(self):
175206
parent = self.parent()
176207
while parent:

app/view/another_window/prize_weight_setting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def __load_existing_weights(self):
116116
weights = []
117117
for item_name, item_info in data.items():
118118
if "weight" in item_info and item_info["weight"]:
119-
weights.append(item_info["weight"])
119+
weights.append(item_info["weight"])
120120

121121
self.initial_weights = weights.copy()
122122

pyproject.toml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ dependencies = [
1111
"pyside6>6.6.3.1",
1212
"pysidesix-frameless-window>=0.7.4",
1313
"darkdetect==0.8.0",
14-
1514
# === 核心/日志/工具 ===
1615
# 注意:`asyncio` 为标准库,通常不应列在依赖中;已注释以提示移除
1716
# "asyncio~=4.0.0",
1817
"loguru==0.7.3",
1918
"colorama==0.4.6",
2019
"packaging==25.0",
21-
2220
# === 数据处理 / 图像 / 文件 ===
2321
"numpy>=2.0.0",
2422
"pandas>2.0.3",
@@ -29,35 +27,28 @@ dependencies = [
2927
"pyqrcode~=1.2.1",
3028
"pypng~=0.20220715.0",
3129
"colorthief==0.2.1",
32-
3330
# === 网络 / TTS 服务 ===
3431
"requests==2.32.5",
3532
"edge-tts==7.2.3",
36-
3733
# === 音频 / TTS 本地 ===
3834
"pyttsx3==2.99",
3935
"sounddevice==0.5.3",
4036
"soundfile==0.13.1",
41-
4237
# === 系统 / 工具 ===
4338
"psutil~=7.1.3",
4439
"keyboard==0.13.5",
45-
4640
# === 安全 / 加密 ===
4741
"pyotp==2.9.0",
4842
"pycryptodome==3.23.0",
49-
5043
# === 平台(Windows) ===
5144
"pywin32==311; platform_system=='Windows'",
5245
"win32_setctime==1.2.0; platform_system == 'Windows'",
5346
"winshell==0.6; platform_system == 'Windows'",
5447
"comtypes==1.4.13",
5548
"wmi==1.5.1; platform_system == 'Windows'",
5649
"pycaw==20251023",
57-
5850
# === 平台(Linux) ===
5951
"pulsectl==24.8.0; platform_system == 'Linux'",
60-
6152
# === 构建 / 打包 / 模板 / 开发时工具 ===
6253
"sip~=6.14.0",
6354
"jinja2~=3.1.6",

0 commit comments

Comments
 (0)