MqttMonitor/MqttMonitorWidget.py

changeset 86
620022b14cb4
parent 85
1ee3f393af03
child 92
2fb5c08019fd
--- a/MqttMonitor/MqttMonitorWidget.py	Sun Apr 25 17:41:32 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sun Apr 25 17:57:07 2021 +0200
@@ -10,6 +10,7 @@
 import os
 import collections
 import copy
+import contextlib
 
 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QFileInfo
 from PyQt5.QtGui import QFont, QTextCursor, QBrush
@@ -47,7 +48,7 @@
         @param parent reference to the parent widget
         @type QWidget
         """
-        super(MqttMonitorWidget, self).__init__(parent)
+        super().__init__(parent)
         self.setupUi(self)
         
         self.__plugin = plugin
@@ -248,10 +249,11 @@
         # ensure, the client loop is stopped
         self.__client.stopLoop()
         
-        if rc > 0:
-            msg = mqttErrorMessage(rc)
-        else:
-            msg = self.tr("Connection to Broker shut down cleanly.")
+        msg = (
+            mqttErrorMessage(rc)
+            if rc > 0 else
+            self.tr("Connection to Broker shut down cleanly.")
+        )
         self.__flashBrokerStatusLabel(msg)
         
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
@@ -279,13 +281,10 @@
         @param message log message
         @type str
         """
-        try:
+        with contextlib.suppress(KeyError):
             if MqttClient.LogLevelMap[level] < self.logLevelComboBox.itemData(
                     self.logLevelComboBox.currentIndex()):
                 return
-        except KeyError:
-            # always show unknown log levels
-            pass
         
         scrollbarValue = self.logEdit.verticalScrollBar().value()
         
@@ -372,13 +371,10 @@
         """
         if mid in self.__topicQueue:
             topic = self.__topicQueue.pop(mid)
-            try:
+            with contextlib.suppress(ValueError):
                 self.__subscribedTopics.remove(topic)
                 self.__updateUnsubscribeTopicComboBox()
                 self.__updatePublishTopicComboBox()
-            except ValueError:
-                # ignore it
-                pass
     
     #######################################################################
     ## Slots handling UI interactions
@@ -725,7 +721,7 @@
             fn = Utilities.toNativeSeparators(fn)
             try:
                 with open(fn, "w") as f:
-                 f.write(self.logEdit.toPlainText())
+                    f.write(self.logEdit.toPlainText())
             except EnvironmentError as err:
                 E5MessageBox.critical(
                     self,
@@ -788,20 +784,14 @@
         self.brokerPortComboBox.clear()
         
         # step 2a: populate the broker name list
-        if brokerPortList:
-            currentBroker = brokerPortList[0][0]
-        else:
-            currentBroker = ""
+        currentBroker = brokerPortList[0][0] if brokerPortList else ""
         brokerSet = {b[0].strip() for b in brokerPortList}
         self.brokerComboBox.addItems(sorted(brokerSet))
         index = self.brokerComboBox.findText(currentBroker)
         self.brokerComboBox.setCurrentIndex(index)
         
         # step 2b: populate the broker ports list
-        if brokerPortList:
-            currentPort = brokerPortList[0][1]
-        else:
-            currentPort = 1883
+        currentPort = brokerPortList[0][1] if brokerPortList else 1883
         currentPortStr = "{0:5}".format(currentPort)
         portsSet = {b[1] for b in brokerPortList}
         portsSet.update({1883, 8883})
@@ -917,12 +907,9 @@
         if topic.startswith(MqttMonitorWidget.BrokerStatusTopicLoadPrefix):
             self.__handleBrokerLoadStatusMessage(topic, payloadStr)
         else:
-            try:
+            with contextlib.suppress(KeyError):
                 label = self.__statusLabelMapping[topic]
                 label.setText(payloadStr)
-            except KeyError:
-                # ignore topics not shown in display
-                pass
     
     def __handleBrokerLoadStatusMessage(self, topic, payloadStr):
         """
@@ -937,27 +924,25 @@
         subtopic, topicElement = topic.rsplit("/", 1)
         self.__statusLoadValues[subtopic][topicElement] = payloadStr
         
-        try:
+        with contextlib.suppress(KeyError):
             label = self.__statusLabelMapping[subtopic]
             label.setText("{0} / {1} / {2}".format(
                 self.__statusLoadValues[subtopic]["1min"],
                 self.__statusLoadValues[subtopic]["5min"],
                 self.__statusLoadValues[subtopic]["15min"],
             ))
-        except KeyError:
-            # ignore topics not shown in display
-            pass
     
     def __clearBrokerStatusLabels(self):
         """
         Private method to clear the broker status labels.
         """
         for statusLabelKey in self.__statusLabelMapping:
-            if statusLabelKey.startswith(
-                    MqttMonitorWidget.BrokerStatusTopicLoadPrefix):
-                label = "- / - / -"
-            else:
-                label = "-"
+            label = (
+                "- / - / -"
+                if statusLabelKey.startswith(
+                    MqttMonitorWidget.BrokerStatusTopicLoadPrefix) else
+                "-"
+            )
             self.__statusLabelMapping[statusLabelKey].setText(label)
     
     def __loadDefaultDictFactory(self):

eric ide

mercurial