Plugins/CheckerPlugins/Pep8/Pep8Checker.py

changeset 2862
a1448560d7dc
parent 2302
f29e9405c851
child 2863
62171fa4a6a4
equal deleted inserted replaced
2861:cdcbca0cea82 2862:a1448560d7dc
6 """ 6 """
7 Module implementing the PEP 8 checker. 7 Module implementing the PEP 8 checker.
8 """ 8 """
9 9
10 import os 10 import os
11 import optparse 11 ##import optparse
12 12
13 from PyQt4.QtCore import QProcess, QCoreApplication 13 from PyQt4.QtCore import QProcess, QCoreApplication
14 14
15 from . import pep8 15 from . import pep8
16 16
18 import Utilities 18 import Utilities
19 19
20 from eric5config import getConfig 20 from eric5config import getConfig
21 21
22 22
23 class Pep8Checker(pep8.Checker): 23 ##class Pep8Checker(pep8.Checker):
24 """ 24 ## """
25 Class implementing the PEP 8 checker. 25 ## Class implementing the PEP 8 checker.
26 """ 26 ## """
27 def __init__(self, filename, lines, repeat=False, 27 ## def __init__(self, filename, lines, repeat=False,
28 select="", ignore=""): 28 ## select="", ignore=""):
29 """ 29 ## """
30 Constructor 30 ## Constructor
31 31 ##
32 @param filename name of the file to check (string) 32 ## @param filename name of the file to check (string)
33 @param lines source of the file (list of strings) 33 ## @param lines source of the file (list of strings)
34 @keyparam repeat flag indicating to repeat message categories (boolean) 34 ## @keyparam repeat flag indicating to repeat message categories (boolean)
35 @keyparam select list of message IDs to check for 35 ## @keyparam select list of message IDs to check for
36 (comma separated string) 36 ## (comma separated string)
37 @keyparam ignore list of message IDs to ignore 37 ## @keyparam ignore list of message IDs to ignore
38 (comma separated string) 38 ## (comma separated string)
39 """ 39 ## """
40 pep8.options = optparse.Values() 40 ## pep8.options = optparse.Values()
41 41 ##
42 pep8.options.verbose = 0 42 ## pep8.options.verbose = 0
43 43 ##
44 pep8.options.repeat = repeat 44 ## pep8.options.repeat = repeat
45 if select: 45 ## if select:
46 pep8.options.select = [s.strip() for s in select.split(',') 46 ## pep8.options.select = [s.strip() for s in select.split(',')
47 if s.strip()] 47 ## if s.strip()]
48 else: 48 ## else:
49 pep8.options.select = [] 49 ## pep8.options.select = []
50 if ignore: 50 ## if ignore:
51 pep8.options.ignore = [i.strip() for i in ignore.split(',') 51 ## pep8.options.ignore = [i.strip() for i in ignore.split(',')
52 if i.strip()] 52 ## if i.strip()]
53 else: 53 ## else:
54 pep8.options.ignore = [] 54 ## pep8.options.ignore = []
55 pep8.options.physical_checks = pep8.find_checks('physical_line') 55 ## pep8.options.physical_checks = pep8.find_checks('physical_line')
56 pep8.options.logical_checks = pep8.find_checks('logical_line') 56 ## pep8.options.logical_checks = pep8.find_checks('logical_line')
57 pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0) 57 ## pep8.options.counters = dict.fromkeys(pep8.BENCHMARK_KEYS, 0)
58 pep8.options.messages = {} 58 ## pep8.options.messages = {}
59 59 ##
60 pep8.Checker.__init__(self, filename, lines) 60 ## pep8.Checker.__init__(self, filename, lines)
61 61 ##
62 self.messages = [] 62 ## self.messages = []
63 self.statistics = {} 63 ## self.statistics = {}
64 64 ##
65 def __ignore_code(self, code): 65 ## def __ignore_code(self, code):
66 """ 66 ## """
67 Private method to check, if the message for the given code should 67 ## Private method to check, if the message for the given code should
68 be ignored. 68 ## be ignored.
69 69 ##
70 If codes are selected and the code has a selected prefix and does not 70 ## If codes are selected and the code has a selected prefix and does not
71 have an ignored prefix, it is not ignored. If codes are selected and 71 ## have an ignored prefix, it is not ignored. If codes are selected and
72 the code does not have a selected prefix, it is ignored. If no codes 72 ## the code does not have a selected prefix, it is ignored. If no codes
73 are selected, the code is ignored, if it has a prefix, that is 73 ## are selected, the code is ignored, if it has a prefix, that is
74 contained in the ignored codes. 74 ## contained in the ignored codes.
75 75 ##
76 @param code code to be checked (string) 76 ## @param code code to be checked (string)
77 @return flag indicating, that the code should be ignored (boolean) 77 ## @return flag indicating, that the code should be ignored (boolean)
78 """ 78 ## """
79 if pep8.options.select: 79 ## if pep8.options.select:
80 if code.startswith(tuple(pep8.options.select)): 80 ## if code.startswith(tuple(pep8.options.select)):
81 if code.startswith(tuple(pep8.options.ignore)): 81 ## if code.startswith(tuple(pep8.options.ignore)):
82 return True 82 ## return True
83 else: 83 ## else:
84 return False 84 ## return False
85 else: 85 ## else:
86 return True 86 ## return True
87 else: 87 ## else:
88 if code.startswith(tuple(pep8.options.ignore)): 88 ## if code.startswith(tuple(pep8.options.ignore)):
89 return True 89 ## return True
90 else: 90 ## else:
91 return False 91 ## return False
92 92 ##
93 def report_error_args(self, line_number, offset, code, check, *args): 93 ## def report_error_args(self, line_number, offset, code, check, *args):
94 """ 94 ## """
95 Public method to collect the error messages. 95 ## Public method to collect the error messages.
96 96 ##
97 @param line_number line number of the issue (integer) 97 ## @param line_number line number of the issue (integer)
98 @param offset position within line of the issue (integer) 98 ## @param offset position within line of the issue (integer)
99 @param code message code (string) 99 ## @param code message code (string)
100 @param check reference to the checker function (function) 100 ## @param check reference to the checker function (function)
101 @param args arguments for the message (list) 101 ## @param args arguments for the message (list)
102 """ 102 ## """
103 if self.__ignore_code(code): 103 ## if self.__ignore_code(code):
104 return 104 ## return
105 105 ##
106 text = pep8.getMessage(code, *args) 106 ## text = pep8.getMessage(code, *args)
107 if code in self.statistics: 107 ## if code in self.statistics:
108 self.statistics[code] += 1 108 ## self.statistics[code] += 1
109 else: 109 ## else:
110 self.statistics[code] = 1 110 ## self.statistics[code] = 1
111 self.file_errors += 1 111 ## self.file_errors += 1
112 if self.statistics[code] == 1 or pep8.options.repeat: 112 ## if self.statistics[code] == 1 or pep8.options.repeat:
113 self.messages.append( 113 ## self.messages.append(
114 (self.filename, self.line_offset + line_number, 114 ## (self.filename, self.line_offset + line_number,
115 offset + 1, text) 115 ## offset + 1, text)
116 ) 116 ## )
117 117 ##
118 118 ##
119 class Pep8Py2Checker(object): 119 class Pep8Py2Checker(object):
120 """ 120 """
121 Class implementing the PEP 8 checker interface for Python 2. 121 Class implementing the PEP 8 checker interface for Python 2.
122 """ 122 """
123 def __init__(self, filename, lines, repeat=False, 123 def __init__(self, filename, lines, repeat=False,

eric ide

mercurial