comparison cmd2.py @ 231:18bf83a77e0e

woot, sql within by
author catherine@dellzilla
date Mon, 23 Mar 2009 12:07:31 -0400
parents fea183146819
children c8b8477ca549
comparison
equal deleted inserted replaced
230:fea183146819 231:18bf83a77e0e
750 750
751 def do_shell(self, arg): 751 def do_shell(self, arg):
752 'execute a command as if at the OS prompt.' 752 'execute a command as if at the OS prompt.'
753 os.system(arg) 753 os.system(arg)
754 754
755 def do_py(self, arg): 755 def _attempt_py_command(self, arg):
756 try:
757 result = eval(arg, self.pystate)
758 print repr(result)
759 except SyntaxError:
760 exec(arg, self.pystate)
761 return
762
763 def do_py(self, arg, escape = 'cmd'):
756 ''' 764 '''
757 py <command>: Executes a Python command. 765 py <command>: Executes a Python command.
758 py: Enters interactive Python mode (end with `end py`). 766 py: Enters interactive Python mode (end with `end py`).
759 ''' 767 '''
760 if arg.strip(): 768 if arg.strip():
761 try: 769 self._attempt_py_command(arg)
762 result = eval(arg, self.pystate)
763 print repr(result)
764 except SyntaxError:
765 try:
766 exec(arg, self.pystate)
767 except Exception:
768 raise
769 except Exception, e:
770 print e
771 else: 770 else:
772 print 'Now accepting python commands; end with `end py`' 771 print 'Now accepting python commands; end with `end py`'
773 buffer = [self.pseudo_raw_input('>>> ')] 772 buffer = [self.pseudo_raw_input('>>> ')]
774 while buffer[-1].lower().split()[:2] != ['end','py']: 773 while buffer[-1].lower().split()[:2] != ['end','py']:
775 try: 774 if buffer[-1].lower().split()[:1] == [escape]:
776 buf = '\n'.join(buffer) 775 self.onecmd(' '.join(buffer[-1].split()[1:]))
776 buffer = [self.pseudo_raw_input('>>> ')]
777 continue
778 else:
777 try: 779 try:
778 result = eval(buf, self.pystate) 780 self._attempt_py_command('\n'.join(buffer))
779 print repr(result) 781 buffer = [self.pseudo_raw_input('>>> ')]
780 except SyntaxError: 782 except SyntaxError:
781 exec(buf, self.pystate) 783 buffer.append(self.pseudo_raw_input('... '))
782 buffer = [self.pseudo_raw_input('>>> ')] 784 except Exception, e:
783 except SyntaxError: 785 print e
784 buffer.append(self.pseudo_raw_input('... ')) 786 buffer = [self.pseudo_raw_input('>>> ')]
785 except Exception, e:
786 print e
787 buffer = [self.pseudo_raw_input('>>> ')]
788 787
789 def do_history(self, arg): 788 def do_history(self, arg):
790 """history [arg]: lists past commands issued 789 """history [arg]: lists past commands issued
791 790
792 no arg -> list all 791 no arg -> list all