diff -r 000000000000 -r 1c1ac27f3cf1 PyLint/PyLintExecDialog.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/PyLint/PyLintExecDialog.py Fri Jul 29 19:03:10 2011 +0200 @@ -0,0 +1,357 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2005 - 2011 Detlev Offenbach <detlev@die-offenbachs.de> +# + +""" +Module implementing a dialog to show the results of the PyLint run. +""" + +import os + +from PyQt4.QtCore import * +from PyQt4.QtGui import * + +from E5Gui import E5MessageBox, E5FileDialog +from E5Gui.E5Application import e5App + +from .Ui_PyLintExecDialog import Ui_PyLintExecDialog + +import Preferences +import Utilities + + +class PyLintExecDialog(QWidget, Ui_PyLintExecDialog): + """ + Class implementing a dialog to show the results of the PyLint run. + + This class starts a QProcess and displays a dialog that + shows the results of the PyLint command process. + """ + def __init__(self, parent=None): + """ + Constructor + + @param parent parent widget of this dialog (QWidget) + """ + QWidget.__init__(self, parent) + self.setupUi(self) + + self.saveButton = self.buttonBox.addButton( + self.trUtf8("Save Report..."), QDialogButtonBox.ActionRole) + self.saveButton.setToolTip( + self.trUtf8("Press to save the report to a file")) + self.saveButton.setEnabled(False) + + self.refreshButton = self.buttonBox.addButton( + self.trUtf8("Refresh"), QDialogButtonBox.ActionRole) + self.refreshButton.setToolTip(self.trUtf8("Press to refresh the result display")) + self.refreshButton.setEnabled(False) + + self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) + + self.messageList.header().setSortIndicator(0, Qt.AscendingOrder) + + self.process = None + self.noResults = True + self.htmlOutput = False + self.parsedOutput = False + self.__scrollPosition = -1 # illegal value + + self.typeDict = { + 'C': self.trUtf8('Convention'), + 'R': self.trUtf8('Refactor'), + 'W': self.trUtf8('Warning'), + 'E': self.trUtf8('Error'), + 'F': self.trUtf8('Fatal'), + } + + def start(self, args, fn, reportFile, ppath): + """ + Public slot to start PyLint. + + @param args commandline arguments for documentation programPyLint + (list of strings) + @param fn filename or dirname to be processed by PyLint (string) + @param reportFile filename of file to write the report to (string) + @param ppath project path (string) + @return flag indicating the successful start of the process (boolean) + """ + self.errorGroup.hide() + + self.args = args[:] + self.fn = fn + self.reportFile = reportFile + self.ppath = ppath + + self.pathname = os.path.dirname(fn) + self.filename = os.path.basename(fn) + + self.contents.clear() + self.errors.clear() + self.messageList.clear() + + self.buttonBox.button(QDialogButtonBox.Close).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(True) + self.buttonBox.button(QDialogButtonBox.Cancel).setDefault(True) + self.saveButton.setEnabled(False) + self.refreshButton.setEnabled(False) + + program = args[0] + del args[0] + args.append(self.filename) + + self.process = QProcess() + self.process.setWorkingDirectory(self.pathname) + + self.process.readyReadStandardError.connect(self.__readStderr) + self.process.finished.connect(self.__finish) + + self.__ioEncoding = Preferences.getSystem("IOEncoding") + if "--output-format=parseable" in args: + self.reportFile = None + self.contents.hide() + self.process.readyReadStandardOutput.connect(self.__readParseStdout) + self.parsedOutput = True + else: + self.process.readyReadStandardOutput.connect(self.__readStdout) + self.messageList.hide() + if "--output-format=html" in args: + self.contents.setAcceptRichText(True) + self.contents.setHtml('<b>Processing your request...</b>') + self.htmlOutput = True + else: + self.contents.setAcceptRichText(False) + self.contents.setCurrentFont( + Preferences.getEditorOtherFonts("MonospacedFont")) + self.htmlOutput = False + self.parsedOutput = False + self.noResults = True + + self.buf = "" + + self.process.start(program, args) + procStarted = self.process.waitForStarted() + if not procStarted: + E5MessageBox.critical(None, + self.trUtf8('Process Generation Error'), + self.trUtf8( + 'The process {0} could not be started. ' + 'Ensure, that it is in the search path.' + ).format(program)) + else: + self.setCursor(QCursor(Qt.WaitCursor)) + return procStarted + + def on_buttonBox_clicked(self, button): + """ + Private slot called by a button of the button box clicked. + + @param button button that was clicked (QAbstractButton) + """ + if button == self.buttonBox.button(QDialogButtonBox.Close): + self.close() + elif button == self.buttonBox.button(QDialogButtonBox.Cancel): + self.__finish() + elif button == self.saveButton: + self.on_saveButton_clicked() + elif button == self.refreshButton: + self.on_refreshButton_clicked() + + def __finish(self): + """ + Private slot called when the process finished. + + It is called when the process finished or + the user pressed the button. + """ + self.unsetCursor() + + if self.htmlOutput: + self.contents.setHtml(self.buf) + else: + cursor = self.contents.textCursor() + cursor.movePosition(QTextCursor.Start) + self.contents.setTextCursor(cursor) + + if self.process is not None and \ + self.process.state() != QProcess.NotRunning: + self.process.terminate() + QTimer.singleShot(2000, self.process.kill) + self.process.waitForFinished(3000) + + self.buttonBox.button(QDialogButtonBox.Close).setEnabled(True) + self.buttonBox.button(QDialogButtonBox.Cancel).setEnabled(False) + self.buttonBox.button(QDialogButtonBox.Close).setDefault(True) + self.refreshButton.setEnabled(True) + if self.parsedOutput: + QApplication.processEvents() + self.messageList.sortItems(self.messageList.sortColumn(), + self.messageList.header().sortIndicatorOrder()) + self.messageList.header().resizeSections(QHeaderView.ResizeToContents) + self.messageList.header().setStretchLastSection(True) + else: + if self.__scrollPosition != -1: + self.contents.verticalScrollBar().setValue(self.__scrollPosition) + + self.process = None + + if self.reportFile: + self.__writeReport() + elif not self.parsedOutput: + self.saveButton.setEnabled(True) + + if self.noResults: + self.__createItem(self.trUtf8('No PyLint errors found.'), "", "", "") + + @pyqtSlot() + def on_refreshButton_clicked(self): + """ + Private slot to refresh the status display. + """ + self.__scrollPosition = self.contents.verticalScrollBar().value() + self.start(self.args, self.fn, self.reportFile, self.ppath) + + def __readStdout(self): + """ + Private slot to handle the readyReadStandardOutput signal. + + It reads the output of the process, formats it and inserts it into + the contents pane. + """ + self.process.setReadChannel(QProcess.StandardOutput) + + while self.process.canReadLine(): + s = self.process.readLine() + self.buf += s + os.linesep + if not self.htmlOutput: + self.contents.insertPlainText(s) + self.contents.ensureCursorVisible() + + def __createItem(self, file, line, type_, message): + """ + Private method to create an entry in the message list. + + @param file filename of file (string) + @param line linenumber of message (integer or string) + @param type_ type of message (string) + @param message message text (string) + """ + itm = QTreeWidgetItem(self.messageList, [ + file, str(line), type_, message]) + itm.setTextAlignment(1, Qt.AlignRight) + itm.setTextAlignment(2, Qt.AlignHCenter) + + def __readParseStdout(self): + """ + Private slot to handle the readyReadStandardOutput signal for parseable output. + + It reads the output of the process, formats it and inserts it into + the message list pane. + """ + self.process.setReadChannel(QProcess.StandardOutput) + + while self.process.canReadLine(): + s = str(self.process.readLine(), self.__ioEncoding, 'replace') + if s: + try: + if Utilities.isWindowsPlatform(): + drive, s = os.path.splitdrive(s) + fname, lineno, fullmessage = s.split(':') + fname = drive + fname + else: + fname, lineno, fullmessage = s.split(':') + type_, message = fullmessage.strip().split(']', 1) + type_ = type_.strip()[1:].split(',', 1)[0] + message = message.strip() + if type_ and type_[0] in self.typeDict: + if len(type_) == 1: + self.__createItem(fname, lineno, + self.typeDict[type_], message) + else: + self.__createItem(fname, lineno, + "{0} {1}".format( + self.typeDict[type_[0]], type_[1:]), + message) + self.noResults = False + except ValueError: + continue + + def __readStderr(self): + """ + Private slot to handle the readyReadStandardError signal. + + It reads the error output of the process and inserts it into the + error pane. + """ + self.process.setReadChannel(QProcess.StandardError) + + while self.process.canReadLine(): + self.errorGroup.show() + s = str(self.process.readLine(), self.__ioEncoding, 'replace') + self.errors.insertPlainText(s) + self.errors.ensureCursorVisible() + + def on_messageList_itemActivated(self, itm, column): + """ + Private slot to handle the itemActivated signal of the message list. + + @param itm The message item that was activated (QTreeWidgetItem) + @param column column the item was activated in (integer) + """ + if self.noResults: + return + + fn = os.path.join(self.pathname, itm.text(0)) + lineno = int(itm.text(1)) + + e5App().getObject("ViewManager").openSourceFile(fn, lineno) + + def __writeReport(self): + """ + Private slot to write the report to a report file. + """ + self.reportFile = self.reportFile + if os.path.exists(self.reportFile): + res = E5MessageBox.warning(self, + self.trUtf8("PyLint Report"), + self.trUtf8( + """<p>The PyLint report file <b>{0}</b> already exists.</p>""")\ + .format(self.reportFile), + QMessageBox.StandardButtons( + QMessageBox.Cancel | \ + QMessageBox.Ignore), + QMessageBox.Cancel) + if res == QMessageBox.Cancel: + return + + try: + f = open(self.reportFile, 'w') + f.write(self.buf) + f.close() + except IOError as why: + E5MessageBox.critical(self, self.trUtf8('PyLint Report'), + self.trUtf8('<p>The PyLint report file <b>{0}</b> could not be written.' + '<br>Reason: {1}</p>') + .format(self.reportFile, str(why))) + + @pyqtSlot() + def on_saveButton_clicked(self): + """ + Private slot to save the report to a file. + """ + if self.htmlOutput: + filter = self.trUtf8("HTML Files (*.html);;All Files (*)") + else: + filter = self.trUtf8("Text Files (*.txt);;All Files (*)") + + self.reportFile = E5FileDialog.getSaveFileName( + self, + self.trUtf8("PyLint Report"), + self.ppath, + filter, + None, + QFileDialog.Options(QFileDialog.DontConfirmOverwrite)) + if self.reportFile: + self.__writeReport()