<s> import logging <EOL> from . utils . osx import loopback <EOL> from . utils import tunnel <EOL> from . utils import hosts <EOL> from . utils import is_osx <EOL> from . exceptions import MoxieException <EOL> class Route ( object ) : <EOL> def __init__ ( self , destination , local_address , ports , proxy ) : <EOL> self . destination = destination <EOL> self . local_address = local_address <EOL> self . ports = ports <EOL> self . proxy = proxy <EOL> def is_valid ( self ) : <EOL> return self . destination and self . proxy and len ( self . ports ) > <NUM_LIT:0> <EOL> def start ( self ) : <EOL> if is_osx ( ) : <EOL> loopback . add ( self . local_address ) <EOL> for port in self . ports : <EOL> tunnel . start ( self . local_address , port , self . destination , self . proxy ) <EOL> hosts . set ( self . destination , self . local_address ) <EOL> logging . info ( "<STR_LIT>" , self . destination , '<STR_LIT:U+002C>' . join ( map ( str , self . ports ) ) , self . proxy ) <EOL> def status ( self , port ) : <EOL> if is_osx ( ) : <EOL> has_local_address = self . local_address in loopback . list_addresses ( ) <EOL> else : <EOL> has_local_address = True <EOL> hosts_mapping = hosts . get ( self . destination ) <EOL> is_tunnel_running = tunnel . status ( self . local_address , port , self . destination , self . proxy ) <EOL> if not is_tunnel_running and hosts_mapping != self . local_address : <EOL> return False <EOL> elif not has_local_address : <EOL> raise MoxieException ( "<STR_LIT>" . format ( self . local_address ) ) <EOL> elif not is_tunnel_running : <EOL> raise MoxieException ( "<STR_LIT>" ) <EOL> elif hosts_mapping != self . local_address : <EOL> raise MoxieException ( "<STR_LIT>" . format ( self . destination , self . local_address ) ) <EOL> else : <EOL> return True <EOL> def stop ( self ) : <EOL> if is_osx ( ) : <EOL> loopback . remove ( self . local_address ) <EOL> for port in self . ports : <EOL> tunnel . stop ( self . local_address , port , self . destination , self . proxy ) <EOL> hosts . remove ( self . destination ) <EOL> logging . info ( "<STR_LIT>" , self . destination , '<STR_LIT:U+002C>' . join ( map ( str , self . ports ) ) , self . proxy ) <EOL> def __getstate__ ( self ) : <EOL> return { <EOL> '<STR_LIT>' : self . destination , <EOL> '<STR_LIT>' : self . ports , <EOL> '<STR_LIT>' : self . proxy <EOL> } </s>
