eric6/HexEdit/HexEditMainWindow.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8176
31965986ecd1
parent 8265
0090cfa83159
--- a/eric6/HexEdit/HexEditMainWindow.py	Fri Apr 02 11:59:41 2021 +0200
+++ b/eric6/HexEdit/HexEditMainWindow.py	Sat May 01 14:27:20 2021 +0200
@@ -8,6 +8,7 @@
 """
 
 import os
+import contextlib
 
 from PyQt5.QtCore import (
     pyqtSignal, pyqtSlot, QFile, QFileInfo, QSize, QCoreApplication, QLocale
@@ -57,7 +58,7 @@
             eric (boolean)
         @param project reference to the project object (Project)
         """
-        super(HexEditMainWindow, self).__init__(parent)
+        super().__init__(parent)
         self.setObjectName("eric6_hex_editor")
         
         self.__srHistory = {
@@ -919,12 +920,10 @@
 
             Preferences.setGeometry("HexEditorGeometry", self.saveGeometry())
             
-            try:
+            with contextlib.suppress(ValueError):
                 if self.__fromEric or len(self.__class__.windows) > 1:
                     del self.__class__.windows[
                         self.__class__.windows.index(self)]
-            except ValueError:
-                pass
             
             if not self.__fromEric:
                 Preferences.syncPreferences()
@@ -940,9 +939,12 @@
         """
         Private slot called to open a binary file in new hex editor window.
         """
-        if not self.__lastOpenPath:
-            if self.__project and self.__project.isOpen():
-                self.__lastOpenPath = self.__project.getProjectPath()
+        if (
+            not self.__lastOpenPath and
+            self.__project is not None and
+            self.__project.isOpen()
+        ):
+            self.__lastOpenPath = self.__project.getProjectPath()
         
         fileName = E5FileDialog.getOpenFileName(
             self,
@@ -1012,9 +1014,12 @@
         Private slot to open a binary file.
         """
         if self.__maybeSave():
-            if not self.__lastOpenPath:
-                if self.__project and self.__project.isOpen():
-                    self.__lastOpenPath = self.__project.getProjectPath()
+            if (
+                not self.__lastOpenPath and
+                self.__project is not None and
+                self.__project.isOpen()
+            ):
+                self.__lastOpenPath = self.__project.getProjectPath()
             
             fileName = E5FileDialog.getOpenFileName(
                 self,
@@ -1040,10 +1045,11 @@
         @return flag indicating success
         @rtype bool
         """
-        if not self.__fileName:
-            ok = self.__saveHexFileAs()
-        else:
-            ok = self.__saveHexDataFile(self.__fileName)
+        ok = (
+            self.__saveHexDataFile(self.__fileName)
+            if self.__fileName else
+            self.__saveHexFileAs()
+        )
         
         if ok:
             self.__editor.undoStack().setClean()
@@ -1057,9 +1063,12 @@
         @return flag indicating success
         @rtype bool
         """
-        if not self.__lastSavePath:
-            if self.__project and self.__project.isOpen():
-                self.__lastSavePath = self.__project.getProjectPath()
+        if (
+            not self.__lastSavePath and
+            self.__project is not None and
+            self.__project.isOpen()
+        ):
+            self.__lastSavePath = self.__project.getProjectPath()
         if not self.__lastSavePath and self.__lastOpenPath:
             self.__lastSavePath = self.__lastOpenPath
         
@@ -1136,9 +1145,12 @@
         @type bool
         """
         savePath = self.__lastSavePath
-        if not savePath:
-            if self.__project and self.__project.isOpen():
-                savePath = self.__project.getProjectPath()
+        if (
+            not savePath and
+            self.__project is not None and
+            self.__project.isOpen()
+        ):
+            savePath = self.__project.getProjectPath()
         if not savePath and self.__lastOpenPath:
             savePath = self.__lastOpenPath
         
@@ -1175,10 +1187,11 @@
                 .format(fileName, file.errorString()))
             return
         
-        if selectionOnly:
-            readableData = self.__editor.selectionToReadableString()
-        else:
-            readableData = self.__editor.toReadableString()
+        readableData = (
+            self.__editor.selectionToReadableString()
+            if selectionOnly else
+            self.__editor.toReadableString()
+        )
         res = file.write(readableData.encode("latin1")) != -1
         file.close()
         
@@ -1224,10 +1237,11 @@
         # insert filename into list of recently opened files
         self.__addToRecentList(fileName)
         
-        if not self.__fileName:
-            shownName = self.tr("Untitled")
-        else:
-            shownName = self.__strippedName(self.__fileName)
+        shownName = (
+            self.tr("Untitled")
+            if not self.__fileName else
+            self.__strippedName(self.__fileName)
+        )
         
         self.setWindowTitle(self.tr("{0}[*] - {1}")
                             .format(shownName, self.tr("Hex Editor")))
@@ -1309,10 +1323,11 @@
         """
         self.__replaceWidget.hide()
         self.__gotoWidget.hide()
-        if self.__editor.hasSelection():
-            txt = self.__editor.selectionToHexString()
-        else:
-            txt = ""
+        txt = (
+            self.__editor.selectionToHexString()
+            if self.__editor.hasSelection() else
+            ""
+        )
         self.__searchWidget.show(txt)
         
     def __replace(self):
@@ -1321,10 +1336,11 @@
         """
         self.__searchWidget.hide()
         self.__gotoWidget.hide()
-        if self.__editor.hasSelection():
-            txt = self.__editor.selectionToHexString()
-        else:
-            txt = ""
+        txt = (
+            self.__editor.selectionToHexString()
+            if self.__editor.hasSelection() else
+            ""
+        )
         self.__replaceWidget.show(txt)
     
     def __goto(self):
@@ -1365,10 +1381,12 @@
         """
         Private slot to set the preferences.
         """
-        from Preferences.ConfigurationDialog import ConfigurationDialog
+        from Preferences.ConfigurationDialog import (
+            ConfigurationDialog, ConfigurationMode
+        )
         dlg = ConfigurationDialog(
             None, 'Configuration', True, fromEric=True,
-            displayMode=ConfigurationDialog.HexEditorMode)
+            displayMode=ConfigurationMode.HEXEDITORMODE)
         dlg.preferencesChanged.connect(
             self.__preferencesChangedByLocalPreferencesDialog)
         dlg.show()
@@ -1417,12 +1435,8 @@
         
         self.__recentMenu.clear()
         
-        idx = 1
-        for rs in self.__recent:
-            if idx < 10:
-                formatStr = '&{0:d}. {1}'
-            else:
-                formatStr = '{0:d}. {1}'
+        for idx, rs in enumerate(self.__recent, start=1):
+            formatStr = '&{0:d}. {1}' if idx < 10 else '{0:d}. {1}'
             act = self.__recentMenu.addAction(
                 formatStr.format(
                     idx,
@@ -1430,7 +1444,6 @@
                         rs, HexEditMainWindow.maxMenuFilePathLen)))
             act.setData(rs)
             act.setEnabled(QFileInfo(rs).exists())
-            idx += 1
         
         self.__recentMenu.addSeparator()
         self.__recentMenu.addAction(self.tr('&Clear'), self.__clearRecent)

eric ide

mercurial