Plugins/PluginCodeStyleChecker.py

branch
Py2 comp.
changeset 3456
96232974dcdb
parent 3178
f25fc1364c88
parent 3442
927186c0d409
child 3484
645c12de6b0c
--- a/Plugins/PluginCodeStyleChecker.py	Sun Jan 05 23:22:17 2014 +0100
+++ b/Plugins/PluginCodeStyleChecker.py	Sun Mar 30 22:00:14 2014 +0200
@@ -11,12 +11,13 @@
 
 import os
 
-from PyQt4.QtCore import QObject
+from PyQt4.QtCore import QObject, pyqtSignal
+from PyQt4.QtGui import QApplication
 
 from E5Gui.E5Application import e5App
-
 from E5Gui.E5Action import E5Action
 
+from Utilities import determinePythonVersion
 import Preferences
 
 # Start-Of-Header
@@ -44,7 +45,12 @@
 class CodeStyleCheckerPlugin(QObject):
     """
     Class implementing the code style checker plug-in.
+    
+    @signal styleChecked(str, dict, int, list) emited when the style check was
+        done.
     """
+    styleChecked = pyqtSignal(str, dict, int, list)
+    
     def __init__(self, ui):
         """
         Constructor
@@ -55,6 +61,17 @@
         self.__ui = ui
         self.__initialize()
         
+        self.backgroundService = e5App().getObject("BackgroundService")
+        
+        path = os.path.join(
+            os.path.dirname(__file__), 'CheckerPlugins', 'CodeStyleChecker')
+        for lang in ['Python2', 'Python3']:
+            self.backgroundService.serviceConnect(
+                'style', lang, path, 'CodeStyleChecker',
+                self.__translateStyleCheck,
+                lambda fx, fn, ver, msg: self.styleChecked.emit(
+                    fn, {}, 0, [[1, 1, '---- ' + msg, False, False]]))
+
     def __initialize(self):
         """
         Private slot to (re)initialize the plugin.
@@ -70,6 +87,57 @@
         self.__editorAct = None
         self.__editorCodeStyleCheckerDialog = None
 
+    def styleCheck(self, lang, filename, source, args):
+        """
+        Method to prepare a style check on one Python source file in another
+        task.
+
+        @param lang language of the file or None to determine by internal
+            algorithm (str or None)
+        @param filename source filename (string)
+        @param source string containing the code to check (string)
+        @param args arguments used by the codeStyleCheck function (list of
+            excludeMessages (str), includeMessages (str), repeatMessages
+            (bool), fixCodes (str), noFixCodes (str), fixIssues (bool),
+            maxLineLength (int), hangClosing (bool), docType (str), errors
+            (list of str), eol (str), encoding (str))
+        """
+        if lang is None:
+            lang = 'Python{0}'.format(determinePythonVersion(filename, source))
+        if lang not in ['Python2', 'Python3']:
+            return
+        
+        data = [source, args]
+        self.backgroundService.enqueueRequest('style', lang, filename, data)
+    
+    def __translateStyleCheck(self, fn, codeStyleCheckerStats, results):
+        """
+        Privat slot called after perfoming a style check on one file.
+        
+        @param fn filename of the just checked file (str)
+        @param codeStyleCheckerStats stats of style and name check (dict)
+        @param results tuple for each found violation of style (tuple of
+            lineno (int), position (int), text (str), fixed (bool),
+            autofixing (bool), fixedMsg (str))
+        """
+        from CheckerPlugins.CodeStyleChecker.translations import \
+            getTranslatedMessage
+            
+        fixes = 0
+        for result in results:
+            msg = getTranslatedMessage(result[2])
+        
+            fixedMsg = result.pop()
+            if fixedMsg:
+                fixes += 1
+                trFixedMsg = getTranslatedMessage(fixedMsg)
+                
+                msg += "\n" + QApplication.translate(
+                    'CodeStyleCheckerDialog', "Fix: {0}").format(trFixedMsg)
+            
+            result[2] = msg
+        self.styleChecked.emit(fn, codeStyleCheckerStats, fixes, results)
+
     def activate(self):
         """
         Public method to activate this plugin.
@@ -203,7 +271,7 @@
         
         from CheckerPlugins.CodeStyleChecker.CodeStyleCheckerDialog import \
             CodeStyleCheckerDialog
-        self.__projectCodeStyleCheckerDialog = CodeStyleCheckerDialog()
+        self.__projectCodeStyleCheckerDialog = CodeStyleCheckerDialog(self)
         self.__projectCodeStyleCheckerDialog.show()
         self.__projectCodeStyleCheckerDialog.prepare(files, project)
     
@@ -224,7 +292,8 @@
         
         from CheckerPlugins.CodeStyleChecker.CodeStyleCheckerDialog import \
             CodeStyleCheckerDialog
-        self.__projectBrowserCodeStyleCheckerDialog = CodeStyleCheckerDialog()
+        self.__projectBrowserCodeStyleCheckerDialog = CodeStyleCheckerDialog(
+            self)
         self.__projectBrowserCodeStyleCheckerDialog.show()
         if isDir:
             self.__projectBrowserCodeStyleCheckerDialog.start(
@@ -268,8 +337,7 @@
         if menuName == "Checks":
             if not self.__editorAct in menu.actions():
                 menu.addAction(self.__editorAct)
-            self.__editorAct.setEnabled(
-                editor.isPy3File() or editor.isPy2File())
+            self.__editorAct.setEnabled(editor.getPyVersion())
     
     def __editorCodeStyleCheck(self):
         """
@@ -281,7 +349,8 @@
             if editor.checkDirty() and editor.getFileName() is not None:
                 from CheckerPlugins.CodeStyleChecker.CodeStyleCheckerDialog \
                     import CodeStyleCheckerDialog
-                self.__editorCodeStyleCheckerDialog = CodeStyleCheckerDialog()
+                self.__editorCodeStyleCheckerDialog = CodeStyleCheckerDialog(
+                    self)
                 self.__editorCodeStyleCheckerDialog.show()
                 self.__editorCodeStyleCheckerDialog.start(
                     editor.getFileName(),

eric ide

mercurial