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 | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 8 | - Run module executes the module's code in the __main__ namespace. The window |
| 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 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 12 | XXX Redesign this interface (yet again) as follows: |
| 13 | |
| 14 | - Present a dialog box for ``Run script'' |
| 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 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 20 | import tkMessageBox |
| 21 | |
| 22 | indent_message = """Error: Inconsistent indentation detected! |
| 23 | |
| 24 | This means that either: |
| 25 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 26 | 1) your indentation is outright incorrect (easy to fix), or |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 27 | |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 28 | 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] | 29 | how many spaces a tab is worth. |
| 30 | |
| 31 | To fix case 2, change all tabs to spaces by using Select All followed \ |
| 32 | by Untabify Region (both in the Edit menu).""" |
| 33 | |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 34 | |
| 35 | # XXX TBD Implement stop-execution KBK 11Jun02 |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | class ScriptBinding: |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 37 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 38 | menudefs = [ |
Kurt B. Kaiser | 969de45 | 2002-06-12 03:28:57 +0000 | [diff] [blame] | 39 | ('run', [None, |
Kurt B. Kaiser | 6eadc6c | 2002-12-03 23:09:23 +0000 | [diff] [blame] | 40 | ('Check module', '<<check-module>>'), |
| 41 | ('Run script', '<<run-script>>'), ]), ] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 42 | |
| 43 | def __init__(self, editwin): |
| 44 | self.editwin = editwin |
| 45 | # Provide instance variables referenced by Debugger |
| 46 | # XXX This should be done differently |
| 47 | self.flist = self.editwin.flist |
| 48 | self.root = self.flist.root |
| 49 | |
| 50 | def check_module_event(self, event): |
| 51 | filename = self.getfilename() |
| 52 | if not filename: |
| 53 | return |
| 54 | if not self.tabnanny(filename): |
| 55 | return |
| 56 | if not self.checksyntax(filename): |
| 57 | return |
| 58 | |
| 59 | def tabnanny(self, filename): |
| 60 | import tabnanny |
| 61 | import tokenize |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 62 | f = open(filename, 'r') |
| 63 | try: |
Kurt B. Kaiser | 05bab1e | 2002-09-18 03:05:19 +0000 | [diff] [blame] | 64 | tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | except tokenize.TokenError, msg: |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 66 | self.errorbox("Token error", "Token error:\n%s" % msg) |
| 67 | return False |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 68 | except tabnanny.NannyNag, nag: |
| 69 | # The error messages from tabnanny are too confusing... |
| 70 | self.editwin.gotoline(nag.get_lineno()) |
| 71 | self.errorbox("Tab/space error", indent_message) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 72 | return False |
| 73 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 74 | |
| 75 | def checksyntax(self, filename): |
| 76 | f = open(filename, 'r') |
| 77 | source = f.read() |
| 78 | f.close() |
| 79 | if '\r' in source: |
| 80 | import re |
| 81 | source = re.sub(r"\r\n", "\n", source) |
| 82 | if source and source[-1] != '\n': |
| 83 | source = source + '\n' |
| 84 | try: |
| 85 | compile(source, filename, "exec") |
| 86 | except (SyntaxError, OverflowError), err: |
| 87 | try: |
| 88 | msg, (errorfilename, lineno, offset, line) = err |
| 89 | if not errorfilename: |
| 90 | err.args = msg, (filename, lineno, offset, line) |
| 91 | err.filename = filename |
| 92 | except: |
| 93 | lineno = None |
| 94 | msg = "*** " + str(err) |
| 95 | if lineno: |
| 96 | self.editwin.gotoline(lineno) |
| 97 | self.errorbox("Syntax error", |
| 98 | "There's an error in your program:\n" + msg) |
Neal Norwitz | 6453c1f | 2002-11-30 19:18:46 +0000 | [diff] [blame] | 99 | return True |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 101 | def run_script_event(self, event): |
| 102 | filename = self.getfilename() |
| 103 | if not filename: |
| 104 | return |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | flist = self.editwin.flist |
| 106 | shell = flist.open_shell() |
| 107 | interp = shell.interp |
Kurt B. Kaiser | 63857a4 | 2002-09-05 02:31:20 +0000 | [diff] [blame] | 108 | if interp.tkconsole.executing: |
| 109 | interp.display_executing_dialog() |
| 110 | return |
| 111 | interp.restart_subprocess() |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 112 | # XXX Too often this discards arguments the user just set... |
| 113 | interp.runcommand("""if 1: |
| 114 | _filename = %s |
| 115 | import sys as _sys |
| 116 | from os.path import basename as _basename |
| 117 | if (not _sys.argv or |
| 118 | _basename(_sys.argv[0]) != _basename(_filename)): |
Kurt B. Kaiser | d694c1f | 2002-07-28 03:35:31 +0000 | [diff] [blame] | 119 | # XXX 25 July 2002 KBK should this be sys.argv not _sys.argv? |
Chui Tey | 5d2af63 | 2002-05-26 13:36:41 +0000 | [diff] [blame] | 120 | _sys.argv = [_filename] |
| 121 | del _filename, _sys, _basename |
| 122 | \n""" % `filename`) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 123 | interp.execfile(filename) |
| 124 | |
| 125 | def getfilename(self): |
| 126 | # Logic to make sure we have a saved filename |
| 127 | # XXX Better logic would offer to save! |
| 128 | if not self.editwin.get_saved(): |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 129 | name = (self.editwin.short_title() or |
| 130 | self.editwin.long_title() or |
| 131 | "Untitled") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 132 | self.errorbox("Not saved", |
Steven M. Gava | 42f6c64 | 2001-07-12 06:46:53 +0000 | [diff] [blame] | 133 | "The buffer for %s is not saved.\n" % name + |
| 134 | "Please save it first!") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 135 | self.editwin.text.focus_set() |
| 136 | return |
| 137 | filename = self.editwin.io.filename |
| 138 | if not filename: |
| 139 | self.errorbox("No file name", |
| 140 | "This window has no file name") |
| 141 | return |
| 142 | return filename |
| 143 | |
| 144 | def errorbox(self, title, message): |
| 145 | # XXX This should really be a function of EditorWindow... |
| 146 | tkMessageBox.showerror(title, message, master=self.editwin.text) |
| 147 | self.editwin.text.focus_set() |