From 2e44079b5b1b191ef72523d173cdd14e0c5ff511 Mon Sep 17 00:00:00 2001 From: muzing Date: Tue, 26 Dec 2023 23:06:27 +0800 Subject: [PATCH] Update pyinstaller_option_widget MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 改变函数 `load_pyinst_options()` 的返回值类型,使数据更精练,为添加控件界面 ToolTip 做准备; 增强文件读取的异常处理能力; --- .../Widgets/pyinstaller_option_widget.py | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/py2exe_gui/Widgets/pyinstaller_option_widget.py b/src/py2exe_gui/Widgets/pyinstaller_option_widget.py index c37cf4e..6ea01cb 100644 --- a/src/py2exe_gui/Widgets/pyinstaller_option_widget.py +++ b/src/py2exe_gui/Widgets/pyinstaller_option_widget.py @@ -11,31 +11,32 @@ from ..Constants import RUNTIME_INFO from ..Utilities import QtFileOpen -def get_options() -> list[dict]: +def load_pyinst_options() -> dict[str, str]: """ 从数据文件中读取并解析 PyInstaller 命令选项信息,按运行时平台筛选后返回 \n - :return: 选项信息字典列表 + :return: 选项信息字典,{option: description} """ - with QtFileOpen(":/Texts/PyInstaller_Options", encoding="utf-8") as option_file: - option_file_text = option_file.read() - - if option_file_text == "": - warnings.warn("PyInstaller_Options 加载失败,检查资源文件", Warning, stacklevel=1) - return [] + try: + with QtFileOpen(":/Texts/PyInstaller_Options", encoding="utf-8") as option_file: + option_file_text = option_file.read() + except OSError as e: + warnings.warn( + f"PyInstaller Options 加载失败,错误信息:{e}", RuntimeWarning, stacklevel=1 + ) + return dict() data = yaml.load(option_file_text, Loader=yaml.Loader) + option_dict = dict() - def filter_option(option) -> bool: - # 根据当前运行的平台,筛选有效的 PyInstaller 选项 - return ( + for option in data["options"]: + if ( option["platform"] == ["all"] or RUNTIME_INFO.platform.value in option["platform"] - ) + ): + option_dict.update({option["option"]: option["description"]}) - current_options = [option for option in data["options"] if filter_option(option)] - - return current_options + return option_dict class PyinstallerOptionTable(QTableWidget): @@ -47,23 +48,22 @@ class PyinstallerOptionTable(QTableWidget): super().__init__() self.setWindowTitle("PyInstaller 命令选项") - self.setMinimumSize(650, 430) + self.setMinimumSize(700, 430) self.setWindowIcon(QPixmap(":/Icons/PyInstaller")) - self.option_list = get_options() - - self.setRowCount(len(self.option_list)) + self.option_dict = load_pyinst_options() + self.setRowCount(len(self.option_dict)) self.setColumnCount(2) - self.set_items() + self._set_items() self.setHorizontalHeaderLabels(["选项", "描述"]) # 将第二列的宽度设置为自动调整 self.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) - def set_items(self) -> None: + def _set_items(self) -> None: """ - 为表格控件中填充条目 + 为表格控件中填充条目 \n """ - for index, option in enumerate(self.option_list): - self.setItem(index, 0, QTableWidgetItem(option["option"])) - self.setItem(index, 1, QTableWidgetItem(option["description"])) + for index, (option, description) in enumerate(self.option_dict.items()): + self.setItem(index, 0, QTableWidgetItem(option)) + self.setItem(index, 1, QTableWidgetItem(description))