Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report txt: fix printing of non-ascii details #844

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions leapp/utils/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,38 @@ def importance(message):
return SEVERITY_LEVELS.get(message['severity'], 99)


def _treat_str(text):
"""
Ensure the given text is decoded.

This is problem in case of Py2 when non-asci characters are present.
"""
return text.decode('utf-8') if isinstance(text, six.binary_type) else text


def _report_detail_to_string(report_detail):
detail = u''
external_links = report_detail[ExternalLink.name] if ExternalLink.name in report_detail.keys() else None
remediation = Remediation.from_dict(report_detail)

if external_links:
external_links_text = u'Related links:'
external_links_text = u'Related links:\n'
for link in external_links:
external_links_text += u'\n - {}: {}'.format(link['title'], link['url'])
# Note(pstodulk): IRI could contain unicode characters. Even when it's
# still unexpected , let's reather treat it as well.
pirat89 marked this conversation as resolved.
Show resolved Hide resolved
external_links_text += u' - {}: {}\n'.format(
_treat_str(link['title']),
_treat_str(link['url'])
)
detail += external_links_text

if remediation:
# NOTE(ivasilev) Decoding is necessary in case of python2 as remediation is an encoded string,
# while io.open expects "true text" input. For python3 repr will return proper py3 str, no
# decoding will be needed.
# This hassle and clumsiness makes me sad, so suggestions are welcome.
remediation_text = '\nRemediation: {}\n'.format(remediation)
if isinstance(remediation_text, six.binary_type):
# This will be true for py2 where repr returns an encoded string
remediation_text = remediation_text.decode('utf-8')
detail += remediation_text
remediation_text = 'Remediation: {}\n'.format(remediation)
detail += _treat_str(remediation_text)

return detail

Expand All @@ -156,8 +167,7 @@ def generate_report_file(messages_to_report, context, path, report_schema='1.1.0
f.write(u'Risk Factor: {} {}\n'.format(message['severity'], flag))
f.write(u'Title: {}\n'.format(message['title']))
f.write(u'Summary: {}\n'.format(message['summary']))
report_detail = message.get('detail', {})
detail = _report_detail_to_string(report_detail)
detail = _report_detail_to_string(message.get('detail', {}))
f.write(detail)
if report_schema_tuple > (1, 0, 0):
# report-schema 1.0.0 doesn't have a stable report key
Expand Down
Loading