--- a/Plugins/PluginSyntaxChecker.py Fri Jan 17 23:38:29 2014 +0100 +++ b/Plugins/PluginSyntaxChecker.py Fri Jan 31 22:11:45 2014 +0100 @@ -12,10 +12,11 @@ import os from PyQt4.QtCore import QObject - -from E5Gui.E5Application import e5App +from PyQt4.QtGui import QApplication from E5Gui.E5Action import E5Action +from E5Gui.E5Application import e5App +from eric5config import getConfig import Preferences @@ -51,6 +52,31 @@ self.__ui = ui self.__initialize() + from Plugins.CheckerPlugins.SyntaxChecker.SyntaxCheckService import \ + SyntaxCheckService + self.syntaxCheckService = SyntaxCheckService() + e5App().registerObject("SyntaxCheckService", self.syntaxCheckService) + + ericPath = getConfig('ericDir') + path = os.path.join(ericPath, 'Plugins', 'CheckerPlugins', + 'SyntaxChecker') + + self.syntaxCheckService.addLanguage('Python2', path, 'SyntaxCheck', + self.__getPythonOptions, + lambda: Preferences.getPython("PythonExtensions"), + self.__translateSyntaxCheck, + lambda fx, lng, fn, msg: \ + self.syntaxCheckService.syntaxChecked.emit( + fn, True, fn, 0, 0, '', msg, [])) + + self.syntaxCheckService.addLanguage('Python3', path, 'SyntaxCheck', + self.__getPythonOptions, + lambda: Preferences.getPython("Python3Extensions"), + self.__translateSyntaxCheck, + lambda fx, lng, fn, msg: \ + self.syntaxCheckService.syntaxChecked.emit( + fn, True, fn, 0, 0, '', msg, [])) + def __initialize(self): """ Private slot to (re)initialize the plugin. @@ -66,6 +92,49 @@ self.__editorAct = None self.__editorSyntaxCheckerDialog = None + def __getPythonOptions(self): + """ + Private methode to determine the syntax check options. + + @return state of checkFlakes and ignoreStarImportWarnings (bool, bool) + """ + checkFlakes = Preferences.getFlakes("IncludeInSyntaxCheck") + ignoreStarImportWarnings = Preferences.getFlakes( + "IgnoreStarImportWarnings") + return checkFlakes, ignoreStarImportWarnings + + def __translateSyntaxCheck( + self, fn, nok, fname, line, index, code, error, warnings): + """ + Slot to translate the resulting messages. + + If checkFlakes is True, warnings contains a list of strings containing + the warnings (marker, file name, line number, message) + The values are only valid, if nok is False. + + @param fn filename of the checked file (str) + @param nok flag if an error in the source was found (boolean) + @param fname filename of the checked file (str) # TODO: remove dubl. + @param line number where the error occured (int) + @param index the column where the error occured (int) + @param code the part of the code where the error occured (str) + @param error the name of the error (str) + @param warnings a list of strings containing the warnings + (marker, file name, line number, col, message, list(msg_args)) + """ + for warning in warnings: + # Translate messages + msg_args = warning.pop() + translated = QApplication.translate( + 'py3Flakes', warning[4]).format(*msg_args) + # Avoid leading "u" at Python2 unicode strings + if translated.startswith("u'"): + translated = translated[1:] + warning[4] = translated.replace(" u'", " '") + + self.syntaxCheckService.syntaxChecked.emit( + fn, nok, line, index, code, error, warnings) + def activate(self): """ Public method to activate this plugin. @@ -152,7 +221,7 @@ if menuName == "Checks" and self.__projectAct is not None: self.__projectAct.setEnabled( e5App().getObject("Project").getProjectLanguage() in - ["Python3", "Python2", "Python"]) + self.syntaxCheckService.getLanguages()) def __projectBrowserShowMenu(self, menuName, menu): """ @@ -164,7 +233,7 @@ """ if menuName == "Checks" and \ e5App().getObject("Project").getProjectLanguage() in \ - ["Python3", "Python2", "Python"]: + self.syntaxCheckService.getLanguages(): self.__projectBrowserMenu = menu if self.__projectBrowserAct is None: self.__projectBrowserAct = E5Action( @@ -187,11 +256,10 @@ project = e5App().getObject("Project") project.saveAllScripts() ppath = project.getProjectPath() + extensions = tuple(self.syntaxCheckService.getExtensions()) files = [os.path.join(ppath, file) for file in project.pdata["SOURCES"] - if file.endswith( - tuple(Preferences.getPython("Python3Extensions")) + - tuple(Preferences.getPython("PythonExtensions")))] + if file.endswith(extensions)] from CheckerPlugins.SyntaxChecker.SyntaxCheckerDialog import \ SyntaxCheckerDialog @@ -254,7 +322,7 @@ if not self.__editorAct in menu.actions(): menu.addAction(self.__editorAct) self.__editorAct.setEnabled( - editor.isPy3File() or editor.isPy2File()) + editor.getLanguage() in self.syntaxCheckService.getLanguages()) def __editorSyntaxCheck(self): """