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