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. |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 6 | It also runs the tabnanny to catch any inconsistent tabs. |
| 7 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 8 | - Import module is equivalent to either import or reload of the |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 9 | current module. The window must have been saved previously. The |
| 10 | module is added to sys.modules, and is also added to the __main__ |
| 11 | namespace. Output goes to the shell window. |
| 12 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 13 | - Run module does the same but executes the module's |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 14 | code in the __main__ namespace. |
| 15 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 16 | XXX Redesign this interface (yet again) as follows: |
| 17 | |
| 18 | - Present a dialog box for ``Run script'' |
| 19 | |
| 20 | - Allow specify command line arguments in the dialog box |
| 21 | |
| 22 | - Restart the interpreter when running a script |
| 23 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 24 | """ |
| 25 | |
| 26 | import sys |
| 27 | import os |
| 28 | import imp |
| 29 | import tkMessageBox |
| 30 | |
| 31 | indent_message = """Error: Inconsistent indentation detected! |
| 32 | |
| 33 | This means that either: |
| 34 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 35 | 1) your indentation is outright incorrect (easy to fix), or |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 37 | 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] | 38 | how many spaces a tab is worth. |
| 39 | |
| 40 | To fix case 2, change all tabs to spaces by using Select All followed \ |
| 41 | by Untabify Region (both in the Edit menu).""" |
| 42 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 43 | |
| 44 | # XXX TBD Implement stop-execution KBK 11Jun02 |
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, |
| 49 | # ('Check module', '<<check-module>>'), |
| 50 | # ('Import module', '<<import-module>>'), |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 51 | ('Run script', '<<run-script>>'), |
| 52 | ] |
| 53 | ), |
| 54 | ] |
| 55 | |
| 56 | def __init__(self, editwin): |
| 57 | self.editwin = editwin |
| 58 | # Provide instance variables referenced by Debugger |
| 59 | # XXX This should be done differently |
| 60 | self.flist = self.editwin.flist |
| 61 | self.root = self.flist.root |
| 62 | |
| 63 | def check_module_event(self, event): |
| 64 | filename = self.getfilename() |
| 65 | if not filename: |
| 66 | return |
| 67 | if not self.tabnanny(filename): |
| 68 | return |
| 69 | if not self.checksyntax(filename): |
| 70 | return |
| 71 | |
| 72 | def tabnanny(self, filename): |
| 73 | import tabnanny |
| 74 | import tokenize |
| 75 | tabnanny.reset_globals() |
| 76 | f = open(filename, 'r') |
| 77 | try: |
| 78 | tokenize.tokenize(f.readline, tabnanny.tokeneater) |
| 79 | except tokenize.TokenError, msg: |
| 80 | self.errorbox("Token error", |
| 81 | "Token error:\n%s" % str(msg)) |
| 82 | return 0 |
| 83 | except tabnanny.NannyNag, nag: |
| 84 | # The error messages from tabnanny are too confusing... |
| 85 | self.editwin.gotoline(nag.get_lineno()) |
| 86 | self.errorbox("Tab/space error", indent_message) |
| 87 | return 0 |
| 88 | return 1 |
| 89 | |
| 90 | def checksyntax(self, filename): |
| 91 | f = open(filename, 'r') |
| 92 | source = f.read() |
| 93 | f.close() |
| 94 | if '\r' in source: |
| 95 | import re |
| 96 | source = re.sub(r"\r\n", "\n", source) |
| 97 | if source and source[-1] != '\n': |
| 98 | source = source + '\n' |
| 99 | try: |
| 100 | compile(source, filename, "exec") |
| 101 | except (SyntaxError, OverflowError), err: |
| 102 | try: |
| 103 | msg, (errorfilename, lineno, offset, line) = err |
| 104 | if not errorfilename: |
| 105 | err.args = msg, (filename, lineno, offset, line) |
| 106 | err.filename = filename |
| 107 | except: |
| 108 | lineno = None |
| 109 | msg = "*** " + str(err) |
| 110 | if lineno: |
| 111 | self.editwin.gotoline(lineno) |
| 112 | self.errorbox("Syntax error", |
| 113 | "There's an error in your program:\n" + msg) |
| 114 | return 1 |
| 115 | |
| 116 | def import_module_event(self, event): |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 117 | flist = self.editwin.flist |
| 118 | shell = flist.open_shell() |
| 119 | interp = shell.interp |
| 120 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 121 | filename = self.getfilename() |
| 122 | if not filename: |
| 123 | return |
| 124 | |
| 125 | modname, ext = os.path.splitext(os.path.basename(filename)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 126 | |
| 127 | dir = os.path.dirname(filename) |
| 128 | dir = os.path.normpath(os.path.abspath(dir)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 129 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 130 | interp.runcode("""if 1: |
| 131 | import sys as _sys |
| 132 | if %s not in _sys.path: |
| 133 | _sys.path.insert(0, %s) |
| 134 | if _sys.modules.get(%s): |
| 135 | del _sys |
| 136 | import %s |
| 137 | reload(%s) |
| 138 | else: |
| 139 | del _sys |
| 140 | import %s |
| 141 | \n""" % (`dir`, `dir`, `modname`, modname, modname, modname)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 142 | |
| 143 | def run_script_event(self, event): |
| 144 | filename = self.getfilename() |
| 145 | if not filename: |
| 146 | return |
| 147 | |
| 148 | flist = self.editwin.flist |
| 149 | shell = flist.open_shell() |
| 150 | interp = shell.interp |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 151 | # XXX Too often this discards arguments the user just set... |
| 152 | interp.runcommand("""if 1: |
| 153 | _filename = %s |
| 154 | import sys as _sys |
| 155 | from os.path import basename as _basename |
| 156 | if (not _sys.argv or |
| 157 | _basename(_sys.argv[0]) != _basename(_filename)): |
| 158 | _sys.argv = [_filename] |
| 159 | del _filename, _sys, _basename |
| 160 | \n""" % `filename`) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 161 | interp.execfile(filename) |
| 162 | |
| 163 | def getfilename(self): |
| 164 | # Logic to make sure we have a saved filename |
| 165 | # XXX Better logic would offer to save! |
| 166 | if not self.editwin.get_saved(): |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 167 | name = (self.editwin.short_title() or |
| 168 | self.editwin.long_title() or |
| 169 | "Untitled") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 170 | self.errorbox("Not saved", |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 171 | "The buffer for %s is not saved.\n" % name + |
| 172 | "Please save it first!") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 173 | self.editwin.text.focus_set() |
| 174 | return |
| 175 | filename = self.editwin.io.filename |
| 176 | if not filename: |
| 177 | self.errorbox("No file name", |
| 178 | "This window has no file name") |
| 179 | return |
| 180 | return filename |
| 181 | |
| 182 | def errorbox(self, title, message): |
| 183 | # XXX This should really be a function of EditorWindow... |
| 184 | tkMessageBox.showerror(title, message, master=self.editwin.text) |
| 185 | self.editwin.text.focus_set() |