Skip to content

Commit

Permalink
Merge pull request #10870 from rhmdnd/sanitize-yaml-lines-for-generat…
Browse files Browse the repository at this point in the history
…e-profile-script

Sanitize lines for clean YAML output when generating profiles
  • Loading branch information
Mab879 authored Jul 21, 2023
2 parents 4bfaefb + 58922f0 commit a96ccb9
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion utils/generate_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ class LiteralUnicode(str):


def literal_unicode_representer(dumper, data):
return dumper.represent_scalar(u'tag:yaml.org,2002:str', data, style='|')
# NOTE(rhmdnd): pyyaml will not format a string using the style we define for the scalar below
# if any strings in the data end with a space (e.g., 'some text ' instead of 'some text'). This
# has been reported upstream in https://github.com/yaml/pyyaml/issues/121. This particular code
# goes through every line of data and strips any whitespace characters from the end of the
# string, and reconstructs the string with newlines so that it will format properly.
text = [line.rstrip() for line in data.splitlines()]
sanitized = '\n'.join(text)
return dumper.represent_scalar(u'tag:yaml.org,2002:str', sanitized, style='|')


yaml.add_representer(LiteralUnicode, literal_unicode_representer)
Expand Down

0 comments on commit a96ccb9

Please sign in to comment.