|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 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 McCabeChecker(object): |
|
17 """ |
|
18 Class implementing a checker for code complexity iaw. McCabe. |
|
19 """ |
|
20 Codes = [ |
|
21 "C101", |
|
22 |
|
23 "C901", |
|
24 ] |
|
25 |
|
26 def __init__(self, source, filename, select, ignore, maxComplexity=10): |
|
27 """ |
|
28 Constructor |
|
29 |
|
30 @param source source code to be checked |
|
31 @type list of str |
|
32 @param filename name of the source file |
|
33 @type str |
|
34 @param select list of selected codes |
|
35 @type list of str |
|
36 @param ignore list of codes to be ignored |
|
37 @type list of str |
|
38 @param maxComplexity maximum allowed complexity value |
|
39 @type int |
|
40 """ |
|
41 self.__filename = filename |
|
42 self.__source = source[:] |
|
43 self.__maxComplexity = maxComplexity |
|
44 self.__select = tuple(select) |
|
45 self.__ignore = ('',) if select else tuple(ignore) |
|
46 |
|
47 # statistics counters |
|
48 self.counters = {} |
|
49 |
|
50 # collection of detected errors |
|
51 self.errors = [] |
|
52 |
|
53 def __error(self, lineNumber, offset, code, *args): |
|
54 """ |
|
55 Private method to record an issue. |
|
56 |
|
57 @param lineNumber line number of the issue |
|
58 @type int |
|
59 @param offset position within line of the issue |
|
60 @type int |
|
61 @param code message code |
|
62 @type str |
|
63 @param args arguments for the message |
|
64 @type list |
|
65 """ |
|
66 if code in self.counters: |
|
67 self.counters[code] += 1 |
|
68 else: |
|
69 self.counters[code] = 1 |
|
70 |
|
71 if code: |
|
72 # record the issue with one based line number |
|
73 self.errors.append( |
|
74 (self.__filename, lineNumber, offset, (code, args))) |
|
75 |
|
76 def __reportInvalidSyntax(self): |
|
77 """ |
|
78 Private method to report a syntax error. |
|
79 """ |
|
80 exc_type, exc = sys.exc_info()[:2] |
|
81 if len(exc.args) > 1: |
|
82 offset = exc.args[1] |
|
83 if len(offset) > 2: |
|
84 offset = offset[1:3] |
|
85 else: |
|
86 offset = (1, 0) |
|
87 self.__error(offset[0] - 1, offset[1] or 0, |
|
88 'C901', exc_type.__name__, exc.args[0]) |
|
89 |
|
90 def __ignoreCode(self, code): |
|
91 """ |
|
92 Private method to check if the error code should be ignored. |
|
93 |
|
94 @param code message code to check for (string) |
|
95 @return flag indicating to ignore the given code (boolean) |
|
96 """ |
|
97 return (code.startswith(self.__ignore) and |
|
98 not code.startswith(self.__select)) |
|
99 |
|
100 def run(self): |
|
101 """ |
|
102 Public method to check the given source for code complexity. |
|
103 """ |
|
104 if not self.__filename or not self.__source: |
|
105 # don't do anything, if essential data is missing |
|
106 return |
|
107 |
|
108 if self.__ignoreCode("C101"): |
|
109 # don't do anything, if this should be ignored |
|
110 return |
|
111 |
|
112 try: |
|
113 tree = compile(''.join(self.__source), self.__filename, 'exec', |
|
114 ast.PyCF_ONLY_AST) |
|
115 except (SyntaxError, TypeError): |
|
116 self.__reportInvalidSyntax() |
|
117 return |
|
118 |
|
119 visitor = PathGraphingAstVisitor() |
|
120 visitor.preorder(tree, visitor) |
|
121 for graph in visitor.graphs.values(): |
|
122 if graph.complexity() > self.__maxComplexity: |
|
123 self.__error(graph.lineno, 0, "C101", |
|
124 graph.entity, graph.complexity()) |