Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: OutputWindow.py
from Tkinter import *
[0] Fix | Delete
from idlelib.EditorWindow import EditorWindow
[1] Fix | Delete
import re
[2] Fix | Delete
import tkMessageBox
[3] Fix | Delete
from idlelib import IOBinding
[4] Fix | Delete
[5] Fix | Delete
class OutputWindow(EditorWindow):
[6] Fix | Delete
[7] Fix | Delete
"""An editor window that can serve as an output file.
[8] Fix | Delete
[9] Fix | Delete
Also the future base class for the Python shell window.
[10] Fix | Delete
This class has no input facilities.
[11] Fix | Delete
"""
[12] Fix | Delete
[13] Fix | Delete
def __init__(self, *args):
[14] Fix | Delete
EditorWindow.__init__(self, *args)
[15] Fix | Delete
self.text.bind("<<goto-file-line>>", self.goto_file_line)
[16] Fix | Delete
[17] Fix | Delete
# Customize EditorWindow
[18] Fix | Delete
[19] Fix | Delete
def ispythonsource(self, filename):
[20] Fix | Delete
# No colorization needed
[21] Fix | Delete
return 0
[22] Fix | Delete
[23] Fix | Delete
def short_title(self):
[24] Fix | Delete
return "Output"
[25] Fix | Delete
[26] Fix | Delete
def maybesave(self):
[27] Fix | Delete
# Override base class method -- don't ask any questions
[28] Fix | Delete
if self.get_saved():
[29] Fix | Delete
return "yes"
[30] Fix | Delete
else:
[31] Fix | Delete
return "no"
[32] Fix | Delete
[33] Fix | Delete
# Act as output file
[34] Fix | Delete
[35] Fix | Delete
def write(self, s, tags=(), mark="insert"):
[36] Fix | Delete
# Tk assumes that byte strings are Latin-1;
[37] Fix | Delete
# we assume that they are in the locale's encoding
[38] Fix | Delete
if isinstance(s, str):
[39] Fix | Delete
try:
[40] Fix | Delete
s = unicode(s, IOBinding.encoding)
[41] Fix | Delete
except UnicodeError:
[42] Fix | Delete
# some other encoding; let Tcl deal with it
[43] Fix | Delete
pass
[44] Fix | Delete
self.text.insert(mark, s, tags)
[45] Fix | Delete
self.text.see(mark)
[46] Fix | Delete
self.text.update()
[47] Fix | Delete
[48] Fix | Delete
def writelines(self, lines):
[49] Fix | Delete
for line in lines:
[50] Fix | Delete
self.write(line)
[51] Fix | Delete
[52] Fix | Delete
def flush(self):
[53] Fix | Delete
pass
[54] Fix | Delete
[55] Fix | Delete
# Our own right-button menu
[56] Fix | Delete
[57] Fix | Delete
rmenu_specs = [
[58] Fix | Delete
("Cut", "<<cut>>", "rmenu_check_cut"),
[59] Fix | Delete
("Copy", "<<copy>>", "rmenu_check_copy"),
[60] Fix | Delete
("Paste", "<<paste>>", "rmenu_check_paste"),
[61] Fix | Delete
(None, None, None),
[62] Fix | Delete
("Go to file/line", "<<goto-file-line>>", None),
[63] Fix | Delete
]
[64] Fix | Delete
[65] Fix | Delete
file_line_pats = [
[66] Fix | Delete
# order of patterns matters
[67] Fix | Delete
r'file "([^"]*)", line (\d+)',
[68] Fix | Delete
r'([^\s]+)\((\d+)\)',
[69] Fix | Delete
r'^(\s*\S.*?):\s*(\d+):', # Win filename, maybe starting with spaces
[70] Fix | Delete
r'([^\s]+):\s*(\d+):', # filename or path, ltrim
[71] Fix | Delete
r'^\s*(\S.*?):\s*(\d+):', # Win abs path with embedded spaces, ltrim
[72] Fix | Delete
]
[73] Fix | Delete
[74] Fix | Delete
file_line_progs = None
[75] Fix | Delete
[76] Fix | Delete
def goto_file_line(self, event=None):
[77] Fix | Delete
if self.file_line_progs is None:
[78] Fix | Delete
l = []
[79] Fix | Delete
for pat in self.file_line_pats:
[80] Fix | Delete
l.append(re.compile(pat, re.IGNORECASE))
[81] Fix | Delete
self.file_line_progs = l
[82] Fix | Delete
# x, y = self.event.x, self.event.y
[83] Fix | Delete
# self.text.mark_set("insert", "@%d,%d" % (x, y))
[84] Fix | Delete
line = self.text.get("insert linestart", "insert lineend")
[85] Fix | Delete
result = self._file_line_helper(line)
[86] Fix | Delete
if not result:
[87] Fix | Delete
# Try the previous line. This is handy e.g. in tracebacks,
[88] Fix | Delete
# where you tend to right-click on the displayed source line
[89] Fix | Delete
line = self.text.get("insert -1line linestart",
[90] Fix | Delete
"insert -1line lineend")
[91] Fix | Delete
result = self._file_line_helper(line)
[92] Fix | Delete
if not result:
[93] Fix | Delete
tkMessageBox.showerror(
[94] Fix | Delete
"No special line",
[95] Fix | Delete
"The line you point at doesn't look like "
[96] Fix | Delete
"a valid file name followed by a line number.",
[97] Fix | Delete
parent=self.text)
[98] Fix | Delete
return
[99] Fix | Delete
filename, lineno = result
[100] Fix | Delete
edit = self.flist.open(filename)
[101] Fix | Delete
edit.gotoline(lineno)
[102] Fix | Delete
[103] Fix | Delete
def _file_line_helper(self, line):
[104] Fix | Delete
for prog in self.file_line_progs:
[105] Fix | Delete
match = prog.search(line)
[106] Fix | Delete
if match:
[107] Fix | Delete
filename, lineno = match.group(1, 2)
[108] Fix | Delete
try:
[109] Fix | Delete
f = open(filename, "r")
[110] Fix | Delete
f.close()
[111] Fix | Delete
break
[112] Fix | Delete
except IOError:
[113] Fix | Delete
continue
[114] Fix | Delete
else:
[115] Fix | Delete
return None
[116] Fix | Delete
try:
[117] Fix | Delete
return filename, int(lineno)
[118] Fix | Delete
except TypeError:
[119] Fix | Delete
return None
[120] Fix | Delete
[121] Fix | Delete
# These classes are currently not used but might come in handy
[122] Fix | Delete
[123] Fix | Delete
class OnDemandOutputWindow:
[124] Fix | Delete
[125] Fix | Delete
tagdefs = {
[126] Fix | Delete
# XXX Should use IdlePrefs.ColorPrefs
[127] Fix | Delete
"stdout": {"foreground": "blue"},
[128] Fix | Delete
"stderr": {"foreground": "#007700"},
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
def __init__(self, flist):
[132] Fix | Delete
self.flist = flist
[133] Fix | Delete
self.owin = None
[134] Fix | Delete
[135] Fix | Delete
def write(self, s, tags, mark):
[136] Fix | Delete
if not self.owin:
[137] Fix | Delete
self.setup()
[138] Fix | Delete
self.owin.write(s, tags, mark)
[139] Fix | Delete
[140] Fix | Delete
def setup(self):
[141] Fix | Delete
self.owin = owin = OutputWindow(self.flist)
[142] Fix | Delete
text = owin.text
[143] Fix | Delete
for tag, cnf in self.tagdefs.items():
[144] Fix | Delete
if cnf:
[145] Fix | Delete
text.tag_configure(tag, **cnf)
[146] Fix | Delete
text.tag_raise('sel')
[147] Fix | Delete
self.write = self.owin.write
[148] Fix | Delete
[149] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function