<s> from SipConf import SipConf <EOL> from urllib import quote , unquote <EOL> class SipURL ( object ) : <EOL> username = None <EOL> userparams = None <EOL> password = None <EOL> host = None <EOL> port = None <EOL> headers = None <EOL> usertype = None <EOL> transport = None <EOL> ttl = None <EOL> maddr = None <EOL> method = None <EOL> tag = None <EOL> other = None <EOL> lr = False <EOL> def __init__ ( self , url = None , username = None , password = None , host = None , port = None , headers = None , usertype = None , transport = None , ttl = None , maddr = None , method = None , tag = None , other = None , userparams = None , lr = False , relaxedparser = False ) : <EOL> self . other = [ ] <EOL> self . userparams = [ ] <EOL> if url == None : <EOL> self . username = username <EOL> if userparams != None : <EOL> self . userparams = userparams <EOL> self . password = password <EOL> if host == None : <EOL> self . host = SipConf . my_address <EOL> self . port = SipConf . my_port <EOL> else : <EOL> self . host = host <EOL> self . port = port <EOL> self . headers = headers <EOL> self . usertype = usertype <EOL> self . transport = transport <EOL> self . ttl = ttl <EOL> self . maddr = maddr <EOL> self . method = method <EOL> self . tag = tag <EOL> if other != None : <EOL> self . other = other <EOL> self . lr = lr <EOL> return <EOL> sidx = url . find ( '<STR_LIT::>' ) <EOL> if sidx == <NUM_LIT:3> or ( sidx != - <NUM_LIT:1> and sidx < url . find ( '<STR_LIT:.>' ) ) : <EOL> if not url . lower ( ) . startswith ( '<STR_LIT>' ) : <EOL> raise ValueError ( '<STR_LIT>' + url [ : <NUM_LIT:4> ] ) <EOL> url = url [ <NUM_LIT:4> : ] <EOL> ear = url . find ( '<STR_LIT:@>' ) + <NUM_LIT:1> <EOL> parts = url [ ear : ] . split ( '<STR_LIT:;>' ) <EOL> userdomain , params = url [ <NUM_LIT:0> : ear ] + parts [ <NUM_LIT:0> ] , parts [ <NUM_LIT:1> : ] <EOL> if len ( params ) == <NUM_LIT:0> and '<STR_LIT:?>' in userdomain : <EOL> self . headers = { } <EOL> userdomain , headers = userdomain . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> for header in headers . split ( '<STR_LIT:&>' ) : <EOL> k , v = header . split ( '<STR_LIT:=>' ) <EOL> self . headers [ k . lower ( ) ] = unquote ( v ) <EOL> udparts = userdomain . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> if len ( udparts ) == <NUM_LIT:2> : <EOL> userpass , hostport = udparts <EOL> upparts = userpass . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( upparts ) > <NUM_LIT:1> : <EOL> self . password = upparts [ <NUM_LIT:1> ] <EOL> uparts = upparts [ <NUM_LIT:0> ] . split ( '<STR_LIT:;>' ) <EOL> if len ( uparts ) > <NUM_LIT:1> : <EOL> self . userparams = uparts [ <NUM_LIT:1> : ] <EOL> self . username = unquote ( uparts [ <NUM_LIT:0> ] ) <EOL> else : <EOL> hostport = udparts [ <NUM_LIT:0> ] <EOL> if relaxedparser and len ( hostport ) == <NUM_LIT:0> : <EOL> self . host = '<STR_LIT>' <EOL> elif hostport [ <NUM_LIT:0> ] == '<STR_LIT:[>' : <EOL> hpparts = hostport . split ( '<STR_LIT:]>' , <NUM_LIT:1> ) <EOL> self . host = hpparts [ <NUM_LIT:0> ] + '<STR_LIT:]>' <EOL> if len ( hpparts [ <NUM_LIT:1> ] ) > <NUM_LIT:0> : <EOL> hpparts = hpparts [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( hpparts ) > <NUM_LIT:1> : <EOL> self . port = int ( hpparts [ <NUM_LIT:1> ] ) <EOL> else : <EOL> hpparts = hostport . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( hpparts ) == <NUM_LIT:1> : <EOL> self . host = hpparts [ <NUM_LIT:0> ] <EOL> else : <EOL> self . host = hpparts [ <NUM_LIT:0> ] <EOL> try : <EOL> self . port = int ( hpparts [ <NUM_LIT:1> ] ) <EOL> except Exception , e : <EOL> portparts = hpparts [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> if len ( portparts ) != <NUM_LIT:2> or portparts [ <NUM_LIT:0> ] != portparts [ <NUM_LIT:1> ] : <EOL> raise e <EOL> self . port = int ( portparts [ <NUM_LIT:0> ] ) <EOL> for p in params : <EOL> if p == params [ - <NUM_LIT:1> ] and '<STR_LIT:?>' in p : <EOL> self . headers = { } <EOL> p , headers = p . split ( '<STR_LIT:?>' , <NUM_LIT:1> ) <EOL> for header in headers . split ( '<STR_LIT:&>' ) : <EOL> k , v = header . split ( '<STR_LIT:=>' ) <EOL> self . headers [ k . lower ( ) ] = unquote ( v ) <EOL> nv = p . split ( '<STR_LIT:=>' , <NUM_LIT:1> ) <EOL> if len ( nv ) == <NUM_LIT:1> : <EOL> if p == '<STR_LIT>' : <EOL> self . lr = True <EOL> else : <EOL> self . other . append ( p ) <EOL> continue <EOL> name , value = nv <EOL> if name == '<STR_LIT:user>' : <EOL> self . usertype = value <EOL> elif name == '<STR_LIT>' : <EOL> self . transport = value <EOL> elif name == '<STR_LIT>' : <EOL> self . ttl = int ( value ) <EOL> elif name == '<STR_LIT>' : <EOL> self . maddr = value <EOL> elif name == '<STR_LIT>' : <EOL> self . method = value <EOL> elif name == '<STR_LIT>' : <EOL> self . tag = value <EOL> elif name == '<STR_LIT>' : <EOL> self . lr = True <EOL> else : <EOL> self . other . append ( p ) <EOL> def __str__ ( self ) : <EOL> return self . localStr ( ) <EOL> def localStr ( self , local_addr = None , local_port = None ) : <EOL> l = [ ] ; w = l . append <EOL> w ( '<STR_LIT>' ) <EOL> if self . username != None : <EOL> w ( self . username ) <EOL> for v in self . userparams : <EOL> w ( '<STR_LIT>' % v ) <EOL> if self . password != None : <EOL> w ( '<STR_LIT>' % self . password ) <EOL> w ( '<STR_LIT:@>' ) <EOL> if local_addr != None and '<STR_LIT>' in dir ( self . host ) : <EOL> w ( local_addr ) <EOL> else : <EOL> w ( str ( self . host ) ) <EOL> if self . port != None : <EOL> if local_port != None and '<STR_LIT>' in dir ( self . port ) : <EOL> w ( '<STR_LIT>' % local_port ) <EOL> else : <EOL> w ( '<STR_LIT>' % self . port ) <EOL> if self . usertype != None : <EOL> w ( '<STR_LIT>' % self . usertype ) <EOL> for n in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> v = getattr ( self , n ) <EOL> if v != None : <EOL> w ( '<STR_LIT>' % ( n , v ) ) <EOL> if self . lr : <EOL> w ( '<STR_LIT>' ) <EOL> for v in self . other : <EOL> w ( '<STR_LIT>' % v ) <EOL> if self . headers : <EOL> w ( '<STR_LIT:?>' ) <EOL> w ( '<STR_LIT:&>' . join ( [ ( '<STR_LIT>' % ( h . capitalize ( ) , quote ( v ) ) ) for ( h , v ) in self . headers . items ( ) ] ) ) <EOL> return '<STR_LIT>' . join ( l ) <EOL> def getCopy ( self ) : <EOL> return SipURL ( username = self . username , password = self . password , host = self . host , port = self . port , headers = self . headers , usertype = self . usertype , transport = self . transport , ttl = self . ttl , maddr = self . maddr , method = self . method , tag = self . tag , other = list ( self . other ) , userparams = list ( self . userparams ) , lr = self . lr ) <EOL> def getHost ( self ) : <EOL> return self . host <EOL> def getPort ( self ) : <EOL> if self . port != None : <EOL> return self . port <EOL> else : <EOL> return SipConf . default_port <EOL> def getAddr ( self ) : <EOL> if self . port != None : <EOL> return ( self . host , self . port ) <EOL> else : <EOL> return ( self . host , SipConf . default_port ) <EOL> def setAddr ( self , addr ) : <EOL> self . host , self . port = addr </s>
