9 str = unicode # __IGNORE_EXCEPTION __IGNORE_WARNING__ |
9 str = unicode # __IGNORE_EXCEPTION __IGNORE_WARNING__ |
10 except NameError: |
10 except NameError: |
11 pass |
11 pass |
12 |
12 |
13 import multiprocessing |
13 import multiprocessing |
14 import sys |
|
15 |
14 |
16 |
15 |
17 def initService(): |
16 def initService(): |
18 """ |
17 """ |
19 Initialize the service and return the entry point. |
18 Initialize the service and return the entry point. |
20 |
19 |
21 @return the entry point for the background client (function) |
20 @return the entry point for the background client (function) |
22 """ |
21 """ |
23 return codeMetrics |
22 return rawCodeMetrics |
24 |
23 |
25 |
24 |
26 def initBatchService(): |
25 def initBatchService(): |
27 """ |
26 """ |
28 Initialize the batch service and return the entry point. |
27 Initialize the batch service and return the entry point. |
29 |
28 |
30 @return the entry point for the background client (function) |
29 @return the entry point for the background client (function) |
31 """ |
30 """ |
32 return batchCodeMetrics |
31 return batchRawCodeMetrics |
33 |
32 |
34 |
33 |
35 def codeMetrics(file, text="", type_=""): |
34 def rawCodeMetrics(file, text=""): |
36 """ |
35 """ |
37 Private function to calculate selected code metrics of one file. |
36 Private function to calculate the raw code metrics of one file. |
38 |
37 |
39 @param file source filename |
38 @param file source filename |
40 @type str |
39 @type str |
41 @param text source text |
40 @param text source text |
42 @param str |
41 @param str |
43 @return tuple containing the filename and the result list |
42 @return tuple containing the result dictionary |
44 @rtype (str, list) |
43 @rtype (tuple of dict) |
45 """ |
44 """ |
46 if type_ == "raw": |
45 return __rawCodeMetrics(file, text) |
47 return __rawCodeMetrics(file, text) |
|
48 elif type_ == "mi": |
|
49 return __maintainabilityIndex(file, text) |
|
50 |
|
51 res = {"error": "Unknown metrics '{0}'.".format(type_)} |
|
52 return (res, ) |
|
53 |
46 |
54 |
47 |
55 def batchCodeMetrics(argumentsList, send, fx, cancelled): |
48 def batchRawCodeMetrics(argumentsList, send, fx, cancelled): |
56 """ |
49 """ |
57 Module function to calculate selected code metrics for a batch of files. |
50 Module function to calculate the raw code metrics for a batch of files. |
58 |
51 |
59 @param argumentsList list of arguments tuples as given for check |
52 @param argumentsList list of arguments tuples as given for check |
60 @type list |
53 @type list |
61 @param send reference to send function |
54 @param send reference to send function |
62 @type function |
55 @type function |
109 @param input input queue |
102 @param input input queue |
110 @type multiprocessing.Queue |
103 @type multiprocessing.Queue |
111 @param output output queue |
104 @param output output queue |
112 @type multiprocessing.Queue |
105 @type multiprocessing.Queue |
113 """ |
106 """ |
114 for filename, source, type_ in iter(input.get, 'STOP'): |
107 for filename, source in iter(input.get, 'STOP'): |
115 if type_ == "raw": |
108 result = __rawCodeMetrics(filename, source) |
116 result = __rawCodeMetrics(filename, source) |
|
117 elif type_ == "mi": |
|
118 result = __maintainabilityIndex(filename, source) |
|
119 else: |
|
120 result = {} |
|
121 output.put((filename, result)) |
109 output.put((filename, result)) |
122 |
110 |
123 |
111 |
124 def __rawCodeMetrics(file, text=""): |
112 def __rawCodeMetrics(file, text=""): |
125 """ |
113 """ |
135 from radon.raw import analyze |
123 from radon.raw import analyze |
136 try: |
124 try: |
137 res = __raw2Dict(analyze(text)) |
125 res = __raw2Dict(analyze(text)) |
138 except Exception as err: |
126 except Exception as err: |
139 res = {"error": str(err)} |
127 res = {"error": str(err)} |
140 return ("raw", res) |
128 return (res, ) |
141 |
129 |
142 |
130 |
143 def __raw2Dict(obj): |
131 def __raw2Dict(obj): |
144 """ |
132 """ |
145 Private function to convert an object holding raw analysis results into a |
133 Private function to convert an object holding raw analysis results into a |
154 for a in obj._fields: |
142 for a in obj._fields: |
155 v = getattr(obj, a, None) |
143 v = getattr(obj, a, None) |
156 if v is not None: |
144 if v is not None: |
157 result[a] = v |
145 result[a] = v |
158 return result |
146 return result |
159 |
|
160 |
|
161 def __maintainabilityIndex(file, text=""): |
|
162 """ |
|
163 Private function to calculate the maintainability index for one Python |
|
164 file. |
|
165 |
|
166 @param file source filename |
|
167 @type str |
|
168 @param text source text |
|
169 @type str |
|
170 @return tuple containing the result dictionary |
|
171 @rtype (tuple of dict) |
|
172 """ |
|
173 from radon.metrics import mi_visit, mi_rank |
|
174 |
|
175 # Check type for py2: if not str it's unicode |
|
176 if sys.version_info[0] == 2: |
|
177 try: |
|
178 text = text.encode('utf-8') |
|
179 except UnicodeError: |
|
180 pass |
|
181 |
|
182 try: |
|
183 mi = mi_visit(text, True) |
|
184 rank = mi_rank(mi) |
|
185 res = {"mi": mi, "rank": rank} |
|
186 except Exception as err: |
|
187 res = {"error": str(err)} |
|
188 return ("mi", res) |
|