<s> """<STR_LIT>""" <EOL> import cmd <EOL> import sys <EOL> import os <EOL> import bot . client . ctrl_client as ctrl_client_mod <EOL> import bot . client . sub_client as sub_client_mod <EOL> class CLI ( cmd . Cmd ) : <EOL> """<STR_LIT>""" <EOL> prompt = "<STR_LIT>" <EOL> def __init__ ( self , ctrl_addr , sub_addr ) : <EOL> """<STR_LIT>""" <EOL> cmd . Cmd . __init__ ( self ) <EOL> try : <EOL> self . ctrl_client = ctrl_client_mod . CtrlClient ( ctrl_addr ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" . format ( ctrl_addr , e ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> try : <EOL> self . sub_client = sub_client_mod . SubClient ( sub_addr ) <EOL> except Exception , e : <EOL> print "<STR_LIT>" . format ( sub_addr , e ) <EOL> sys . exit ( - <NUM_LIT:1> ) <EOL> def default ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> obj_name , _ , rest = raw_args . partition ( "<STR_LIT:U+0020>" ) <EOL> if obj_name in self . ctrl_client . objects : <EOL> method_name , _ , params = rest . partition ( "<STR_LIT:U+0020>" ) <EOL> if method_name in self . ctrl_client . objects [ obj_name ] : <EOL> try : <EOL> param_dict = { } <EOL> for param in params . split ( ) : <EOL> key , value = param . split ( "<STR_LIT::>" ) <EOL> try : <EOL> if "<STR_LIT:.>" in value : <EOL> value = float ( value ) <EOL> else : <EOL> value = int ( value ) <EOL> except ValueError : <EOL> if value == "<STR_LIT:True>" : <EOL> value = True <EOL> elif value == "<STR_LIT:False>" : <EOL> value = False <EOL> elif value . startswith ( "<STR_LIT:'>" ) and value . endswith ( "<STR_LIT:'>" ) : <EOL> value = value [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> param_dict [ key ] = value <EOL> except IndexError : <EOL> print "<STR_LIT>" <EOL> return <EOL> except ValueError : <EOL> print "<STR_LIT>" <EOL> return <EOL> result = self . ctrl_client . call ( <EOL> obj_name , method_name , param_dict ) <EOL> print "<STR_LIT>" , result <EOL> else : <EOL> print "<STR_LIT>" , method_name <EOL> else : <EOL> print "<STR_LIT>" , obj_name <EOL> def completenames ( self , text , * ignored ) : <EOL> """<STR_LIT>""" <EOL> cmd_match_names = cmd . Cmd . completenames ( self , text , * ignored ) <EOL> obj_names = self . ctrl_client . objects . keys ( ) <EOL> api_match_names = [ x for x in obj_names if x . startswith ( text ) ] <EOL> return cmd_match_names + api_match_names <EOL> def completedefault ( self , text , line , begidx , endidx ) : <EOL> """<STR_LIT>""" <EOL> obj , _ , rest = line . partition ( "<STR_LIT:U+0020>" ) <EOL> if obj in self . ctrl_client . objects : <EOL> method , _ , params = rest . strip ( ) . partition ( "<STR_LIT:U+0020>" ) <EOL> if method == text : <EOL> method_names = self . ctrl_client . objects [ obj ] <EOL> match_names = [ x for x in method_names if x . startswith ( text ) ] <EOL> return match_names <EOL> def do_list ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> print <EOL> print "<STR_LIT>" <EOL> print <EOL> for obj_name , methods in sorted ( self . ctrl_client . objects . items ( ) ) : <EOL> print "<STR_LIT>" . format ( obj_name ) <EOL> for method in methods : <EOL> print "<STR_LIT>" . format ( method ) <EOL> print <EOL> def help_list ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT:list>" <EOL> print "<STR_LIT>" <EOL> def do_ping ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> reply_time = self . ctrl_client . ping ( ) <EOL> print "<STR_LIT>" . format ( reply_time ) <EOL> def help_ping ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_sub_add ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> topic = raw_args . split ( ) [ <NUM_LIT:0> ] <EOL> except ( ValueError , IndexError ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> self . sub_client . add_topic ( topic ) <EOL> def help_sub_add ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_sub_del ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> topic = raw_args . split ( ) [ <NUM_LIT:0> ] <EOL> except ( ValueError , IndexError ) : <EOL> print "<STR_LIT>" <EOL> return <EOL> self . sub_client . del_topic ( topic ) <EOL> def help_sub_del ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_sub ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> self . sub_client . print_msgs ( ) <EOL> def help_sub ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_stop ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> self . ctrl_client . stop_full ( ) <EOL> def help_stop ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_kill ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> self . ctrl_client . exit_server ( ) <EOL> def help_kill ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_die ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> self . ctrl_client . clean_up ( ) <EOL> self . sub_client . clean_up ( ) <EOL> print "<STR_LIT>" <EOL> return True <EOL> def help_die ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_shell ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> os . system ( cmd ) <EOL> def help_shell ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def do_EOF ( self , raw_args ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> self . ctrl_client . clean_up ( ) <EOL> self . sub_client . clean_up ( ) <EOL> print "<STR_LIT>" <EOL> return True <EOL> def help_EOF ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> def help_help ( self ) : <EOL> """<STR_LIT>""" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> if len ( sys . argv ) == <NUM_LIT:1> : <EOL> print "<STR_LIT>" <EOL> CLI ( "<STR_LIT>" , "<STR_LIT>" ) . cmdloop ( ) <EOL> elif len ( sys . argv ) == <NUM_LIT:3> : <EOL> ctrl_addr = sys . argv [ <NUM_LIT:1> ] <EOL> sub_addr = sys . argv [ <NUM_LIT:2> ] <EOL> CLI ( ctrl_addr , sub_addr ) . cmdloop ( ) <EOL> else : <EOL> print "<STR_LIT>" </s>
