<s> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import unicode_literals <EOL> from __future__ import with_statement <EOL> from datetime import datetime <EOL> from . util import NetLocation <EOL> from . util import basestring <EOL> def hexIdFromHash ( thehash ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT:$>" + ( thehash + "<STR_LIT:=>" ) . decode ( "<STR_LIT>" ) . encode ( "<STR_LIT>" ) . upper ( ) <EOL> def hashFromHexId ( hexid ) : <EOL> """<STR_LIT>""" <EOL> if hexid [ <NUM_LIT:0> ] == '<STR_LIT:$>' : <EOL> hexid = hexid [ <NUM_LIT:1> : ] <EOL> return hexid . decode ( "<STR_LIT>" ) . encode ( "<STR_LIT>" ) [ : - <NUM_LIT:2> ] <EOL> class PortRange ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , a , b ) : <EOL> self . min = a <EOL> self . max = b <EOL> def __cmp__ ( self , b ) : <EOL> if b >= self . min and b <= self . max : <EOL> return <NUM_LIT:0> <EOL> return <NUM_LIT:1> <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . min , self . max ) <EOL> class Router ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , controller ) : <EOL> self . controller = controller <EOL> self . _flags = [ ] <EOL> self . bandwidth = <NUM_LIT:0> <EOL> self . name_is_unique = False <EOL> self . accepted_ports = None <EOL> self . rejected_ports = None <EOL> self . id_hex = None <EOL> self . _location = None <EOL> self . from_consensus = False <EOL> self . ip = '<STR_LIT>' <EOL> self . ip_v6 = [ ] <EOL> unique_name = property ( lambda x : x . name_is_unique and x . name or x . id_hex ) <EOL> "<STR_LIT>" <EOL> @ property <EOL> def modified ( self ) : <EOL> if self . _modified is None : <EOL> self . _modified = datetime . strptime ( <EOL> self . _modified_unparsed , <EOL> '<STR_LIT>' <EOL> ) <EOL> return self . _modified <EOL> def update ( self , name , idhash , orhash , modified , ip , orport , dirport ) : <EOL> self . name = name <EOL> self . id_hash = idhash <EOL> self . or_hash = orhash <EOL> self . _modified_unparsed = modified <EOL> self . _modified = None <EOL> self . ip = ip <EOL> self . or_port = orport <EOL> self . dir_port = dirport <EOL> self . _location = None <EOL> self . id_hex = hexIdFromHash ( self . id_hash ) <EOL> @ property <EOL> def location ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _location : <EOL> return self . _location <EOL> if self . ip != '<STR_LIT>' : <EOL> self . _location = NetLocation ( self . ip ) <EOL> else : <EOL> self . _location = NetLocation ( None ) <EOL> if not self . _location . countrycode and self . ip != '<STR_LIT>' : <EOL> d = self . controller . get_info_raw ( '<STR_LIT>' + self . ip ) <EOL> d . addCallback ( self . _set_country ) <EOL> d . addErrback ( lambda _ : None ) <EOL> return self . _location <EOL> @ property <EOL> def flags ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _flags <EOL> @ flags . setter <EOL> def flags ( self , flags ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( flags , basestring ) : <EOL> flags = flags . split ( ) <EOL> self . _flags = [ x . lower ( ) for x in flags ] <EOL> self . name_is_unique = '<STR_LIT>' in self . _flags <EOL> @ property <EOL> def bandwidth ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _bandwidth <EOL> @ bandwidth . setter <EOL> def bandwidth ( self , bw ) : <EOL> self . _bandwidth = int ( bw ) <EOL> @ property <EOL> def policy ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . accepted_ports : <EOL> return '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( map ( str , self . accepted_ports ) ) <EOL> elif self . rejected_ports : <EOL> return '<STR_LIT>' + '<STR_LIT:U+002C>' . join ( map ( str , self . rejected_ports ) ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ policy . setter <EOL> def policy ( self , args ) : <EOL> """<STR_LIT>""" <EOL> word = args [ <NUM_LIT:0> ] <EOL> if word == '<STR_LIT>' : <EOL> self . accepted_ports = None <EOL> self . rejected_ports = [ ] <EOL> target = self . rejected_ports <EOL> elif word == '<STR_LIT>' : <EOL> self . accepted_ports = [ ] <EOL> self . rejected_ports = None <EOL> target = self . accepted_ports <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" % word ) <EOL> for port in args [ <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) : <EOL> if '<STR_LIT:->' in port : <EOL> ( a , b ) = port . split ( '<STR_LIT:->' ) <EOL> target . append ( PortRange ( int ( a ) , int ( b ) ) ) <EOL> else : <EOL> target . append ( int ( port ) ) <EOL> def accepts_port ( self , port ) : <EOL> """<STR_LIT>""" <EOL> if self . rejected_ports is None and self . accepted_ports is None : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if self . rejected_ports : <EOL> for x in self . rejected_ports : <EOL> if port == x : <EOL> return False <EOL> return True <EOL> for x in self . accepted_ports : <EOL> if port == x : <EOL> return True <EOL> return False <EOL> def _set_country ( self , c ) : <EOL> """<STR_LIT>""" <EOL> self . location . countrycode = c . split ( ) [ <NUM_LIT:0> ] . split ( '<STR_LIT:=>' ) [ <NUM_LIT:1> ] . strip ( ) . upper ( ) <EOL> def __repr__ ( self ) : <EOL> n = self . id_hex <EOL> if self . name_is_unique : <EOL> n = self . name <EOL> return "<STR_LIT>" % ( n , self . location . countrycode , <EOL> self . policy ) </s>
