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) |