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