Update runtime-info

完善获取运行时信息的方式:
添加 `RuntimeInfo` 具名元组数据类,便于管理运行时信息;
添加获取运行时语言代码功能;
This commit is contained in:
muzing 2023-12-09 11:14:44 +08:00
parent b80b6df629
commit 4d46410709
7 changed files with 60 additions and 42 deletions

View File

@ -31,22 +31,3 @@ def get_platform() -> PLATFORM:
return PLATFORM.macos
else:
return PLATFORM.others
# 以全局变量形式,保存当前运行时的平台信息
RUNTIME_PLATFORM = get_platform()
# 各平台的命令行续行符
line_continuation_text = {"shell": "\\", "cmd": "^", "powershell": "`"}
def get_line_continuation() -> str:
"""
获取当前运行平台对应的命令行续行符 \n
:return: line continuation character
"""
if PLATFORM.windows == RUNTIME_PLATFORM:
return line_continuation_text["powershell"]
else:
return line_continuation_text["shell"]

View File

@ -3,4 +3,5 @@
from .packaging import Packaging
from .packaging_task import PackagingTask
from .runtime_info import RUNTIME_INFO
from .validators import FilePathValidator, InterpreterValidator

View File

@ -0,0 +1,19 @@
# Licensed under the GPLv3 License: https://www.gnu.org/licenses/gpl-3.0.html
# For details: https://github.com/muziing/Py2exe-GUI/blob/main/README.md#license
"""
运行时信息
"""
from locale import LC_CTYPE, getlocale
from typing import NamedTuple, Optional
from ..Constants import PLATFORM, get_platform
class RuntimeInfo(NamedTuple):
platform: PLATFORM
language_code: Optional[str]
RUNTIME_INFO = RuntimeInfo(get_platform(), getlocale(category=LC_CTYPE)[0])

View File

@ -5,12 +5,28 @@ from typing import Optional
from PySide6.QtWidgets import QTextBrowser, QWidget
from ..Constants import get_line_continuation
from ..Constants import PLATFORM
from ..Core import RUNTIME_INFO
# 一组适合浅色背景的颜色
colors = ["#FD6D5A", "#FEB40B", "#6DC354", "#994487", "#518CD8", "#443295"]
def get_line_continuation() -> str:
"""
获取当前运行平台对应的命令行续行符 \n
:return: line continuation character
"""
# 各平台的命令行续行符
line_continuation_text = {"shell": "\\", "cmd": "^", "powershell": "`"}
if PLATFORM.windows == RUNTIME_INFO.platform:
return line_continuation_text["powershell"]
else:
return line_continuation_text["shell"]
def wrap_font_tag(raw_text: str, *, color: str, **kwargs):
"""
辅助函数用于为字符添加<font>标签包裹属性可通过可变关键字参数传入 \n

View File

@ -19,7 +19,8 @@ from PySide6.QtWidgets import (
)
from ..Constants.packaging_constants import PyinstallerArgs
from ..Constants.platform_constants import PLATFORM, RUNTIME_PLATFORM
from ..Constants.platform_constants import PLATFORM
from ..Core import RUNTIME_INFO
from .arguments_browser import ArgumentsBrowser
from .dialog_widgets import IconFileDlg, ScriptFileDlg
@ -58,7 +59,7 @@ class CenterWidget(QWidget):
self.fd_group = QButtonGroup()
# 应用图标(仅 Windows 与 macOS
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
self.icon_path_label = QLabel()
self.icon_file_dlg = IconFileDlg()
self.icon_browse_btn = QPushButton()
@ -67,7 +68,7 @@ class CenterWidget(QWidget):
# TODO 重构不同平台功能判断,减少 if RUNTIME_PLATFORM in () 语句重复次数
# 是否为stdio启用终端仅 Windows 与 macOS
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
self.console_checkbox = QCheckBox()
# 清理缓存与临时文件
@ -103,7 +104,7 @@ class CenterWidget(QWidget):
self.fd_group.addButton(self.one_dir_btn, 0)
self.fd_group.addButton(self.one_file_btn, 1)
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
self.icon_path_label.setText("应用图标:")
self.icon_path_le.setReadOnly(True)
self.icon_path_le.setPlaceholderText("图标文件路径")
@ -202,18 +203,18 @@ class CenterWidget(QWidget):
self.parent().packager.run_packaging_process()
# 连接信号与槽
self.script_browse_btn.clicked.connect(self.script_file_dlg.open) # type: ignore
self.script_file_dlg.fileSelected.connect(script_file_selected) # type: ignore
self.project_name_le.editingFinished.connect(project_name_selected) # type: ignore
self.fd_group.idClicked.connect(one_fd_selected) # type: ignore
self.run_packaging_btn.clicked.connect(run_packaging) # type: ignore
self.script_browse_btn.clicked.connect(self.script_file_dlg.open)
self.script_file_dlg.fileSelected.connect(script_file_selected)
self.project_name_le.editingFinished.connect(project_name_selected)
self.fd_group.idClicked.connect(one_fd_selected)
self.run_packaging_btn.clicked.connect(run_packaging)
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open) # type: ignore
self.icon_file_dlg.fileSelected.connect(icon_file_selected) # type: ignore
self.console_checkbox.toggled.connect(console_selected) # type: ignore
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
self.icon_browse_btn.clicked.connect(self.icon_file_dlg.open)
self.icon_file_dlg.fileSelected.connect(icon_file_selected)
self.console_checkbox.toggled.connect(console_selected)
self.clean_checkbox.toggled.connect(clean_selected) # type: ignore
self.clean_checkbox.toggled.connect(clean_selected)
def _set_layout(self) -> None:
"""
@ -243,7 +244,7 @@ class CenterWidget(QWidget):
main_layout.addLayout(fd_layout)
main_layout.addStretch(10)
if RUNTIME_PLATFORM in (PLATFORM.windows, PLATFORM.macos):
if RUNTIME_INFO.platform in (PLATFORM.windows, PLATFORM.macos):
main_layout.addWidget(self.console_checkbox)
main_layout.addStretch(10)
icon_layout = QGridLayout()

View File

@ -14,7 +14,8 @@ from PySide6.QtWidgets import (
QWidget,
)
from ..Constants import PLATFORM, RUNTIME_PLATFORM
from ..Constants import PLATFORM
from ..Core import RUNTIME_INFO
from ..Core.subprocess_tool import SubProcessTool
@ -45,7 +46,7 @@ class SubProcessDlg(QDialog):
self.setModal(True) # 设置为模态对话框
# 连接信号与槽
self.multifunction_btn.clicked.connect(self.handle_multifunction) # type: ignore
self.multifunction_btn.clicked.connect(self.handle_multifunction)
# 布局管理器
main_layout = QVBoxLayout()
@ -96,12 +97,12 @@ class SubProcessDlg(QDialog):
self.close()
elif btn_text == "打开输出位置":
dist_path = self.parent().packaging_task.script_path.parent / "dist"
if PLATFORM.windows == RUNTIME_PLATFORM:
if PLATFORM.windows == RUNTIME_INFO.platform:
from os import startfile as os_startfile # fmt: skip
os_startfile(dist_path) # noqa
elif PLATFORM.linux == RUNTIME_PLATFORM:
elif PLATFORM.linux == RUNTIME_INFO.platform:
subprocess.call(["xdg-open", dist_path])
elif PLATFORM.macos == RUNTIME_PLATFORM:
elif PLATFORM.macos == RUNTIME_INFO.platform:
subprocess.call(["open", dist_path])
elif btn_text == "关闭":
self.close()

View File

@ -6,7 +6,6 @@ import sys
from PySide6.QtGui import QCloseEvent
from PySide6.QtWidgets import QApplication
from .Constants.platform_constants import RUNTIME_PLATFORM # noqa
from .Core import Packaging, PackagingTask # noqa
from .Resources import compiled_resources # noqa
from .Widgets import MainWindow, SubProcessDlg # noqa
@ -57,7 +56,7 @@ class MainApp(MainWindow):
super().closeEvent(event)
def main():
def main() -> None:
"""
应用程序主入口函数
"""