<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import string , re <EOL> try : <EOL> _unicode = unicode <EOL> except NameError : <EOL> class _unicode ( object ) : <EOL> pass <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _whitespace = '<STR_LIT>' <EOL> class TextWrapper : <EOL> """<STR_LIT>""" <EOL> whitespace_trans = string . maketrans ( _whitespace , '<STR_LIT:U+0020>' * len ( _whitespace ) ) <EOL> unicode_whitespace_trans = { } <EOL> uspace = ord ( u'<STR_LIT:U+0020>' ) <EOL> for x in map ( ord , _whitespace ) : <EOL> unicode_whitespace_trans [ x ] = uspace <EOL> wordsep_re = re . compile ( <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> wordsep_simple_re = re . compile ( r'<STR_LIT>' ) <EOL> sentence_end_re = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> % string . lowercase ) <EOL> def __init__ ( self , <EOL> width = <NUM_LIT> , <EOL> initial_indent = "<STR_LIT>" , <EOL> subsequent_indent = "<STR_LIT>" , <EOL> expand_tabs = True , <EOL> replace_whitespace = True , <EOL> fix_sentence_endings = False , <EOL> break_long_words = True , <EOL> drop_whitespace = True , <EOL> break_on_hyphens = True ) : <EOL> self . width = width <EOL> self . initial_indent = initial_indent <EOL> self . subsequent_indent = subsequent_indent <EOL> self . expand_tabs = expand_tabs <EOL> self . replace_whitespace = replace_whitespace <EOL> self . fix_sentence_endings = fix_sentence_endings <EOL> self . break_long_words = break_long_words <EOL> self . drop_whitespace = drop_whitespace <EOL> self . break_on_hyphens = break_on_hyphens <EOL> self . wordsep_re_uni = re . compile ( self . wordsep_re . pattern , re . U ) <EOL> self . wordsep_simple_re_uni = re . compile ( <EOL> self . wordsep_simple_re . pattern , re . U ) <EOL> def _munge_whitespace ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if self . expand_tabs : <EOL> text = text . expandtabs ( ) <EOL> if self . replace_whitespace : <EOL> if isinstance ( text , str ) : <EOL> text = text . translate ( self . whitespace_trans ) <EOL> elif isinstance ( text , _unicode ) : <EOL> text = text . translate ( self . unicode_whitespace_trans ) <EOL> return text <EOL> def _split ( self , text ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( text , _unicode ) : <EOL> if self . break_on_hyphens : <EOL> pat = self . wordsep_re_uni <EOL> else : <EOL> pat = self . wordsep_simple_re_uni <EOL> else : <EOL> if self . break_on_hyphens : <EOL> pat = self . wordsep_re <EOL> else : <EOL> pat = self . wordsep_simple_re <EOL> chunks = pat . split ( text ) <EOL> chunks = filter ( None , chunks ) <EOL> return chunks <EOL> def _fix_sentence_endings ( self , chunks ) : <EOL> """<STR_LIT>""" <EOL> i = <NUM_LIT:0> <EOL> patsearch = self . sentence_end_re . search <EOL> while i < len ( chunks ) - <NUM_LIT:1> : <EOL> if chunks [ i + <NUM_LIT:1> ] == "<STR_LIT:U+0020>" and patsearch ( chunks [ i ] ) : <EOL> chunks [ i + <NUM_LIT:1> ] = "<STR_LIT:U+0020>" <EOL> i += <NUM_LIT:2> <EOL> else : <EOL> i += <NUM_LIT:1> <EOL> def _handle_long_word ( self , reversed_chunks , cur_line , cur_len , width ) : <EOL> """<STR_LIT>""" <EOL> if width < <NUM_LIT:1> : <EOL> space_left = <NUM_LIT:1> <EOL> else : <EOL> space_left = width - cur_len <EOL> if self . break_long_words : <EOL> cur_line . append ( reversed_chunks [ - <NUM_LIT:1> ] [ : space_left ] ) <EOL> reversed_chunks [ - <NUM_LIT:1> ] = reversed_chunks [ - <NUM_LIT:1> ] [ space_left : ] <EOL> elif not cur_line : <EOL> cur_line . append ( reversed_chunks . pop ( ) ) <EOL> def _wrap_chunks ( self , chunks ) : <EOL> """<STR_LIT>""" <EOL> lines = [ ] <EOL> if self . width <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" % self . width ) <EOL> chunks . reverse ( ) <EOL> while chunks : <EOL> cur_line = [ ] <EOL> cur_len = <NUM_LIT:0> <EOL> if lines : <EOL> indent = self . subsequent_indent <EOL> else : <EOL> indent = self . initial_indent <EOL> width = self . width - len ( indent ) <EOL> if self . drop_whitespace and chunks [ - <NUM_LIT:1> ] . strip ( ) == '<STR_LIT>' and lines : <EOL> del chunks [ - <NUM_LIT:1> ] <EOL> while chunks : <EOL> l = len ( chunks [ - <NUM_LIT:1> ] ) <EOL> if cur_len + l <= width : <EOL> cur_line . append ( chunks . pop ( ) ) <EOL> cur_len += l <EOL> else : <EOL> break <EOL> if chunks and len ( chunks [ - <NUM_LIT:1> ] ) > width : <EOL> self . _handle_long_word ( chunks , cur_line , cur_len , width ) <EOL> if self . drop_whitespace and cur_line and cur_line [ - <NUM_LIT:1> ] . strip ( ) == '<STR_LIT>' : <EOL> del cur_line [ - <NUM_LIT:1> ] <EOL> if cur_line : <EOL> lines . append ( indent + '<STR_LIT>' . join ( cur_line ) ) <EOL> return lines <EOL> def wrap ( self , text ) : <EOL> """<STR_LIT>""" <EOL> text = self . _munge_whitespace ( text ) <EOL> chunks = self . _split ( text ) <EOL> if self . fix_sentence_endings : <EOL> self . _fix_sentence_endings ( chunks ) <EOL> return self . _wrap_chunks ( chunks ) <EOL> def fill ( self , text ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:\n>" . join ( self . wrap ( text ) ) <EOL> def wrap ( text , width = <NUM_LIT> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> w = TextWrapper ( width = width , ** kwargs ) <EOL> return w . wrap ( text ) <EOL> def fill ( text , width = <NUM_LIT> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> w = TextWrapper ( width = width , ** kwargs ) <EOL> return w . fill ( text ) <EOL> _whitespace_only_re = re . compile ( '<STR_LIT>' , re . MULTILINE ) <EOL> _leading_whitespace_re = re . compile ( '<STR_LIT>' , re . MULTILINE ) <EOL> def dedent ( text ) : <EOL> """<STR_LIT>""" <EOL> margin = None <EOL> text = _whitespace_only_re . sub ( '<STR_LIT>' , text ) <EOL> indents = _leading_whitespace_re . findall ( text ) <EOL> for indent in indents : <EOL> if margin is None : <EOL> margin = indent <EOL> elif indent . startswith ( margin ) : <EOL> pass <EOL> elif margin . startswith ( indent ) : <EOL> margin = indent <EOL> else : <EOL> margin = "<STR_LIT>" <EOL> break <EOL> if <NUM_LIT:0> and margin : <EOL> for line in text . split ( "<STR_LIT:\n>" ) : <EOL> assert not line or line . startswith ( margin ) , "<STR_LIT>" % ( line , margin ) <EOL> if margin : <EOL> text = re . sub ( r'<STR_LIT>' + margin , '<STR_LIT>' , text ) <EOL> return text <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print dedent ( "<STR_LIT>" ) </s>
