Mercurial > python-cmd2
comparison cmd2.py @ 234:ff99b39259d5
maybe quit and exit work now?
author | catherine@dellzilla |
---|---|
date | Tue, 24 Mar 2009 13:29:54 -0400 |
parents | d2cc9a090b7f |
children | 78ad20c2eed0 |
comparison
equal
deleted
inserted
replaced
233:d2cc9a090b7f | 234:ff99b39259d5 |
---|---|
219 except IOError: | 219 except IOError: |
220 result = '< %s' % fname[0] # wasn't a file after all | 220 result = '< %s' % fname[0] # wasn't a file after all |
221 else: | 221 else: |
222 result = getPasteBuffer() | 222 result = getPasteBuffer() |
223 return result | 223 return result |
224 | |
225 class EmbeddedConsoleExit(Exception): | |
226 pass | |
227 | |
228 class InteractiveConsole(code.InteractiveConsole): | |
229 def runcode(self, code): | |
230 """Execute a code object. | |
231 | |
232 When an exception occurs, self.showtraceback() is called to | |
233 display a traceback. All exceptions are caught except | |
234 SystemExit, which is reraised. | |
235 | |
236 A note about KeyboardInterrupt: this exception may occur | |
237 elsewhere in this code, and may not always be caught. The | |
238 caller should be prepared to deal with it. | |
239 | |
240 Copied directly from code.InteractiveInterpreter, except for | |
241 EmbeddedConsoleExit exceptions. | |
242 """ | |
243 try: | |
244 exec code in self.locals | |
245 except (SystemExit, EmbeddedConsoleExit): | |
246 raise | |
247 except: | |
248 self.showtraceback() | |
249 else: | |
250 if code.softspace(sys.stdout, 0): | |
251 print | |
224 | 252 |
225 class Cmd(cmd.Cmd): | 253 class Cmd(cmd.Cmd): |
226 echo = False | 254 echo = False |
227 case_insensitive = True | 255 case_insensitive = True |
228 continuation_prompt = '> ' | 256 continuation_prompt = '> ' |
761 return | 789 return |
762 | 790 |
763 def do_py(self, arg): | 791 def do_py(self, arg): |
764 ''' | 792 ''' |
765 py <command>: Executes a Python command. | 793 py <command>: Executes a Python command. |
766 py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or `exit()`. | 794 py: Enters interactive Python mode; end with `Ctrl-D`. |
767 Do not end with Ctrl-Z, or it will end your entire cmd2 session! | 795 Do not end with Ctrl-Z, or it will end your entire cmd2 session! |
768 Non-python commands can be issued with cmd('your non-python command here'). | 796 Non-python commands can be issued with cmd('your non-python command here'). |
769 ''' | 797 ''' |
770 if arg.strip(): | 798 if arg.strip(): |
771 interp = code.InteractiveInterpreter(locals=self.pystate) | 799 interp = code.InteractiveInterpreter(locals=self.pystate) |
772 interp.runcode(arg) | 800 interp.runcode(arg) |
773 else: | 801 else: |
774 interp = code.InteractiveConsole(locals=self.pystate) | 802 interp = InteractiveConsole(locals=self.pystate) |
775 def quit(): | 803 def quit(): |
776 interp.push(chr(4)) | 804 raise EmbeddedConsoleExit |
777 self.pystate['quit'] = quit | 805 self.pystate['quit'] = quit |
778 self.pystate['exit'] = quit | 806 self.pystate['exit'] = quit |
779 self.pystate[self.nonpythoncommand] = self.onecmd | 807 try: |
780 interp.interact() | 808 interp.interact() |
809 except (EmbeddedConsoleExit, SystemExit): | |
810 return | |
781 | 811 |
782 def do_history(self, arg): | 812 def do_history(self, arg): |
783 """history [arg]: lists past commands issued | 813 """history [arg]: lists past commands issued |
784 | 814 |
785 no arg -> list all | 815 no arg -> list all |