Re-factor dedent to fix warts

- textwarp.dedent does not support bytes on py3
- make sure to preserve trailing newlines
This commit is contained in:
Daniel A. Wozniak 2018-08-24 10:46:50 -07:00 committed by rallytime
parent b5034067f8
commit 0e18b157e3
No known key found for this signature in database
GPG key ID: E8F1A4B90D0DEA19

View file

@ -1611,8 +1611,11 @@ def dedent(text, linesep=os.linesep):
'''
A wrapper around textwrap.dedent that also sets line endings.
'''
if isinstance(text, six.text_type):
linesep = salt.utils.to_unicode(linesep)
else:
linesep = salt.utils.to_str(linesep)
return linesep.join(textwrap.dedent(text).splitlines())
linesep = salt.utils.to_unicode(linesep)
unicode_text = textwrap.dedent(salt.utils.to_unicode(text))
clean_text = linesep.join(unicode_text.splitlines())
if unicode_text.endswith(salt.utils.to_unicode('\n')):
clean_text = clean_text + linesep
if not isinstance(text, six.text_type):
return salt.utils.to_bytes(clean_text)
return clean_text