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