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 | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 20 | import os |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 21 | import re |
| 22 | import string |
| 23 | import tabnanny |
| 24 | import tokenize |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 25 | import tkMessageBox |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 26 | import PyShell |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 27 | |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 28 | from configHandler import idleConf |
| 29 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 30 | IDENTCHARS = string.ascii_letters + string.digits + "_" |
| 31 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 32 | indent_message = """Error: Inconsistent indentation detected! |
| 33 | |
| 34 | This means that either: |
| 35 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 36 | 1) your indentation is outright incorrect (easy to fix), or |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 37 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 38 | 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] | 39 | how many spaces a tab is worth. |
| 40 | |
| 41 | To fix case 2, change all tabs to spaces by using Select All followed \ |
| 42 | by Untabify Region (both in the Edit menu).""" |
| 43 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 44 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 45 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 46 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 47 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 48 | ('run', [None, |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 49 | ('Check Module', '<<check-module>>'), |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 50 | ('Run Module', '<<run-module>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 51 | |
| 52 | def __init__(self, editwin): |
| 53 | self.editwin = editwin |
| 54 | # Provide instance variables referenced by Debugger |
| 55 | # XXX This should be done differently |
| 56 | self.flist = self.editwin.flist |
| 57 | self.root = self.flist.root |
| 58 | |
| 59 | def check_module_event(self, event): |
| 60 | filename = self.getfilename() |
| 61 | if not filename: |
| 62 | return |
| 63 | if not self.tabnanny(filename): |
| 64 | return |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 65 | self.checksyntax(filename) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 66 | |
| 67 | def tabnanny(self, filename): |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 68 | f = open(filename, 'r') |
| 69 | try: |
Kurt B. Kaiser | 05bab1e | 2002-09-18 03:05:19 +0000 | [diff] [blame] | 70 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 71 | except tokenize.TokenError, msg: |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 72 | msgtxt, (lineno, start) = msg |
| 73 | self.editwin.gotoline(lineno) |
| 74 | self.errorbox("Tabnanny Tokenizing Error", |
| 75 | "Token Error: %s" % msgtxt) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 76 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 77 | except tabnanny.NannyNag, nag: |
| 78 | # The error messages from tabnanny are too confusing... |
| 79 | self.editwin.gotoline(nag.get_lineno()) |
| 80 | self.errorbox("Tab/space error", indent_message) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 81 | return False |
| 82 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 83 | |
| 84 | def checksyntax(self, filename): |
| 85 | f = open(filename, 'r') |
| 86 | source = f.read() |
| 87 | f.close() |
| 88 | if '\r' in source: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 89 | source = re.sub(r"\r\n", "\n", source) |
| 90 | if source and source[-1] != '\n': |
| 91 | source = source + '\n' |
Kurt B. Kaiser | 3f8ace9 | 2003-06-05 02:38:32 +0000 | [diff] [blame] | 92 | text = self.editwin.text |
| 93 | text.tag_remove("ERROR", "1.0", "end") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 94 | try: |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 95 | # If successful, return the compiled code |
| 96 | return compile(source, filename, "exec") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 97 | except (SyntaxError, OverflowError), err: |
| 98 | try: |
| 99 | msg, (errorfilename, lineno, offset, line) = err |
| 100 | if not errorfilename: |
| 101 | err.args = msg, (filename, lineno, offset, line) |
| 102 | err.filename = filename |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 103 | self.colorize_syntax_error(msg, lineno, offset) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 104 | except: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | msg = "*** " + str(err) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 106 | self.errorbox("Syntax error", |
| 107 | "There's an error in your program:\n" + msg) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 108 | return False |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 109 | |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 110 | def colorize_syntax_error(self, msg, lineno, offset): |
| 111 | text = self.editwin.text |
| 112 | pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 113 | text.tag_add("ERROR", pos) |
| 114 | char = text.get(pos) |
| 115 | if char and char in IDENTCHARS: |
| 116 | text.tag_add("ERROR", pos + " wordstart", pos) |
| 117 | if '\n' == text.get(pos): # error at line end |
| 118 | text.mark_set("insert", pos) |
| 119 | else: |
| 120 | text.mark_set("insert", pos + "+1c") |
| 121 | text.see(pos) |
Kurt B. Kaiser | 6655e4b | 2002-12-31 16:03:23 +0000 | [diff] [blame] | 122 | |
Kurt B. Kaiser | eb9637e | 2003-01-26 04:17:16 +0000 | [diff] [blame] | 123 | def run_module_event(self, event): |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 124 | """Run the module after setting up the environment. |
| 125 | |
| 126 | First check the syntax. If OK, make sure the shell is active and |
| 127 | then transfer the arguments, set the run environment's working |
| 128 | directory to the directory of the module being executed and also |
| 129 | add that directory to its sys.path if not already included. |
| 130 | |
| 131 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 132 | filename = self.getfilename() |
| 133 | if not filename: |
| 134 | return |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 135 | code = self.checksyntax(filename) |
| 136 | if not code: |
| 137 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 138 | flist = self.editwin.flist |
| 139 | shell = flist.open_shell() |
| 140 | interp = shell.interp |
Kurt B. Kaiser | 7f38ec0 | 2003-05-15 03:19:42 +0000 | [diff] [blame] | 141 | if PyShell.use_subprocess: |
| 142 | shell.restart_shell() |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 143 | dirname = os.path.dirname(filename) |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 144 | # XXX Too often this discards arguments the user just set... |
| 145 | interp.runcommand("""if 1: |
| 146 | _filename = %s |
| 147 | import sys as _sys |
| 148 | from os.path import basename as _basename |
| 149 | if (not _sys.argv or |
| 150 | _basename(_sys.argv[0]) != _basename(_filename)): |
| 151 | _sys.argv = [_filename] |
Kurt B. Kaiser | ce5b6d5 | 2003-05-31 23:44:18 +0000 | [diff] [blame] | 152 | import os as _os |
| 153 | _os.chdir(%s) |
| 154 | del _filename, _sys, _basename, _os |
| 155 | \n""" % (`filename`, `dirname`)) |
Kurt B. Kaiser | 11659ad | 2003-05-15 23:23:21 +0000 | [diff] [blame] | 156 | interp.prepend_syspath(filename) |
Kurt B. Kaiser | 92b5ca3 | 2002-12-17 21:16:12 +0000 | [diff] [blame] | 157 | interp.runcode(code) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 158 | |
| 159 | def getfilename(self): |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 160 | """Get source filename. If not saved, offer to save (or create) file |
| 161 | |
| 162 | The debugger requires a source file. Make sure there is one, and that |
| 163 | the current version of the source buffer has been saved. If the user |
| 164 | declines to save or cancels the Save As dialog, return None. |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 165 | |
| 166 | If the user has configured IDLE for Autosave, the file will be |
| 167 | silently saved if it already exists and is dirty. |
Kurt B. Kaiser | 8d1f11b | 2003-05-26 22:20:34 +0000 | [diff] [blame] | 168 | |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 169 | """ |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 170 | filename = self.editwin.io.filename |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 171 | if not self.editwin.get_saved(): |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 172 | autosave = idleConf.GetOption('main', 'General', |
| 173 | 'autosave', type='bool') |
| 174 | if autosave and filename: |
Kurt B. Kaiser | d5e1cef | 2002-12-19 03:25:34 +0000 | [diff] [blame] | 175 | self.editwin.io.save(None) |
| 176 | else: |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 177 | reply = self.ask_save_dialog() |
| 178 | self.editwin.text.focus_set() |
| 179 | if reply == "ok": |
| 180 | self.editwin.io.save(None) |
| 181 | filename = self.editwin.io.filename |
| 182 | else: |
| 183 | filename = None |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 184 | return filename |
| 185 | |
Kurt B. Kaiser | 6c638b6 | 2003-05-26 06:23:10 +0000 | [diff] [blame] | 186 | def ask_save_dialog(self): |
| 187 | msg = "Source Must Be Saved\n" + 5*' ' + "OK to Save?" |
| 188 | mb = tkMessageBox.Message(title="Save Before Run or Check", |
| 189 | message=msg, |
| 190 | icon=tkMessageBox.QUESTION, |
| 191 | type=tkMessageBox.OKCANCEL, |
| 192 | default=tkMessageBox.OK, |
| 193 | master=self.editwin.text) |
| 194 | return mb.show() |
| 195 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 196 | def errorbox(self, title, message): |
| 197 | # XXX This should really be a function of EditorWindow... |
| 198 | tkMessageBox.showerror(title, message, master=self.editwin.text) |
| 199 | self.editwin.text.focus_set() |