src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/Simplify/SimplifyChecker.py

branch
eric7
changeset 11150
73d80859079c
parent 11147
dee6e106b4d3
equal deleted inserted replaced
11149:fc45672fae42 11150:73d80859079c
6 """ 6 """
7 Module implementing the checker for simplifying Python code. 7 Module implementing the checker for simplifying Python code.
8 """ 8 """
9 9
10 import ast 10 import ast
11 import copy 11
12 from CodeStyleTopicChecker import CodeStyleTopicChecker
12 13
13 from .SimplifyNodeVisitor import SimplifyNodeVisitor 14 from .SimplifyNodeVisitor import SimplifyNodeVisitor
14 15
15 16
16 class SimplifyChecker: 17 class SimplifyChecker(CodeStyleTopicChecker):
17 """ 18 """
18 Class implementing a checker for to help simplifying Python code. 19 Class implementing a checker for to help simplifying Python code.
19 """ 20 """
20 21
21 Codes = [ 22 Codes = [
77 "Y-907", 78 "Y-907",
78 "Y-909", 79 "Y-909",
79 "Y-910", 80 "Y-910",
80 "Y-911", 81 "Y-911",
81 ] 82 ]
83 Category = "Y"
82 84
83 def __init__(self, source, filename, tree, selected, ignored, expected, repeat): 85 def __init__(self, source, filename, tree, selected, ignored, expected, repeat):
84 """ 86 """
85 Constructor 87 Constructor
86 88
97 @param expected list of expected codes 99 @param expected list of expected codes
98 @type list of str 100 @type list of str
99 @param repeat flag indicating to report each occurrence of a code 101 @param repeat flag indicating to report each occurrence of a code
100 @type bool 102 @type bool
101 """ 103 """
102 self.__select = tuple(selected) 104 super().__init__(
103 self.__ignore = tuple(ignored) 105 SimplifyChecker.Category,
104 self.__expected = expected[:] 106 source,
105 self.__repeat = repeat 107 filename,
106 self.__filename = filename 108 tree,
107 self.__source = source[:] 109 selected,
108 self.__tree = copy.deepcopy(tree) 110 ignored,
109 111 expected,
110 # statistics counters 112 repeat,
111 self.counters = {} 113 [],
112
113 # collection of detected errors
114 self.errors = []
115
116 self.__checkCodes = (code for code in self.Codes if not self.__ignoreCode(code))
117
118 def __ignoreCode(self, code):
119 """
120 Private method to check if the message code should be ignored.
121
122 @param code message code to check for
123 @type str
124 @return flag indicating to ignore the given code
125 @rtype bool
126 """
127 return code in self.__ignore or (
128 code.startswith(self.__ignore) and not code.startswith(self.__select)
129 ) 114 )
130 115
131 def __error(self, lineNumber, offset, code, *args): 116 checkersWithCodes = [
132 """ 117 (
133 Private method to record an issue. 118 self.__checkCodeSimplifications,
134 119 (
135 @param lineNumber line number of the issue 120 "Y-101",
136 @type int 121 "Y-102",
137 @param offset position within line of the issue 122 "Y-103",
138 @type int 123 "Y-104",
139 @param code message code 124 "Y-105",
140 @type str 125 "Y-106",
141 @param args arguments for the message 126 "Y-107",
142 @type list 127 "Y-108",
143 """ 128 "Y-109",
144 if self.__ignoreCode(code): 129 "Y-110",
145 return 130 "Y-111",
146 131 "Y-112",
147 # record the issue with one based line number 132 "Y-113",
148 errorInfo = { 133 "Y-114",
149 "file": self.__filename, 134 "Y-115",
150 "line": lineNumber + 1, 135 "Y-116",
151 "offset": offset, 136 "Y-117",
152 "code": code, 137 "Y-118",
153 "args": args, 138 "Y-119",
154 } 139 "Y-120",
155 140 "Y-121",
156 if errorInfo not in self.errors: 141 "Y-122",
157 # this issue was not seen before 142 "Y-123",
158 if code in self.counters: 143 "Y-181",
159 self.counters[code] += 1 144 "Y-182",
160 else: 145 "Y-201",
161 self.counters[code] = 1 146 "Y-202",
162 147 "Y-203",
163 # Don't care about expected codes 148 "Y-204",
164 if code in self.__expected: 149 "Y-205",
165 return 150 "Y-206",
166 151 "Y-207",
167 if code and (self.counters[code] == 1 or self.__repeat): 152 "Y-208",
168 self.errors.append(errorInfo) 153 "Y-211",
169 154 "Y-212",
170 def run(self): 155 "Y-213",
171 """ 156 "Y-221",
172 Public method to check the given source against functions 157 "Y-222",
173 to be replaced by 'pathlib' equivalents. 158 "Y-223",
174 """ 159 "Y-224",
175 if not self.__filename: 160 "Y-301",
176 # don't do anything, if essential data is missing 161 "Y-401",
177 return 162 "Y-402",
178 163 "Y-411",
179 if not self.__checkCodes: 164 "Y-901",
180 # don't do anything, if no codes were selected 165 "Y-904",
181 return 166 "Y-905",
182 167 "Y-906",
168 "Y-907",
169 "Y-909",
170 "Y-910",
171 "Y-911",
172 ),
173 ),
174 ]
175 self._initializeCheckers(checkersWithCodes)
176
177 def __checkCodeSimplifications(self):
178 """
179 Private method to check for code simplifications.
180 """
183 # Add parent information 181 # Add parent information
184 self.__addMeta(self.__tree) 182 self.__addMeta(self.tree)
185 183
186 visitor = SimplifyNodeVisitor(self.__error) 184 visitor = SimplifyNodeVisitor(self.addErrorFromNode)
187 visitor.visit(self.__tree) 185 visitor.visit(self.tree)
188 186
189 def __addMeta(self, root, level=0): 187 def __addMeta(self, root, level=0):
190 """ 188 """
191 Private method to amend the nodes of the given AST tree with backward and 189 Private method to amend the nodes of the given AST tree with backward and
192 forward references. 190 forward references.

eric ide

mercurial