Implemented some fixers for wrong naming conventions.

Mon, 09 Sep 2013 18:43:23 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 09 Sep 2013 18:43:23 +0200
changeset 2895
4a44d92757f9
parent 2894
8e4264045fc9
child 2896
9fa71ee50b3d

Implemented some fixers for wrong naming conventions.

Plugins/CheckerPlugins/Pep8/Pep8CodeSelectionDialog.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8Dialog.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8Fixer.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8NamingChecker.py file | annotate | diff | comparison | revisions
Plugins/CheckerPlugins/Pep8/Pep8StatisticsDialog.py file | annotate | diff | comparison | revisions
--- a/Plugins/CheckerPlugins/Pep8/Pep8CodeSelectionDialog.py	Sun Sep 08 13:16:07 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8CodeSelectionDialog.py	Mon Sep 09 18:43:23 2013 +0200
@@ -11,6 +11,7 @@
 from PyQt4.QtGui import QDialog, QTreeWidgetItem
 
 from . import pep8
+from .Pep8NamingChecker import Pep8NamingChecker
 
 from .Ui_Pep8CodeSelectionDialog import Ui_Pep8CodeSelectionDialog
 
@@ -37,17 +38,22 @@
             from .Pep8Fixer import Pep8FixableIssues
             selectableCodes = Pep8FixableIssues
         else:
-            selectableCodes = pep8.pep8_messages.keys()
-            # TODO: include message from naming checker
-        for code in sorted(selectableCodes, key=lambda a: a[1:]):
-            # TODO: sort by complete code
+            selectableCodes = list(pep8.pep8_messages.keys())
+            selectableCodes.extend(Pep8NamingChecker.Messages.keys())
+        for code in sorted(selectableCodes):
             if code in pep8.pep8_messages_sample_args:
-                message = QCoreApplication.translate("pep8",
-                    pep8.pep8_messages[code]).format(
-                        *pep8.pep8_messages_sample_args[code])
+                message = QCoreApplication.translate(
+                    "pep8", pep8.pep8_messages[code]).format(
+                    *pep8.pep8_messages_sample_args[code])
+            elif code in pep8.pep8_messages:
+                message = QCoreApplication.translate(
+                    "pep8", pep8.pep8_messages[code])
+            elif code in Pep8NamingChecker.Messages:
+                message = QCoreApplication.translate(
+                    "Pep8NamingChecker",
+                    Pep8NamingChecker.Messages[code])
             else:
-                message = QCoreApplication.translate("pep8",
-                    pep8.pep8_messages[code])
+                continue
             itm = QTreeWidgetItem(self.codeTable, [code, message])
             if code in codeList:
                 itm.setSelected(True)
--- a/Plugins/CheckerPlugins/Pep8/Pep8Dialog.py	Sun Sep 08 13:16:07 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Dialog.py	Mon Sep 09 18:43:23 2013 +0200
@@ -14,8 +14,6 @@
 from PyQt4.QtGui import QDialog, QTreeWidgetItem, QAbstractButton, \
     QDialogButtonBox, QApplication, QHeaderView, QIcon
 
-from . import pep8
-
 from E5Gui.E5Application import e5App
 
 from .Ui_Pep8Dialog import Ui_Pep8Dialog
@@ -24,8 +22,12 @@
 import Preferences
 import Utilities
 
+from . import pep8
 from .Pep8NamingChecker import Pep8NamingChecker
 
+# register the name checker
+pep8.register_check(Pep8NamingChecker, Pep8NamingChecker.Codes)
+
 
 class Pep8Report(pep8.BaseReport):
     """
@@ -111,9 +113,6 @@
         self.__statistics = {}
         
         self.on_loadDefaultButton_clicked()
-        
-        # register the name checker
-        pep8.register_check(Pep8NamingChecker, Pep8NamingChecker.Codes)
     
     def __resort(self):
         """
--- a/Plugins/CheckerPlugins/Pep8/Pep8Fixer.py	Sun Sep 08 13:16:07 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Fixer.py	Mon Sep 09 18:43:23 2013 +0200
@@ -21,18 +21,18 @@
 import Utilities
 
 Pep8FixableIssues = ["E101", "E111", "E121", "E122", "E123", "E124",
-                     "E125", "E126", "E127", "E128", "E133", "W191",
-                     "E201", "E202", "E203", "E211", "E221", "E222",
-                     "E223", "E224", "E225", "E226", "E227", "E228",
-                     "E231", "E241", "E242", "E251", "E261", "E262",
-                     "E271", "E272", "E273", "E274", "W291", "W292",
-                     "W293", "E301", "E302", "E303", "E304", "W391",
-                     "E401", "E501", "E502", "W603", "E701", "E702",
-                     "E703", "E711", "E712"
+                     "E125", "E126", "E127", "E128", "E133", "E201",
+                     "E202", "E203", "E211", "E221", "E222", "E223",
+                     "E224", "E225", "E226", "E227", "E228", "E231",
+                     "E241", "E242", "E251", "E261", "E262", "E271",
+                     "E272", "E273", "E274", "E301", "E302", "E303",
+                     "E304", "E401", "E501", "E502", "E701", "E702",
+                     "E703", "E711", "E712",
+                     "N804", "N805", "N806",
+                     "W191", "W291", "W292", "W293", "W391", "W603",
                      ]
 
 
-# TODO: add fixes for N804, N805
 class Pep8Fixer(QObject):
     """
     Class implementing a fixer for certain PEP 8 issues.
@@ -87,7 +87,6 @@
             "E127": self.__fixE127,
             "E128": self.__fixE127,
             "E133": self.__fixE126,
-            "W191": self.__fixE101,
             "E201": self.__fixE201,
             "E202": self.__fixE201,
             "E203": self.__fixE201,
@@ -110,23 +109,27 @@
             "E272": self.__fixE221,
             "E273": self.__fixE221,
             "E274": self.__fixE221,
-            "W291": self.__fixW291,
-            "W292": self.__fixW292,
-            "W293": self.__fixW291,
             "E301": self.__fixE301,
             "E302": self.__fixE302,
             "E303": self.__fixE303,
             "E304": self.__fixE304,
-            "W391": self.__fixW391,
             "E401": self.__fixE401,
             "E501": self.__fixE501,
             "E502": self.__fixE502,
-            "W603": self.__fixW603,
             "E701": self.__fixE701,
             "E702": self.__fixE702,
             "E703": self.__fixE702,
             "E711": self.__fixE711,
             "E712": self.__fixE711,
+            "N804": self.__fixN804,
+            "N805": self.__fixN804,
+            "N806": self.__fixN806,
+            "W191": self.__fixE101,
+            "W291": self.__fixW291,
+            "W292": self.__fixW292,
+            "W293": self.__fixW291,
+            "W391": self.__fixW391,
+            "W603": self.__fixW603,
         }
         self.__modified = False
         self.__stackLogical = []    # these need to be fixed before the file
@@ -486,7 +489,7 @@
     def __fixE121(self, code, line, pos, apply=False):
         """
         Private method to fix the indentation of continuation lines and
-        closing brackets (E121,E124).
+        closing brackets (E121, E124).
         
         @param code code of the issue (string)
         @param line line number of the issue (integer)
@@ -1165,6 +1168,106 @@
         self.__source[line] = " ".join([left, center, right])
         return (1, self.trUtf8("Comparison to None/True/False corrected."), 0)
     
+    def __fixN804(self, code, line, pos, apply=False):
+        """
+        Private method to fix a wrong first argument of normal and
+        class methods (N804, N805).
+        
+        @param code code of the issue (string)
+        @param line line number of the issue (integer)
+        @param pos position inside line (integer)
+        @keyparam apply flag indicating, that the fix should be applied
+            (boolean)
+        @return value indicating an applied/deferred fix (-1, 0, 1),
+            a message for the fix (string) and an ID for a deferred
+            fix (integer)
+        """
+        if apply:
+            line = line - 1
+            text = self.__source[line]
+            if code == "N804":
+                arg = "cls"
+            else:
+                arg = "self"
+            
+            if text.rstrip().endswith("("):
+                newText = text + self.__getIndent(text) + \
+                    self.__indentWord + arg + "," + self.__getEol()
+            else:
+                index = text.find("(") + 1
+                left = text[:index]
+                right = text[index:]
+                if right.startswith(")"):
+                    center = arg
+                else:
+                    center = arg + ", "
+                newText = left + center + right
+            self.__source[line] = newText
+            return (1, self.trUtf8("'{0}' argument added.").format(arg), 0)
+        else:
+            id = self.__getID()
+            self.__stack.append((id, code, line, pos))
+            return (-1, "", id)
+    
+    def __fixN806(self, code, line, pos, apply=False):
+        """
+        Private method to fix a wrong first argument of static methods
+        (N806).
+        
+        @param code code of the issue (string)
+        @param line line number of the issue (integer)
+        @param pos position inside line (integer)
+        @keyparam apply flag indicating, that the fix should be applied
+            (boolean)
+        @return value indicating an applied/deferred fix (-1, 0, 1),
+            a message for the fix (string) and an ID for a deferred
+            fix (integer)
+        """
+        if apply:
+            line = line - 1
+            text = self.__source[line]
+            index = text.find("(") + 1
+            left = text[:index]
+            right = text[index:]
+            
+            if right.startswith(("cls", "self")):
+                # cls or self are on the definition line
+                if right.startswith("cls"):
+                    right = right[3:]
+                    arg = "cls"
+                else:
+                    right = right[4:]
+                    arg = "self"
+                right = right.lstrip(", ")
+                newText = left + right
+                self.__source[line] = newText
+            else:
+                # they are on the next line
+                line = line + 1
+                text = self.__source[line]
+                indent = self.__getIndent(text)
+                right = text.lstrip()
+                if right.startswith("cls"):
+                    right = right[3:]
+                    arg = "cls"
+                else:
+                    right = right[4:]
+                    arg = "self"
+                right = right.lstrip(", ")
+                if right.startswith("):"):
+                    # merge with previous line
+                    self.__source[line - 1] = \
+                        self.__source[line - 1].rstrip() + right
+                    self.__source[line] = ""
+                else:
+                    self.__source[line] = indent + right
+            
+            return (1, self.trUtf8("'{0}' argument removed.").format(arg), 0)
+        else:
+            id = self.__getID()
+            self.__stack.append((id, code, line, pos))
+            return (-1, "", id)
+    
     def __fixW291(self, code, line, pos):
         """
         Private method to fix trailing whitespace (W291, W293).
--- a/Plugins/CheckerPlugins/Pep8/Pep8NamingChecker.py	Sun Sep 08 13:16:07 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8NamingChecker.py	Mon Sep 09 18:43:23 2013 +0200
@@ -313,10 +313,15 @@
                 return
         
         argNames = self.__getArgNames(node)
+        functionType = getattr(node, "function_type", "function")
+        
         if not argNames:
+            if functionType == "method":
+                yield self.__error(node, "N805")
+            elif functionType == "classmethod":
+                yield self.__error(node, "N804")
             return
         
-        functionType = getattr(node, "function_type", "function")
         if functionType == "method":
             if argNames[0] != "self":
                 yield self.__error(node, "N805")
--- a/Plugins/CheckerPlugins/Pep8/Pep8StatisticsDialog.py	Sun Sep 08 13:16:07 2013 +0200
+++ b/Plugins/CheckerPlugins/Pep8/Pep8StatisticsDialog.py	Mon Sep 09 18:43:23 2013 +0200
@@ -11,6 +11,7 @@
 from PyQt4.QtGui import QDialog, QTreeWidgetItem
 
 from . import pep8
+from .Pep8NamingChecker import Pep8NamingChecker
 
 from .Ui_Pep8StatisticsDialog import Ui_Pep8StatisticsDialog
 
@@ -42,15 +43,17 @@
         
         totalIssues = 0
         
-        for code in sorted(stats.keys(), key=lambda a: a[1:]):
+        for code in sorted(stats.keys()):
             if code in pep8.pep8_messages_sample_args:
-                message = QCoreApplication.translate("pep8",
-                    pep8.pep8_messages[code]).format(
-                        *pep8.pep8_messages_sample_args[code])
+                message = QCoreApplication.translate(
+                    "pep8", pep8.pep8_messages[code]).format(
+                    *pep8.pep8_messages_sample_args[code])
             elif code in pep8.pep8_messages:
-                # TODO: add naming checker messages
-                message = QCoreApplication.translate("pep8",
-                    pep8.pep8_messages[code])
+                message = QCoreApplication.translate(
+                    "pep8", pep8.pep8_messages[code])
+            elif code in Pep8NamingChecker.Messages:
+                message = QCoreApplication.translate(
+                    "Pep8NamingChecker", Pep8NamingChecker.Messages[code])
             else:
                 continue
             self.__createItem(stats[code], code, message)

eric ide

mercurial