comparison cmd2.py @ 218:5a45cf2e7455

beginning to manage multi-line py
author catherine@dellzilla
date Wed, 18 Mar 2009 12:12:39 -0400
parents 0a2fd4b5dad7
children 35be1f8332a9
comparison
equal deleted inserted replaced
217:0a2fd4b5dad7 218:5a45cf2e7455
271 271
272 def __init__(self, *args, **kwargs): 272 def __init__(self, *args, **kwargs):
273 cmd.Cmd.__init__(self, *args, **kwargs) 273 cmd.Cmd.__init__(self, *args, **kwargs)
274 self.history = History() 274 self.history = History()
275 self._init_parser() 275 self._init_parser()
276 self.pyenvironment = {}
276 277
277 def do_shortcuts(self, args): 278 def do_shortcuts(self, args):
278 """Lists single-key shortcuts available.""" 279 """Lists single-key shortcuts available."""
279 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items()) 280 result = "\n".join('%s: %s' % (sc[0], sc[1]) for sc in self.shortcuts.items())
280 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result)) 281 self.stdout.write("Single-key shortcuts for other commands:\n%s\n" % (result))
734 735
735 def do_shell(self, arg): 736 def do_shell(self, arg):
736 'execute a command as if at the OS prompt.' 737 'execute a command as if at the OS prompt.'
737 os.system(arg) 738 os.system(arg)
738 739
740 def do_py(self, arg):
741 '''Executes a python command'''
742 if arg.strip():
743 try:
744 exec(arg, self.pyenvironment)
745 except Exception, e:
746 print e
747 else:
748 print 'Now accepting python commands; end with `\\py`'
749 buffer = [self.pseudo_raw_input('>>> ')]
750 while not buffer[-1].strip().startswith('\\py'):
751 try:
752 exec('\n'.join(buffer), self.pyenvironment)
753 buffer = [self.pseudo_raw_input('>>> ')]
754 except SyntaxError:
755 buffer.append(self.pseudo_raw_input('... '))
756
739 def do_history(self, arg): 757 def do_history(self, arg):
740 """history [arg]: lists past commands issued 758 """history [arg]: lists past commands issued
741 759
742 no arg -> list all 760 no arg -> list all
743 arg is integer -> list one history item, by index 761 arg is integer -> list one history item, by index