Skip to content

Commit 12b8254

Browse files
committed
Optimize PyinstallerArgs class with enum
使用枚举类型存储打包选项;
1 parent 4c84fe0 commit 12b8254

3 files changed

Lines changed: 18 additions & 24 deletions

File tree

src/py2exe_gui/Constants/packaging_constants.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,13 @@
55
打包相关的常量
66
"""
77

8-
# TODO 优化打包选项列表的数据结构
8+
import enum
99

10-
pyinstaller_args_list: list = [
11-
"script_path",
12-
"icon_path",
13-
"FD",
14-
"console",
15-
"out_name",
16-
]
1710

18-
19-
class PyinstallerArgs:
20-
script_path = "script_path"
21-
icon_path = "icon_path"
22-
FD = "FD"
23-
console = "console"
24-
out_name = "out_name"
11+
@enum.unique
12+
class PyinstallerArgs(enum.IntFlag):
13+
script_path = enum.auto()
14+
icon_path = enum.auto()
15+
FD = enum.auto()
16+
console = enum.auto()
17+
out_name = enum.auto()

src/py2exe_gui/Core/packaging.py

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

77
from PySide6 import QtCore
88

9-
from ..Constants.packaging_constants import PyinstallerArgs, pyinstaller_args_list
9+
from ..Constants.packaging_constants import PyinstallerArgs
1010
from .subprocess_tool import SubProcessTool
1111

1212

@@ -26,20 +26,20 @@ def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
2626

2727
super(Packaging, self).__init__(parent)
2828

29-
self.args_dict: dict = dict.fromkeys(pyinstaller_args_list, "")
29+
self.args_dict: dict = dict.fromkeys(PyinstallerArgs, "")
3030
self._args: List[str] = []
3131
self._subprocess_working_dir: str = ""
3232
self.subprocess: SubProcessTool = SubProcessTool(self, program="pyinstaller")
3333

3434
@QtCore.Slot(tuple)
35-
def set_pyinstaller_args(self, arg: tuple[str, str]) -> None:
35+
def set_pyinstaller_args(self, arg: tuple[PyinstallerArgs, str]) -> None:
3636
"""
3737
解析传递来的PyInstaller运行参数,并添加至命令参数字典 \n
3838
:param arg: 运行参数
3939
"""
4040

4141
arg_key, arg_value = arg
42-
if arg_key in pyinstaller_args_list:
42+
if type(arg_key) == PyinstallerArgs:
4343
self.args_dict[arg_key] = arg_value
4444
self._add_pyinstaller_args()
4545
self._set_subprocess_working_dir()

src/py2exe_gui/Core/packaging_task.py

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

77
from PySide6 import QtCore
88

9+
from ..Constants import PyinstallerArgs
910
from .validators import FilePathValidator
1011

1112

@@ -32,7 +33,7 @@ def __init__(self, parent: Optional[QtCore.QObject] = None) -> None:
3233
self.FD: Optional[bool] = None
3334
self.console: Optional[str] = None
3435

35-
def handle_option(self, option: tuple[str, str]):
36+
def handle_option(self, option: tuple[PyinstallerArgs, str]):
3637
"""
3738
处理用户在界面选择的打包选项,进行有效性验证并保存 \n
3839
:param option: 选项
@@ -41,27 +42,27 @@ def handle_option(self, option: tuple[str, str]):
4142
arg_key, arg_value = option
4243

4344
# 进行有效性验证,有效则保存并发射option_set信号,无效则发射option_error信号
44-
if arg_key == "script_path":
45+
if arg_key == PyinstallerArgs.script_path:
4546
script_path = Path(arg_value)
4647
if FilePathValidator.validate_script(script_path):
4748
self.script_path = script_path
4849
self.ready_to_pack.emit(True)
4950
self.option_set.emit(option)
5051
self.out_name = script_path.stem # 输出名默认与脚本名相同
51-
self.option_set.emit(("out_name", self.out_name))
52+
self.option_set.emit((PyinstallerArgs.out_name, self.out_name))
5253
else:
5354
self.ready_to_pack.emit(False)
5455
self.option_error.emit(arg_key)
5556

56-
elif arg_key == "icon_path":
57+
elif arg_key == PyinstallerArgs.icon_path:
5758
icon_path = Path(arg_value)
5859
if FilePathValidator.validate_icon(icon_path):
5960
self.icon_path = icon_path
6061
self.option_set.emit(option)
6162
else:
6263
self.option_error.emit(arg_key)
6364

64-
elif arg_key == "out_name":
65+
elif arg_key == PyinstallerArgs.out_name:
6566
self.out_name = arg_value
6667
self.option_set.emit(option)
6768

0 commit comments

Comments
 (0)