Mercurial > python-cmd2
changeset 233:d2cc9a090b7f
now using code.InteractiveConsole
author | catherine@dellzilla |
---|---|
date | Tue, 24 Mar 2009 12:13:27 -0400 |
parents | c8b8477ca549 |
children | ff99b39259d5 |
files | cmd2.py |
diffstat | 1 files changed, 18 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/cmd2.py Mon Mar 23 17:08:18 2009 -0400 +++ b/cmd2.py Tue Mar 24 12:13:27 2009 -0400 @@ -26,7 +26,7 @@ flagReader.py options are still supported for backward compatibility """ import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest -import unittest, string, datetime, urllib, inspect +import unittest, string, datetime, urllib, inspect, code from optparse import make_option __version__ = '0.4.8' @@ -220,8 +220,8 @@ result = '< %s' % fname[0] # wasn't a file after all else: result = getPasteBuffer() - return result - + return result + class Cmd(cmd.Cmd): echo = False case_insensitive = True @@ -234,6 +234,7 @@ defaultExtension = 'txt' default_file_name = 'command.txt' abbrev = True + nonpythoncommand = 'cmd' settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', 'case_insensitive', 'echo', 'timing', 'abbrev'] settable.sort() @@ -759,31 +760,25 @@ exec(arg, self.pystate) return - def do_py(self, arg, escape = 'cmd'): + def do_py(self, arg): ''' py <command>: Executes a Python command. - py: Enters interactive Python mode (end with `end py`). + py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or `exit()`. + Do not end with Ctrl-Z, or it will end your entire cmd2 session! + Non-python commands can be issued with cmd('your non-python command here'). ''' if arg.strip(): - self._attempt_py_command(arg) + interp = code.InteractiveInterpreter(locals=self.pystate) + interp.runcode(arg) else: - print 'Now accepting python commands; end with `end py`' - buffer = [self.pseudo_raw_input('>>> ')] - while buffer[-1].lower().split()[:2] != ['end','py']: - if buffer[-1].lower().split()[:1] == [escape]: - self.onecmd(' '.join(buffer[-1].split()[1:])) - buffer = [self.pseudo_raw_input('>>> ')] - continue - else: - try: - self._attempt_py_command('\n'.join(buffer)) - buffer = [self.pseudo_raw_input('>>> ')] - except SyntaxError: - buffer.append(self.pseudo_raw_input('... ')) - except Exception, e: - print e - buffer = [self.pseudo_raw_input('>>> ')] - + interp = code.InteractiveConsole(locals=self.pystate) + def quit(): + interp.push(chr(4)) + self.pystate['quit'] = quit + self.pystate['exit'] = quit + self.pystate[self.nonpythoncommand] = self.onecmd + interp.interact() + def do_history(self, arg): """history [arg]: lists past commands issued