Remove the existing indentation from each line of a chunk of
    text, s, and then prefix each line with a new indent string.

    Also removes trailing whitespace from each line, and leading and
    trailing blank lines.

def _reindent(s, indent, reformat=True):
    """
    Remove the existing indentation from each line of a chunk of
    text, s, and then prefix each line with a new indent string.

    Also removes trailing whitespace from each line, and leading and
    trailing blank lines.

    """
    s = textwrap.dedent(s)
    s = s.split('\n')
    s = [x.rstrip() for x in s]
    while s and (not s[0]):
        s = s[1:]
    while s and (not s[-1]):
        s = s[:-1]
    if reformat:
        s = '\n'.join(s)
        s = textwrap.wrap(s, initial_indent=indent, subsequent_indent=indent)
    else:
        s = [indent + x for x in s]
    return '\n'.join(s) + '\n'