Continued implementing a PRP 8 checker for Python2.

Wed, 12 Jan 2011 08:54:34 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 12 Jan 2011 08:54:34 +0100
changeset 843
522c8befcf49
parent 842
984b5535cd26
child 844
9508a1f87bc3

Continued implementing a PRP 8 checker for Python2.

Plugins/CheckerPlugins/Pep8/Pep8Checker.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8Dialog.py file | annotate | diff | comparison | revisions
Plugins/PluginPep8Checker.py file | annotate | diff | comparison | revisions
UtilitiesPython2/Pep8Checker.py file | annotate | diff | comparison | revisions
--- a/Plugins/CheckerPlugins/Pep8/Pep8Checker.py	Tue Jan 11 19:59:37 2011 +0100
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Checker.py	Wed Jan 12 08:54:34 2011 +0100
@@ -7,10 +7,18 @@
 Module implementing the PEP 8 checker.
 """
 
+import os
 import optparse
 
+from PyQt4.QtCore import QProcess
+
 from . import pep8
 
+import Preferences
+import Utilities
+
+from eric5config import getConfig
+
 
 class Pep8Checker(pep8.Checker):
     """
@@ -106,3 +114,79 @@
                 (self.filename, self.line_offset + line_number,
                  offset + 1, text)
             )
+
+class Pep8Py2Checker(object):
+    """
+    Class implementing the PEP 8 checker interface for Python 2.
+    """
+    def __init__(self, filename, lines, repeat=False,
+                 select="", ignore=""):
+        """
+        Constructor
+        
+        @param filename name of the file to check (string)
+        @param lines source of the file (list of strings) (ignored)
+        @keyparam repeat flag indicating to repeat message categories (boolean)
+        @keyparam select list of message IDs to check for
+            (comma separated string)
+        @keyparam ignore list of message IDs to ignore
+            (comma separated string)
+        """
+        self.messages = []
+        
+        interpreter = Preferences.getDebugger("PythonInterpreter")
+        if interpreter == "" or not Utilities.isExecutable(interpreter):
+            self.messages.append(filename, "1", "1",
+                self.trUtf8("Python2 interpreter not configured."))
+            return
+        
+        checker = os.path.join(getConfig('ericDir'), 
+                               "UtilitiesPython2", "Pep8Checker.py")
+        
+        args = [checker]
+        if repeat:
+            args.append("-r")
+        if select:
+            args.append("-s")
+            args.append(select)
+        if ignore:
+            args.append("-i")
+            args.append(ignore)
+        args.append("-f")
+        args.append(filename)
+        
+        proc = QProcess()
+        proc.setProcessChannelMode(QProcess.MergedChannels)
+        proc.start(interpreter, args)
+        finished = proc.waitForFinished(15000)
+        if finished:
+            output = \
+                str(proc.readAllStandardOutput(), 
+                        Preferences.getSystem("IOEncoding"), 
+                        'replace').splitlines()
+            if output[0] == "ERROR":
+                self.messages.append(filename, "1", "1", output[2])
+                return
+            
+            if output[0] == "NO_PEP8":
+                return
+            
+            index = 0
+            while index < len(output):
+                fname = output[index + 1]
+                lineno = int(output[index + 2])
+                position = int(output[index + 3])
+                code = output[index + 4]
+                arglen = int(output[index + 5])
+                args = []
+                argindex = 0
+                while argindex < arglen:
+                    args.append(output[index + 6 + argindex])
+                    argindex += 1
+                index += 6 + arglen
+                
+                text = pep8.getMessage(code, *args)
+                self.messages.append((fname, lineno, position, text))
+        else:
+            self.messages.append(filename, "1", "1",
+                self.trUtf8("Python2 interpreter did not finish within 15s."))
--- a/Plugins/CheckerPlugins/Pep8/Pep8Dialog.py	Tue Jan 11 19:59:37 2011 +0100
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Dialog.py	Wed Jan 12 08:54:34 2011 +0100
@@ -18,7 +18,7 @@
 
 from E5Gui.E5Application import e5App
 
-from .Pep8Checker import Pep8Checker
+from .Pep8Checker import Pep8Checker, Pep8Py2Checker
 from .Pep8CodeSelectionDialog import Pep8CodeSelectionDialog
 
 from .Ui_Pep8Dialog import Ui_Pep8Dialog
@@ -173,9 +173,9 @@
             for ext in Preferences.getPython("Python3Extensions"):
                 files.extend(
                     Utilities.direntries(fn, 1, '*{0}'.format(ext), 0))
-##            for ext in Preferences.getPython("PythonExtensions"):
-##                files.extend(
-##                    Utilities.direntries(fn, 1, '*{0}'.format(ext), 0))
+            for ext in Preferences.getPython("PythonExtensions"):
+                files.extend(
+                    Utilities.direntries(fn, 1, '*{0}'.format(ext), 0))
         else:
             files = [fn]
         
@@ -192,10 +192,9 @@
         py3files = [f for f in files \
                     if f.endswith(
                         tuple(Preferences.getPython("Python3Extensions")))]
-        py2files = []
-##        py2files = [f for f in files \
-##                    if f.endswith(
-##                        tuple(Preferences.getPython("PythonExtensions")))]
+        py2files = [f for f in files \
+                    if f.endswith(
+                        tuple(Preferences.getPython("PythonExtensions")))]
         
         if (codestring and len(py3files) == 1) or \
            (codestring and len(py2files) == 1) or \
@@ -247,8 +246,18 @@
                     self.__project.isProjectFile(file) and \
                     self.__project.getProjectLanguage() in ["Python", 
                                                             "Python2"]):
-                    # TODO: include PEP 8 check for python 2
-                    pass
+                    checker = Pep8Py2Checker(file, [], 
+                        repeat = repeatMessages, 
+                        select = includeMessages,
+                        ignore = excludeMessages)
+                    checker.messages.sort(key = lambda a: a[1])
+                    for message in checker.messages:
+                        fname, lineno, position, text = message
+                        if not source[lineno - 1].strip()\
+                           .endswith("__IGNORE_WARNING__"):
+                            self.noResults = False
+                            self.__createResultItem(
+                                fname, lineno, position, text)
                 else:
                     checker = Pep8Checker(file, source, 
                         repeat = repeatMessages, 
--- a/Plugins/PluginPep8Checker.py	Tue Jan 11 19:59:37 2011 +0100
+++ b/Plugins/PluginPep8Checker.py	Wed Jan 12 08:54:34 2011 +0100
@@ -151,8 +151,7 @@
         if menuName == "Checks" and self.__projectAct is not None:
             self.__projectAct.setEnabled(
                 e5App().getObject("Project").getProjectLanguage() in \
-                    ["Python3"])
-##                    ["Python3", "Python2", "Python"])
+                    ["Python3", "Python2", "Python"])
     
     def __projectBrowserShowMenu(self, menuName, menu):
         """
@@ -164,8 +163,7 @@
         """
         if menuName == "Checks" and \
            e5App().getObject("Project").getProjectLanguage() in \
-                ["Python3"]:
-##                ["Python3", "Python2", "Python"]:
+                ["Python3", "Python2", "Python"]:
             self.__projectBrowserMenu = menu
             if self.__projectBrowserAct is None:
                 self.__projectBrowserAct = E5Action(
@@ -192,9 +190,8 @@
         files = [os.path.join(ppath, file) \
             for file in project.pdata["SOURCES"] \
                 if file.endswith(
-                    tuple(Preferences.getPython("Python3Extensions")))]
-##                    tuple(Preferences.getPython("Python3Extensions")) +
-##                    tuple(Preferences.getPython("PythonExtensions")))]
+                    tuple(Preferences.getPython("Python3Extensions")) +
+                    tuple(Preferences.getPython("PythonExtensions")))]
         
         self.__projectPep8CheckerDialog = Pep8Dialog()
         self.__projectPep8CheckerDialog.show()
@@ -259,8 +256,7 @@
         if menuName == "Checks":
             if not self.__editorAct in menu.actions():
                 menu.addAction(self.__editorAct)
-            self.__editorAct.setEnabled(editor.isPy3File())
-##            self.__editorAct.setEnabled(editor.isPy3File() or editor.isPy2File())
+            self.__editorAct.setEnabled(editor.isPy3File() or editor.isPy2File())
     
     def __editorPep8Check(self):
         """
--- a/UtilitiesPython2/Pep8Checker.py	Tue Jan 11 19:59:37 2011 +0100
+++ b/UtilitiesPython2/Pep8Checker.py	Wed Jan 12 08:54:34 2011 +0100
@@ -15,6 +15,7 @@
 
 import pep8
 
+
 class Pep8Checker(pep8.Checker):
     """
     Class implementing the PEP 8 checker for Python2.
@@ -110,9 +111,9 @@
             )
 
 if __name__ == "__main__":
-    repeat=False
-    select=""
-    ignore=""
+    repeat = False
+    select = ""
+    ignore = ""
     filename = ""
     
     if "-f" not in sys.argv:
@@ -121,7 +122,7 @@
         print "No file name given."
     else:
         try:
-            optlist, args = getopt.getopt(sys.argv[1:],"rf:i:s:")
+            optlist, args = getopt.getopt(sys.argv[1:], "rf:i:s:")
         except getopt.GetoptError:
             print "ERROR"
             print ""
@@ -148,11 +149,11 @@
             print "I/O Error: %s" % unicode(msg)
             sys.exit(1)
         
-        checker = Pep8Checker(filename, codestring, repeat=repeat, 
+        checker = Pep8Checker(filename, codestring, repeat=repeat,
                               select=select, ignore=ignore)
         checker.check_all()
         if len(checker.messages) > 0:
-            checker.messages.sort(key = lambda a: a[1])
+            checker.messages.sort(key=lambda a: a[1])
             for message in checker.messages:
                 fname, lineno, position, code, args = message
                 print "PEP8"

eric ide

mercurial