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