@@ -789,6 +789,44 @@ def onecmd_plus_hooks(self, line):
789789 finally :
790790 return self .postparsing_postcmd (stop )
791791
792+ def runcmds_plus_hooks (self , cmds ):
793+ """Convenience method to run multiple commands by onecmd_plus_hooks.
794+
795+ This method adds the given cmds to the command queue and processes the
796+ queue until completion or an error causes it to abort. Scripts that are
797+ loaded will have their commands added to the queue. Scripts may even
798+ load other scripts recursively. This means, however, that you should not
799+ use this method if there is a running cmdloop or some other event-loop.
800+ This method is only intended to be used in "one-off" scenarios.
801+
802+ NOTE: You may need this method even if you only have one command. If
803+ that command is a load, then you will need this command to fully process
804+ all the subsequent commands that are loaded from the script file. This
805+ is an improvement over onecmd_plus_hooks, which expects to be used
806+ inside of a command loop which does the processing of loaded commands.
807+
808+ Example: cmd_obj.runcmds_plus_hooks(['load myscript.txt'])
809+
810+ :param cmds: list - Command strings suitable for onecmd_plus_hooks.
811+ :return: bool - True implies the entire application should exit.
812+
813+ """
814+ stop = False
815+ self .cmdqueue = list (cmds ) + self .cmdqueue
816+ try :
817+ while self .cmdqueue and not stop :
818+ stop = self .onecmd_plus_hooks (self .cmdqueue .pop (0 ))
819+ finally :
820+ # Clear out the command queue and script directory stack, just in
821+ # case we hit an error and they were not completed.
822+ self .cmdqueue = []
823+ self ._script_dir = []
824+ # NOTE: placing this return here inside the finally block will
825+ # swallow exceptions. This is consistent with what is done in
826+ # onecmd_plus_hooks and _cmdloop, although it may not be
827+ # necessary/desired here.
828+ return stop
829+
792830 def _complete_statement (self , line ):
793831 """Keep accepting lines of input until the command is complete."""
794832 if not line or (not pyparsing .Or (self .commentGrammars ).setParseAction (lambda x : '' ).transformString (line )):
@@ -1775,18 +1813,13 @@ def do_load(self, file_path):
17751813 return
17761814
17771815 try :
1778- # Specify file encoding in Python 3, but Python 2 doesn't allow that argument to open()
1779- if six .PY3 :
1780- # Add all commands in the script to the command queue
1781- with open (expanded_path , encoding = 'utf-8' ) as target :
1782- self .cmdqueue .extend (target .read ().splitlines ())
1783- else :
1784- # Add all commands in the script to the command queue
1785- with open (expanded_path ) as target :
1786- self .cmdqueue .extend (target .read ().splitlines ())
1787-
1788- # Append in an "end of script (eos)" command to cleanup the self._script_dir list
1789- self .cmdqueue .append ('eos' )
1816+ # Read all lines of the script and insert into the head of the
1817+ # command queue. Add an "end of script (eos)" command to cleanup the
1818+ # self._script_dir list when done. Specify file encoding in Python
1819+ # 3, but Python 2 doesn't allow that argument to open().
1820+ kwargs = {'encoding' : 'utf-8' } if six .PY3 else {}
1821+ with open (expanded_path , ** kwargs ) as target :
1822+ self .cmdqueue = target .read ().splitlines () + ['eos' ] + self .cmdqueue
17901823 except IOError as e :
17911824 self .perror ('Problem accessing script from {}:\n {}' .format (expanded_path , e ))
17921825 return
0 commit comments