66
77from app .Language .obtain_language import *
88from 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
1219class 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 :
0 commit comments