<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import json <EOL> import re <EOL> from tornado . util import PY3 , unicode_type , basestring_type <EOL> if PY3 : <EOL> from urllib . parse import parse_qs as _parse_qs <EOL> import html . entities as htmlentitydefs <EOL> import urllib . parse as urllib_parse <EOL> unichr = chr <EOL> else : <EOL> from urlparse import parse_qs as _parse_qs <EOL> import htmlentitydefs <EOL> import urllib as urllib_parse <EOL> try : <EOL> import typing <EOL> except ImportError : <EOL> pass <EOL> _XHTML_ESCAPE_RE = re . compile ( '<STR_LIT>' ) <EOL> _XHTML_ESCAPE_DICT = { '<STR_LIT:&>' : '<STR_LIT>' , '<STR_LIT:<>' : '<STR_LIT>' , '<STR_LIT:>>' : '<STR_LIT>' , '<STR_LIT:">' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> def xhtml_escape ( value ) : <EOL> """<STR_LIT>""" <EOL> return _XHTML_ESCAPE_RE . sub ( lambda match : _XHTML_ESCAPE_DICT [ match . group ( <NUM_LIT:0> ) ] , <EOL> to_basestring ( value ) ) <EOL> def xhtml_unescape ( value ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( r"<STR_LIT>" , _convert_entity , _unicode ( value ) ) <EOL> def json_encode ( value ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( value ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def json_decode ( value ) : <EOL> """<STR_LIT>""" <EOL> return json . loads ( to_basestring ( value ) ) <EOL> def squeeze ( value ) : <EOL> """<STR_LIT>""" <EOL> return re . sub ( r"<STR_LIT>" , "<STR_LIT:U+0020>" , value ) . strip ( ) <EOL> def url_escape ( value , plus = True ) : <EOL> """<STR_LIT>""" <EOL> quote = urllib_parse . quote_plus if plus else urllib_parse . quote <EOL> return quote ( utf8 ( value ) ) <EOL> if not PY3 : <EOL> def url_unescape ( value , encoding = '<STR_LIT:utf-8>' , plus = True ) : <EOL> """<STR_LIT>""" <EOL> unquote = ( urllib_parse . unquote_plus if plus else urllib_parse . unquote ) <EOL> if encoding is None : <EOL> return unquote ( utf8 ( value ) ) <EOL> else : <EOL> return unicode_type ( unquote ( utf8 ( value ) ) , encoding ) <EOL> parse_qs_bytes = _parse_qs <EOL> else : <EOL> def url_unescape ( value , encoding = '<STR_LIT:utf-8>' , plus = True ) : <EOL> """<STR_LIT>""" <EOL> if encoding is None : <EOL> if plus : <EOL> value = to_basestring ( value ) . replace ( '<STR_LIT:+>' , '<STR_LIT:U+0020>' ) <EOL> return urllib_parse . unquote_to_bytes ( value ) <EOL> else : <EOL> unquote = ( urllib_parse . unquote_plus if plus <EOL> else urllib_parse . unquote ) <EOL> return unquote ( to_basestring ( value ) , encoding = encoding ) <EOL> def parse_qs_bytes ( qs , keep_blank_values = False , strict_parsing = False ) : <EOL> """<STR_LIT>""" <EOL> result = _parse_qs ( qs , keep_blank_values , strict_parsing , <EOL> encoding = '<STR_LIT>' , errors = '<STR_LIT:strict>' ) <EOL> encoded = { } <EOL> for k , v in result . items ( ) : <EOL> encoded [ k ] = [ i . encode ( '<STR_LIT>' ) for i in v ] <EOL> return encoded <EOL> _UTF8_TYPES = ( bytes , type ( None ) ) <EOL> def utf8 ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _UTF8_TYPES ) : <EOL> return value <EOL> if not isinstance ( value , unicode_type ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % type ( value ) <EOL> ) <EOL> return value . encode ( "<STR_LIT:utf-8>" ) <EOL> _TO_UNICODE_TYPES = ( unicode_type , type ( None ) ) <EOL> def to_unicode ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _TO_UNICODE_TYPES ) : <EOL> return value <EOL> if not isinstance ( value , bytes ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % type ( value ) <EOL> ) <EOL> return value . decode ( "<STR_LIT:utf-8>" ) <EOL> _unicode = to_unicode <EOL> if str is unicode_type : <EOL> native_str = to_unicode <EOL> else : <EOL> native_str = utf8 <EOL> _BASESTRING_TYPES = ( basestring_type , type ( None ) ) <EOL> def to_basestring ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , _BASESTRING_TYPES ) : <EOL> return value <EOL> if not isinstance ( value , bytes ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % type ( value ) <EOL> ) <EOL> return value . decode ( "<STR_LIT:utf-8>" ) <EOL> def recursive_unicode ( obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , dict ) : <EOL> return dict ( ( recursive_unicode ( k ) , recursive_unicode ( v ) ) for ( k , v ) in obj . items ( ) ) <EOL> elif isinstance ( obj , list ) : <EOL> return list ( recursive_unicode ( i ) for i in obj ) <EOL> elif isinstance ( obj , tuple ) : <EOL> return tuple ( recursive_unicode ( i ) for i in obj ) <EOL> elif isinstance ( obj , bytes ) : <EOL> return to_unicode ( obj ) <EOL> else : <EOL> return obj <EOL> _URL_RE = re . compile ( to_unicode ( r"""<STR_LIT>""" ) ) <EOL> def linkify ( text , shorten = False , extra_params = "<STR_LIT>" , <EOL> require_protocol = False , permitted_protocols = [ "<STR_LIT:http>" , "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> if extra_params and not callable ( extra_params ) : <EOL> extra_params = "<STR_LIT:U+0020>" + extra_params . strip ( ) <EOL> def make_link ( m ) : <EOL> url = m . group ( <NUM_LIT:1> ) <EOL> proto = m . group ( <NUM_LIT:2> ) <EOL> if require_protocol and not proto : <EOL> return url <EOL> if proto and proto not in permitted_protocols : <EOL> return url <EOL> href = m . group ( <NUM_LIT:1> ) <EOL> if not proto : <EOL> href = "<STR_LIT>" + href <EOL> if callable ( extra_params ) : <EOL> params = "<STR_LIT:U+0020>" + extra_params ( href ) . strip ( ) <EOL> else : <EOL> params = extra_params <EOL> max_len = <NUM_LIT:30> <EOL> if shorten and len ( url ) > max_len : <EOL> before_clip = url <EOL> if proto : <EOL> proto_len = len ( proto ) + <NUM_LIT:1> + len ( m . group ( <NUM_LIT:3> ) or "<STR_LIT>" ) <EOL> else : <EOL> proto_len = <NUM_LIT:0> <EOL> parts = url [ proto_len : ] . split ( "<STR_LIT:/>" ) <EOL> if len ( parts ) > <NUM_LIT:1> : <EOL> url = url [ : proto_len ] + parts [ <NUM_LIT:0> ] + "<STR_LIT:/>" + parts [ <NUM_LIT:1> ] [ : <NUM_LIT:8> ] . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if len ( url ) > max_len * <NUM_LIT> : <EOL> url = url [ : max_len ] <EOL> if url != before_clip : <EOL> amp = url . rfind ( '<STR_LIT:&>' ) <EOL> if amp > max_len - <NUM_LIT:5> : <EOL> url = url [ : amp ] <EOL> url += "<STR_LIT>" <EOL> if len ( url ) >= len ( before_clip ) : <EOL> url = before_clip <EOL> else : <EOL> params += '<STR_LIT>' % href <EOL> return u'<STR_LIT>' % ( href , params , url ) <EOL> text = _unicode ( xhtml_escape ( text ) ) <EOL> return _URL_RE . sub ( make_link , text ) <EOL> def _convert_entity ( m ) : <EOL> if m . group ( <NUM_LIT:1> ) == "<STR_LIT:#>" : <EOL> try : <EOL> if m . group ( <NUM_LIT:2> ) [ : <NUM_LIT:1> ] . lower ( ) == '<STR_LIT:x>' : <EOL> return unichr ( int ( m . group ( <NUM_LIT:2> ) [ <NUM_LIT:1> : ] , <NUM_LIT:16> ) ) <EOL> else : <EOL> return unichr ( int ( m . group ( <NUM_LIT:2> ) ) ) <EOL> except ValueError : <EOL> return "<STR_LIT>" % m . group ( <NUM_LIT:2> ) <EOL> try : <EOL> return _HTML_UNICODE_MAP [ m . group ( <NUM_LIT:2> ) ] <EOL> except KeyError : <EOL> return "<STR_LIT>" % m . group ( <NUM_LIT:2> ) <EOL> def _build_unicode_map ( ) : <EOL> unicode_map = { } <EOL> for name , value in htmlentitydefs . name2codepoint . items ( ) : <EOL> unicode_map [ name ] = unichr ( value ) <EOL> return unicode_map <EOL> _HTML_UNICODE_MAP = _build_unicode_map ( ) </s>
