<s> import sys <EOL> import os <EOL> import random <EOL> class View : <EOL> def __init__ ( self , caller , err = sys . stderr , out = sys . stdout , exit = sys . exit , <EOL> execvp = os . execvp ) : <EOL> self . caller = caller <EOL> self . err = err <EOL> self . out = out <EOL> self . exit = exit <EOL> self . execvp = execvp <EOL> def stderr ( self , msg ) : <EOL> self . err . write ( "<STR_LIT>" . format ( msg ) ) <EOL> def stdout ( self , msg ) : <EOL> self . out . write ( "<STR_LIT>" . format ( msg ) ) <EOL> def help ( self ) : <EOL> self . stderr ( "<STR_LIT>" . format ( self . caller ) ) <EOL> self . exit ( <NUM_LIT:1> ) <EOL> def valid_targets ( self , targets ) : <EOL> for key in sorted ( [ key for key in targets ] ) : <EOL> self . stdout ( key ) <EOL> self . exit ( <NUM_LIT:0> ) <EOL> def invalid_target ( self , attempted_target ) : <EOL> self . stderr ( "<STR_LIT>" . format ( attempted_target ) ) <EOL> self . exit ( <NUM_LIT:1> ) <EOL> def connect_to ( self , selected_ip , all_ips , target ) : <EOL> self . stderr ( "<STR_LIT>" . format ( len ( all_ips ) , target ) ) <EOL> for ip in all_ips : <EOL> if ip == selected_ip : <EOL> self . stderr ( "<STR_LIT>" . format ( ip ) ) <EOL> else : <EOL> self . stderr ( "<STR_LIT>" . format ( ip ) ) <EOL> cmd = '<STR_LIT>' <EOL> arguments = [ cmd , selected_ip ] <EOL> arguments . extend ( sys . argv [ <NUM_LIT:2> : ] ) <EOL> self . execvp ( cmd , arguments ) <EOL> self . exit ( <NUM_LIT:2> ) <EOL> class InputHandler : <EOL> def __init__ ( self , argv , targets , ui = View ) : <EOL> self . argv = argv <EOL> self . ui = ui ( argv [ <NUM_LIT:0> ] ) <EOL> self . targets = targets <EOL> def handle_argv ( self ) : <EOL> if len ( self . argv ) == <NUM_LIT:1> : <EOL> self . ui . help ( ) <EOL> else : <EOL> arg1 = self . argv [ <NUM_LIT:1> ] <EOL> if arg1 == '<STR_LIT>' : <EOL> self . ui . valid_targets ( self . targets ) <EOL> elif arg1 in self . targets : <EOL> ips = sorted ( self . targets [ arg1 ] ) <EOL> selected_ip = random . choice ( ips ) <EOL> self . ui . connect_to ( selected_ip , ips , arg1 ) <EOL> else : <EOL> self . ui . invalid_target ( arg1 ) </s>
