10 import os |
10 import os |
11 import contextlib |
11 import contextlib |
12 import pathlib |
12 import pathlib |
13 |
13 |
14 from PyQt6.QtCore import ( |
14 from PyQt6.QtCore import ( |
15 pyqtSignal, pyqtSlot, QFile, QSize, QCoreApplication, QLocale, QIODevice |
15 pyqtSignal, pyqtSlot, QSize, QCoreApplication, QLocale |
16 ) |
16 ) |
17 from PyQt6.QtGui import QKeySequence, QAction |
17 from PyQt6.QtGui import QKeySequence, QAction |
18 from PyQt6.QtWidgets import ( |
18 from PyQt6.QtWidgets import ( |
19 QWhatsThis, QLabel, QWidget, QVBoxLayout, QDialog, QFrame, QMenu |
19 QWhatsThis, QLabel, QWidget, QVBoxLayout, QDialog, QFrame, QMenu |
20 ) |
20 ) |
982 Private method to load a binary file. |
982 Private method to load a binary file. |
983 |
983 |
984 @param fileName name of the binary file to load |
984 @param fileName name of the binary file to load |
985 @type str |
985 @type str |
986 """ |
986 """ |
987 file = QFile(fileName) |
987 if not os.path.exists(fileName): |
988 if not file.exists(): |
|
989 EricMessageBox.warning( |
988 EricMessageBox.warning( |
990 self, self.tr("eric Hex Editor"), |
989 self, self.tr("eric Hex Editor"), |
991 self.tr("The file '{0}' does not exist.") |
990 self.tr("The file '{0}' does not exist.") |
992 .format(fileName)) |
991 .format(fileName)) |
993 return |
992 return |
994 |
993 |
995 if not file.open(QIODevice.OpenModeFlag.ReadOnly): |
994 try: |
|
995 with open(fileName, "rb") as f: |
|
996 data = f.read() |
|
997 except OSError as err: |
996 EricMessageBox.warning( |
998 EricMessageBox.warning( |
997 self, self.tr("eric Hex Editor"), |
999 self, self.tr("eric Hex Editor"), |
998 self.tr("Cannot read file '{0}:\n{1}.") |
1000 self.tr("<p>Cannot read file <b>{0}</b>.</p><p>Reason:{1}</p>") |
999 .format(fileName, file.errorString())) |
1001 .format(fileName, str(err))) |
1000 return |
1002 return |
1001 |
|
1002 data = file.readAll() |
|
1003 file.close() |
|
1004 |
1003 |
1005 self.__lastOpenPath = os.path.dirname(fileName) |
1004 self.__lastOpenPath = os.path.dirname(fileName) |
1006 self.__editor.setData(data) |
1005 self.__editor.setData(data) |
1007 self.__setCurrentFile(fileName) |
1006 self.__setCurrentFile(fileName) |
1008 |
1007 |
1103 @param fileName name of the file to write to |
1102 @param fileName name of the file to write to |
1104 @type str |
1103 @type str |
1105 @return flag indicating success |
1104 @return flag indicating success |
1106 @rtype bool |
1105 @rtype bool |
1107 """ |
1106 """ |
1108 file = QFile(fileName) |
1107 try: |
1109 if not file.open(QIODevice.OpenModeFlag.WriteOnly): |
1108 with open(fileName, "wb") as f: |
|
1109 f.write(self.__editor.data()) |
|
1110 except OSError as err: |
1110 EricMessageBox.warning( |
1111 EricMessageBox.warning( |
1111 self, self.tr("eric Hex Editor"), |
1112 self, self.tr("eric Hex Editor"), |
1112 self.tr("Cannot write file '{0}:\n{1}.") |
1113 self.tr("<p>Cannot write file <b>{0}</b>.</p>" |
1113 .format(fileName, file.errorString())) |
1114 "<p>Reason:{1}</p>") |
1114 |
1115 .format(fileName, str(err))) |
1115 self.__checkActions() |
1116 self.__checkActions() |
1116 |
|
1117 return False |
|
1118 |
|
1119 res = file.write(self.__editor.data()) != -1 |
|
1120 file.close() |
|
1121 |
|
1122 if not res: |
|
1123 EricMessageBox.warning( |
|
1124 self, self.tr("eric Hex Editor"), |
|
1125 self.tr("Cannot write file '{0}:\n{1}.") |
|
1126 .format(fileName, file.errorString())) |
|
1127 |
|
1128 self.__checkActions() |
|
1129 |
|
1130 return False |
1117 return False |
1131 |
1118 |
1132 self.__editor.setModified(False, setCleanState=True) |
1119 self.__editor.setModified(False, setCleanState=True) |
1133 |
1120 |
1134 self.__setCurrentFile(fileName) |
1121 self.__setCurrentFile(fileName) |