# HG changeset patch # User catherine@dellzilla # Date 1237392759 14400 # Node ID 5a45cf2e74552009164323384d3aec950a66a0f8 # Parent 0a2fd4b5dad708da42a6b48d1758a14b0bdb0df9 beginning to manage multi-line py diff -r 0a2fd4b5dad7 -r 5a45cf2e7455 cmd2.py --- a/cmd2.py Mon Mar 16 23:31:06 2009 -0400 +++ b/cmd2.py Wed Mar 18 12:12:39 2009 -0400 @@ -273,6 +273,7 @@ cmd.Cmd.__init__(self, *args, **kwargs) self.history = History() self._init_parser() + self.pyenvironment = {} def do_shortcuts(self, args): """Lists single-key shortcuts available.""" @@ -736,6 +737,23 @@ 'execute a command as if at the OS prompt.' os.system(arg) + def do_py(self, arg): + '''Executes a python command''' + if arg.strip(): + try: + exec(arg, self.pyenvironment) + except Exception, e: + print e + else: + print 'Now accepting python commands; end with `\\py`' + buffer = [self.pseudo_raw_input('>>> ')] + while not buffer[-1].strip().startswith('\\py'): + try: + exec('\n'.join(buffer), self.pyenvironment) + buffer = [self.pseudo_raw_input('>>> ')] + except SyntaxError: + buffer.append(self.pseudo_raw_input('... ')) + def do_history(self, arg): """history [arg]: lists past commands issued