def openButtonClicked(self): """Brings up an open dialogue""" # By some unknown reasons the following simple way of getting a file is # not working: # fileName = QFileDialog.getOpenFileName(self, 'Open file', # QDir.currentPath()) # # There is however a workaround. Here it is: dialog = QFileDialog(self) if dialog.exec_() != QDialog.Accepted: return fileNames = dialog.selectedFiles() fileName = str(fileNames[0]) if not os.path.exists(fileName): QMessageBox.critical( self, 'Error', 'The selected file (' + fileName + ') does not exist') return # Check that the file is a python one warning = isPythonFile(fileName) if warning is not None: QMessageBox.critical(self, 'Error', warning) return # set the new file name self.fName = fileName self.updateWindowTitle() # initiate the process self.proceedWithFile()
def exceptionHook(excType, excValue, tracebackObj): """Catches unhandled exceptions""" globalData = GlobalData() # Keyboard interrupt is a special case if issubclass(excType, KeyboardInterrupt): if globalData.application is not None: globalData.application.quit() return error = "%s: %s" % (excType.__name__, excValue) stackTraceString = "".join( traceback.format_exception(excType, excValue, tracebackObj)) # Save the traceback to a file explicitly together with a log window # content. excptFileName = SETTINGS_DIR + "unhandledexceptions.log" try: savedOK = True with open(excptFileName, 'a', encoding=DEFAULT_ENCODING) as diskfile: diskfile.write("------ Unhandled exception report at " + str(datetime.datetime.now()) + "\n") diskfile.write("Traceback:\n") diskfile.write(stackTraceString) diskfile.write("Log window:\n") if globalData.mainWindow is not None: # i.e. the log window is available, save its content too logWindowContent = globalData.mainWindow.getLogViewerContent() logWindowContent = logWindowContent.strip() if logWindowContent: diskfile.write(logWindowContent) diskfile.write('\n') else: diskfile.write('Nothing is there\n') else: diskfile.write('Has not been created yet\n') diskfile.write('------\n\n') except: savedOK = False # This output will be to a console if the application has not started yet # or to a log window otherwise. logging.error('Unhandled exception is caught\n%s', stackTraceString) # Display the message as a QT modal dialog box if the application # has started if globalData.application is not None: message = "<html><body>" if savedOK: message += "Stack trace and log window content saved in " + \ excptFileName + ".<br>" else: message += "Failed to save stack trace and log window " \ "content in " + excptFileName + ".<br>" lines = stackTraceString.split('\n') if len(lines) > 32: message += "First 32 lines of the stack trace " \ "(the rest is truncated):" \ "<pre>" + "\n".join(lines[:32]) + "<pre>" else: message += "Stack trace:" + \ "<pre>" + stackTraceString + "</pre>" message += "</body></html>" QMessageBox.critical(None, "Unhandled exception: " + error, message) globalData.application.exit(1)