Fix more pylint warnings.

* add .pylintrc to use 2 space indentation
* rename single-letter local variables

Test: ./warn.py build.log > warnings.html
Change-Id: I2ca56a6cb130a9d6c73328c5592ad7cde8a974ab
This commit is contained in:
Chih-Hung Hsieh
2021-04-30 14:30:58 -07:00
parent 10b66f6975
commit a606822f35
3 changed files with 89 additions and 91 deletions

View File

@@ -144,12 +144,11 @@ def create_warnings(warn_patterns, project_names):
2D warnings array where warnings[p][s] is # of warnings in project name p of
severity level s
"""
# pylint:disable=invalid-name
warnings = {p: {s.value: 0 for s in Severity.levels} for p in project_names}
for i in warn_patterns:
s = i['severity'].value
for p in i['projects']:
warnings[p][s] += i['projects'][p]
for pattern in warn_patterns:
value = pattern['severity'].value
for project in pattern['projects']:
warnings[project][value] += pattern['projects'][project]
return warnings
@@ -173,11 +172,11 @@ def emit_table_header(total_by_severity):
"""Returns list of HTML-formatted content for severity stats."""
stats_header = ['Project']
for s in Severity.levels:
if total_by_severity[s.value]:
for severity in Severity.levels:
if total_by_severity[severity.value]:
stats_header.append(
'<span style=\'background-color:{}\'>{}</span>'.format(
s.color, s.column_header))
severity.color, severity.column_header))
stats_header.append('TOTAL')
return stats_header
@@ -200,15 +199,15 @@ def emit_row_counts_per_project(warnings, total_by_project, total_by_severity,
total_all_projects = 0
stats_rows = []
for p in project_names:
if total_by_project[p]:
one_row = [p]
for s in Severity.levels:
if total_by_severity[s.value]:
one_row.append(warnings[p][s.value])
one_row.append(total_by_project[p])
for p_name in project_names:
if total_by_project[p_name]:
one_row = [p_name]
for severity in Severity.levels:
if total_by_severity[severity.value]:
one_row.append(warnings[p_name][severity.value])
one_row.append(total_by_project[p_name])
stats_rows.append(one_row)
total_all_projects += total_by_project[p]
total_all_projects += total_by_project[p_name]
return total_all_projects, stats_rows
@@ -226,10 +225,10 @@ def emit_row_counts_per_severity(total_by_severity, stats_header, stats_rows,
total_all_severities = 0
one_row = ['<b>TOTAL</b>']
for s in Severity.levels:
if total_by_severity[s.value]:
one_row.append(total_by_severity[s.value])
total_all_severities += total_by_severity[s.value]
for severity in Severity.levels:
if total_by_severity[severity.value]:
one_row.append(total_by_severity[severity.value])
total_all_severities += total_by_severity[severity.value]
one_row.append(total_all_projects)
stats_rows.append(one_row)
writer('<script>')
@@ -328,8 +327,8 @@ def dump_fixed(writer, warn_patterns):
for text in fixed_patterns:
cur_row_class = 1 - cur_row_class
# remove last '\n'
t = text[:-1] if text[-1] == '\n' else text
writer('<tr><td class="c' + str(cur_row_class) + '">' + t + '</td></tr>')
out_text = text[:-1] if text[-1] == '\n' else text
writer('<tr><td class="c' + str(cur_row_class) + '">' + out_text + '</td></tr>')
writer('</table></div>')
writer('</blockquote>')
@@ -339,10 +338,10 @@ def write_severity(csvwriter, sev, kind, warn_patterns):
total = 0
for pattern in warn_patterns:
if pattern['severity'] == sev and pattern['members']:
n = len(pattern['members'])
total += n
num_members = len(pattern['members'])
total += num_members
warning = kind + ': ' + (pattern['description'] or '?')
csvwriter.writerow([n, '', warning])
csvwriter.writerow([num_members, '', warning])
# print number of warnings for each project, ordered by project name
projects = sorted(pattern['projects'].keys())
for project in projects:
@@ -355,8 +354,8 @@ def dump_csv(csvwriter, warn_patterns):
"""Dump number of warnings in CSV format to writer."""
sort_warnings(warn_patterns)
total = 0
for s in Severity.levels:
total += write_severity(csvwriter, s, s.column_header, warn_patterns)
for severity in Severity.levels:
total += write_severity(csvwriter, severity, severity.column_header, warn_patterns)
csvwriter.writerow([total, '', 'All warnings'])
@@ -379,35 +378,35 @@ def dump_csv_with_description(csvwriter, warning_records, warning_messages,
csvwriter.writerow(output)
# Return s with escaped backslash and quotation characters.
def escape_string(s):
return s.replace('\\', '\\\\').replace('"', '\\"')
# Return line with escaped backslash and quotation characters.
def escape_string(line):
return line.replace('\\', '\\\\').replace('"', '\\"')
# Return s without trailing '\n' and escape the quotation characters.
def strip_escape_string(s):
if not s:
return s
s = s[:-1] if s[-1] == '\n' else s
return escape_string(s)
# Return line without trailing '\n' and escape the quotation characters.
def strip_escape_string(line):
if not line:
return line
line = line[:-1] if line[-1] == '\n' else line
return escape_string(line)
def emit_warning_array(name, writer, warn_patterns):
writer('var warning_{} = ['.format(name))
for w in warn_patterns:
for pattern in warn_patterns:
if name == 'severity':
writer('{},'.format(w[name].value))
writer('{},'.format(pattern[name].value))
else:
writer('{},'.format(w[name]))
writer('{},'.format(pattern[name]))
writer('];')
def emit_warning_arrays(writer, warn_patterns):
emit_warning_array('severity', writer, warn_patterns)
writer('var warning_description = [')
for w in warn_patterns:
if w['members']:
writer('"{}",'.format(escape_string(w['description'])))
for pattern in warn_patterns:
if pattern['members']:
writer('"{}",'.format(escape_string(pattern['description'])))
else:
writer('"",') # no such warning
writer('];')
@@ -566,32 +565,32 @@ def emit_const_string(name, value, writer):
# Emit a JavaScript const integer array.
def emit_const_int_array(name, array, writer):
writer('const ' + name + ' = [')
for n in array:
writer(str(n) + ',')
for item in array:
writer(str(item) + ',')
writer('];')
# Emit a JavaScript const string array.
def emit_const_string_array(name, array, writer):
writer('const ' + name + ' = [')
for s in array:
writer('"' + strip_escape_string(s) + '",')
for item in array:
writer('"' + strip_escape_string(item) + '",')
writer('];')
# Emit a JavaScript const string array for HTML.
def emit_const_html_string_array(name, array, writer):
writer('const ' + name + ' = [')
for s in array:
writer('"' + html.escape(strip_escape_string(s)) + '",')
for item in array:
writer('"' + html.escape(strip_escape_string(item)) + '",')
writer('];')
# Emit a JavaScript const object array.
def emit_const_object_array(name, array, writer):
writer('const ' + name + ' = [')
for x in array:
writer(str(x) + ',')
for item in array:
writer(str(item) + ',')
writer('];')
@@ -671,8 +670,8 @@ def write_html(flags, project_names, warn_patterns, html_path, warning_messages,
warning_links, warning_records, header_str):
"""Write warnings html file."""
if html_path:
with open(html_path, 'w') as f:
dump_html(flags, f, warning_messages, warning_links, warning_records,
with open(html_path, 'w') as outf:
dump_html(flags, outf, warning_messages, warning_links, warning_records,
header_str, warn_patterns, project_names)
@@ -680,12 +679,12 @@ def write_out_csv(flags, warn_patterns, warning_messages, warning_links,
warning_records, header_str, project_names):
"""Write warnings csv file."""
if flags.csvpath:
with open(flags.csvpath, 'w') as f:
dump_csv(csv.writer(f, lineterminator='\n'), warn_patterns)
with open(flags.csvpath, 'w') as outf:
dump_csv(csv.writer(outf, lineterminator='\n'), warn_patterns)
if flags.csvwithdescription:
with open(flags.csvwithdescription, 'w') as f:
dump_csv_with_description(csv.writer(f, lineterminator='\n'),
with open(flags.csvwithdescription, 'w') as outf:
dump_csv_with_description(csv.writer(outf, lineterminator='\n'),
warning_records, warning_messages,
warn_patterns, project_names)