Mercurial > python-cmd2
changeset 218:5a45cf2e7455
beginning to manage multi-line py
author | catherine@dellzilla |
---|---|
date | Wed, 18 Mar 2009 12:12:39 -0400 |
parents | 0a2fd4b5dad7 |
children | 35be1f8332a9 |
files | cmd2.py |
diffstat | 1 files changed, 18 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- 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