|
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
|
|
|
for visitor in radon.complexity.cc_visit("".join(file)): |
|
28
|
|
|
rank = radon.complexity.cc_rank(visitor.complexity) |
|
29
|
|
|
severity_map = { |
|
30
|
|
|
RESULT_SEVERITY.INFO: radon_ranks_info, |
|
31
|
|
|
RESULT_SEVERITY.NORMAL: radon_ranks_normal, |
|
32
|
|
|
RESULT_SEVERITY.MAJOR: radon_ranks_major |
|
33
|
|
|
} |
|
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
|
|
|
|