-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake8chart.py
200 lines (157 loc) · 5.62 KB
/
flake8chart.py
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# -*- coding: utf-8 -*-
"""flake8chart: flake8 stats visualised
"""
import sys
import string
import itertools
import time
import pprint
import csv
try:
import click
import pygal
except ImportError as e:
print("error: {0}".format(e))
sys.exit(1)
if sys.version_info[0] == 3:
import functools
reduce = functools.reduce
# ref: http://flake8.readthedocs.org/en/latest/warnings.html
CODES = {"E1": "Indentation",
"E2": "Whitespace",
"E3": "Blank line",
"E4": "Import",
"E5": "Line length",
"E7": "Statement",
"E9": "Runtime",
"W1": "Indentation",
"W2": "Whitespace",
"W3": "Blank line",
"W6": "Deprecation",
"F4": "Module",
"F8": "Name",
"I1": "Import order",
"I2": "Import section",
"D1": "Docstrings missing",
"D2": "Docstrings whitespace",
"D3": "Docstrings quotes",
"D4": "Docstrings content",
"C9": "McCabe complexity",
"N8": "Naming conventions"}
CHART_TYPE_PIE = "PIE"
CHART_TYPE_BAR = "BAR"
CHART_TYPES = (CHART_TYPE_PIE, CHART_TYPE_BAR,)
CHART_TITLE = "flake8 stats"
def split_str(s, sep=None, maxsplit=2):
return (s.split(sep=sep, maxsplit=maxsplit)
if sys.version_info[0] == 3 else
string.split(s, sep=sep, maxsplit=maxsplit))
def is_stat(items):
return len(items) == 3 and items[0].isdigit() and len(items[1]) == 4
def pipe(fn, *fns):
def _pipe(*args, **kwargs):
return reduce(lambda v, f: f(v),
fns,
fn(*args, **kwargs))
return _pipe
def dict_rows(rows):
for r in rows:
split = split_str(r)
if not is_stat(split):
continue
yield {"count": int(split[0]),
"code": split[1],
"desc": split[2]}
def group_by_code(rows):
return itertools.groupby(sorted(rows, key=lambda r: r["code"]),
key=lambda r: r["code"][:2])
def calc_sum(rows):
return ({"code": "{code}: {desc}".format(code=code,
desc=CODES.get(code, "?")),
"count": sum(r["count"] if isinstance(r["count"], int) else
int(r["count"]) for r in iterable)}
for code, iterable in rows)
def sort_by_count(rows):
return sorted(rows, key=lambda r: r["count"], reverse=True)
def get_total(rows):
return sum(r["count"] for r in rows)
def chart_pie(rows, title="Title", filename="pie_chart.svg"):
chart = pygal.Pie()
chart.title = title
for row in rows:
chart.add(row["code"], row["count"])
return chart.render_to_file(filename)
def chart_bar(rows, title="Title", filename="bar_chart.svg"):
chart = pygal.Bar()
chart.title = title
for row in rows:
chart.add(row["code"], row["count"])
return chart.render_to_file(filename)
def elapsed(f):
def _elapsed(*args, **kwargs):
start = time.time()
result = f(*args, **kwargs)
click.echo("time elapsed: %.2f seconds" % (time.time() - start,))
return result
return _elapsed
@click.command()
@click.option("--chart-type",
default=CHART_TYPE_PIE,
type=click.Choice(CHART_TYPES),
help="type of chart (default: '{0}')".format(CHART_TYPE_PIE))
@click.option("--chart-title",
default=CHART_TITLE,
help="title of chart (default: '{0}')".format(CHART_TITLE))
@click.option("--chart-output",
default="flake8_stats.svg",
help=("name of SVG file to export (default: '{0}')"
"".format("flake8_stats.svg")))
@click.option("--csv-output",
help="name of CSV file to export")
@elapsed
def flake8chart(chart_type, chart_title, chart_output, csv_output):
click.echo("chart-type: {0}".format(chart_type))
click.echo("chart-title: {0}".format(chart_title))
click.echo("chart_output: {0}".format(chart_output))
click.echo("csv-output: {0}".format(csv_output))
# sort stats by count
stats = sorted([r for r in sys.stdin.read().split("\n")
if is_stat(split_str(r))],
key=lambda r: int(r.split()[0]),
reverse=True)
if not stats:
click.echo("error: no stats found")
return
click.echo("stats:")
click.echo(pprint.pformat(stats))
# aggregate stats
pipeline = pipe(dict_rows,
group_by_code,
calc_sum,
sort_by_count,)
stats_summary = pipeline(stats)
click.echo("stats summary:")
click.echo(pprint.pformat(stats_summary))
# export chart (svg)
if stats_summary:
if chart_type.upper() == "PIE":
chart_pie(stats_summary,
title="{0} (%)".format(chart_title),
filename=chart_output)
click.echo("pie chart exported: '{0}'".format(chart_output))
else:
chart_bar(stats_summary,
title=("{0} "
"(total: {1})".format(chart_title,
get_total(stats_summary))),
filename=chart_output)
click.echo("bar chart exported: '{0}'".format(chart_output))
# export stats summary (csv)
if csv_output:
with open(csv_output, "w") as file:
writer = csv.DictWriter(file, fieldnames=["code", "count"])
writer.writeheader()
writer.writerows(stats_summary)
click.echo("csv output exported: '{0}'".format(csv_output))
if __name__ == "__main__":
flake8chart()