eric6/Plugins/CheckerPlugins/CodeStyleChecker/mccabe.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7639
422fd05e9c91
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 """ Meager code path measurement tool.
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2 Ned Batchelder
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 http://nedbatchelder.com/blog/200803/python_code_complexity_microtool.html
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 MIT License.
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5 """
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 import collections
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 import ast
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
10 __version__ = '0.6.1_eric6'
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 class ASTVisitor(object):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 """Performs a depth-first walk of the AST."""
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 def __init__(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17 self.node = None
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 self._cache = {}
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 def default(self, node, *args):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 for child in ast.iter_child_nodes(node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22 self.dispatch(child, *args)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 def dispatch(self, node, *args):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 self.node = node
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 klass = node.__class__
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 meth = self._cache.get(klass)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 if meth is None:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 className = klass.__name__
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 meth = getattr(self.visitor, 'visit' + className, self.default)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 self._cache[klass] = meth
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 return meth(node, *args)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 def preorder(self, tree, visitor, *args):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 """Do preorder walk of tree using visitor"""
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 self.visitor = visitor
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 visitor.visit = self.dispatch
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 self.dispatch(tree, *args) # XXX *args make sense?
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
40
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
41 class PathNode(object):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
42 def __init__(self, name, look="circle"):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43 self.name = name
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 self.look = look
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 def to_dot(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47 print('node [shape=%s,label="%s"] %d;' % (
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 self.look, self.name, self.dot_id()))
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 def dot_id(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 return id(self)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 class PathGraph(object):
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
55 def __init__(self, name, entity, lineno, column=0):
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 self.name = name
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 self.entity = entity
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 self.lineno = lineno
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
59 self.column = column
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 self.nodes = collections.defaultdict(list)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 def connect(self, n1, n2):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 self.nodes[n1].append(n2)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 # Ensure that the destination node is always counted.
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 self.nodes[n2] = []
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67 def to_dot(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 print('subgraph {')
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 for node in self.nodes:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 node.to_dot()
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 for node, nexts in self.nodes.items():
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 for next in nexts:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 print('%s -- %s;' % (node.dot_id(), next.dot_id()))
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 print('}')
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 def complexity(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 """ Return the McCabe complexity for the graph.
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78 V-E+2
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 """
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 num_edges = sum([len(n) for n in self.nodes.values()])
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 num_nodes = len(self.nodes)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 return num_edges - num_nodes + 2
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 class PathGraphingAstVisitor(ASTVisitor):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 """ A visitor for a parsed Abstract Syntax Tree which finds executable
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 statements.
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 """
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 def __init__(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 super(PathGraphingAstVisitor, self).__init__()
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 self.classname = ""
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93 self.graphs = {}
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 self.reset()
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 def reset(self):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97 self.graph = None
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 self.tail = None
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 def dispatch_list(self, node_list):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 for node in node_list:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 self.dispatch(node)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104 def visitFunctionDef(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 if self.classname:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 entity = '%s%s' % (self.classname, node.name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108 else:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 entity = node.name
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
111 name = '%d:%d: %r' % (node.lineno, node.col_offset, entity)
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 if self.graph is not None:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 # closure
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 pathnode = self.appendPathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117 self.dispatch_list(node.body)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 bottom = PathNode("", look='point')
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 self.graph.connect(self.tail, bottom)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 self.graph.connect(pathnode, bottom)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 self.tail = bottom
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 else:
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
123 self.graph = PathGraph(name, entity, node.lineno, node.col_offset)
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 pathnode = PathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 self.dispatch_list(node.body)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 self.graphs["%s%s" % (self.classname, node.name)] = self.graph
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 self.reset()
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4423
diff changeset
130 visitAsyncFunctionDef = visitFunctionDef
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4423
diff changeset
131
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 def visitClassDef(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 old_classname = self.classname
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 self.classname += node.name + "."
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135 self.dispatch_list(node.body)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 self.classname = old_classname
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 def appendPathNode(self, name):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 if not self.tail:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 return
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141 pathnode = PathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
142 self.graph.connect(self.tail, pathnode)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
143 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
144 return pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
145
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
146 def visitSimpleStatement(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
147 if node.lineno is None:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
148 lineno = 0
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
149 else:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
150 lineno = node.lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
151 name = "Stmt %d" % lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
152 self.appendPathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
153
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
154 def default(self, node, *args):
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
155 if isinstance(node, ast.stmt):
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
156 self.visitSimpleStatement(node)
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
157 else:
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
158 super(PathGraphingAstVisitor, self).default(node, *args)
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
159
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
160 def visitLoop(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
161 name = "Loop %d" % node.lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
162 self._subgraph(node, name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
163
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
164 visitAsyncFor = visitFor = visitWhile = visitLoop
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
165
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
166 def visitIf(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
167 name = "If %d" % node.lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
168 self._subgraph(node, name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
169
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
170 def _subgraph(self, node, name, extra_blocks=()):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
171 """create the subgraphs representing any `if` and `for` statements"""
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
172 if self.graph is None:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
173 # global loop
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
174 self.graph = PathGraph(name, name, node.lineno, node.col_offset)
4423
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
175 pathnode = PathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
176 self._subgraph_parse(node, pathnode, extra_blocks)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
177 self.graphs["%s%s" % (self.classname, name)] = self.graph
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
178 self.reset()
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
179 else:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
180 pathnode = self.appendPathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
181 self._subgraph_parse(node, pathnode, extra_blocks)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
182
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
183 def _subgraph_parse(self, node, pathnode, extra_blocks):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
184 """parse the body and any `else` block of `if` and `for` statements"""
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
185 loose_ends = []
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
186 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
187 self.dispatch_list(node.body)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
188 loose_ends.append(self.tail)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
189 for extra in extra_blocks:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
190 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
191 self.dispatch_list(extra.body)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
192 loose_ends.append(self.tail)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
193 if node.orelse:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
194 self.tail = pathnode
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
195 self.dispatch_list(node.orelse)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 loose_ends.append(self.tail)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 else:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 loose_ends.append(pathnode)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199 if pathnode:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 bottom = PathNode("", look='point')
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 for le in loose_ends:
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 self.graph.connect(le, bottom)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 self.tail = bottom
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 def visitTryExcept(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206 name = "TryExcept %d" % node.lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 self._subgraph(node, name, extra_blocks=node.handlers)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 visitTry = visitTryExcept
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 def visitWith(self, node):
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 name = "With %d" % node.lineno
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 self.appendPathNode(name)
14f3ca3dcef6 Added a code complexity checker iaw. McCabe to the code style checker.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 self.dispatch_list(node.body)
5620
6bca68319bb4 Updated the McCabe checker to version 0.6.1.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4555
diff changeset
215
4465
2fbb5bdb8ddf Prepared eric for Python 3.5 to support the new 'async def' function definition.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4423
diff changeset
216 visitAsyncWith = visitWith

eric ide

mercurial