<s> """<STR_LIT>""" <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> uses_relative = [ '<STR_LIT>' , '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> uses_netloc = [ '<STR_LIT>' , '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> non_hierarchical = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> uses_params = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:http>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> uses_query = [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> uses_fragment = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:file>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> scheme_chars = ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> MAX_CACHE_SIZE = <NUM_LIT:20> <EOL> _parse_cache = { } <EOL> def clear_cache ( ) : <EOL> """<STR_LIT>""" <EOL> _parse_cache . clear ( ) <EOL> class ResultMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def username ( self ) : <EOL> netloc = self . netloc <EOL> if "<STR_LIT:@>" in netloc : <EOL> userinfo = netloc . rsplit ( "<STR_LIT:@>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if "<STR_LIT::>" in userinfo : <EOL> userinfo = userinfo . split ( "<STR_LIT::>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> return userinfo <EOL> return None <EOL> @ property <EOL> def password ( self ) : <EOL> netloc = self . netloc <EOL> if "<STR_LIT:@>" in netloc : <EOL> userinfo = netloc . rsplit ( "<STR_LIT:@>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> if "<STR_LIT::>" in userinfo : <EOL> return userinfo . split ( "<STR_LIT::>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> return None <EOL> @ property <EOL> def hostname ( self ) : <EOL> netloc = self . netloc . split ( '<STR_LIT:@>' ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT:[>' in netloc and '<STR_LIT:]>' in netloc : <EOL> return netloc . split ( '<STR_LIT:]>' ) [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] . lower ( ) <EOL> elif '<STR_LIT::>' in netloc : <EOL> return netloc . split ( '<STR_LIT::>' ) [ <NUM_LIT:0> ] . lower ( ) <EOL> elif netloc == '<STR_LIT>' : <EOL> return None <EOL> else : <EOL> return netloc . lower ( ) <EOL> @ property <EOL> def port ( self ) : <EOL> netloc = self . netloc . split ( '<STR_LIT:@>' ) [ - <NUM_LIT:1> ] . split ( '<STR_LIT:]>' ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT::>' in netloc : <EOL> port = netloc . split ( '<STR_LIT::>' ) [ <NUM_LIT:1> ] <EOL> return int ( port , <NUM_LIT:10> ) <EOL> else : <EOL> return None <EOL> from collections import namedtuple <EOL> class SplitResult ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) , ResultMixin ) : <EOL> __slots__ = ( ) <EOL> def geturl ( self ) : <EOL> return urlunsplit ( self ) <EOL> class ParseResult ( namedtuple ( '<STR_LIT>' , '<STR_LIT>' ) , ResultMixin ) : <EOL> __slots__ = ( ) <EOL> def geturl ( self ) : <EOL> return urlunparse ( self ) <EOL> def urlparse ( url , scheme = '<STR_LIT>' , allow_fragments = True ) : <EOL> """<STR_LIT>""" <EOL> tuple = urlsplit ( url , scheme , allow_fragments ) <EOL> scheme , netloc , url , query , fragment = tuple <EOL> if scheme in uses_params and '<STR_LIT:;>' in url : <EOL> url , params = _splitparams ( url ) <EOL> else : <EOL> params = '<STR_LIT>' <EOL> return ParseResult ( scheme , netloc , url , params , query , fragment ) <EOL> def _splitparams ( url ) : <EOL> if '<STR_LIT:/>' in url : <EOL> i = url . find ( '<STR_LIT:;>' , url . rfind ( '<STR_LIT:/>' ) ) <EOL> if i < <NUM_LIT:0> : <EOL> return url , '<STR_LIT>' <EOL> else : <EOL> i = url . find ( '<STR_LIT:;>' ) <EOL> return url [ : i ] , url [ i + <NUM_LIT:1> : ] <EOL> def _splitnetloc ( url , start = <NUM_LIT:0> ) : <EOL> delim = len ( url ) <EOL> for c in '<STR_LIT>' : <EOL> wdelim = url . find ( c , start ) <EOL> if wdelim >= <NUM_LIT:0> : <EOL> delim = min ( delim , wdelim ) <EOL> return url [ start : delim ] , url [ delim : ] <EOL> def urlsplit ( url , scheme = '<STR_LIT>' , allow_fragments = True ) : <EOL> """<STR_LIT>""" <EOL> allow_fragments = bool ( allow_fragments ) <EOL> key = url , scheme , allow_fragments , type ( url ) , type ( scheme ) <EOL> cached = _parse_cache . get ( key , None ) <EOL> if cached : <EOL> return cached <EOL> if len ( _parse_cache ) >= MAX_CACHE_SIZE : <EOL> clear_cache ( ) <EOL> netloc = query = fragment = '<STR_LIT>' <EOL> i = url . find ( '<STR_LIT::>' ) <EOL> if i > <NUM_LIT:0> : <EOL> if url [ : i ] == '<STR_LIT:http>' : <EOL> scheme = url [ : i ] . lower ( ) <EOL> url = url [ i + <NUM_LIT:1> : ] <EOL> if url [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> netloc , url = _splitnetloc ( url , <NUM_LIT:2> ) <EOL> if ( ( '<STR_LIT:[>' in netloc and '<STR_LIT:]>' not in netloc ) or <EOL> ( '<STR_LIT:]>' in netloc and '<STR_LIT:[>' not in netloc ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if allow_fragments and '<STR_LIT:#>' in url : <EOL> url , fragment = url . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:?>' in url : <EOL> url , query = url . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> v = SplitResult ( scheme , netloc , url , query , fragment ) <EOL> _parse_cache [ key ] = v <EOL> return v <EOL> for c in url [ : i ] : <EOL> if c not in scheme_chars : <EOL> break <EOL> else : <EOL> try : <EOL> _testportnum = int ( url [ i + <NUM_LIT:1> : ] ) <EOL> except ValueError : <EOL> scheme , url = url [ : i ] . lower ( ) , url [ i + <NUM_LIT:1> : ] <EOL> if url [ : <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> netloc , url = _splitnetloc ( url , <NUM_LIT:2> ) <EOL> if ( ( '<STR_LIT:[>' in netloc and '<STR_LIT:]>' not in netloc ) or <EOL> ( '<STR_LIT:]>' in netloc and '<STR_LIT:[>' not in netloc ) ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if allow_fragments and scheme in uses_fragment and '<STR_LIT:#>' in url : <EOL> url , fragment = url . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) <EOL> if scheme in uses_query and '<STR_LIT:?>' in url : <EOL> url , query = url . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> v = SplitResult ( scheme , netloc , url , query , fragment ) <EOL> _parse_cache [ key ] = v <EOL> return v <EOL> def urlunparse ( data ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , url , params , query , fragment = data <EOL> if params : <EOL> url = "<STR_LIT>" % ( url , params ) <EOL> return urlunsplit ( ( scheme , netloc , url , query , fragment ) ) <EOL> def urlunsplit ( data ) : <EOL> """<STR_LIT>""" <EOL> scheme , netloc , url , query , fragment = data <EOL> if netloc or ( scheme and scheme in uses_netloc and url [ : <NUM_LIT:2> ] != '<STR_LIT>' ) : <EOL> if url and url [ : <NUM_LIT:1> ] != '<STR_LIT:/>' : url = '<STR_LIT:/>' + url <EOL> url = '<STR_LIT>' + ( netloc or '<STR_LIT>' ) + url <EOL> if scheme : <EOL> url = scheme + '<STR_LIT::>' + url <EOL> if query : <EOL> url = url + '<STR_LIT:?>' + query <EOL> if fragment : <EOL> url = url + '<STR_LIT:#>' + fragment <EOL> return url <EOL> def urljoin ( base , url , allow_fragments = True ) : <EOL> """<STR_LIT>""" <EOL> if not base : <EOL> return url <EOL> if not url : <EOL> return base <EOL> bscheme , bnetloc , bpath , bparams , bquery , bfragment = urlparse ( base , '<STR_LIT>' , allow_fragments ) <EOL> scheme , netloc , path , params , query , fragment = urlparse ( url , bscheme , allow_fragments ) <EOL> if scheme != bscheme or scheme not in uses_relative : <EOL> return url <EOL> if scheme in uses_netloc : <EOL> if netloc : <EOL> return urlunparse ( ( scheme , netloc , path , <EOL> params , query , fragment ) ) <EOL> netloc = bnetloc <EOL> if path [ : <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> return urlunparse ( ( scheme , netloc , path , <EOL> params , query , fragment ) ) <EOL> if not path and not params : <EOL> path = bpath <EOL> params = bparams <EOL> if not query : <EOL> query = bquery <EOL> return urlunparse ( ( scheme , netloc , path , <EOL> params , query , fragment ) ) <EOL> segments = bpath . split ( '<STR_LIT:/>' ) [ : - <NUM_LIT:1> ] + path . split ( '<STR_LIT:/>' ) <EOL> if segments [ - <NUM_LIT:1> ] == '<STR_LIT:.>' : <EOL> segments [ - <NUM_LIT:1> ] = '<STR_LIT>' <EOL> while '<STR_LIT:.>' in segments : <EOL> segments . remove ( '<STR_LIT:.>' ) <EOL> while <NUM_LIT:1> : <EOL> i = <NUM_LIT:1> <EOL> n = len ( segments ) - <NUM_LIT:1> <EOL> while i < n : <EOL> if ( segments [ i ] == '<STR_LIT:..>' <EOL> and segments [ i - <NUM_LIT:1> ] not in ( '<STR_LIT>' , '<STR_LIT:..>' ) ) : <EOL> del segments [ i - <NUM_LIT:1> : i + <NUM_LIT:1> ] <EOL> break <EOL> i = i + <NUM_LIT:1> <EOL> else : <EOL> break <EOL> if segments == [ '<STR_LIT>' , '<STR_LIT:..>' ] : <EOL> segments [ - <NUM_LIT:1> ] = '<STR_LIT>' <EOL> elif len ( segments ) >= <NUM_LIT:2> and segments [ - <NUM_LIT:1> ] == '<STR_LIT:..>' : <EOL> segments [ - <NUM_LIT:2> : ] = [ '<STR_LIT>' ] <EOL> return urlunparse ( ( scheme , netloc , '<STR_LIT:/>' . join ( segments ) , <EOL> params , query , fragment ) ) <EOL> def urldefrag ( url ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:#>' in url : <EOL> s , n , p , a , q , frag = urlparse ( url ) <EOL> defrag = urlunparse ( ( s , n , p , a , q , '<STR_LIT>' ) ) <EOL> return defrag , frag <EOL> else : <EOL> return url , '<STR_LIT>' <EOL> _hexdig = '<STR_LIT>' <EOL> _hextochr = dict ( ( a + b , chr ( int ( a + b , <NUM_LIT:16> ) ) ) <EOL> for a in _hexdig for b in _hexdig ) <EOL> def unquote ( s ) : <EOL> """<STR_LIT>""" <EOL> res = s . split ( '<STR_LIT:%>' ) <EOL> if len ( res ) == <NUM_LIT:1> : <EOL> return s <EOL> s = res [ <NUM_LIT:0> ] <EOL> for item in res [ <NUM_LIT:1> : ] : <EOL> try : <EOL> s += _hextochr [ item [ : <NUM_LIT:2> ] ] + item [ <NUM_LIT:2> : ] <EOL> except KeyError : <EOL> s += '<STR_LIT:%>' + item <EOL> except UnicodeDecodeError : <EOL> s += unichr ( int ( item [ : <NUM_LIT:2> ] , <NUM_LIT:16> ) ) + item [ <NUM_LIT:2> : ] <EOL> return s <EOL> def parse_qs ( qs , keep_blank_values = <NUM_LIT:0> , strict_parsing = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> dict = { } <EOL> for name , value in parse_qsl ( qs , keep_blank_values , strict_parsing ) : <EOL> if name in dict : <EOL> dict [ name ] . append ( value ) <EOL> else : <EOL> dict [ name ] = [ value ] <EOL> return dict <EOL> def parse_qsl ( qs , keep_blank_values = <NUM_LIT:0> , strict_parsing = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> pairs = [ s2 for s1 in qs . split ( '<STR_LIT:&>' ) for s2 in s1 . split ( '<STR_LIT:;>' ) ] <EOL> r = [ ] <EOL> for name_value in pairs : <EOL> if not name_value and not strict_parsing : <EOL> continue <EOL> nv = name_value . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( nv ) != <NUM_LIT:2> : <EOL> if strict_parsing : <EOL> raise ValueError , "<STR_LIT>" % ( name_value , ) <EOL> if keep_blank_values : <EOL> nv . append ( '<STR_LIT>' ) <EOL> else : <EOL> continue <EOL> if len ( nv [ <NUM_LIT:1> ] ) or keep_blank_values : <EOL> name = unquote ( nv [ <NUM_LIT:0> ] . replace ( '<STR_LIT:+>' , '<STR_LIT:U+0020>' ) ) <EOL> value = unquote ( nv [ <NUM_LIT:1> ] . replace ( '<STR_LIT:+>' , '<STR_LIT:U+0020>' ) ) <EOL> r . append ( ( name , value ) ) <EOL> return r </s>
