Plugins/CheckerPlugins/Pep8/Pep8Checker.py

changeset 832
eb5ff61f927b
child 843
522c8befcf49
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugins/CheckerPlugins/Pep8/Pep8Checker.py	Sun Jan 09 18:16:46 2011 +0100
@@ -0,0 +1,108 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2011 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the PEP 8 checker.
+"""
+
+import optparse
+
+from . import pep8
+
+
+class Pep8Checker(pep8.Checker):
+    """
+    Class implementing the PEP 8 checker.
+    """
+    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)
+        @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)
+        """
+        pep8.options = optparse.Values()
+        
+        pep8.options.verbose = 0
+        
+        pep8.options.repeat = repeat
+        if select:
+            pep8.options.select = [s.strip() for s in select.split(',')
+                                   if s.strip()]
+        else:
+            pep8.options.select = []
+        if ignore:
+            pep8.options.ignore = [i.strip() for i in ignore.split(',')
+                                   if i.strip()]
+        else:
+            pep8.options.ignore = []
+        pep8.options.physical_checks = pep8.find_checks('physical_line')
+        pep8.options.logical_checks = pep8.find_checks('logical_line')
+        pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
+        pep8.options.messages = {}
+        
+        pep8.Checker.__init__(self, filename, lines)
+        
+        self.messages = []
+    
+    def __ignore_code(self, code):
+        """
+        Private method to check, if the message for the given code should
+        be ignored.
+        
+        If codes are selected and the code has a selected prefix and does not
+        have an ignored prefix, it is not ignored. If codes are selected and
+        the code does not have a selected prefix, it is ignored. If no codes
+        are selected, the code is ignored, if it has a prefix, that is
+        contained in the ignored codes.
+        
+        @param code code to be checked (string)
+        @return flag indicating, that the code should be ignored (boolean)
+        """
+        if pep8.options.select:
+            if code.startswith(tuple(pep8.options.select)):
+                if code.startswith(tuple(pep8.options.ignore)):
+                    return True
+                else:
+                    return False
+            else:
+                return True
+        else:
+            if code.startswith(tuple(pep8.options.ignore)):
+                return True
+            else:
+                return False
+    
+    def report_error_args(self, line_number, offset, code, check, *args):
+        """
+        Public method to collect the error messages.
+        
+        @param line_number line number of the issue (integer)
+        @param offset position within line of the issue (integer)
+        @param code message code (string)
+        @param check reference to the checker function (function)
+        @param args arguments for the message (list)
+        """
+        if self.__ignore_code(code):
+            return
+        
+        text = pep8.getMessage(code, *args)
+        if code in pep8.options.counters:
+            pep8.options.counters[code] += 1
+        else:
+            pep8.options.counters[code] = 1
+            pep8.options.messages[code] = text[5:]
+        self.file_errors += 1
+        if pep8.options.counters[code] == 1 or pep8.options.repeat:
+            self.messages.append(
+                (self.filename, self.line_offset + line_number,
+                 offset + 1, text)
+            )

eric ide

mercurial