<s> from __future__ import unicode_literals <EOL> import re <EOL> import unicodedata <EOL> from gzip import GzipFile <EOL> from io import BytesIO <EOL> from django . utils import six <EOL> from django . utils . encoding import force_text <EOL> from django . utils . functional import SimpleLazyObject , keep_lazy , keep_lazy_text <EOL> from django . utils . safestring import SafeText , mark_safe <EOL> from django . utils . six . moves import html_entities <EOL> from django . utils . translation import pgettext , ugettext as _ , ugettext_lazy <EOL> if six . PY2 : <EOL> from django . utils . encoding import force_unicode <EOL> def capfirst ( x ) : <EOL> return x and force_text ( x ) [ <NUM_LIT:0> ] . upper ( ) + force_text ( x ) [ <NUM_LIT:1> : ] <EOL> capfirst = keep_lazy_text ( capfirst ) <EOL> re_words = re . compile ( r'<STR_LIT>' , re . U | re . S ) <EOL> re_chars = re . compile ( r'<STR_LIT>' , re . U | re . S ) <EOL> re_tag = re . compile ( r'<STR_LIT>' , re . S ) <EOL> re_newlines = re . compile ( r'<STR_LIT>' ) <EOL> re_camel_case = re . compile ( r'<STR_LIT>' ) <EOL> @ keep_lazy_text <EOL> def wrap ( text , width ) : <EOL> """<STR_LIT>""" <EOL> text = force_text ( text ) <EOL> def _generator ( ) : <EOL> for line in text . splitlines ( True ) : <EOL> max_width = min ( ( line . endswith ( '<STR_LIT:\n>' ) and width + <NUM_LIT:1> or width ) , width ) <EOL> while len ( line ) > max_width : <EOL> space = line [ : max_width + <NUM_LIT:1> ] . rfind ( '<STR_LIT:U+0020>' ) + <NUM_LIT:1> <EOL> if space == <NUM_LIT:0> : <EOL> space = line . find ( '<STR_LIT:U+0020>' ) + <NUM_LIT:1> <EOL> if space == <NUM_LIT:0> : <EOL> yield line <EOL> line = '<STR_LIT>' <EOL> break <EOL> yield '<STR_LIT>' % line [ : space - <NUM_LIT:1> ] <EOL> line = line [ space : ] <EOL> max_width = min ( ( line . endswith ( '<STR_LIT:\n>' ) and width + <NUM_LIT:1> or width ) , width ) <EOL> if line : <EOL> yield line <EOL> return '<STR_LIT>' . join ( _generator ( ) ) <EOL> class Truncator ( SimpleLazyObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , text ) : <EOL> super ( Truncator , self ) . __init__ ( lambda : force_text ( text ) ) <EOL> def add_truncation_text ( self , text , truncate = None ) : <EOL> if truncate is None : <EOL> truncate = pgettext ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> truncate = force_text ( truncate ) <EOL> if '<STR_LIT>' in truncate : <EOL> return truncate % { '<STR_LIT>' : text } <EOL> if text . endswith ( truncate ) : <EOL> return text <EOL> return '<STR_LIT>' % ( text , truncate ) <EOL> def chars ( self , num , truncate = None , html = False ) : <EOL> """<STR_LIT>""" <EOL> self . _setup ( ) <EOL> length = int ( num ) <EOL> text = unicodedata . normalize ( '<STR_LIT>' , self . _wrapped ) <EOL> truncate_len = length <EOL> for char in self . add_truncation_text ( '<STR_LIT>' , truncate ) : <EOL> if not unicodedata . combining ( char ) : <EOL> truncate_len -= <NUM_LIT:1> <EOL> if truncate_len == <NUM_LIT:0> : <EOL> break <EOL> if html : <EOL> return self . _truncate_html ( length , truncate , text , truncate_len , False ) <EOL> return self . _text_chars ( length , truncate , text , truncate_len ) <EOL> def _text_chars ( self , length , truncate , text , truncate_len ) : <EOL> """<STR_LIT>""" <EOL> s_len = <NUM_LIT:0> <EOL> end_index = None <EOL> for i , char in enumerate ( text ) : <EOL> if unicodedata . combining ( char ) : <EOL> continue <EOL> s_len += <NUM_LIT:1> <EOL> if end_index is None and s_len > truncate_len : <EOL> end_index = i <EOL> if s_len > length : <EOL> return self . add_truncation_text ( text [ : end_index or <NUM_LIT:0> ] , <EOL> truncate ) <EOL> return text <EOL> def words ( self , num , truncate = None , html = False ) : <EOL> """<STR_LIT>""" <EOL> self . _setup ( ) <EOL> length = int ( num ) <EOL> if html : <EOL> return self . _truncate_html ( length , truncate , self . _wrapped , length , True ) <EOL> return self . _text_words ( length , truncate ) <EOL> def _text_words ( self , length , truncate ) : <EOL> """<STR_LIT>""" <EOL> words = self . _wrapped . split ( ) <EOL> if len ( words ) > length : <EOL> words = words [ : length ] <EOL> return self . add_truncation_text ( '<STR_LIT:U+0020>' . join ( words ) , truncate ) <EOL> return '<STR_LIT:U+0020>' . join ( words ) <EOL> def _truncate_html ( self , length , truncate , text , truncate_len , words ) : <EOL> """<STR_LIT>""" <EOL> if words and length <= <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> html4_singlets = ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:input>' <EOL> ) <EOL> pos = <NUM_LIT:0> <EOL> end_text_pos = <NUM_LIT:0> <EOL> current_len = <NUM_LIT:0> <EOL> open_tags = [ ] <EOL> regex = re_words if words else re_chars <EOL> while current_len <= length : <EOL> m = regex . search ( text , pos ) <EOL> if not m : <EOL> break <EOL> pos = m . end ( <NUM_LIT:0> ) <EOL> if m . group ( <NUM_LIT:1> ) : <EOL> current_len += <NUM_LIT:1> <EOL> if current_len == truncate_len : <EOL> end_text_pos = pos <EOL> continue <EOL> tag = re_tag . match ( m . group ( <NUM_LIT:0> ) ) <EOL> if not tag or current_len >= truncate_len : <EOL> continue <EOL> closing_tag , tagname , self_closing = tag . groups ( ) <EOL> tagname = tagname . lower ( ) <EOL> if self_closing or tagname in html4_singlets : <EOL> pass <EOL> elif closing_tag : <EOL> try : <EOL> i = open_tags . index ( tagname ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> open_tags = open_tags [ i + <NUM_LIT:1> : ] <EOL> else : <EOL> open_tags . insert ( <NUM_LIT:0> , tagname ) <EOL> if current_len <= length : <EOL> return text <EOL> out = text [ : end_text_pos ] <EOL> truncate_text = self . add_truncation_text ( '<STR_LIT>' , truncate ) <EOL> if truncate_text : <EOL> out += truncate_text <EOL> for tag in open_tags : <EOL> out += '<STR_LIT>' % tag <EOL> return out <EOL> @ keep_lazy_text <EOL> def get_valid_filename ( s ) : <EOL> """<STR_LIT>""" <EOL> s = force_text ( s ) . strip ( ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) <EOL> return re . sub ( r'<STR_LIT>' , '<STR_LIT>' , s ) <EOL> @ keep_lazy_text <EOL> def get_text_list ( list_ , last_word = ugettext_lazy ( '<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> if len ( list_ ) == <NUM_LIT:0> : <EOL> return '<STR_LIT>' <EOL> if len ( list_ ) == <NUM_LIT:1> : <EOL> return force_text ( list_ [ <NUM_LIT:0> ] ) <EOL> return '<STR_LIT>' % ( <EOL> _ ( '<STR_LIT:U+002CU+0020>' ) . join ( force_text ( i ) for i in list_ [ : - <NUM_LIT:1> ] ) , <EOL> force_text ( last_word ) , force_text ( list_ [ - <NUM_LIT:1> ] ) ) <EOL> @ keep_lazy_text <EOL> def normalize_newlines ( text ) : <EOL> """<STR_LIT>""" <EOL> text = force_text ( text ) <EOL> return re_newlines . sub ( '<STR_LIT:\n>' , text ) <EOL> @ keep_lazy_text <EOL> def phone2numeric ( phone ) : <EOL> """<STR_LIT>""" <EOL> char2number = { <EOL> '<STR_LIT:a>' : '<STR_LIT:2>' , '<STR_LIT:b>' : '<STR_LIT:2>' , '<STR_LIT:c>' : '<STR_LIT:2>' , '<STR_LIT:d>' : '<STR_LIT:3>' , '<STR_LIT:e>' : '<STR_LIT:3>' , '<STR_LIT:f>' : '<STR_LIT:3>' , '<STR_LIT:g>' : '<STR_LIT:4>' , <EOL> '<STR_LIT:h>' : '<STR_LIT:4>' , '<STR_LIT:i>' : '<STR_LIT:4>' , '<STR_LIT>' : '<STR_LIT:5>' , '<STR_LIT:k>' : '<STR_LIT:5>' , '<STR_LIT:l>' : '<STR_LIT:5>' , '<STR_LIT:m>' : '<STR_LIT>' , '<STR_LIT:n>' : '<STR_LIT>' , <EOL> '<STR_LIT:o>' : '<STR_LIT>' , '<STR_LIT:p>' : '<STR_LIT>' , '<STR_LIT:q>' : '<STR_LIT>' , '<STR_LIT:r>' : '<STR_LIT>' , '<STR_LIT:s>' : '<STR_LIT>' , '<STR_LIT:t>' : '<STR_LIT>' , '<STR_LIT:u>' : '<STR_LIT>' , <EOL> '<STR_LIT:v>' : '<STR_LIT>' , '<STR_LIT:w>' : '<STR_LIT>' , '<STR_LIT:x>' : '<STR_LIT>' , '<STR_LIT:y>' : '<STR_LIT>' , '<STR_LIT:z>' : '<STR_LIT>' , <EOL> } <EOL> return '<STR_LIT>' . join ( char2number . get ( c , c ) for c in phone . lower ( ) ) <EOL> def compress_string ( s ) : <EOL> zbuf = BytesIO ( ) <EOL> zfile = GzipFile ( mode = '<STR_LIT:wb>' , compresslevel = <NUM_LIT:6> , fileobj = zbuf ) <EOL> zfile . write ( s ) <EOL> zfile . close ( ) <EOL> return zbuf . getvalue ( ) <EOL> class StreamingBuffer ( object ) : <EOL> def __init__ ( self ) : <EOL> self . vals = [ ] <EOL> def write ( self , val ) : <EOL> self . vals . append ( val ) <EOL> def read ( self ) : <EOL> if not self . vals : <EOL> return b'<STR_LIT>' <EOL> ret = b'<STR_LIT>' . join ( self . vals ) <EOL> self . vals = [ ] <EOL> return ret <EOL> def flush ( self ) : <EOL> return <EOL> def close ( self ) : <EOL> return <EOL> def compress_sequence ( sequence ) : <EOL> buf = StreamingBuffer ( ) <EOL> zfile = GzipFile ( mode = '<STR_LIT:wb>' , compresslevel = <NUM_LIT:6> , fileobj = buf ) <EOL> yield buf . read ( ) <EOL> for item in sequence : <EOL> zfile . write ( item ) <EOL> data = buf . read ( ) <EOL> if data : <EOL> yield data <EOL> zfile . close ( ) <EOL> yield buf . read ( ) <EOL> smart_split_re = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> def smart_split ( text ) : <EOL> r"""<STR_LIT>""" <EOL> text = force_text ( text ) <EOL> for bit in smart_split_re . finditer ( text ) : <EOL> yield bit . group ( <NUM_LIT:0> ) <EOL> def _replace_entity ( match ) : <EOL> text = match . group ( <NUM_LIT:1> ) <EOL> if text [ <NUM_LIT:0> ] == '<STR_LIT:#>' : <EOL> text = text [ <NUM_LIT:1> : ] <EOL> try : <EOL> if text [ <NUM_LIT:0> ] in '<STR_LIT>' : <EOL> c = int ( text [ <NUM_LIT:1> : ] , <NUM_LIT:16> ) <EOL> else : <EOL> c = int ( text ) <EOL> return six . unichr ( c ) <EOL> except ValueError : <EOL> return match . group ( <NUM_LIT:0> ) <EOL> else : <EOL> try : <EOL> return six . unichr ( html_entities . name2codepoint [ text ] ) <EOL> except ( ValueError , KeyError ) : <EOL> return match . group ( <NUM_LIT:0> ) <EOL> _entity_re = re . compile ( r"<STR_LIT>" ) <EOL> @ keep_lazy_text <EOL> def unescape_entities ( text ) : <EOL> return _entity_re . sub ( _replace_entity , force_text ( text ) ) <EOL> @ keep_lazy_text <EOL> def unescape_string_literal ( s ) : <EOL> r"""<STR_LIT>""" <EOL> if s [ <NUM_LIT:0> ] not in "<STR_LIT>" or s [ - <NUM_LIT:1> ] != s [ <NUM_LIT:0> ] : <EOL> raise ValueError ( "<STR_LIT>" % s ) <EOL> quote = s [ <NUM_LIT:0> ] <EOL> return s [ <NUM_LIT:1> : - <NUM_LIT:1> ] . replace ( r'<STR_LIT>' % quote , quote ) . replace ( r'<STR_LIT:\\>' , '<STR_LIT:\\>' ) <EOL> @ keep_lazy ( six . text_type , SafeText ) <EOL> def slugify ( value , allow_unicode = False ) : <EOL> """<STR_LIT>""" <EOL> value = force_text ( value ) <EOL> if allow_unicode : <EOL> value = unicodedata . normalize ( '<STR_LIT>' , value ) <EOL> value = re . sub ( '<STR_LIT>' , '<STR_LIT>' , value , flags = re . U ) . strip ( ) . lower ( ) <EOL> return mark_safe ( re . sub ( '<STR_LIT>' , '<STR_LIT:->' , value , flags = re . U ) ) <EOL> value = unicodedata . normalize ( '<STR_LIT>' , value ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) . decode ( '<STR_LIT:ascii>' ) <EOL> value = re . sub ( '<STR_LIT>' , '<STR_LIT>' , value ) . strip ( ) . lower ( ) <EOL> return mark_safe ( re . sub ( '<STR_LIT>' , '<STR_LIT:->' , value ) ) <EOL> def camel_case_to_spaces ( value ) : <EOL> """<STR_LIT>""" <EOL> return re_camel_case . sub ( r'<STR_LIT>' , value ) . strip ( ) . lower ( ) </s>
