Skip to content

Commit 21e6239

Browse files
committed
Use QtFileOpen
将读取已编译文本资源文件的功能交由 `QtFileOpen` 与 `PyQTextFileIo` 处理; 完善 `PyQTextFileIo` 的类型注解,使其通过 mypy 检查;
1 parent 7ae1210 commit 21e6239

3 files changed

Lines changed: 27 additions & 21 deletions

File tree

src/py2exe_gui/Utilities/open_qfile.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import warnings
88
from typing import Optional, Union
99

10-
from PySide6.QtCore import QFile, QIODevice
10+
from PySide6.QtCore import QByteArray, QFile, QIODevice
1111

1212

1313
class QtFileOpen:
@@ -91,29 +91,36 @@ def __init__(
9191
self._file.open(self.mode)
9292

9393
@classmethod
94-
def _parse_mode(cls, py_mode: str) -> QIODevice:
94+
def _parse_mode(cls, py_mode: str) -> QIODevice.OpenModeFlag:
9595
"""
9696
解析文件打开模式,将 Python open() 风格转换至 QIODevice.OpenModeFlag
9797
https://docs.python.org/zh-cn/3/library/functions.html#open
9898
https://doc.qt.io/qt-6/qiodevicebase.html#OpenModeFlag-enum
9999
:return: mode
100100
"""
101101

102-
qt_mode: QIODevice = QIODevice.Text
102+
qt_mode: QIODevice.OpenModeFlag = QIODevice.OpenModeFlag.Text
103103

104104
# 暂不支持写入
105105
if "r" in py_mode and "+" not in py_mode:
106-
qt_mode = qt_mode | QIODevice.ReadOnly
106+
qt_mode = qt_mode | QIODevice.OpenModeFlag.ReadOnly
107107
elif "w" in py_mode:
108-
qt_mode = qt_mode | QIODevice.WriteOnly
108+
qt_mode = qt_mode | QIODevice.OpenModeFlag.WriteOnly
109109
elif "+" in py_mode:
110-
qt_mode = qt_mode | QIODevice.ReadWrite
110+
qt_mode = qt_mode | QIODevice.OpenModeFlag.ReadWrite
111111

112112
if "x" in py_mode:
113-
qt_mode = qt_mode | QIODevice.NewOnly
113+
qt_mode = qt_mode | QIODevice.OpenModeFlag.NewOnly
114114

115115
return qt_mode
116116

117+
@classmethod
118+
def qba_to_str(cls, qba: QByteArray, encoding: str = locale.getencoding()) -> str:
119+
"""
120+
将 QByteArray 转换为 str
121+
"""
122+
return qba.data().decode(encoding=encoding)
123+
117124
def readable(self) -> bool:
118125
"""
119126
当前文件是否可读 \n
@@ -135,12 +142,13 @@ def read(self, size: int = -1) -> str:
135142

136143
if size < 0 or size is None:
137144
# 读取文件,并将 QByteArray 转为 str
138-
text = str(self._file.readAll(), encoding=self.encoding)
145+
text = self.qba_to_str(self._file.readAll(), encoding=self.encoding)
139146
else:
140147
# 已知问题:性能太差
141148
# PySide6.QtCore.QIODevice.read(maxlen) 以字节而非字符方式计算长度,行为不一致
142149
# 而 QTextStream 对字符编码支持太差,许多编码并不支持
143-
text = str(self._file.readAll(), encoding=self.encoding)[0:size] # 性能太差
150+
text_all = self.qba_to_str(self._file.readAll(), self.encoding)
151+
text = text_all[0:size] # 性能太差
144152

145153
return text
146154

@@ -166,7 +174,7 @@ def readline(self, size: int = -1, /) -> str:
166174
if size == 0:
167175
return ""
168176
else:
169-
line = str(self._file.readLine(), encoding=self.encoding)
177+
line = self.qba_to_str(self._file.readLine(), self.encoding)
170178
if size < 0:
171179
return line
172180
else:
@@ -185,7 +193,7 @@ def readlines(self, hint: int = -1, /) -> list[str]:
185193
raise OSError(f"File '{self._file.fileName()}' is not Readable.")
186194

187195
if hint <= 0 or hint is None:
188-
temp = str(self._file.readAll(), encoding=self.encoding)
196+
temp = self.qba_to_str(self._file.readAll(), self.encoding)
189197
all_lines = temp.splitlines(keepends=True)
190198
else:
191199
all_lines = []

src/py2exe_gui/Widgets/dialog_widgets.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33

44
from typing import Optional
55

6-
from PySide6.QtCore import QFile, QIODevice, Qt
6+
from PySide6.QtCore import Qt
77
from PySide6.QtGui import QPixmap
88
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget
99

10+
from ..Utilities import QtFileOpen
11+
1012

1113
class ScriptFileDlg(QFileDialog):
1214
"""
@@ -79,10 +81,8 @@ def about_text(self) -> str:
7981
"""
8082

8183
# 因使用qrc/rcc系统,所以使用Qt风格读取文本文件
82-
about_file = QFile(":/Texts/About_Text")
83-
about_file.open(QIODevice.ReadOnly | QIODevice.Text) # type: ignore
84-
about_text = str(about_file.readAll(), encoding="utf-8") # type: ignore
85-
about_file.close()
84+
with QtFileOpen(":/Texts/About_Text", encoding="utf-8") as about_file:
85+
about_text = about_file.read()
8686

8787
if about_text:
8888
self._about_text = about_text

src/py2exe_gui/Widgets/pyinstaller_option_widget.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import warnings
55

66
import yaml
7-
from PySide6.QtCore import QFile, QIODevice
87
from PySide6.QtGui import QPixmap
98
from PySide6.QtWidgets import QHeaderView, QTableWidget, QTableWidgetItem
109

1110
from ..Constants import RUNTIME_INFO
11+
from ..Utilities import QtFileOpen
1212

1313

1414
def get_options() -> list[dict]:
@@ -17,10 +17,8 @@ def get_options() -> list[dict]:
1717
:return: 选项信息字典列表
1818
"""
1919

20-
option_file = QFile(":/Texts/PyInstaller_Options")
21-
option_file.open(QIODevice.ReadOnly | QIODevice.Text) # type: ignore
22-
option_file_text = str(option_file.readAll(), encoding="utf-8") # type: ignore
23-
option_file.close()
20+
with QtFileOpen(":/Texts/PyInstaller_Options", encoding="utf-8") as option_file:
21+
option_file_text = option_file.read()
2422

2523
if option_file_text == "":
2624
warnings.warn("PyInstaller_Options 加载失败,检查资源文件", Warning, stacklevel=1)

0 commit comments

Comments
 (0)