Plugins/PluginSyntaxChecker.py

branch
Py2 comp.
changeset 3456
96232974dcdb
parent 3178
f25fc1364c88
parent 3412
9364dab2d472
child 3484
645c12de6b0c
diff -r f25fc1364c88 -r 96232974dcdb Plugins/PluginSyntaxChecker.py
--- a/Plugins/PluginSyntaxChecker.py	Sun Jan 05 23:22:17 2014 +0100
+++ b/Plugins/PluginSyntaxChecker.py	Sun Mar 30 22:00:14 2014 +0200
@@ -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,33 @@
         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, {'error': (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, {'error': (fn, 0, 0, '', msg)}))
+
     def __initialize(self):
         """
         Private slot to (re)initialize the plugin.
@@ -66,6 +94,45 @@
         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, problems):
+        """
+        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 problems dictionary with the keys 'error' and 'warnings' which
+            hold a list containing details about the error/ warnings
+            (file name, line number, column, codestring (only at syntax
+            errors), the message, a list with arguments for the message)
+        """
+        warnings = problems.get('warnings', [])
+        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'", " '")
+        
+        problems['warnings'] = warnings
+        self.syntaxCheckService.syntaxChecked.emit(fn, problems)
+
     def activate(self):
         """
         Public method to activate this plugin.
@@ -152,7 +219,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 +231,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 +254,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 +320,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):
         """

eric ide

mercurial