RadonMetrics/CodeMetricsCalculator.py

changeset 9
7f6e04213998
parent 6
13e9698a9981
child 10
8b1920a22df3
equal deleted inserted replaced
8:d02708288a22 9:7f6e04213998
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
14 15
15 16
16 def initService(): 17 def initService():
17 """ 18 """
18 Initialize the service and return the entry point. 19 Initialize the service and return the entry point.
134 from radon.raw import analyze 135 from radon.raw import analyze
135 try: 136 try:
136 res = __raw2Dict(analyze(text)) 137 res = __raw2Dict(analyze(text))
137 except Exception as err: 138 except Exception as err:
138 res = {"error": str(err)} 139 res = {"error": str(err)}
139 return (res, ) 140 return ("raw", res)
140 141
141 142
142 def __raw2Dict(obj): 143 def __raw2Dict(obj):
143 """ 144 """
144 Private function to convert an object holding raw analysis results into a 145 Private function to convert an object holding raw analysis results into a
168 @type str 169 @type str
169 @return tuple containing the result dictionary 170 @return tuple containing the result dictionary
170 @rtype (tuple of dict) 171 @rtype (tuple of dict)
171 """ 172 """
172 from radon.metrics import mi_visit, mi_rank 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
173 try: 182 try:
174 mi = mi_visit(text) 183 mi = mi_visit(text, True)
175 rank = mi_rank(mi) 184 rank = mi_rank(mi)
176 res = {"mi": mi, "rank": rank} 185 res = {"mi": mi, "rank": rank}
177 except Exception as err: 186 except Exception as err:
178 res = {"error": str(err)} 187 res = {"error": str(err)}
179 return (res, ) 188 return ("mi", res)

eric ide

mercurial