<s> from cmd import Cmd <EOL> from os import popen <EOL> from sys import stdout <EOL> from kamaki . cli import exec_cmd , print_error_message , print_subcommands_help <EOL> from kamaki . cli . argument import ArgumentParseManager <EOL> from kamaki . cli . utils import print_dict , split_input , pref_enc <EOL> from kamaki . cli . history import History <EOL> from kamaki . cli . errors import CLIError <EOL> from kamaki . clients import ClientError <EOL> from kamaki . cli . logger import add_file_logger <EOL> log = add_file_logger ( __name__ ) <EOL> def init_shell ( exe_string , parser , username = '<STR_LIT>' , userid = '<STR_LIT>' ) : <EOL> parser . arguments . pop ( '<STR_LIT:version>' , None ) <EOL> shell = Shell ( ) <EOL> shell . set_prompt ( exe_string ) <EOL> from kamaki import __version__ as version <EOL> shell . greet ( version , username , userid ) <EOL> shell . do_EOF = shell . do_exit <EOL> from kamaki . cli . cmdtree import CommandTree <EOL> shell . cmd_tree = CommandTree ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> return shell <EOL> class Shell ( Cmd ) : <EOL> """<STR_LIT>""" <EOL> _prefix = '<STR_LIT:[>' <EOL> _suffix = '<STR_LIT>' <EOL> cmd_tree = None <EOL> _history = None <EOL> _context_stack = [ ] <EOL> _prompt_stack = [ ] <EOL> _parser = None <EOL> astakos = None <EOL> cloud = None <EOL> undoc_header = '<STR_LIT>' <EOL> def emptyline ( self ) : <EOL> self . lastcmd = '<STR_LIT>' <EOL> def postcmd ( self , post , line ) : <EOL> if self . _context_stack : <EOL> self . _roll_command ( ) <EOL> self . _restore ( self . _context_stack . pop ( ) ) <EOL> self . set_prompt ( <EOL> self . _prompt_stack . pop ( ) [ len ( self . _prefix ) : - len ( self . _suffix ) ] ) <EOL> return Cmd . postcmd ( self , post , line ) <EOL> def precmd ( self , line ) : <EOL> if line . startswith ( '<STR_LIT:/>' ) : <EOL> start , end = len ( self . _prefix ) , - len ( self . _suffix ) <EOL> cur_cmd_path = self . prompt . replace ( '<STR_LIT:U+0020>' , '<STR_LIT:_>' ) [ start : end ] <EOL> if cur_cmd_path != self . cmd_tree . name : <EOL> cur_cmd = self . cmd_tree . get_command ( cur_cmd_path ) <EOL> self . _context_stack . append ( self . _backup ( ) ) <EOL> self . _prompt_stack . append ( self . prompt ) <EOL> new_context = self <EOL> self . _roll_command ( cur_cmd . path ) <EOL> new_context . set_prompt ( self . cmd_tree . name ) <EOL> for grp_cmd in self . cmd_tree . get_subcommands ( ) : <EOL> self . _register_command ( grp_cmd . path ) <EOL> return line [ <NUM_LIT:1> : ] <EOL> return line <EOL> def greet ( self , version , username = '<STR_LIT>' , userid = '<STR_LIT>' ) : <EOL> print ( '<STR_LIT>' % version ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' ) <EOL> if username or userid : <EOL> print ( '<STR_LIT>' % ( username , userid ) ) <EOL> def set_prompt ( self , new_prompt ) : <EOL> self . prompt = '<STR_LIT>' % ( self . _prefix , new_prompt , self . _suffix ) <EOL> def cmdloop ( self ) : <EOL> while True : <EOL> try : <EOL> Cmd . cmdloop ( self ) <EOL> except KeyboardInterrupt : <EOL> print ( '<STR_LIT>' ) <EOL> continue <EOL> break <EOL> def do_exit ( self , line ) : <EOL> print ( '<STR_LIT>' ) <EOL> start , end = len ( self . _prefix ) , - len ( self . _suffix ) <EOL> if self . prompt [ start : end ] == self . cmd_tree . name : <EOL> exit ( <NUM_LIT:0> ) <EOL> return True <EOL> def do_shell ( self , line ) : <EOL> output = popen ( line ) . read ( ) <EOL> print ( output ) <EOL> @ property <EOL> def path ( self ) : <EOL> if self . _cmd : <EOL> return self . _cmd . path <EOL> return '<STR_LIT>' <EOL> @ classmethod <EOL> def _register_method ( cls , method , name ) : <EOL> cls . __dict__ [ name ] = method <EOL> @ classmethod <EOL> def _unregister_method ( cls , name ) : <EOL> try : <EOL> cls . __dict__ . pop ( name ) <EOL> except KeyError : <EOL> pass <EOL> def _roll_command ( self , cmd_path = None ) : <EOL> for subname in self . cmd_tree . subnames ( cmd_path ) : <EOL> self . _unregister_method ( '<STR_LIT>' % subname ) <EOL> self . _unregister_method ( '<STR_LIT>' % subname ) <EOL> self . _unregister_method ( '<STR_LIT>' % subname ) <EOL> @ classmethod <EOL> def _backup ( cls ) : <EOL> return dict ( cls . __dict__ ) <EOL> @ classmethod <EOL> def _restore ( cls , oldcontext ) : <EOL> cls . __dict__ = oldcontext <EOL> @ staticmethod <EOL> def _create_help_method ( cmd_name , args , required , descr , syntax ) : <EOL> tmp_args = dict ( args ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> tmp_args . pop ( '<STR_LIT>' , None ) <EOL> help_parser = ArgumentParseManager ( <EOL> cmd_name , tmp_args , required , <EOL> syntax = syntax , description = descr , check_required = False ) <EOL> return help_parser . print_help <EOL> def _register_command ( self , cmd_path ) : <EOL> cmd = self . cmd_tree . get_command ( cmd_path ) <EOL> arguments = self . _parser . arguments <EOL> def do_method ( new_context , line ) : <EOL> """<STR_LIT>""" <EOL> line = line . decode ( pref_enc ) <EOL> subcmd , cmd_args = cmd . parse_out ( split_input ( line ) ) <EOL> self . _history . add ( '<STR_LIT:U+0020>' . join ( [ cmd . path . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) , line ] ) ) <EOL> cmd_parser = ArgumentParseManager ( <EOL> cmd . name , dict ( self . _parser . arguments ) ) <EOL> cmd_parser . parser . description = subcmd . help <EOL> if subcmd . is_command : <EOL> try : <EOL> cls = subcmd . cmd_class <EOL> cmd_parser . required = getattr ( cls , '<STR_LIT>' , None ) <EOL> ldescr = getattr ( cls , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if subcmd . path == '<STR_LIT>' : <EOL> instance = cls ( <EOL> dict ( cmd_parser . arguments ) , self . astakos , <EOL> cmd_tree = self . cmd_tree ) <EOL> else : <EOL> instance = cls ( <EOL> dict ( cmd_parser . arguments ) , <EOL> self . astakos , self . cloud ) <EOL> cmd_parser . update_arguments ( instance . arguments ) <EOL> cmd_parser . arguments = instance . arguments <EOL> subpath = subcmd . path . split ( '<STR_LIT:_>' ) [ <EOL> ( len ( cmd . path . split ( '<STR_LIT:_>' ) ) - <NUM_LIT:1> ) : ] <EOL> cmd_parser . syntax = '<STR_LIT>' % ( <EOL> '<STR_LIT:U+0020>' . join ( subpath ) , instance . syntax ) <EOL> help_method = self . _create_help_method ( <EOL> cmd . name , cmd_parser . arguments , <EOL> cmd_parser . required , subcmd . help , cmd_parser . syntax ) <EOL> if '<STR_LIT>' in cmd_args or '<STR_LIT>' in cmd_args : <EOL> help_method ( ) <EOL> if ldescr . strip ( ) : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT:%s>' % ldescr ) <EOL> return <EOL> cmd_parser . parse ( cmd_args ) <EOL> for name , arg in instance . arguments . items ( ) : <EOL> arg . value = getattr ( <EOL> cmd_parser . parsed , name , arg . default ) <EOL> exec_cmd ( instance , cmd_parser . unparsed , help_method ) <EOL> except ( ClientError , CLIError ) as err : <EOL> print_error_message ( err ) <EOL> elif ( '<STR_LIT>' in cmd_args or '<STR_LIT>' in cmd_args ) or len ( cmd_args ) : <EOL> print ( '<STR_LIT:%s>' % cmd . help ) <EOL> print_subcommands_help ( cmd ) <EOL> else : <EOL> backup_context = self . _backup ( ) <EOL> old_prompt = self . prompt <EOL> new_context . _roll_command ( cmd . parent_path ) <EOL> new_context . set_prompt ( subcmd . path . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) ) <EOL> newcmds = [ scmd for scmd in subcmd . subcommands . values ( ) ] <EOL> for scmd in newcmds : <EOL> new_context . _register_command ( scmd . path ) <EOL> new_context . cmdloop ( ) <EOL> self . prompt = old_prompt <EOL> self . _restore ( backup_context ) <EOL> self . _register_method ( do_method , '<STR_LIT>' % cmd . name ) <EOL> def help_method ( self ) : <EOL> print ( '<STR_LIT>' % ( cmd . help , cmd . name ) ) <EOL> if cmd . is_command : <EOL> cls = cmd . cmd_class <EOL> ldescr = getattr ( cls , '<STR_LIT>' , '<STR_LIT>' ) <EOL> plist = self . prompt [ len ( self . _prefix ) : - len ( self . _suffix ) ] <EOL> plist = plist . split ( '<STR_LIT:U+0020>' ) <EOL> clist = cmd . path . split ( '<STR_LIT:_>' ) <EOL> upto = <NUM_LIT:0> <EOL> if ldescr : <EOL> print ( '<STR_LIT:%s>' % ldescr ) <EOL> for i , term in enumerate ( plist ) : <EOL> try : <EOL> if clist [ i ] == term : <EOL> upto += <NUM_LIT:1> <EOL> except IndexError : <EOL> break <EOL> print ( '<STR_LIT>' % ( '<STR_LIT:U+0020>' . join ( clist [ upto : ] ) , cls . syntax ) ) <EOL> if cmd . subcommands : <EOL> print_subcommands_help ( cmd ) <EOL> self . _register_method ( help_method , '<STR_LIT>' % cmd . name ) <EOL> def complete_method ( self , text , line , begidx , endidx ) : <EOL> subcmd , cmd_args = cmd . parse_out ( split_input ( line ) [ <NUM_LIT:1> : ] ) <EOL> if subcmd . is_command : <EOL> cls = subcmd . cmd_class <EOL> instance = cls ( dict ( arguments ) ) <EOL> empty , sep , subname = subcmd . path . partition ( cmd . path ) <EOL> cmd_name = '<STR_LIT>' % ( cmd . name , subname . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) ) <EOL> print ( '<STR_LIT>' % ( <EOL> cls . help , cmd_name , cls . syntax ) ) <EOL> cmd_args = { } <EOL> for arg in instance . arguments . values ( ) : <EOL> cmd_args [ '<STR_LIT:U+002C>' . join ( arg . parsed_name ) ] = arg . help <EOL> print_dict ( cmd_args , indent = <NUM_LIT:2> ) <EOL> stdout . write ( '<STR_LIT>' % ( self . prompt , line ) ) <EOL> return subcmd . subnames ( ) <EOL> self . _register_method ( complete_method , '<STR_LIT>' % cmd . name ) <EOL> @ property <EOL> def doc_header ( self ) : <EOL> tmp_partition = self . prompt . partition ( self . _prefix ) <EOL> tmp_partition = tmp_partition [ <NUM_LIT:2> ] . partition ( self . _suffix ) <EOL> hdr = tmp_partition [ <NUM_LIT:0> ] . strip ( ) <EOL> return '<STR_LIT>' % hdr <EOL> def run ( self , astakos , cloud , parser , path = '<STR_LIT>' ) : <EOL> self . astakos = astakos <EOL> self . cloud = cloud <EOL> self . _parser = parser <EOL> cnf = parser . arguments [ '<STR_LIT>' ] <EOL> self . _history = History ( cnf . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . _history . limit = cnf . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if path : <EOL> cmd = self . cmd_tree . get_command ( path ) <EOL> intro = cmd . path . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> else : <EOL> intro = self . cmd_tree . name <EOL> acceptable = parser . arguments [ '<STR_LIT>' ] . groups <EOL> total = self . cmd_tree . groups . keys ( ) <EOL> self . cmd_tree . exclude ( set ( total ) . difference ( acceptable ) ) <EOL> for subcmd in self . cmd_tree . get_subcommands ( path ) : <EOL> self . _register_command ( subcmd . path ) <EOL> self . set_prompt ( intro ) <EOL> try : <EOL> self . cmdloop ( ) <EOL> except Exception as e : <EOL> print ( '<STR_LIT>' % e ) <EOL> from traceback import print_stack <EOL> print_stack ( ) </s>
