RadonMetrics/CyclomaticComplexityCalculator.py

changeset 69
cdf51e6abaee
parent 68
69445de59a30
child 75
e7ed383ac6a7
equal deleted inserted replaced
68:69445de59a30 69:cdf51e6abaee
5 5
6 """ 6 """
7 Module implementing the cyclomatic complexity service. 7 Module implementing the cyclomatic complexity service.
8 """ 8 """
9 9
10 from __future__ import unicode_literals 10 import queue
11
12 try:
13 str = unicode # __IGNORE_EXCEPTION__ __IGNORE_WARNING__
14 except NameError:
15 pass
16
17 try:
18 import Queue as queue # Py2
19 except ImportError:
20 import queue
21
22 import sys
23 import multiprocessing 11 import multiprocessing
24 12
25 13
26 def initService(): 14 def initService():
27 """ 15 """
93 for task in argumentsList[:initialTasks]: 81 for task in argumentsList[:initialTasks]:
94 taskQueue.put(task) 82 taskQueue.put(task)
95 83
96 # Start worker processes 84 # Start worker processes
97 for _ in range(NumberOfProcesses): 85 for _ in range(NumberOfProcesses):
98 multiprocessing.Process(target=worker, args=(taskQueue, doneQueue))\ 86 multiprocessing.Process(
99 .start() 87 target=worker, args=(taskQueue, doneQueue)).start()
100 88
101 # Get and send results 89 # Get and send results
102 endIndex = len(argumentsList) - initialTasks 90 endIndex = len(argumentsList) - initialTasks
103 for i in range(len(argumentsList)): 91 for i in range(len(argumentsList)):
104 resultSent = False 92 resultSent = False
155 @return tuple containing the result dictionary 143 @return tuple containing the result dictionary
156 @rtype (tuple of dict) 144 @rtype (tuple of dict)
157 """ 145 """
158 from radon.complexity import cc_visit, cc_rank 146 from radon.complexity import cc_visit, cc_rank
159 147
160 # Check type for py2: if not str it's unicode
161 if sys.version_info[0] == 2:
162 try:
163 text = text.encode('utf-8')
164 except UnicodeError:
165 pass
166
167 try: 148 try:
168 cc = cc_visit(text) 149 cc = cc_visit(text)
169 res = {"result": [v for v in map(__cc2Dict, cc) 150 res = {"result": [v for v in map(__cc2Dict, cc)
170 if v["type"] != "method"]} 151 if v["type"] != "method"]}
171 totalCC = 0 152 totalCC = 0
203 184
204 result = { 185 result = {
205 'type': __getType(obj), 186 'type': __getType(obj),
206 'rank': cc_rank(obj.complexity), 187 'rank': cc_rank(obj.complexity),
207 } 188 }
208 attrs = set(Function._fields) - set(('is_method', 'closures')) 189 attrs = set(Function._fields) - {'is_method', 'closures'}
209 attrs.add("fullname") 190 attrs.add("fullname")
210 for attr in attrs: 191 for attr in attrs:
211 v = getattr(obj, attr, None) 192 v = getattr(obj, attr, None)
212 if v is not None: 193 if v is not None:
213 result[attr] = v 194 result[attr] = v

eric ide

mercurial