Failed Conditions
Pull Request — master (#1522)
by Abdeali
01:30
created

bears.python.RadonBear.run()   B

Complexity

Conditions 5

Size

Total Lines 35

Duplication

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