Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: GrepDialog.py
from __future__ import print_function
[0] Fix | Delete
import os
[1] Fix | Delete
import fnmatch
[2] Fix | Delete
import re # for htest
[3] Fix | Delete
import sys
[4] Fix | Delete
from Tkinter import StringVar, BooleanVar, Checkbutton # for GrepDialog
[5] Fix | Delete
from Tkinter import Tk, Text, Button, SEL, END # for htest
[6] Fix | Delete
from idlelib import SearchEngine
[7] Fix | Delete
from idlelib.SearchDialogBase import SearchDialogBase
[8] Fix | Delete
# Importing OutputWindow fails due to import loop
[9] Fix | Delete
# EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow
[10] Fix | Delete
[11] Fix | Delete
def grep(text, io=None, flist=None):
[12] Fix | Delete
root = text._root()
[13] Fix | Delete
engine = SearchEngine.get(root)
[14] Fix | Delete
if not hasattr(engine, "_grepdialog"):
[15] Fix | Delete
engine._grepdialog = GrepDialog(root, engine, flist)
[16] Fix | Delete
dialog = engine._grepdialog
[17] Fix | Delete
searchphrase = text.get("sel.first", "sel.last")
[18] Fix | Delete
dialog.open(text, searchphrase, io)
[19] Fix | Delete
[20] Fix | Delete
class GrepDialog(SearchDialogBase):
[21] Fix | Delete
[22] Fix | Delete
title = "Find in Files Dialog"
[23] Fix | Delete
icon = "Grep"
[24] Fix | Delete
needwrapbutton = 0
[25] Fix | Delete
[26] Fix | Delete
def __init__(self, root, engine, flist):
[27] Fix | Delete
SearchDialogBase.__init__(self, root, engine)
[28] Fix | Delete
self.flist = flist
[29] Fix | Delete
self.globvar = StringVar(root)
[30] Fix | Delete
self.recvar = BooleanVar(root)
[31] Fix | Delete
[32] Fix | Delete
def open(self, text, searchphrase, io=None):
[33] Fix | Delete
SearchDialogBase.open(self, text, searchphrase)
[34] Fix | Delete
if io:
[35] Fix | Delete
path = io.filename or ""
[36] Fix | Delete
else:
[37] Fix | Delete
path = ""
[38] Fix | Delete
dir, base = os.path.split(path)
[39] Fix | Delete
head, tail = os.path.splitext(base)
[40] Fix | Delete
if not tail:
[41] Fix | Delete
tail = ".py"
[42] Fix | Delete
self.globvar.set(os.path.join(dir, "*" + tail))
[43] Fix | Delete
[44] Fix | Delete
def create_entries(self):
[45] Fix | Delete
SearchDialogBase.create_entries(self)
[46] Fix | Delete
self.globent = self.make_entry("In files:", self.globvar)[0]
[47] Fix | Delete
[48] Fix | Delete
def create_other_buttons(self):
[49] Fix | Delete
f = self.make_frame()[0]
[50] Fix | Delete
[51] Fix | Delete
btn = Checkbutton(f, anchor="w",
[52] Fix | Delete
variable=self.recvar,
[53] Fix | Delete
text="Recurse down subdirectories")
[54] Fix | Delete
btn.pack(side="top", fill="both")
[55] Fix | Delete
btn.select()
[56] Fix | Delete
[57] Fix | Delete
def create_command_buttons(self):
[58] Fix | Delete
SearchDialogBase.create_command_buttons(self)
[59] Fix | Delete
self.make_button("Search Files", self.default_command, 1)
[60] Fix | Delete
[61] Fix | Delete
def default_command(self, event=None):
[62] Fix | Delete
prog = self.engine.getprog()
[63] Fix | Delete
if not prog:
[64] Fix | Delete
return
[65] Fix | Delete
path = self.globvar.get()
[66] Fix | Delete
if not path:
[67] Fix | Delete
self.top.bell()
[68] Fix | Delete
return
[69] Fix | Delete
from idlelib.OutputWindow import OutputWindow # leave here!
[70] Fix | Delete
save = sys.stdout
[71] Fix | Delete
try:
[72] Fix | Delete
sys.stdout = OutputWindow(self.flist)
[73] Fix | Delete
self.grep_it(prog, path)
[74] Fix | Delete
finally:
[75] Fix | Delete
sys.stdout = save
[76] Fix | Delete
[77] Fix | Delete
def grep_it(self, prog, path):
[78] Fix | Delete
dir, base = os.path.split(path)
[79] Fix | Delete
list = self.findfiles(dir, base, self.recvar.get())
[80] Fix | Delete
list.sort()
[81] Fix | Delete
self.close()
[82] Fix | Delete
pat = self.engine.getpat()
[83] Fix | Delete
print("Searching %r in %s ..." % (pat, path))
[84] Fix | Delete
hits = 0
[85] Fix | Delete
try:
[86] Fix | Delete
for fn in list:
[87] Fix | Delete
try:
[88] Fix | Delete
with open(fn) as f:
[89] Fix | Delete
for lineno, line in enumerate(f, 1):
[90] Fix | Delete
if line[-1:] == '\n':
[91] Fix | Delete
line = line[:-1]
[92] Fix | Delete
if prog.search(line):
[93] Fix | Delete
sys.stdout.write("%s: %s: %s\n" %
[94] Fix | Delete
(fn, lineno, line))
[95] Fix | Delete
hits += 1
[96] Fix | Delete
except IOError as msg:
[97] Fix | Delete
print(msg)
[98] Fix | Delete
print(("Hits found: %s\n"
[99] Fix | Delete
"(Hint: right-click to open locations.)"
[100] Fix | Delete
% hits) if hits else "No hits.")
[101] Fix | Delete
except AttributeError:
[102] Fix | Delete
# Tk window has been closed, OutputWindow.text = None,
[103] Fix | Delete
# so in OW.write, OW.text.insert fails.
[104] Fix | Delete
pass
[105] Fix | Delete
[106] Fix | Delete
def findfiles(self, dir, base, rec):
[107] Fix | Delete
try:
[108] Fix | Delete
names = os.listdir(dir or os.curdir)
[109] Fix | Delete
except os.error as msg:
[110] Fix | Delete
print(msg)
[111] Fix | Delete
return []
[112] Fix | Delete
list = []
[113] Fix | Delete
subdirs = []
[114] Fix | Delete
for name in names:
[115] Fix | Delete
fn = os.path.join(dir, name)
[116] Fix | Delete
if os.path.isdir(fn):
[117] Fix | Delete
subdirs.append(fn)
[118] Fix | Delete
else:
[119] Fix | Delete
if fnmatch.fnmatch(name, base):
[120] Fix | Delete
list.append(fn)
[121] Fix | Delete
if rec:
[122] Fix | Delete
for subdir in subdirs:
[123] Fix | Delete
list.extend(self.findfiles(subdir, base, rec))
[124] Fix | Delete
return list
[125] Fix | Delete
[126] Fix | Delete
def close(self, event=None):
[127] Fix | Delete
if self.top:
[128] Fix | Delete
self.top.grab_release()
[129] Fix | Delete
self.top.withdraw()
[130] Fix | Delete
[131] Fix | Delete
[132] Fix | Delete
def _grep_dialog(parent): # htest #
[133] Fix | Delete
from idlelib.PyShell import PyShellFileList
[134] Fix | Delete
root = Tk()
[135] Fix | Delete
root.title("Test GrepDialog")
[136] Fix | Delete
width, height, x, y = list(map(int, re.split('[x+]', parent.geometry())))
[137] Fix | Delete
root.geometry("+%d+%d"%(x, y + 150))
[138] Fix | Delete
[139] Fix | Delete
flist = PyShellFileList(root)
[140] Fix | Delete
text = Text(root, height=5)
[141] Fix | Delete
text.pack()
[142] Fix | Delete
[143] Fix | Delete
def show_grep_dialog():
[144] Fix | Delete
text.tag_add(SEL, "1.0", END)
[145] Fix | Delete
grep(text, flist=flist)
[146] Fix | Delete
text.tag_remove(SEL, "1.0", END)
[147] Fix | Delete
[148] Fix | Delete
button = Button(root, text="Show GrepDialog", command=show_grep_dialog)
[149] Fix | Delete
button.pack()
[150] Fix | Delete
root.mainloop()
[151] Fix | Delete
[152] Fix | Delete
if __name__ == "__main__":
[153] Fix | Delete
import unittest
[154] Fix | Delete
unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
[155] Fix | Delete
[156] Fix | Delete
from idlelib.idle_test.htest import run
[157] Fix | Delete
run(_grep_dialog)
[158] Fix | Delete
[159] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function