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.') 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): def show_error_and_exit(err, show_help):
print(err) print(err)
if show_help: if show_help:
@ -155,8 +139,12 @@ class Selector(object):
n = 0 n = 0
n_length = len(str(len(self))) n_length = len(str(len(self)))
for report in self.reports: for report in self.reports:
print(str(n).rjust(n_length) + '. ' + # the goal is to get the following output for each report:
describe_report(report, n_length + 2)) # 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 n += 1
def prompt_report(self): def prompt_report(self):
@ -287,7 +275,7 @@ def get_remote_source_template():
def html_bug_trace(args, report, bug_id): def html_bug_trace(args, report, bug_id):
bug_trace = '' bug_trace = ''
bug_trace += '%s\n' % describe_report(report) bug_trace += '%s\n' % issues.text_of_report(report)
tracer = Tracer(args) tracer = Tracer(args)
tracer.build_report(report) tracer.build_report(report)
bug_trace += str(tracer) bug_trace += str(tracer)
@ -328,7 +316,7 @@ def html_list_of_bugs(args, remote_source_template, selector):
list_of_bugs = '<ol>' list_of_bugs = '<ol>'
for report in selector: for report in selector:
d = { d = {
'description': describe_report(report, 2), 'description': issues.text_of_report(report),
'trace-url': url_of_bug_number(i), 'trace-url': url_of_bug_number(i),
'source-uri': source_uri(report), 'source-uri': source_uri(report),
} }
@ -395,7 +383,7 @@ def main():
report = sel.prompt_report() report = sel.prompt_report()
max_level = sel.prompt_level() max_level = sel.prompt_level()
print(describe_report(report)) print(issues.text_of_report(report))
tracer = Tracer(args, max_level) tracer = Tracer(args, max_level)
tracer.build_report(report) tracer.build_report(report)

@ -136,48 +136,61 @@ def clean_json(args, json_report):
shutil.move(temporary_file, json_report) shutil.move(temporary_file, json_report)
def print_and_save_errors(json_report, bugs_out): def text_of_report(report):
errors = [] filename = report[JSON_INDEX_FILENAME]
with codecs.open(json_report, 'r', encoding=config.LOCALE) as file_in: kind = report[JSON_INDEX_KIND]
errors = filter(lambda row: row[JSON_INDEX_KIND] in line = report[JSON_INDEX_LINE]
[ISSUE_KIND_ERROR, ISSUE_KIND_WARNING], error_type = report[JSON_INDEX_TYPE]
json.load(file_in)) 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 = [] text_errors_list = []
for row in errors: for report in reports:
filename = row[JSON_INDEX_FILENAME] filename = report[JSON_INDEX_FILENAME]
if not os.path.isfile(filename): line = report[JSON_INDEX_LINE]
continue source_context = source.build_source_context(
filename,
kind = row[JSON_INDEX_KIND] source.TERMINAL_FORMATTER,
line = row[JSON_INDEX_LINE] line,
error_type = row[JSON_INDEX_TYPE] )
msg = row[JSON_INDEX_QUALIFIER]
source_context = source.build_source_context(filename,
source.TERMINAL_FORMATTER,
int(line))
indenter = source.Indenter() \ indenter = source.Indenter() \
.indent_push() \ .indent_push() \
.add(source_context) .add(source_context)
source_context = unicode(indenter) source_context = unicode(indenter)
text_errors_list.append(u'%s:%d: %s: %s\n %s\n%s' % ( text = '%s\n%s' % (text_of_report(report), source_context)
filename, text_errors_list.append(text)
line,
kind.lower(),
error_type,
msg,
source_context,
))
n_issues = len(text_errors_list) 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: with codecs.open(bugs_out, 'w', encoding=config.LOCALE) as file_out:
if n_issues == 0: file_out.write(text)
_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)
def _compare_issues(filename_1, line_1, filename_2, line_2): def _compare_issues(filename_1, line_1, filename_2, line_2):

Loading…
Cancel
Save