Mercurial > python-cmd2
comparison cmd2.py @ 233:d2cc9a090b7f
now using code.InteractiveConsole
author | catherine@dellzilla |
---|---|
date | Tue, 24 Mar 2009 12:13:27 -0400 |
parents | c8b8477ca549 |
children | ff99b39259d5 |
comparison
equal
deleted
inserted
replaced
232:c8b8477ca549 | 233:d2cc9a090b7f |
---|---|
24 CHANGES: | 24 CHANGES: |
25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. | 25 As of 0.3.0, options should be specified as `optparse` options. See README.txt. |
26 flagReader.py options are still supported for backward compatibility | 26 flagReader.py options are still supported for backward compatibility |
27 """ | 27 """ |
28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 28 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
29 import unittest, string, datetime, urllib, inspect | 29 import unittest, string, datetime, urllib, inspect, code |
30 from optparse import make_option | 30 from optparse import make_option |
31 __version__ = '0.4.8' | 31 __version__ = '0.4.8' |
32 | 32 |
33 class OptionParser(optparse.OptionParser): | 33 class OptionParser(optparse.OptionParser): |
34 def exit(self, status=0, msg=None): | 34 def exit(self, status=0, msg=None): |
218 result = open(os.path.expanduser(fname[0])).read() | 218 result = open(os.path.expanduser(fname[0])).read() |
219 except IOError: | 219 except IOError: |
220 result = '< %s' % fname[0] # wasn't a file after all | 220 result = '< %s' % fname[0] # wasn't a file after all |
221 else: | 221 else: |
222 result = getPasteBuffer() | 222 result = getPasteBuffer() |
223 return result | 223 return result |
224 | 224 |
225 class Cmd(cmd.Cmd): | 225 class Cmd(cmd.Cmd): |
226 echo = False | 226 echo = False |
227 case_insensitive = True | 227 case_insensitive = True |
228 continuation_prompt = '> ' | 228 continuation_prompt = '> ' |
229 timing = False | 229 timing = False |
232 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() | 232 excludeFromHistory = '''run r list l history hi ed edit li eof'''.split() |
233 noSpecialParse = 'set ed edit exit'.split() | 233 noSpecialParse = 'set ed edit exit'.split() |
234 defaultExtension = 'txt' | 234 defaultExtension = 'txt' |
235 default_file_name = 'command.txt' | 235 default_file_name = 'command.txt' |
236 abbrev = True | 236 abbrev = True |
237 nonpythoncommand = 'cmd' | |
237 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', | 238 settable = ['prompt', 'continuation_prompt', 'default_file_name', 'editor', |
238 'case_insensitive', 'echo', 'timing', 'abbrev'] | 239 'case_insensitive', 'echo', 'timing', 'abbrev'] |
239 settable.sort() | 240 settable.sort() |
240 | 241 |
241 editor = os.environ.get('EDITOR') | 242 editor = os.environ.get('EDITOR') |
757 print repr(result) | 758 print repr(result) |
758 except SyntaxError: | 759 except SyntaxError: |
759 exec(arg, self.pystate) | 760 exec(arg, self.pystate) |
760 return | 761 return |
761 | 762 |
762 def do_py(self, arg, escape = 'cmd'): | 763 def do_py(self, arg): |
763 ''' | 764 ''' |
764 py <command>: Executes a Python command. | 765 py <command>: Executes a Python command. |
765 py: Enters interactive Python mode (end with `end py`). | 766 py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or `exit()`. |
767 Do not end with Ctrl-Z, or it will end your entire cmd2 session! | |
768 Non-python commands can be issued with cmd('your non-python command here'). | |
766 ''' | 769 ''' |
767 if arg.strip(): | 770 if arg.strip(): |
768 self._attempt_py_command(arg) | 771 interp = code.InteractiveInterpreter(locals=self.pystate) |
772 interp.runcode(arg) | |
769 else: | 773 else: |
770 print 'Now accepting python commands; end with `end py`' | 774 interp = code.InteractiveConsole(locals=self.pystate) |
771 buffer = [self.pseudo_raw_input('>>> ')] | 775 def quit(): |
772 while buffer[-1].lower().split()[:2] != ['end','py']: | 776 interp.push(chr(4)) |
773 if buffer[-1].lower().split()[:1] == [escape]: | 777 self.pystate['quit'] = quit |
774 self.onecmd(' '.join(buffer[-1].split()[1:])) | 778 self.pystate['exit'] = quit |
775 buffer = [self.pseudo_raw_input('>>> ')] | 779 self.pystate[self.nonpythoncommand] = self.onecmd |
776 continue | 780 interp.interact() |
777 else: | 781 |
778 try: | |
779 self._attempt_py_command('\n'.join(buffer)) | |
780 buffer = [self.pseudo_raw_input('>>> ')] | |
781 except SyntaxError: | |
782 buffer.append(self.pseudo_raw_input('... ')) | |
783 except Exception, e: | |
784 print e | |
785 buffer = [self.pseudo_raw_input('>>> ')] | |
786 | |
787 def do_history(self, arg): | 782 def do_history(self, arg): |
788 """history [arg]: lists past commands issued | 783 """history [arg]: lists past commands issued |
789 | 784 |
790 no arg -> list all | 785 no arg -> list all |
791 arg is integer -> list one history item, by index | 786 arg is integer -> list one history item, by index |