refactor print_and_save_errors

Summary: public
Refactor reporting of errors to enhance readability and reuse.

Reviewed By: jeremydubreil

Differential Revision: D2679676

fb-gh-sync-id: f4c1cba
master
Jules Villard 9 years ago committed by facebook-github-bot-1
parent 529d103977
commit 22bd029e46

@ -59,22 +59,6 @@ base_parser.add_argument('--html',
help='Generate HTML report.')
def describe_report(report, indent=0):
filename = report[issues.JSON_INDEX_FILENAME]
kind = report[issues.JSON_INDEX_KIND]
line = report[issues.JSON_INDEX_LINE]
error_type = report[issues.JSON_INDEX_TYPE]
msg = report[issues.JSON_INDEX_QUALIFIER]
return '{0}:{1}: {2}: {3}\n {4}{5}\n'.format(
filename,
line,
kind.lower(),
error_type,
' ' * indent,
msg,
)
def show_error_and_exit(err, show_help):
print(err)
if show_help:
@ -155,8 +139,12 @@ class Selector(object):
n = 0
n_length = len(str(len(self)))
for report in self.reports:
print(str(n).rjust(n_length) + '. ' +
describe_report(report, n_length + 2))
# the goal is to get the following output for each report:
# 1234. <first line of report #1234 goes here>
# <second line of report goes here>
msg = issues.text_of_report(report) \
.replace('\n', '\n%s' % ((n_length + 2) * ' '))
print('%s. %s\n' % (str(n).rjust(n_length), msg))
n += 1
def prompt_report(self):
@ -287,7 +275,7 @@ def get_remote_source_template():
def html_bug_trace(args, report, bug_id):
bug_trace = ''
bug_trace += '%s\n' % describe_report(report)
bug_trace += '%s\n' % issues.text_of_report(report)
tracer = Tracer(args)
tracer.build_report(report)
bug_trace += str(tracer)
@ -328,7 +316,7 @@ def html_list_of_bugs(args, remote_source_template, selector):
list_of_bugs = '<ol>'
for report in selector:
d = {
'description': describe_report(report, 2),
'description': issues.text_of_report(report),
'trace-url': url_of_bug_number(i),
'source-uri': source_uri(report),
}
@ -395,7 +383,7 @@ def main():
report = sel.prompt_report()
max_level = sel.prompt_level()
print(describe_report(report))
print(issues.text_of_report(report))
tracer = Tracer(args, max_level)
tracer.build_report(report)

@ -136,48 +136,61 @@ def clean_json(args, json_report):
shutil.move(temporary_file, json_report)
def print_and_save_errors(json_report, bugs_out):
errors = []
with codecs.open(json_report, 'r', encoding=config.LOCALE) as file_in:
errors = filter(lambda row: row[JSON_INDEX_KIND] in
[ISSUE_KIND_ERROR, ISSUE_KIND_WARNING],
json.load(file_in))
def text_of_report(report):
filename = report[JSON_INDEX_FILENAME]
kind = report[JSON_INDEX_KIND]
line = report[JSON_INDEX_LINE]
error_type = report[JSON_INDEX_TYPE]
msg = report[JSON_INDEX_QUALIFIER]
return u'%s:%d: %s: %s\n %s' % (
filename,
line,
kind.lower(),
error_type,
msg,
)
def _text_of_report_list(reports):
text_errors_list = []
for row in errors:
filename = row[JSON_INDEX_FILENAME]
if not os.path.isfile(filename):
continue
kind = row[JSON_INDEX_KIND]
line = row[JSON_INDEX_LINE]
error_type = row[JSON_INDEX_TYPE]
msg = row[JSON_INDEX_QUALIFIER]
source_context = source.build_source_context(filename,
source.TERMINAL_FORMATTER,
int(line))
for report in reports:
filename = report[JSON_INDEX_FILENAME]
line = report[JSON_INDEX_LINE]
source_context = source.build_source_context(
filename,
source.TERMINAL_FORMATTER,
line,
)
indenter = source.Indenter() \
.indent_push() \
.add(source_context)
source_context = unicode(indenter)
text_errors_list.append(u'%s:%d: %s: %s\n %s\n%s' % (
filename,
line,
kind.lower(),
error_type,
msg,
source_context,
))
text = '%s\n%s' % (text_of_report(report), source_context)
text_errors_list.append(text)
n_issues = len(text_errors_list)
if n_issues == 0:
return 'No issues found'
else:
msg = '\nFound %s\n' % utils.get_plural('issue', n_issues)
text_errors = '\n\n'.join(text_errors_list)
return msg + text_errors
def _is_user_visible(report):
filename = report[JSON_INDEX_FILENAME]
kind = report[JSON_INDEX_KIND]
return (os.path.isfile(filename) and
kind in [ISSUE_KIND_ERROR, ISSUE_KIND_WARNING])
def print_and_save_errors(json_report, bugs_out):
errors = utils.load_json_from_path(json_report)
errors = filter(_is_user_visible, errors)
text = _text_of_report_list(errors)
print(text)
with codecs.open(bugs_out, 'w', encoding=config.LOCALE) as file_out:
if n_issues == 0:
_print_and_write(file_out, 'No issues found')
else:
msg = '\nFound %s\n' % utils.get_plural('issue', n_issues)
_print_and_write(file_out, msg)
text_errors = '\n\n'.join(text_errors_list)
_print_and_write(file_out, text_errors)
file_out.write(text)
def _compare_issues(filename_1, line_1, filename_2, line_2):

Loading…
Cancel
Save