Completed
Pull Request — master (#1522)
by Abdeali
02:07
created

bears.python.RadonBear   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %
Metric Value
dl 0
loc 37
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 35 5
1
import radon.complexity
2
import radon.visitors
3
4
from coalib.bears.LocalBear import LocalBear
5
from coalib.results.Result import Result
6
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
7
from coalib.results.SourceRange import SourceRange
8
from coalib.settings.Setting import typed_list
9
10
11
class RadonBear(LocalBear):
12
13
    def run(self, filename, file,
14
            radon_ranks_info: typed_list(str)=(),
15
            radon_ranks_normal: typed_list(str)=('C', 'D'),
16
            radon_ranks_major: typed_list(str)=('E', 'F')):
17
        """
18
        Uses radon to compute complexity of a given file.
19
20
        :param radon_ranks_info:   The ranks (given by radon) to
21
                                   treat as severity INFO.
22
        :param radon_ranks_normal: The ranks (given by radon) to
23
                                   treat as severity NORMAL.
24
        :param radon_ranks_major:  The ranks (given by radon) to
25
                                   treat as severity MAJOR.
26
        """
27
        severity_map = {
28
            RESULT_SEVERITY.INFO: radon_ranks_info,
29
            RESULT_SEVERITY.NORMAL: radon_ranks_normal,
30
            RESULT_SEVERITY.MAJOR: radon_ranks_major
31
        }
32
        for visitor in radon.complexity.cc_visit("".join(file)):
33
            rank = radon.complexity.cc_rank(visitor.complexity)
34
            severity = None
35
            for result_severity, rank_list in severity_map.items():
36
                if rank in rank_list:
37
                    severity = result_severity
38
            if severity is None:
39
                continue
40
41
            visitor_range = SourceRange.from_values(
42
                filename, visitor.lineno, visitor.col_offset, visitor.endline)
43
            message = "{} has a cyclomatic complexity of {}".format(
44
                visitor.name, rank)
45
46
            yield Result(self, message, severity=severity,
47
                         affected_code=(visitor_range,))
48