diff -r df9520c864f2 -r 5d409223cf3f src/eric7/MicroPython/PyBoardDevices.py --- a/src/eric7/MicroPython/PyBoardDevices.py Wed Feb 08 11:54:36 2023 +0100 +++ b/src/eric7/MicroPython/PyBoardDevices.py Wed Feb 08 18:09:19 2023 +0100 @@ -9,15 +9,16 @@ import os -from PyQt6.QtCore import QStandardPaths, pyqtSlot +from PyQt6.QtCore import QStandardPaths, QUrl, pyqtSlot +from PyQt6.QtNetwork import QNetworkRequest -from eric7 import Preferences +from eric7 import Globals, Preferences from eric7.EricWidgets import EricFileDialog, EricMessageBox from eric7.EricWidgets.EricApplication import ericApp from eric7.EricWidgets.EricProcessDialog import EricProcessDialog from eric7.SystemUtilities import FileSystemUtilities -from .MicroPythonDevices import MicroPythonDevice +from .MicroPythonDevices import FirmwareGithubUrls, MicroPythonDevice from .MicroPythonWidget import HAS_QTCHART @@ -228,18 +229,21 @@ @type QMenu """ connected = self.microPython.isConnected() + linkConnected = self.microPython.isLinkConnected() - act = menu.addAction(self.tr("Activate Bootloader"), self.__activateBootloader) - act.setEnabled(connected) - act = menu.addAction( + menu.addAction( + self.tr("Activate Bootloader"), self.__activateBootloader + ).setEnabled(connected) + menu.addAction( self.tr("List DFU-capable Devices"), self.__listDfuCapableDevices - ) - act.setEnabled(not connected) - act = menu.addAction( + ).setEnabled(not linkConnected) + menu.addSeparator() + menu.addAction( + self.tr("Show MicroPython Versions"), self.__showFirmwareVersions + ).setEnabled(connected) + menu.addAction( self.tr("Flash MicroPython Firmware"), self.__flashMicroPython - ) - act.setEnabled(not connected) - menu.addSeparator() + ).setEnabled(not linkConnected) menu.addAction( self.tr("MicroPython Flash Instructions"), self.__showFlashInstructions ) @@ -404,6 +408,74 @@ self.__showDfuDisableInstructions() @pyqtSlot() + def __showFirmwareVersions(self): + """ + Private slot to show the firmware version of the connected device and the + available firmware version. + """ + if self.microPython.isConnected(): + interface = self.microPython.commandsInterface() + if interface is not None: + impInfo = interface.getImplementation() + if impInfo["name"] != "micropython": + EricMessageBox.critical( + None, + self.tr("Show MicroPython Versions"), + self.tr( + """The firmware of the connected device cannot be""" + """ determined or the board does not run MicroPython.""" + """ Aborting...""" + ), + ) + else: + ui = ericApp().getObject("UserInterface") + request = QNetworkRequest(QUrl(FirmwareGithubUrls["micropython"])) + reply = ui.networkAccessManager().head(request) + reply.finished.connect( + lambda: self.__firmwareVersionResponse(reply, impInfo) + ) + + def __firmwareVersionResponse(self, reply, implementation): + """ + Private method handling the response of the latest version request. + + @param reply reference to the reply object + @type QNetworkReply + @param implementation dictionary containing the implementation data of the + connected device + @type dict + """ + latestUrl = reply.url().toString() + tag = latestUrl.rsplit("/", 1)[-1] + while tag and not tag[0].isdecimal(): + # get rid of leading non-decimal characters + tag = tag[1:] + latestVersion = Globals.versionToTuple(tag) + + if implementation["version"] == "unknown": + currentVersionStr = self.tr("unknown") + currentVersion = (0, 0, 0) + else: + currentVersionStr = implementation["version"] + currentVersion = Globals.versionToTuple(currentVersionStr) + + msg = self.tr( + "<h4>MicroPython Version Information</h4>" + "<table>" + "<tr><td>Installed:</td><td>{0}</td></tr>" + "<tr><td>Available:</td><td>{1}</td></tr>" + "</table>" + ).format(currentVersionStr, tag) + if currentVersion < latestVersion: + msg += self.tr("<p><b>Update available!</b></p>") + + EricMessageBox.information( + None, + self.tr("MicroPython Version"), + msg, + ) + + @pyqtSlot() def __activateBootloader(self): """ Private slot to activate the bootloader and disconnect.