_errors.py 967 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # coding: utf-8
  2. """
  3. Helper for formatting exception messages. Exports the following items:
  4. - unwrap()
  5. """
  6. from __future__ import unicode_literals, division, absolute_import, print_function
  7. import re
  8. import textwrap
  9. def unwrap(string, *params):
  10. """
  11. Takes a multi-line string and does the following:
  12. - dedents
  13. - converts newlines with text before and after into a single line
  14. - strips leading and trailing whitespace
  15. :param string:
  16. The string to format
  17. :param *params:
  18. Params to interpolate into the string
  19. :return:
  20. The formatted string
  21. """
  22. output = textwrap.dedent(string)
  23. # Unwrap lines, taking into account bulleted lists, ordered lists and
  24. # underlines consisting of = signs
  25. if output.find('\n') != -1:
  26. output = re.sub('(?<=\\S)\n(?=[^ \n\t\\d\\*\\-=])', ' ', output)
  27. if params:
  28. output = output % params
  29. output = output.strip()
  30. return output