blob: 5ae7efd5445782c0b9eb0c872695207e29489ec6 [file] [log] [blame]
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +01001# Copyright 2015 Hewlett-Packard Development Company, L.P.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may
4# not use this file except in compliance with the License. You may obtain
5# a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations
13# under the License.
Kiall Mac Innes25fb29e2016-04-07 08:07:04 +010014
15import re
16import sys
17import linecache
18
19from prettytable import PrettyTable
20
21PEP8_LINE = r'^((?P<file>.*):(?P<line>\d*):(?P<col>\d*):) ' \
22 '(?P<error>(?P<error_code>\w\d{1,3})(?P<error_desc>.*$))'
23
24HTML = True
25
26
27def main():
28
29 raw_errors = []
30
31 max_filename_len = 0
32 for line in sys.stdin:
33 m = re.match(PEP8_LINE, line)
34 if m:
35 m = m.groupdict()
36 raw_errors.append(m)
37 if len(m['file']) > max_filename_len:
38 max_filename_len = len(m['file'])
39 else:
40 print(line)
41
42 if len(raw_errors) > 0:
43
44 print('Flake8 Results')
45
46 ct = PrettyTable([
47 "File",
48 "Line",
49 "Column",
50 "Error Code",
51 "Error Message",
52 "Code"
53 ])
54
55 ct.align["File"] = "l"
56 ct.align["Error Message"] = "l"
57 ct.align["Code"] = "l"
58
59 for line in raw_errors:
60 ct.add_row(format_dict(line))
61
62 print(ct)
63
64 with open('flake8_results.html', 'w') as f:
65 f.write('<html><head><style type="text/css">table a:link{color:#666;font-weight:700;text-decoration:none}table a:visited{color:#999;font-weight:700;text-decoration:none}table a:active,table a:hover{color:#bd5a35;text-decoration:underline}table{font-family:Arial,Helvetica,sans-serif;color:#666;font-size:12px;text-shadow:1px 1px 0 #fff;background:#eaebec;margin:20px;border:1px solid #ccc;-moz-border-radius:3px;-webkit-border-radius:3px;border-radius:3px;-moz-box-shadow:0 1px 2px #d1d1d1;-webkit-box-shadow:0 1px 2px #d1d1d1;box-shadow:0 1px 2px #d1d1d1}table th{padding:21px 25px 22px;border-top:1px solid #fafafa;border-bottom:1px solid #e0e0e0;background:#ededed;background:-webkit-gradient(linear,left top,left bottom,from(#ededed),to(#ebebeb));background:-moz-linear-gradient(top,#ededed,#ebebeb)}table th:first-child{text-align:left;padding-left:20px}table tr:first-child th:first-child{-moz-border-radius-topleft:3px;-webkit-border-top-left-radius:3px;border-top-left-radius:3px}table tr:first-child th:last-child{-moz-border-radius-topright:3px;-webkit-border-top-right-radius:3px;border-top-right-radius:3px}table tr{text-align:left;padding-left:20px}table td:first-child{text-align:left;padding-left:20px;border-left:0}table td{padding:18px;border-top:1px solid #fff;border-bottom:1px solid #e0e0e0;border-left:1px solid #e0e0e0;background:#fafafa;background:-webkit-gradient(linear,left top,left bottom,from(#fbfbfb),to(#fafafa));background:-moz-linear-gradient(top,#fbfbfb,#fafafa)}table tr.even td{background:#f6f6f6;background:-webkit-gradient(linear,left top,left bottom,from(#f8f8f8),to(#f6f6f6));background:-moz-linear-gradient(top,#f8f8f8,#f6f6f6)}table tr:last-child td{border-bottom:0}table tr:last-child td:first-child{-moz-border-radius-bottomleft:3px;-webkit-border-bottom-left-radius:3px;border-bottom-left-radius:3px}table tr:last-child td:last-child{-moz-border-radius-bottomright:3px;-webkit-border-bottom-right-radius:3px;border-bottom-right-radius:3px}table tr:hover td{background:#f2f2f2;background:-webkit-gradient(linear,left top,left bottom,from(#f2f2f2),to(#f0f0f0));background:-moz-linear-gradient(top,#f2f2f2,#f0f0f0)}</style></head><body>%s</body</html>' % ct.get_html_string(attributes = {"cellspacing": 0})) # noqa
66
67
68def format_dict(raw):
69 output = []
70 if raw['file'].startswith('./'):
71 output.append(raw['file'][2:])
72 else:
73 output.append(raw['file'])
74
75 output.append(raw['line'])
76 output.append(raw['col'])
77 output.append(raw['error_code'])
78
79 output.append(raw['error_desc'].lstrip())
80
81 code_string = linecache.getline(
82 output[0],
83 int(raw['line'])).lstrip().rstrip()
84
85 output.append(code_string)
86
87 return output
88
89if __name__ == '__main__':
90 main()