Update pyinstaller_option_widget

改变函数 `load_pyinst_options()` 的返回值类型,使数据更精练,为添加控件界面 ToolTip 做准备;
增强文件读取的异常处理能力;
This commit is contained in:
muzing 2023-12-26 23:06:27 +08:00 committed by 木子
parent e5651cc0c2
commit 2e44079b5b

View File

@ -11,31 +11,32 @@ from ..Constants import RUNTIME_INFO
from ..Utilities import QtFileOpen from ..Utilities import QtFileOpen
def get_options() -> list[dict]: def load_pyinst_options() -> dict[str, str]:
""" """
从数据文件中读取并解析 PyInstaller 命令选项信息按运行时平台筛选后返回 \n 从数据文件中读取并解析 PyInstaller 命令选项信息按运行时平台筛选后返回 \n
:return: 选项信息字典列表 :return: 选项信息字典{option: description}
""" """
with QtFileOpen(":/Texts/PyInstaller_Options", encoding="utf-8") as option_file: try:
option_file_text = option_file.read() with QtFileOpen(":/Texts/PyInstaller_Options", encoding="utf-8") as option_file:
option_file_text = option_file.read()
if option_file_text == "": except OSError as e:
warnings.warn("PyInstaller_Options 加载失败,检查资源文件", Warning, stacklevel=1) warnings.warn(
return [] f"PyInstaller Options 加载失败,错误信息:{e}", RuntimeWarning, stacklevel=1
)
return dict()
data = yaml.load(option_file_text, Loader=yaml.Loader) data = yaml.load(option_file_text, Loader=yaml.Loader)
option_dict = dict()
def filter_option(option) -> bool: for option in data["options"]:
# 根据当前运行的平台,筛选有效的 PyInstaller 选项 if (
return (
option["platform"] == ["all"] option["platform"] == ["all"]
or RUNTIME_INFO.platform.value in option["platform"] 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 option_dict
return current_options
class PyinstallerOptionTable(QTableWidget): class PyinstallerOptionTable(QTableWidget):
@ -47,23 +48,22 @@ class PyinstallerOptionTable(QTableWidget):
super().__init__() super().__init__()
self.setWindowTitle("PyInstaller 命令选项") self.setWindowTitle("PyInstaller 命令选项")
self.setMinimumSize(650, 430) self.setMinimumSize(700, 430)
self.setWindowIcon(QPixmap(":/Icons/PyInstaller")) self.setWindowIcon(QPixmap(":/Icons/PyInstaller"))
self.option_list = get_options() self.option_dict = load_pyinst_options()
self.setRowCount(len(self.option_dict))
self.setRowCount(len(self.option_list))
self.setColumnCount(2) self.setColumnCount(2)
self.set_items() self._set_items()
self.setHorizontalHeaderLabels(["选项", "描述"]) self.setHorizontalHeaderLabels(["选项", "描述"])
# 将第二列的宽度设置为自动调整 # 将第二列的宽度设置为自动调整
self.horizontalHeader().setSectionResizeMode(1, QHeaderView.ResizeMode.Stretch) 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): for index, (option, description) in enumerate(self.option_dict.items()):
self.setItem(index, 0, QTableWidgetItem(option["option"])) self.setItem(index, 0, QTableWidgetItem(option))
self.setItem(index, 1, QTableWidgetItem(option["description"])) self.setItem(index, 1, QTableWidgetItem(description))