comparison cmd2.py @ 346:49dd1ce6cfd6

quitting after invocation commands
author catherine@Drou
date Tue, 16 Feb 2010 15:47:17 -0500
parents 6fe1e75e3a67
children 432ccab7c6c8
comparison
equal deleted inserted replaced
345:6fe1e75e3a67 346:49dd1ce6cfd6
410 if not self.quiet: 410 if not self.quiet:
411 if self.feedback_to_output: 411 if self.feedback_to_output:
412 self.poutput(msg) 412 self.poutput(msg)
413 else: 413 else:
414 print (msg) 414 print (msg)
415 _STOP_AND_EXIT = 2 415 _STOP_AND_EXIT = 2 # distinguish end of script file from actual exit
416 editor = os.environ.get('EDITOR') 416 editor = os.environ.get('EDITOR')
417 if not editor: 417 if not editor:
418 if sys.platform[:3] == 'win': 418 if sys.platform[:3] == 'win':
419 editor = 'notepad' 419 editor = 'notepad'
420 else: 420 else:
740 if self.abbrev: # accept shortened versions of commands 740 if self.abbrev: # accept shortened versions of commands
741 funcs = [fname for fname in self.keywords if fname.startswith(arg)] 741 funcs = [fname for fname in self.keywords if fname.startswith(arg)]
742 if len(funcs) == 1: 742 if len(funcs) == 1:
743 result = 'do_' + funcs[0] 743 result = 'do_' + funcs[0]
744 return result 744 return result
745 def onecmd_plus_hooks(self, line):
746 line = self.precmd(line)
747 stop = self.onecmd(line)
748 stop = self.postcmd(stop, line)
749 return stop
745 def onecmd(self, line): 750 def onecmd(self, line):
746 """Interpret the argument as though it had been typed in response 751 """Interpret the argument as though it had been typed in response
747 to the prompt. 752 to the prompt.
748 753
749 This may be overridden, but should not normally need to be; 754 This may be overridden, but should not normally need to be;
891 line = self.cmdqueue.pop(0) 896 line = self.cmdqueue.pop(0)
892 else: 897 else:
893 line = self.pseudo_raw_input(self.prompt) 898 line = self.pseudo_raw_input(self.prompt)
894 if (self.echo) and (isinstance(self.stdin, file)): 899 if (self.echo) and (isinstance(self.stdin, file)):
895 self.stdout.write(line + '\n') 900 self.stdout.write(line + '\n')
896 line = self.precmd(line) 901 stop = self.onecmd_plus_hooks(line)
897 stop = self.onecmd(line)
898 stop = self.postcmd(stop, line)
899 self.postloop() 902 self.postloop()
900 finally: 903 finally:
901 if self.use_rawinput and self.completekey: 904 if self.use_rawinput and self.completekey:
902 try: 905 try:
903 import readline 906 import readline
1022 else: 1025 else:
1023 localvars = (self.locals_in_py and self.pystate) or {} 1026 localvars = (self.locals_in_py and self.pystate) or {}
1024 interp = InteractiveConsole(locals=localvars) 1027 interp = InteractiveConsole(locals=localvars)
1025 def quit(): 1028 def quit():
1026 raise EmbeddedConsoleExit 1029 raise EmbeddedConsoleExit
1027 def onecmd(arg): 1030 def onecmd_plus_hooks(arg):
1028 return self.onecmd(arg + '\n') 1031 return self.onecmd_plus_hooks(arg + '\n')
1029 self.pystate['quit'] = quit 1032 self.pystate['quit'] = quit
1030 self.pystate['exit'] = quit 1033 self.pystate['exit'] = quit
1031 self.pystate['cmd'] = onecmd 1034 self.pystate['cmd'] = onecmd_plus_hooks
1032 try: 1035 try:
1033 cprt = 'Type "help", "copyright", "credits" or "license" for more information.' 1036 cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
1034 keepstate = Statekeeper(sys, ('stdin','stdout')) 1037 keepstate = Statekeeper(sys, ('stdin','stdout'))
1035 sys.stdout = self.stdout 1038 sys.stdout = self.stdout
1036 sys.stdin = self.stdin 1039 sys.stdin = self.stdin
1207 """ 1210 """
1208 'run [N]: runs the SQL that was run N commands ago' 1211 'run [N]: runs the SQL that was run N commands ago'
1209 runme = self.last_matching(arg) 1212 runme = self.last_matching(arg)
1210 self.pfeedback(runme) 1213 self.pfeedback(runme)
1211 if runme: 1214 if runme:
1212 runme = self.precmd(runme) 1215 stop = self.onecmd_plus_hooks(runme)
1213 stop = self.onecmd(runme)
1214 stop = self.postcmd(stop, runme)
1215 do_r = do_run 1216 do_r = do_run
1216 1217
1217 def fileimport(self, statement, source): 1218 def fileimport(self, statement, source):
1218 try: 1219 try:
1219 f = open(os.path.expanduser(source)) 1220 f = open(os.path.expanduser(source))
1234 result = runner.run(testcase) 1235 result = runner.run(testcase)
1235 result.printErrors() 1236 result.printErrors()
1236 1237
1237 def run_commands_at_invocation(self, callargs): 1238 def run_commands_at_invocation(self, callargs):
1238 for initial_command in callargs: 1239 for initial_command in callargs:
1239 if self.onecmd(initial_command + '\n') == app._STOP_AND_EXIT: 1240 if self.onecmd_plus_hooks(initial_command + '\n'):
1240 return 1241 return self._STOP_AND_EXIT
1241 1242
1242 def cmdloop(self): 1243 def cmdloop(self):
1243 parser = optparse.OptionParser() 1244 parser = optparse.OptionParser()
1244 parser.add_option('-t', '--test', dest='test', 1245 parser.add_option('-t', '--test', dest='test',
1245 action="store_true", 1246 action="store_true",
1246 help='Test against transcript(s) in FILE (wildcards OK)') 1247 help='Test against transcript(s) in FILE (wildcards OK)')
1247 (callopts, callargs) = parser.parse_args() 1248 (callopts, callargs) = parser.parse_args()
1248 if callopts.test: 1249 if callopts.test:
1249 self.runTranscriptTests(callargs) 1250 self.runTranscriptTests(callargs)
1250 else: 1251 else:
1251 self.run_commands_at_invocation(callargs) 1252 if not self.run_commands_at_invocation(callargs):
1252 self._cmdloop() 1253 self._cmdloop()
1253 1254
1254 class HistoryItem(str): 1255 class HistoryItem(str):
1255 listformat = '-------------------------[%d]\n%s\n' 1256 listformat = '-------------------------[%d]\n%s\n'
1256 def __init__(self, instr): 1257 def __init__(self, instr):
1257 str.__init__(self) 1258 str.__init__(self)
1472 line = transcript.next() 1473 line = transcript.next()
1473 while line.startswith(self.cmdapp.continuation_prompt): 1474 while line.startswith(self.cmdapp.continuation_prompt):
1474 command.append(line[len(self.cmdapp.continuation_prompt):]) 1475 command.append(line[len(self.cmdapp.continuation_prompt):])
1475 line = transcript.next() 1476 line = transcript.next()
1476 command = ''.join(command) 1477 command = ''.join(command)
1477 command = self.cmdapp.precmd(command) 1478 stop = self.cmdapp.onecmd_plus_hooks(command)
1478 stop = self.cmdapp.onecmd(command)
1479 stop = self.cmdapp.postcmd(stop, command)
1480 #TODO: should act on ``stop`` 1479 #TODO: should act on ``stop``
1481 result = self.outputTrap.read() 1480 result = self.outputTrap.read()
1482 if line.startswith(self.cmdapp.prompt): 1481 if line.startswith(self.cmdapp.prompt):
1483 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\ 1482 message = '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing)\nGot:\n%s\n'%\
1484 (fname, lineNum, command, result) 1483 (fname, lineNum, command, result)