forked from bennylope/flake8-codeclimate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake8_codeclimate.py
More file actions
117 lines (99 loc) · 2.8 KB
/
flake8_codeclimate.py
File metadata and controls
117 lines (99 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# encoding: utf-8
"""
Flake8 plugin for Code Climate JSON format reporting::
flake8 --format=codeclimate
"""
import hashlib
import json
from flake8.formatting import base
from pkg_resources import DistributionNotFound, get_distribution
__author__ = 'Ben Lopatin'
__license__ = 'MIT'
try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass
category_complexity = "Complexity"
category_bug = "Bug Risk"
category_style = "Style"
category_clarity = "Clarity"
category_compatibility = "Compatibility"
error_classes = {
"C9": {
"category": category_complexity,
},
"E": {
"category": category_style,
},
"E7": {
"category": category_clarity,
},
"E9": {
"category": category_bug,
},
"F4": {
"category": category_bug,
},
"F6": {
"category": category_bug,
},
"F7": {
"category": category_bug,
},
"F8": {
"category": category_bug,
},
"W": {
"category": category_style,
},
"W6": {
"category": category_compatibility,
},
}
def error_category(error):
try:
return error_classes[error.code]["category"]
except KeyError:
try:
return error_classes[error.code][:2]["category"]
except KeyError:
try:
return error_classes[error.code][:1]["category"]
except KeyError:
return category_bug
class JSONFormatter(base.BaseFormatter):
"""Formatter for Code Climate JSON reporting"""
def create_fingerprint(self, error):
value = ":".join(str(val) for val in error)
return hashlib.sha256(value.encode("utf-8")).hexdigest()
def format(self, error):
return json.dumps({
"type": "issue",
"check_name": error.code, # TODO map codes to names
"description": error.text,
"fingerprint": self.create_fingerprint(error),
"content": {
"body": "`{}`".format(error.physical_line),
},
"categories": [error_category(error)],
"location": {
"path": error.filename,
"lines": {
"begin": error.line_number,
"end": error.line_number,
},
"positions": {
"begin": {
"line": error.line_number,
"column": error.column_number,
},
"end": {
"line": error.line_number,
"column": error.column_number,
},
},
},
"remediation_points": None,
# "severity": Severity, # TODO map severity
})