def wrap_text(text, width=78, initial_indent='', subsequent_indent='', preserve_paragraphs=False):
 from ._textwrap import TextWrapper
 text = text.expandtabs()
 wrapper = TextWrapper(width, initial_indent=initial_indent, subsequent_indent=subsequent_indent, replace_whitespace=False)
 if (not preserve_paragraphs):
  return wrapper.fill(text)
 p = []
 buf = []
 indent = None
 def _flush_par():
  if (not buf):
   return
  if (buf[0].strip() == '\x08'):
   p.append(((indent or 0), True, '\n'.join(buf[1:])))
  else:
   p.append(((indent or 0), False, ' '.join(buf)))
  del buf[:]
 for line in text.splitlines():
  if (not line):
   _flush_par()
   indent = None
  else:
   if (indent is None):
    orig_len = term_len(line)
    line = line.lstrip()
    indent = (orig_len - term_len(line))
   buf.append(line)
 _flush_par()
 rv = []
 for (indent, raw, text) in p:
  with wrapper.extra_indent((' ' * indent)):
   if raw:
    rv.append(wrapper.indent_only(text))
   else:
    rv.append(wrapper.fill(text))
 return '\n\n'.join(rv)

def wrap_text ( text , width = 78 , initial_indent = '' , subsequent_indent = '' , preserve_paragraphs = False ) : from . _textwrap import TextWrapper text = text . expandtabs ( ) wrapper = TextWrapper ( width , initial_indent = initial_indent , subsequent_indent = subsequent_indent , replace_whitespace = False ) if ( not preserve_paragraphs ) : return wrapper . fill ( text ) p = [ ] buf = [ ] indent = None def _flush_par ( ) : if ( not buf ) : return if ( buf [ 0 ] . strip ( ) == '\x08' ) : p . append ( ( ( indent or 0 ) , True , '\n' . join ( buf [ 1 : ] ) ) ) else : p . append ( ( ( indent or 0 ) , False , ' ' . join ( buf ) ) ) del buf [ : ] for line in text . splitlines ( ) : if ( not line ) : _flush_par ( ) indent = None else : if ( indent is None ) : orig_len = term_len ( line ) line = line . lstrip ( ) indent = ( orig_len - term_len ( line ) ) buf . append ( line ) _flush_par ( ) rv = [ ] for ( indent , raw , text ) in p : with wrapper . extra_indent ( ( ' ' * indent ) ) : if raw : rv . append ( wrapper . indent_only ( text ) ) else : rv . append ( wrapper . fill ( text ) ) return '\n\n' . join ( rv )
wrap_text -> [string] split text into multiple lines of no more than width characters each .