Skip to content

Commit 30f6f6e

Browse files
committed
Refactor the flow of packaging_task signals
完善`packaging_task`信号流的重构; 其他代码优化;
1 parent da73ba8 commit 30f6f6e

4 files changed

Lines changed: 85 additions & 15 deletions

File tree

src/py2exe_gui/Core/packaging_task.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class PackagingTask(QtCore.QObject):
1313

1414
# 自定义信号
1515
option_set = QtCore.Signal(tuple) # 用户输入选项通过了验证,已设置为打包选项
16-
option_error = QtCore.Signal() # 用户输入选项有误,需要进一步处理
16+
option_error = QtCore.Signal(str) # 用户输入选项有误,需要进一步处理
17+
ready_to_pack = QtCore.Signal(bool) # 是否已经可以运行该打包任务
1718

1819
def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
1920
"""
@@ -24,8 +25,9 @@ def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
2425

2526
self.script_path: Optional[Path] = None
2627
self.icon_path: Optional[Path] = None
28+
self.out_name: Optional[str] = None
2729

28-
def handle_option(self, option: tuple):
30+
def handle_option(self, option: tuple[str, str]):
2931
"""
3032
处理用户在界面选择的打包选项 \n
3133
:param option: 选项
@@ -35,14 +37,22 @@ def handle_option(self, option: tuple):
3537

3638
# 进行有效性验证,有效则保存并发射option_set信号,无效则发射option_error信号
3739
if arg_key == "script_path":
38-
self.option_set.emit(option)
40+
script_path = Path(arg_value)
41+
if FilePathValidator.validate_script(script_path):
42+
self.out_name = script_path.stem # 输出名默认与脚本名相同
43+
self.ready_to_pack.emit(True)
44+
self.option_set.emit(option)
45+
else:
46+
self.ready_to_pack.emit(False)
47+
self.option_error.emit("script_path")
3948
elif arg_key == "icon_path":
4049
self.option_set.emit(option)
4150
elif arg_key == "FD":
4251
self.option_set.emit(option)
4352
elif arg_key == "console":
4453
self.option_set.emit(option)
4554
elif arg_key == "out_name":
55+
self.out_name = arg_value
4656
self.option_set.emit(option)
4757

4858
def write_to_file(self):

src/py2exe_gui/Core/interpreter_validator.py renamed to src/py2exe_gui/Core/validators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@
44
from typing import Union
55

66

7+
class FilePathValidator:
8+
"""
9+
根据给定的路径验证文件的有效性
10+
"""
11+
12+
@classmethod
13+
def validate_script(cls, script_path: Union[str, Path]) -> bool:
14+
"""
15+
验证脚本路径是否有效 \n
16+
:param script_path: 脚本路径
17+
"""
18+
19+
path = Path(script_path)
20+
# TODO 完善验证方法
21+
if path.exists() and path.is_file():
22+
return True
23+
else:
24+
return False
25+
26+
727
class InterpreterValidator:
828
"""
929
验证给定的可执行文件是否为有效的Python解释器,并获取该解释器相关信息

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ def setup_ui(self) -> None:
9898
self.pyinstaller_args_browser.setMaximumHeight(80)
9999

100100
self.run_packaging_btn.setText("打包!")
101-
# TODO 完成输入检查前、子进程运行时打包按钮设置为不可用
102-
# self.run_packaging_btn.setEnabled(False)
101+
self.run_packaging_btn.setEnabled(False)
103102

104103
def _connect_slots(self) -> None:
105104
"""
@@ -114,7 +113,6 @@ def script_file_selected(file_path: str) -> None:
114113
"""
115114

116115
# TODO 验证有效性、将脚本名作为默认app名
117-
# 将字符串类型的文件路径转成pathlib型的?
118116
self.script_path_le.setText(file_path)
119117
self.parent().statusBar().showMessage(f"打开脚本路径:{file_path}")
120118
self.option_selected.emit(("script_path", file_path))
@@ -209,3 +207,31 @@ def _set_layout(self) -> None:
209207
main_layout.addWidget(self.pyinstaller_args_browser)
210208
main_layout.addWidget(self.run_packaging_btn)
211209
self.setLayout(main_layout)
210+
211+
@QtCore.Slot(tuple)
212+
def handle_option_set(self, option: tuple[str, str]) -> None:
213+
"""
214+
处理option_set信号的槽 \n
215+
:param option: 选项键值对
216+
"""
217+
218+
pass
219+
220+
@QtCore.Slot(str)
221+
def handle_option_error(self, option: str) -> None:
222+
"""
223+
处理option_error信号的槽 \n
224+
:param option: 选项
225+
"""
226+
227+
# 清空重置该项的输入控件,等待用户重新输入
228+
pass
229+
230+
@QtCore.Slot(bool)
231+
def handle_ready_to_pack(self, ready: bool) -> None:
232+
"""
233+
处理ready_to_pack信号的槽 \n
234+
:param ready: 是否可以进行打包
235+
"""
236+
237+
self.run_packaging_btn.setEnabled(ready)

src/py2exe_gui/__main__.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,41 @@ class MainApp(MainWindow):
1717
def __init__(self, *args, **kwargs) -> None:
1818
super(MainApp, self).__init__(*args, **kwargs)
1919

20-
self.packager = Packaging(self)
2120
self.packaging_task = PackagingTask(self)
21+
self.packager = Packaging(self)
22+
self.subprocess_dlg = SubProcessDlg(self)
23+
24+
self._connect_signals()
25+
26+
self.status_bar.showMessage("就绪")
27+
28+
def _connect_signals(self) -> None:
29+
"""
30+
连接各种信号与槽 \n
31+
"""
32+
2233
self.center_widget.option_selected.connect(self.packaging_task.handle_option)
2334
self.packaging_task.option_set.connect(self.packager.set_pyinstaller_args)
35+
self.packaging_task.option_set.connect(self.center_widget.handle_option_set)
36+
self.packaging_task.option_error.connect(self.center_widget.handle_option_error)
37+
self.packaging_task.ready_to_pack.connect(
38+
self.center_widget.handle_ready_to_pack
39+
)
2440

41+
# TODO 优化pyinstaller_args_browser的接口
2542
self.packager.args_settled.connect(
2643
lambda val: self.center_widget.pyinstaller_args_browser.enrich_args_text(
2744
val
2845
)
2946
)
3047

31-
self.subprocess_dlg = SubProcessDlg(self)
32-
3348
def run_packaging():
3449
self.packager.run_packaging_process()
3550
self.subprocess_dlg.show()
3651

3752
self.center_widget.run_packaging_btn.clicked.connect(run_packaging)
3853
self.packager.subprocess.output.connect(self.subprocess_dlg.handle_output)
3954

40-
self.status_bar.showMessage("就绪")
41-
4255
def closeEvent(self, event: QtGui.QCloseEvent) -> None:
4356
"""
4457
重写关闭事件,进行收尾清理 \n
@@ -49,7 +62,8 @@ def closeEvent(self, event: QtGui.QCloseEvent) -> None:
4962
super(MainApp, self).closeEvent(event)
5063

5164

52-
app = QApplication(sys.argv)
53-
window = MainApp()
54-
window.show()
55-
sys.exit(app.exec())
65+
if __name__ == "__main__":
66+
app = QApplication(sys.argv)
67+
window = MainApp()
68+
window.show()
69+
sys.exit(app.exec())

0 commit comments

Comments
 (0)