David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | """Extension to execute code outside the Python shell window. |
| 2 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 3 | This adds the following commands: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 4 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 5 | - Check module does a full syntax check of the current module. |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 6 | It also runs the tabnanny to catch any inconsistent tabs. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 7 | |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 8 | - Run module executes the module's code in the __main__ namespace. The window |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 9 | must have been saved previously. The module is added to sys.modules, and is |
| 10 | also added to the __main__ namespace. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 11 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 12 | XXX GvR Redesign this interface (yet again) as follows: |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 13 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 14 | - Present a dialog box for ``Run Module'' |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 15 | |
| 16 | - Allow specify command line arguments in the dialog box |
| 17 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | """ |
| 19 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 20 | import re |
| 21 | import string |
| 22 | import tabnanny |
| 23 | import tokenize |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 24 | import tkMessageBox |
| 25 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 26 | IDENTCHARS = string.ascii_letters + string.digits + "_" |
| 27 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | indent_message = """Error: Inconsistent indentation detected! |
| 29 | |
| 30 | This means that either: |
| 31 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 32 | 1) your indentation is outright incorrect (easy to fix), or |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 33 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 34 | 2) your indentation mixes tabs and spaces in a way that depends on \ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 35 | how many spaces a tab is worth. |
| 36 | |
| 37 | To fix case 2, change all tabs to spaces by using Select All followed \ |
| 38 | by Untabify Region (both in the Edit menu).""" |
| 39 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 40 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 41 | # XXX 11Jun02 KBK TBD Implement stop-execution |
| 42 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 43 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 44 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 45 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 46 | ('run', [None, |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 47 | ('Check Module', '<<check-module>>'), |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 48 | ('Run Module', '<<run-module>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 49 | |
| 50 | def __init__(self, editwin): |
| 51 | self.editwin = editwin |
| 52 | # Provide instance variables referenced by Debugger |
| 53 | # XXX This should be done differently |
| 54 | self.flist = self.editwin.flist |
| 55 | self.root = self.flist.root |
| 56 | |
| 57 | def check_module_event(self, event): |
| 58 | filename = self.getfilename() |
| 59 | if not filename: |
| 60 | return |
| 61 | if not self.tabnanny(filename): |
| 62 | return |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 63 | self.checksyntax(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 64 | |
| 65 | def tabnanny(self, filename): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 66 | f = open(filename, 'r') |
| 67 | try: |
Kurt B. Kaiser | 05bab1e | 2002-09-18 03:05:19 +0000 | [diff] [blame] | 68 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 69 | except tokenize.TokenError, msg: |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 70 | self.errorbox("Token error", "Token error:\n%s" % msg) |
| 71 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 72 | except tabnanny.NannyNag, nag: |
| 73 | # The error messages from tabnanny are too confusing... |
| 74 | self.editwin.gotoline(nag.get_lineno()) |
| 75 | self.errorbox("Tab/space error", indent_message) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 76 | return False |
| 77 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 78 | |
| 79 | def checksyntax(self, filename): |
| 80 | f = open(filename, 'r') |
| 81 | source = f.read() |
| 82 | f.close() |
| 83 | if '\r' in source: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 84 | source = re.sub(r"\r\n", "\n", source) |
| 85 | if source and source[-1] != '\n': |
| 86 | source = source + '\n' |
| 87 | try: |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 88 | # If successful, return the compiled code |
| 89 | return compile(source, filename, "exec") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 90 | except (SyntaxError, OverflowError), err: |
| 91 | try: |
| 92 | msg, (errorfilename, lineno, offset, line) = err |
| 93 | if not errorfilename: |
| 94 | err.args = msg, (filename, lineno, offset, line) |
| 95 | err.filename = filename |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 96 | self.colorize_syntax_error(msg, lineno, offset) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 97 | except: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 98 | msg = "*** " + str(err) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 99 | self.errorbox("Syntax error", |
| 100 | "There's an error in your program:\n" + msg) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 101 | return False |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 102 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 103 | def colorize_syntax_error(self, msg, lineno, offset): |
| 104 | text = self.editwin.text |
| 105 | pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 106 | text.tag_add("ERROR", pos) |
| 107 | char = text.get(pos) |
| 108 | if char and char in IDENTCHARS: |
| 109 | text.tag_add("ERROR", pos + " wordstart", pos) |
| 110 | if '\n' == text.get(pos): # error at line end |
| 111 | text.mark_set("insert", pos) |
| 112 | else: |
| 113 | text.mark_set("insert", pos + "+1c") |
| 114 | text.see(pos) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 115 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 116 | def run_module_event(self, event): |
| 117 | "Check syntax, if ok run the module in the shell top level" |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 118 | filename = self.getfilename() |
| 119 | if not filename: |
| 120 | return |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 121 | code = self.checksyntax(filename) |
| 122 | if not code: |
| 123 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 124 | flist = self.editwin.flist |
| 125 | shell = flist.open_shell() |
| 126 | interp = shell.interp |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 127 | interp.restart_subprocess() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 128 | # XXX Too often this discards arguments the user just set... |
| 129 | interp.runcommand("""if 1: |
| 130 | _filename = %s |
| 131 | import sys as _sys |
| 132 | from os.path import basename as _basename |
| 133 | if (not _sys.argv or |
| 134 | _basename(_sys.argv[0]) != _basename(_filename)): |
| 135 | _sys.argv = [_filename] |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 136 | del _filename, _sys, _basename |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 137 | \n""" % `filename`) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 138 | interp.runcode(code) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 139 | |
| 140 | def getfilename(self): |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 141 | """Get source filename. If not saved, offer to save (or create) file |
| 142 | |
| 143 | The debugger requires a source file. Make sure there is one, and that |
| 144 | the current version of the source buffer has been saved. If the user |
| 145 | declines to save or cancels the Save As dialog, return None. |
| 146 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 147 | if not self.editwin.get_saved(): |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 148 | msg = """Source Must Be Saved |
| 149 | OK to Save?""" |
| 150 | mb = tkMessageBox.Message( |
| 151 | title="Save Before Run or Check", |
| 152 | message=msg, |
| 153 | icon=tkMessageBox.QUESTION, |
| 154 | type=tkMessageBox.OKCANCEL, |
Kurt B. Kaiser | a5e8bb9 | 2003-01-10 03:06:30 +0000 | [diff] [blame] | 155 | default=tkMessageBox.OK, |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 156 | master=self.editwin.text) |
| 157 | reply = mb.show() |
| 158 | if reply == "ok": |
| 159 | self.editwin.io.save(None) |
| 160 | else: |
| 161 | return None |
| 162 | # filename is None if file doesn't exist |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 163 | filename = self.editwin.io.filename |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 164 | self.editwin.text.focus_set() |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 165 | return filename |
| 166 | |
| 167 | def errorbox(self, title, message): |
| 168 | # XXX This should really be a function of EditorWindow... |
| 169 | tkMessageBox.showerror(title, message, master=self.editwin.text) |
| 170 | self.editwin.text.focus_set() |