Skip to content

Commit 4d46410

Browse files
committed
Update runtime-info
完善获取运行时信息的方式: 添加 `RuntimeInfo` 具名元组数据类,便于管理运行时信息; 添加获取运行时语言代码功能;
1 parent b80b6df commit 4d46410

7 files changed

Lines changed: 60 additions & 42 deletions

File tree

src/py2exe_gui/Constants/platform_constants.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,3 @@ def get_platform() -> PLATFORM:
3131
return PLATFORM.macos
3232
else:
3333
return PLATFORM.others
34-
35-
36-
# 以全局变量形式,保存当前运行时的平台信息
37-
RUNTIME_PLATFORM = get_platform()
38-
39-
# 各平台的命令行续行符
40-
line_continuation_text = {"shell": "\\", "cmd": "^", "powershell": "`"}
41-
42-
43-
def get_line_continuation() -> str:
44-
"""
45-
获取当前运行平台对应的命令行续行符 \n
46-
:return: line continuation character
47-
"""
48-
49-
if PLATFORM.windows == RUNTIME_PLATFORM:
50-
return line_continuation_text["powershell"]
51-
else:
52-
return line_continuation_text["shell"]

src/py2exe_gui/Core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
from .packaging import Packaging
55
from .packaging_task import PackagingTask
6+
from .runtime_info import RUNTIME_INFO
67
from .validators import FilePathValidator, InterpreterValidator
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed under the GPLv3 License: https://www.gnu.org/licenses/gpl-3.0.html
2+
# For details: https://github.com/muziing/Py2exe-GUI/blob/main/README.md#license
3+
4+
"""
5+
运行时信息
6+
"""
7+
8+
from locale import LC_CTYPE, getlocale
9+
from typing import NamedTuple, Optional
10+
11+
from ..Constants import PLATFORM, get_platform
12+
13+
14+
class RuntimeInfo(NamedTuple):
15+
platform: PLATFORM
16+
language_code: Optional[str]
17+
18+
19+
RUNTIME_INFO = RuntimeInfo(get_platform(), getlocale(category=LC_CTYPE)[0])

src/py2exe_gui/Widgets/arguments_browser.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@
55

66
from PySide6.QtWidgets import QTextBrowser, QWidget
77

8-
from ..Constants import get_line_continuation
8+
from ..Constants import PLATFORM
9+
from ..Core import RUNTIME_INFO
910

1011
# 一组适合浅色背景的颜色
1112
colors = ["#FD6D5A", "#FEB40B", "#6DC354", "#994487", "#518CD8", "#443295"]
1213

1314

15+
def get_line_continuation() -> str:
16+
"""
17+
获取当前运行平台对应的命令行续行符 \n
18+
:return: line continuation character
19+
"""
20+
21+
# 各平台的命令行续行符
22+
line_continuation_text = {"shell": "\\", "cmd": "^", "powershell": "`"}
23+
24+
if PLATFORM.windows == RUNTIME_INFO.platform:
25+
return line_continuation_text["powershell"]
26+
else:
27+
return line_continuation_text["shell"]
28+
29+
1430
def wrap_font_tag(raw_text: str, *, color: str, **kwargs):
1531
"""
1632
辅助函数,用于为字符添加<font>标签包裹,属性可通过可变关键字参数传入 \n

src/py2exe_gui/Widgets/center_widget.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
)
2020

2121
from ..Constants.packaging_constants import PyinstallerArgs
22-
from ..Constants.platform_constants import PLATFORM, RUNTIME_PLATFORM
22+
from ..Constants.platform_constants import PLATFORM
23+
from ..Core import RUNTIME_INFO
2324
from .arguments_browser import ArgumentsBrowser
2425
from .dialog_widgets import IconFileDlg, ScriptFileDlg
2526

@@ -58,7 +59,7 @@ def __init__(self, parent: QMainWindow) -> None:
5859
self.fd_group = QButtonGroup()
5960

6061
# 应用图标(仅 Windows 与 macOS)
61-
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
62+
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
6263
self.icon_path_label = QLabel()
6364
self.icon_file_dlg = IconFileDlg()
6465
self.icon_browse_btn = QPushButton()
@@ -67,7 +68,7 @@ def __init__(self, parent: QMainWindow) -> None:
6768
# TODO 重构不同平台功能判断,减少 if RUNTIME_PLATFORM in () 语句重复次数
6869

6970
# 是否为stdio启用终端(仅 Windows 与 macOS)
70-
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
71+
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
7172
self.console_checkbox = QCheckBox()
7273

7374
# 清理缓存与临时文件
@@ -103,7 +104,7 @@ def _setup_ui(self) -> None:
103104
self.fd_group.addButton(self.one_dir_btn, 0)
104105
self.fd_group.addButton(self.one_file_btn, 1)
105106

106-
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
107+
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
107108
self.icon_path_label.setText("应用图标:")
108109
self.icon_path_le.setReadOnly(True)
109110
self.icon_path_le.setPlaceholderText("图标文件路径")
@@ -202,18 +203,18 @@ def run_packaging() -> None:
202203
self.parent().packager.run_packaging_process()
203204

204205
# 连接信号与槽
205-
self.script_browse_btn.clicked.connect(self.script_file_dlg.open) # type: ignore
206-
self.script_file_dlg.fileSelected.connect(script_file_selected) # type: ignore
207-
self.project_name_le.editingFinished.connect(project_name_selected) # type: ignore
208-
self.fd_group.idClicked.connect(one_fd_selected) # type: ignore
209-
self.run_packaging_btn.clicked.connect(run_packaging) # type: ignore
206+
self.script_browse_btn.clicked.connect(self.script_file_dlg.open)
207+
self.script_file_dlg.fileSelected.connect(script_file_selected)
208+
self.project_name_le.editingFinished.connect(project_name_selected)
209+
self.fd_group.idClicked.connect(one_fd_selected)
210+
self.run_packaging_btn.clicked.connect(run_packaging)
210211

211-
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
212-
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
213-
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
214-
self.console_checkbox.toggled.connect(console_selected) # type: ignore
212+
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
213+
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open)
214+
self.icon_file_dlg.fileSelected.connect(icon_file_selected)
215+
self.console_checkbox.toggled.connect(console_selected)
215216

216-
self.clean_checkbox.toggled.connect(clean_selected) # type: ignore
217+
self.clean_checkbox.toggled.connect(clean_selected)
217218

218219
def _set_layout(self) -> None:
219220
"""
@@ -243,7 +244,7 @@ def _set_layout(self) -> None:
243244
main_layout.addLayout(fd_layout)
244245
main_layout.addStretch(10)
245246

246-
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
247+
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
247248
main_layout.addWidget(self.console_checkbox)
248249
main_layout.addStretch(10)
249250
icon_layout = QGridLayout()

src/py2exe_gui/Widgets/subprocess_widget.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
QWidget,
1515
)
1616

17-
from ..Constants import PLATFORM, RUNTIME_PLATFORM
17+
from ..Constants import PLATFORM
18+
from ..Core import RUNTIME_INFO
1819
from ..Core.subprocess_tool import SubProcessTool
1920

2021

@@ -45,7 +46,7 @@ def _setup(self) -> None:
4546
self.setModal(True) # 设置为模态对话框
4647

4748
# 连接信号与槽
48-
self.multifunction_btn.clicked.connect(self.handle_multifunction) # type: ignore
49+
self.multifunction_btn.clicked.connect(self.handle_multifunction)
4950

5051
# 布局管理器
5152
main_layout = QVBoxLayout()
@@ -96,12 +97,12 @@ def handle_multifunction(self) -> None:
9697
self.close()
9798
elif btn_text == "打开输出位置":
9899
dist_path = self.parent().packaging_task.script_path.parent / "dist"
99-
if PLATFORM.windows == RUNTIME_PLATFORM:
100+
if PLATFORM.windows == RUNTIME_INFO.platform:
100101
from os import startfile as os_startfile # fmt: skip
101102
os_startfile(dist_path) # noqa
102-
elif PLATFORM.linux == RUNTIME_PLATFORM:
103+
elif PLATFORM.linux == RUNTIME_INFO.platform:
103104
subprocess.call(["xdg-open", dist_path])
104-
elif PLATFORM.macos == RUNTIME_PLATFORM:
105+
elif PLATFORM.macos == RUNTIME_INFO.platform:
105106
subprocess.call(["open", dist_path])
106107
elif btn_text == "关闭":
107108
self.close()

src/py2exe_gui/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from PySide6.QtGui import QCloseEvent
77
from PySide6.QtWidgets import QApplication
88

9-
from .Constants.platform_constants import RUNTIME_PLATFORM # noqa
109
from .Core import Packaging, PackagingTask # noqa
1110
from .Resources import compiled_resources # noqa
1211
from .Widgets import MainWindow, SubProcessDlg # noqa
@@ -57,7 +56,7 @@ def closeEvent(self, event: QCloseEvent) -> None:
5756
super().closeEvent(event)
5857

5958

60-
def main():
59+
def main() -> None:
6160
"""
6261
应用程序主入口函数
6362
"""

0 commit comments

Comments
 (0)