|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a checker for code complexity. |
|
8 """ |
|
9 |
|
10 import sys |
|
11 import ast |
|
12 |
|
13 from mccabe import PathGraphingAstVisitor |
|
14 |
|
15 |
|
16 class ComplexityChecker(object): |
|
17 """ |
|
18 Class implementing a checker for code complexity. |
|
19 """ |
|
20 Codes = [ |
|
21 "C101", |
|
22 "C111", "C112", |
|
23 |
|
24 "C901", |
|
25 ] |
|
26 |
|
27 def __init__(self, source, filename, select, ignore, args): |
|
28 """ |
|
29 Constructor |
|
30 |
|
31 @param source source code to be checked |
|
32 @type list of str |
|
33 @param filename name of the source file |
|
34 @type str |
|
35 @param select list of selected codes |
|
36 @type list of str |
|
37 @param ignore list of codes to be ignored |
|
38 @type list of str |
|
39 @param args dictionary of arguments for the miscellaneous checks |
|
40 @type dict |
|
41 """ |
|
42 self.__filename = filename |
|
43 self.__source = source[:] |
|
44 self.__select = tuple(select) |
|
45 self.__ignore = ('',) if select else tuple(ignore) |
|
46 self.__args = args |
|
47 |
|
48 self.__defaultArgs = { |
|
49 "McCabeComplexity": 10, |
|
50 "LineComplexity": 15, |
|
51 "LineComplexityScore": 10, |
|
52 } |
|
53 |
|
54 # statistics counters |
|
55 self.counters = {} |
|
56 |
|
57 # collection of detected errors |
|
58 self.errors = [] |
|
59 |
|
60 checkersWithCodes = [ |
|
61 (self.__checkMcCabeComplexity, ("C101",)), |
|
62 (self.__checkLineComplexity, ("C111", "C112")), |
|
63 ] |
|
64 |
|
65 self.__checkers = [] |
|
66 for checker, codes in checkersWithCodes: |
|
67 if any(not (code and self.__ignoreCode(code)) |
|
68 for code in codes): |
|
69 self.__checkers.append(checker) |
|
70 |
|
71 def __ignoreCode(self, code): |
|
72 """ |
|
73 Private method to check if the message code should be ignored. |
|
74 |
|
75 @param code message code to check for |
|
76 @type str |
|
77 @return flag indicating to ignore the given code |
|
78 @rtype bool |
|
79 """ |
|
80 return (code.startswith(self.__ignore) and |
|
81 not code.startswith(self.__select)) |
|
82 |
|
83 def __error(self, lineNumber, offset, code, *args): |
|
84 """ |
|
85 Private method to record an issue. |
|
86 |
|
87 @param lineNumber line number of the issue |
|
88 @type int |
|
89 @param offset position within line of the issue |
|
90 @type int |
|
91 @param code message code |
|
92 @type str |
|
93 @param args arguments for the message |
|
94 @type list |
|
95 """ |
|
96 if self.__ignoreCode(code): |
|
97 return |
|
98 |
|
99 if code in self.counters: |
|
100 self.counters[code] += 1 |
|
101 else: |
|
102 self.counters[code] = 1 |
|
103 |
|
104 if code: |
|
105 # record the issue with one based line number |
|
106 self.errors.append( |
|
107 { |
|
108 "file": self.__filename, |
|
109 "line": lineNumber, |
|
110 "offset": offset, |
|
111 "code": code, |
|
112 "args": args, |
|
113 } |
|
114 ) |
|
115 |
|
116 def __reportInvalidSyntax(self): |
|
117 """ |
|
118 Private method to report a syntax error. |
|
119 """ |
|
120 exc_type, exc = sys.exc_info()[:2] |
|
121 if len(exc.args) > 1: |
|
122 offset = exc.args[1] |
|
123 if len(offset) > 2: |
|
124 offset = offset[1:3] |
|
125 else: |
|
126 offset = (1, 0) |
|
127 self.__error(offset[0] - 1, offset[1] or 0, |
|
128 'C901', exc_type.__name__, exc.args[0]) |
|
129 |
|
130 def run(self): |
|
131 """ |
|
132 Public method to check the given source for code complexity. |
|
133 """ |
|
134 if not self.__filename or not self.__source: |
|
135 # don't do anything, if essential data is missing |
|
136 return |
|
137 |
|
138 if not self.__checkers: |
|
139 # don't do anything, if no codes were selected |
|
140 return |
|
141 |
|
142 try: |
|
143 self.__tree = compile(''.join(self.__source), self.__filename, |
|
144 'exec', ast.PyCF_ONLY_AST) |
|
145 except (SyntaxError, TypeError): |
|
146 self.__reportInvalidSyntax() |
|
147 return |
|
148 |
|
149 for check in self.__checkers: |
|
150 check() |
|
151 |
|
152 def __checkMcCabeComplexity(self): |
|
153 """ |
|
154 Private method to check the McCabe code complexity. |
|
155 """ |
|
156 try: |
|
157 # create the AST again because it is modified by the checker |
|
158 tree = compile(''.join(self.__source), self.__filename, 'exec', |
|
159 ast.PyCF_ONLY_AST) |
|
160 except (SyntaxError, TypeError): |
|
161 # compile errors are already reported by the run() method |
|
162 return |
|
163 |
|
164 maxComplexity = self.__args.get("McCabeComplexity", |
|
165 self.__defaultArgs["McCabeComplexity"]) |
|
166 |
|
167 visitor = PathGraphingAstVisitor() |
|
168 visitor.preorder(tree, visitor) |
|
169 for graph in visitor.graphs.values(): |
|
170 if graph.complexity() > maxComplexity: |
|
171 self.__error(graph.lineno, 0, "C101", |
|
172 graph.entity, graph.complexity()) |
|
173 |
|
174 def __checkLineComplexity(self): |
|
175 """ |
|
176 Private method to check the complexity of a single line of code and |
|
177 the median line complexity of the source code. |
|
178 |
|
179 Complexity is defined as the number of AST nodes produced by a line |
|
180 of code. |
|
181 """ |
|
182 maxLineComplexity = self.__args.get( |
|
183 "LineComplexity", self.__defaultArgs["LineComplexity"]) |
|
184 maxLineComplexityScore = self.__args.get( |
|
185 "LineComplexityScore", self.__defaultArgs["LineComplexityScore"]) |
|
186 |
|
187 visitor = LineComplexityVisitor() |
|
188 visitor.visit(self.__tree) |
|
189 |
|
190 sortedItems = visitor.sortedList() |
|
191 score = visitor.score() |
|
192 |
|
193 for line, complexity in sortedItems: |
|
194 if complexity > maxLineComplexity: |
|
195 self.__error(line, 0, "C111", complexity) |
|
196 |
|
197 if score > maxLineComplexityScore: |
|
198 self.__error(0, 0, "C112", score) |
|
199 |
|
200 |
|
201 class LineComplexityVisitor(ast.NodeVisitor): |
|
202 """ |
|
203 Class calculating the number of AST nodes per line of code |
|
204 and the median nodes/line score. |
|
205 """ |
|
206 def __init__(self): |
|
207 """ |
|
208 Constructor |
|
209 """ |
|
210 super(LineComplexityVisitor, self).__init__() |
|
211 self.__count = {} |
|
212 |
|
213 def visit(self, node): |
|
214 """ |
|
215 Public method to recursively visit all the nodes and add up the |
|
216 instructions. |
|
217 |
|
218 @param node reference to the node |
|
219 @type ast.AST |
|
220 """ |
|
221 if hasattr(node, 'lineno'): |
|
222 self.__count[node.lineno] = self.__count.get(node.lineno, 0) + 1 |
|
223 self.generic_visit(node) |
|
224 |
|
225 def sortedList(self): |
|
226 """ |
|
227 Public method to get a sorted list of (line, nodes) tuples. |
|
228 |
|
229 @return sorted list of (line, nodes) tuples |
|
230 @rtype list of tuple of (int,int) |
|
231 """ |
|
232 lst = [(line, self.__count[line]) |
|
233 for line in sorted(self.__count.keys())] |
|
234 return lst |
|
235 |
|
236 def score(self): |
|
237 """ |
|
238 Public method to calculate the median. |
|
239 |
|
240 @return median line complexity value |
|
241 @rtype float |
|
242 """ |
|
243 lst = self.__count.values() |
|
244 sortedList = sorted(lst) |
|
245 listLength = len(lst) |
|
246 medianIndex = (listLength - 1) // 2 |
|
247 |
|
248 if listLength == 0: |
|
249 return 0.0 |
|
250 elif (listLength % 2): |
|
251 return float(sortedList[medianIndex]) |
|
252 else: |
|
253 return ( |
|
254 (sortedList[medianIndex] + sortedList[medianIndex + 1]) / 2.0 |
|
255 ) |