RadonMetrics/CodeMetricsCalculator.py

changeset 6
13e9698a9981
parent 5
db25d1d5cc3a
child 9
7f6e04213998
diff -r db25d1d5cc3a -r 13e9698a9981 RadonMetrics/CodeMetricsCalculator.py
--- a/RadonMetrics/CodeMetricsCalculator.py	Wed Sep 16 18:51:27 2015 +0200
+++ b/RadonMetrics/CodeMetricsCalculator.py	Wed Sep 16 19:36:25 2015 +0200
@@ -44,6 +44,8 @@
     """
     if type_ == "raw":
         return __rawCodeMetrics(file, text)
+    elif type_ == "mi":
+        return __maintainabilityIndex(file, text)
     
     res = {"error": "Unknown metrics '{0}'.".format(type_)}
     return (res, )
@@ -111,8 +113,10 @@
     for filename, source, type_ in iter(input.get, 'STOP'):
         if type_ == "raw":
             result = __rawCodeMetrics(filename, source)
+        elif type_ == "mi":
+            result = __maintainabilityIndex(filename, source)
         else:
-            result = []
+            result = {}
         output.put((filename, result))
 
 
@@ -124,8 +128,8 @@
     @type str
     @param text source text
     @type str
-    @return tuple containing the result list
-    @rtype (list)
+    @return tuple containing the result dictionary
+    @rtype (tuple of dict)
     """
     from radon.raw import analyze
     try:
@@ -151,3 +155,25 @@
         if v is not None:
             result[a] = v
     return result
+
+
+def __maintainabilityIndex(file, text=""):
+    """
+    Private function to calculate the maintainability index for one Python
+    file.
+    
+    @param file source filename
+    @type str
+    @param text source text
+    @type str
+    @return tuple containing the result dictionary
+    @rtype (tuple of dict)
+    """
+    from radon.metrics import mi_visit, mi_rank
+    try:
+        mi = mi_visit(text)
+        rank = mi_rank(mi)
+        res = {"mi": mi, "rank": rank}
+    except Exception as err:
+        res = {"error": str(err)}
+    return (res, )

eric ide

mercurial