<s> from collections import namedtuple <EOL> from . . exceptions import LocationParseError <EOL> url_attrs = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:host>' , '<STR_LIT:port>' , '<STR_LIT:path>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Url ( namedtuple ( '<STR_LIT>' , url_attrs ) ) : <EOL> """<STR_LIT>""" <EOL> slots = ( ) <EOL> def __new__ ( cls , scheme = None , auth = None , host = None , port = None , path = None , <EOL> query = None , fragment = None ) : <EOL> return super ( Url , cls ) . __new__ ( cls , scheme , auth , host , port , path , <EOL> query , fragment ) <EOL> @ property <EOL> def hostname ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . host <EOL> @ property <EOL> def request_uri ( self ) : <EOL> """<STR_LIT>""" <EOL> uri = self . path or '<STR_LIT:/>' <EOL> if self . query is not None : <EOL> uri += '<STR_LIT:?>' + self . query <EOL> return uri <EOL> @ property <EOL> def netloc ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . port : <EOL> return '<STR_LIT>' % ( self . host , self . port ) <EOL> return self . host <EOL> @ property <EOL> def url ( self ) : <EOL> """<STR_LIT>""" <EOL> scheme , auth , host , port , path , query , fragment = self <EOL> url = '<STR_LIT>' <EOL> if scheme is not None : <EOL> url += scheme + '<STR_LIT>' <EOL> if auth is not None : <EOL> url += auth + '<STR_LIT:@>' <EOL> if host is not None : <EOL> url += host <EOL> if port is not None : <EOL> url += '<STR_LIT::>' + str ( port ) <EOL> if path is not None : <EOL> url += path <EOL> if query is not None : <EOL> url += '<STR_LIT:?>' + query <EOL> if fragment is not None : <EOL> url += '<STR_LIT:#>' + fragment <EOL> return url <EOL> def __str__ ( self ) : <EOL> return self . url <EOL> def split_first ( s , delims ) : <EOL> """<STR_LIT>""" <EOL> min_idx = None <EOL> min_delim = None <EOL> for d in delims : <EOL> idx = s . find ( d ) <EOL> if idx < <NUM_LIT:0> : <EOL> continue <EOL> if min_idx is None or idx < min_idx : <EOL> min_idx = idx <EOL> min_delim = d <EOL> if min_idx is None or min_idx < <NUM_LIT:0> : <EOL> return s , '<STR_LIT>' , None <EOL> return s [ : min_idx ] , s [ min_idx + <NUM_LIT:1> : ] , min_delim <EOL> def parse_url ( url ) : <EOL> """<STR_LIT>""" <EOL> if not url : <EOL> return Url ( ) <EOL> scheme = None <EOL> auth = None <EOL> host = None <EOL> port = None <EOL> path = None <EOL> fragment = None <EOL> query = None <EOL> if '<STR_LIT>' in url : <EOL> scheme , url = url . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> url , path_ , delim = split_first ( url , [ '<STR_LIT:/>' , '<STR_LIT:?>' , '<STR_LIT:#>' ] ) <EOL> if delim : <EOL> path = delim + path_ <EOL> if '<STR_LIT:@>' in url : <EOL> auth , url = url . rsplit ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if url and url [ <NUM_LIT:0> ] == '<STR_LIT:[>' : <EOL> host , url = url . split ( '<STR_LIT:]>' , <NUM_LIT:1> ) <EOL> host += '<STR_LIT:]>' <EOL> if '<STR_LIT::>' in url : <EOL> _host , port = url . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if not host : <EOL> host = _host <EOL> if port : <EOL> if not port . isdigit ( ) : <EOL> raise LocationParseError ( url ) <EOL> port = int ( port ) <EOL> else : <EOL> port = None <EOL> elif not host and url : <EOL> host = url <EOL> if not path : <EOL> return Url ( scheme , auth , host , port , path , query , fragment ) <EOL> if '<STR_LIT:#>' in path : <EOL> path , fragment = path . split ( '<STR_LIT:#>' , <NUM_LIT:1> ) <EOL> if '<STR_LIT:?>' in path : <EOL> path , query = path . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> return Url ( scheme , auth , host , port , path , query , fragment ) <EOL> def get_host ( url ) : <EOL> """<STR_LIT>""" <EOL> p = parse_url ( url ) <EOL> return p . scheme or '<STR_LIT:http>' , p . hostname , p . port </s>
