| 1 |
"""Extension to execute code outside the Python shell window. |
| 2 |
|
| 3 |
This adds the following commands: |
| 4 |
|
| 5 |
- Check module does a full syntax check of the current module. |
| 6 |
It also runs the tabnanny to catch any inconsistent tabs. |
| 7 |
|
| 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. |
| 11 |
|
| 12 |
XXX GvR Redesign this interface (yet again) as follows: |
| 13 |
|
| 14 |
- Present a dialog box for ``Run Module'' |
| 15 |
|
| 16 |
- Allow specify command line arguments in the dialog box |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
import os |
| 21 |
import re |
| 22 |
import string |
| 23 |
import tabnanny |
| 24 |
import tokenize |
| 25 |
import tkMessageBox |
| 26 |
from idlelib import PyShell |
| 27 |
|
| 28 |
from idlelib.configHandler import idleConf |
| 29 |
from idlelib import macosxSupport |
| 30 |
|
| 31 |
IDENTCHARS = string.ascii_letters + string.digits + "_" |
| 32 |
|
| 33 |
indent_message = """Error: Inconsistent indentation detected! |
| 34 |
|
| 35 |
1) Your indentation is outright incorrect (easy to fix), OR |
| 36 |
|
| 37 |
2) Your indentation mixes tabs and spaces. |
| 38 |
|
| 39 |
To fix case 2, change all tabs to spaces by using Edit->Select All followed \ |
| 40 |
by Format->Untabify Region and specify the number of columns used by each tab. |
| 41 |
""" |
| 42 |
|
| 43 |
class ScriptBinding: |
| 44 |
|
| 45 |
menudefs = [ |
| 46 |
('run', [None, |
| 47 |
('Check Module', '<<check-module>>'), |
| 48 |
('Run Module', '<<run-module>>'), ]), ] |
| 49 |
|
| 50 |
def __init__(self, editwin): |
| 51 |
self.editwin = editwin |
| 52 |
|
| 53 |
|
| 54 |
self.flist = self.editwin.flist |
| 55 |
self.root = self.editwin.root |
| 56 |
|
| 57 |
if macosxSupport.isCocoaTk(): |
| 58 |
self.editwin.text_frame.bind('<<run-module-event-2>>', self._run_module_event) |
| 59 |
|
| 60 |
def check_module_event(self, event): |
| 61 |
filename = self.getfilename() |
| 62 |
if not filename: |
| 63 |
return 'break' |
| 64 |
if not self.checksyntax(filename): |
| 65 |
return 'break' |
| 66 |
if not self.tabnanny(filename): |
| 67 |
return 'break' |
| 68 |
|
| 69 |
def tabnanny(self, filename): |
| 70 |
f = open(filename, 'r') |
| 71 |
try: |
| 72 |
tabnanny.process_tokens(tokenize.generate_tokens(f.readline)) |
| 73 |
except tokenize.TokenError as msg: |
| 74 |
msgtxt, (lineno, start) = msg.args |
| 75 |
self.editwin.gotoline(lineno) |
| 76 |
self.errorbox("Tabnanny Tokenizing Error", |
| 77 |
"Token Error: %s" % msgtxt) |
| 78 |
return False |
| 79 |
except tabnanny.NannyNag as nag: |
| 80 |
|
| 81 |
self.editwin.gotoline(nag.get_lineno()) |
| 82 |
self.errorbox("Tab/space error", indent_message) |
| 83 |
return False |
| 84 |
return True |
| 85 |
|
| 86 |
def checksyntax(self, filename): |
| 87 |
self.shell = shell = self.flist.open_shell() |
| 88 |
saved_stream = shell.get_warning_stream() |
| 89 |
shell.set_warning_stream(shell.stderr) |
| 90 |
with open(filename, 'r') as f: |
| 91 |
source = f.read() |
| 92 |
if '\r' in source: |
| 93 |
source = re.sub(r"\r\n", "\n", source) |
| 94 |
source = re.sub(r"\r", "\n", source) |
| 95 |
if source and source[-1] != '\n': |
| 96 |
source = source + '\n' |
| 97 |
text = self.editwin.text |
| 98 |
text.tag_remove("ERROR", "1.0", "end") |
| 99 |
try: |
| 100 |
try: |
| 101 |
|
| 102 |
return compile(source, filename, "exec") |
| 103 |
except (SyntaxError, OverflowError, ValueError) as err: |
| 104 |
try: |
| 105 |
msg, (errorfilename, lineno, offset, line) = err |
| 106 |
if not errorfilename: |
| 107 |
err.args = msg, (filename, lineno, offset, line) |
| 108 |
err.filename = filename |
| 109 |
self.colorize_syntax_error(msg, lineno, offset) |
| 110 |
except: |
| 111 |
msg = "*** " + str(err) |
| 112 |
self.errorbox("Syntax error", |
| 113 |
"There's an error in your program:\n" + msg) |
| 114 |
return False |
| 115 |
finally: |
| 116 |
shell.set_warning_stream(saved_stream) |
| 117 |
|
| 118 |
def colorize_syntax_error(self, msg, lineno, offset): |
| 119 |
text = self.editwin.text |
| 120 |
pos = "0.0 + %d lines + %d chars" % (lineno-1, offset-1) |
| 121 |
text.tag_add("ERROR", pos) |
| 122 |
char = text.get(pos) |
| 123 |
if char and char in IDENTCHARS: |
| 124 |
text.tag_add("ERROR", pos + " wordstart", pos) |
| 125 |
if '\n' == text.get(pos): |
| 126 |
text.mark_set("insert", pos) |
| 127 |
else: |
| 128 |
text.mark_set("insert", pos + "+1c") |
| 129 |
text.see(pos) |
| 130 |
|
| 131 |
def run_module_event(self, event): |
| 132 |
"""Run the module after setting up the environment. |
| 133 |
|
| 134 |
First check the syntax. If OK, make sure the shell is active and |
| 135 |
then transfer the arguments, set the run environment's working |
| 136 |
directory to the directory of the module being executed and also |
| 137 |
add that directory to its sys.path if not already included. |
| 138 |
|
| 139 |
""" |
| 140 |
filename = self.getfilename() |
| 141 |
if not filename: |
| 142 |
return 'break' |
| 143 |
code = self.checksyntax(filename) |
| 144 |
if not code: |
| 145 |
return 'break' |
| 146 |
if not self.tabnanny(filename): |
| 147 |
return 'break' |
| 148 |
interp = self.shell.interp |
| 149 |
if PyShell.use_subprocess: |
| 150 |
interp.restart_subprocess(with_cwd=False, filename=code.co_filename) |
| 151 |
dirname = os.path.dirname(filename) |
| 152 |
|
| 153 |
interp.runcommand("""if 1: |
| 154 |
__file__ = {filename!r} |
| 155 |
import sys as _sys |
| 156 |
from os.path import basename as _basename |
| 157 |
if (not _sys.argv or |
| 158 |
_basename(_sys.argv[0]) != _basename(__file__)): |
| 159 |
_sys.argv = [__file__] |
| 160 |
import os as _os |
| 161 |
_os.chdir({dirname!r}) |
| 162 |
del _sys, _basename, _os |
| 163 |
\n""".format(filename=filename, dirname=dirname)) |
| 164 |
interp.prepend_syspath(filename) |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
interp.runcode(code) |
| 169 |
return 'break' |
| 170 |
|
| 171 |
if macosxSupport.isCocoaTk(): |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
_run_module_event = run_module_event |
| 178 |
|
| 179 |
def run_module_event(self, event): |
| 180 |
self.editwin.text_frame.after(200, |
| 181 |
lambda: self.editwin.text_frame.event_generate('<<run-module-event-2>>')) |
| 182 |
return 'break' |
| 183 |
|
| 184 |
def getfilename(self): |
| 185 |
"""Get source filename. If not saved, offer to save (or create) file |
| 186 |
|
| 187 |
The debugger requires a source file. Make sure there is one, and that |
| 188 |
the current version of the source buffer has been saved. If the user |
| 189 |
declines to save or cancels the Save As dialog, return None. |
| 190 |
|
| 191 |
If the user has configured IDLE for Autosave, the file will be |
| 192 |
silently saved if it already exists and is dirty. |
| 193 |
|
| 194 |
""" |
| 195 |
filename = self.editwin.io.filename |
| 196 |
if not self.editwin.get_saved(): |
| 197 |
autosave = idleConf.GetOption('main', 'General', |
| 198 |
'autosave', type='bool') |
| 199 |
if autosave and filename: |
| 200 |
self.editwin.io.save(None) |
| 201 |
else: |
| 202 |
confirm = self.ask_save_dialog() |
| 203 |
self.editwin.text.focus_set() |
| 204 |
if confirm: |
| 205 |
self.editwin.io.save(None) |
| 206 |
filename = self.editwin.io.filename |
| 207 |
else: |
| 208 |
filename = None |
| 209 |
return filename |
| 210 |
|
| 211 |
def ask_save_dialog(self): |
| 212 |
msg = "Source Must Be Saved\n" + 5*' ' + "OK to Save?" |
| 213 |
confirm = tkMessageBox.askokcancel(title="Save Before Run or Check", |
| 214 |
message=msg, |
| 215 |
default=tkMessageBox.OK, |
| 216 |
parent=self.editwin.text) |
| 217 |
return confirm |
| 218 |
|
| 219 |
def errorbox(self, title, message): |
| 220 |
|
| 221 |
tkMessageBox.showerror(title, message, parent=self.editwin.text) |
| 222 |
self.editwin.text.focus_set() |