2017-06-27 04:01:58 -03:00
|
|
|
"""Grep dialog for Find in Files functionality.
|
|
|
|
|
|
|
|
Inherits from SearchDialogBase for GUI and uses searchengine
|
|
|
|
to prepare search pattern.
|
|
|
|
"""
|
2000-08-14 22:13:23 -03:00
|
|
|
import fnmatch
|
2016-08-31 01:50:55 -03:00
|
|
|
import os
|
2000-08-14 22:13:23 -03:00
|
|
|
import sys
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2016-07-10 21:21:31 -03:00
|
|
|
from tkinter import StringVar, BooleanVar
|
|
|
|
from tkinter.ttk import Checkbutton
|
2016-08-31 01:50:55 -03:00
|
|
|
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib.searchbase import SearchDialogBase
|
2016-08-31 01:50:55 -03:00
|
|
|
from idlelib import searchengine
|
|
|
|
|
|
|
|
# Importing OutputWindow here fails due to import loop
|
2014-06-10 03:49:35 -03:00
|
|
|
# EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2017-06-27 04:01:58 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
def grep(text, io=None, flist=None):
|
2017-06-27 04:01:58 -03:00
|
|
|
"""Create or find singleton GrepDialog instance.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: Text widget that contains the selected text for
|
|
|
|
default search phrase.
|
|
|
|
io: iomenu.IOBinding instance with default path to search.
|
|
|
|
flist: filelist.FileList instance for OutputWindow parent.
|
|
|
|
"""
|
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
root = text._root()
|
2016-05-28 14:22:31 -03:00
|
|
|
engine = searchengine.get(root)
|
2000-08-14 22:13:23 -03:00
|
|
|
if not hasattr(engine, "_grepdialog"):
|
|
|
|
engine._grepdialog = GrepDialog(root, engine, flist)
|
|
|
|
dialog = engine._grepdialog
|
2003-06-07 00:21:17 -03:00
|
|
|
searchphrase = text.get("sel.first", "sel.last")
|
|
|
|
dialog.open(text, searchphrase, io)
|
2000-08-14 22:13:23 -03:00
|
|
|
|
2017-06-27 04:01:58 -03:00
|
|
|
|
2000-08-14 22:13:23 -03:00
|
|
|
class GrepDialog(SearchDialogBase):
|
2017-06-27 04:01:58 -03:00
|
|
|
"Dialog for searching multiple files."
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
title = "Find in Files Dialog"
|
|
|
|
icon = "Grep"
|
|
|
|
needwrapbutton = 0
|
|
|
|
|
|
|
|
def __init__(self, root, engine, flist):
|
2017-06-27 04:01:58 -03:00
|
|
|
"""Create search dialog for searching for a phrase in the file system.
|
|
|
|
|
|
|
|
Uses SearchDialogBase as the basis for the GUI and a
|
|
|
|
searchengine instance to prepare the search.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
globvar: Value of Text Entry widget for path to search.
|
|
|
|
recvar: Boolean value of Checkbutton widget
|
|
|
|
for traversing through subdirectories.
|
|
|
|
"""
|
2000-08-14 22:13:23 -03:00
|
|
|
SearchDialogBase.__init__(self, root, engine)
|
|
|
|
self.flist = flist
|
|
|
|
self.globvar = StringVar(root)
|
|
|
|
self.recvar = BooleanVar(root)
|
|
|
|
|
2003-06-07 00:21:17 -03:00
|
|
|
def open(self, text, searchphrase, io=None):
|
2017-06-27 04:01:58 -03:00
|
|
|
"Make dialog visible on top of others and ready to use."
|
2003-06-07 00:21:17 -03:00
|
|
|
SearchDialogBase.open(self, text, searchphrase)
|
2000-08-14 22:13:23 -03:00
|
|
|
if io:
|
|
|
|
path = io.filename or ""
|
|
|
|
else:
|
|
|
|
path = ""
|
|
|
|
dir, base = os.path.split(path)
|
|
|
|
head, tail = os.path.splitext(base)
|
|
|
|
if not tail:
|
|
|
|
tail = ".py"
|
|
|
|
self.globvar.set(os.path.join(dir, "*" + tail))
|
|
|
|
|
|
|
|
def create_entries(self):
|
2017-06-27 04:01:58 -03:00
|
|
|
"Create base entry widgets and add widget for search path."
|
2000-08-14 22:13:23 -03:00
|
|
|
SearchDialogBase.create_entries(self)
|
2014-07-13 18:27:26 -03:00
|
|
|
self.globent = self.make_entry("In files:", self.globvar)[0]
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def create_other_buttons(self):
|
2017-06-27 04:01:58 -03:00
|
|
|
"Add check button to recurse down subdirectories."
|
2016-07-10 21:21:31 -03:00
|
|
|
btn = Checkbutton(
|
|
|
|
self.make_frame()[0], variable=self.recvar,
|
2000-08-14 22:13:23 -03:00
|
|
|
text="Recurse down subdirectories")
|
|
|
|
btn.pack(side="top", fill="both")
|
|
|
|
|
|
|
|
def create_command_buttons(self):
|
2017-06-27 04:01:58 -03:00
|
|
|
"Create base command buttons and add button for search."
|
2000-08-14 22:13:23 -03:00
|
|
|
SearchDialogBase.create_command_buttons(self)
|
|
|
|
self.make_button("Search Files", self.default_command, 1)
|
|
|
|
|
|
|
|
def default_command(self, event=None):
|
2017-06-27 04:01:58 -03:00
|
|
|
"""Grep for search pattern in file path. The default command is bound
|
|
|
|
to <Return>.
|
|
|
|
|
|
|
|
If entry values are populated, set OutputWindow as stdout
|
|
|
|
and perform search. The search dialog is closed automatically
|
|
|
|
when the search begins.
|
|
|
|
"""
|
2000-08-14 22:13:23 -03:00
|
|
|
prog = self.engine.getprog()
|
|
|
|
if not prog:
|
|
|
|
return
|
|
|
|
path = self.globvar.get()
|
|
|
|
if not path:
|
|
|
|
self.top.bell()
|
|
|
|
return
|
2016-05-28 14:22:31 -03:00
|
|
|
from idlelib.outwin import OutputWindow # leave here!
|
2000-08-14 22:13:23 -03:00
|
|
|
save = sys.stdout
|
|
|
|
try:
|
|
|
|
sys.stdout = OutputWindow(self.flist)
|
|
|
|
self.grep_it(prog, path)
|
|
|
|
finally:
|
|
|
|
sys.stdout = save
|
|
|
|
|
|
|
|
def grep_it(self, prog, path):
|
2017-06-27 04:01:58 -03:00
|
|
|
"""Search for prog within the lines of the files in path.
|
|
|
|
|
|
|
|
For the each file in the path directory, open the file and
|
|
|
|
search each line for the matching pattern. If the pattern is
|
|
|
|
found, write the file and line information to stdout (which
|
|
|
|
is an OutputWindow).
|
|
|
|
"""
|
2000-08-14 22:13:23 -03:00
|
|
|
dir, base = os.path.split(path)
|
|
|
|
list = self.findfiles(dir, base, self.recvar.get())
|
|
|
|
list.sort()
|
|
|
|
self.close()
|
|
|
|
pat = self.engine.getpat()
|
2017-06-27 04:01:58 -03:00
|
|
|
print(f"Searching {pat!r} in {path} ...")
|
2000-08-14 22:13:23 -03:00
|
|
|
hits = 0
|
2014-06-10 03:49:35 -03:00
|
|
|
try:
|
|
|
|
for fn in list:
|
|
|
|
try:
|
|
|
|
with open(fn, errors='replace') as f:
|
|
|
|
for lineno, line in enumerate(f, 1):
|
|
|
|
if line[-1:] == '\n':
|
|
|
|
line = line[:-1]
|
|
|
|
if prog.search(line):
|
2017-06-27 04:01:58 -03:00
|
|
|
sys.stdout.write(f"{fn}: {lineno}: {line}\n")
|
2014-06-10 03:49:35 -03:00
|
|
|
hits += 1
|
|
|
|
except OSError as msg:
|
|
|
|
print(msg)
|
2017-06-27 04:01:58 -03:00
|
|
|
print(f"Hits found: {hits}\n(Hint: right-click to open locations.)"
|
|
|
|
if hits else "No hits.")
|
2014-06-10 03:49:35 -03:00
|
|
|
except AttributeError:
|
|
|
|
# Tk window has been closed, OutputWindow.text = None,
|
|
|
|
# so in OW.write, OW.text.insert fails.
|
|
|
|
pass
|
2000-08-14 22:13:23 -03:00
|
|
|
|
|
|
|
def findfiles(self, dir, base, rec):
|
2017-06-27 04:01:58 -03:00
|
|
|
"""Return list of files in the dir that match the base pattern.
|
|
|
|
|
|
|
|
If rec is True, recursively iterate through subdirectories.
|
|
|
|
"""
|
2000-08-14 22:13:23 -03:00
|
|
|
try:
|
|
|
|
names = os.listdir(dir or os.curdir)
|
2014-02-23 01:37:16 -04:00
|
|
|
except OSError as msg:
|
2007-02-09 01:37:30 -04:00
|
|
|
print(msg)
|
2000-08-14 22:13:23 -03:00
|
|
|
return []
|
|
|
|
list = []
|
|
|
|
subdirs = []
|
|
|
|
for name in names:
|
|
|
|
fn = os.path.join(dir, name)
|
|
|
|
if os.path.isdir(fn):
|
|
|
|
subdirs.append(fn)
|
|
|
|
else:
|
|
|
|
if fnmatch.fnmatch(name, base):
|
|
|
|
list.append(fn)
|
|
|
|
if rec:
|
|
|
|
for subdir in subdirs:
|
|
|
|
list.extend(self.findfiles(subdir, base, rec))
|
|
|
|
return list
|
|
|
|
|
2014-06-10 03:49:35 -03:00
|
|
|
|
2014-10-17 02:31:35 -03:00
|
|
|
def _grep_dialog(parent): # htest #
|
2016-07-10 21:21:31 -03:00
|
|
|
from tkinter import Toplevel, Text, SEL, END
|
|
|
|
from tkinter.ttk import Button
|
2016-08-31 01:50:55 -03:00
|
|
|
from idlelib.pyshell import PyShellFileList
|
2016-06-21 19:41:38 -03:00
|
|
|
top = Toplevel(parent)
|
|
|
|
top.title("Test GrepDialog")
|
2016-07-10 18:28:10 -03:00
|
|
|
x, y = map(int, parent.geometry().split('+')[1:])
|
2017-06-27 04:01:58 -03:00
|
|
|
top.geometry(f"+{x}+{y + 175}")
|
2014-05-29 02:46:26 -03:00
|
|
|
|
2016-06-21 19:41:38 -03:00
|
|
|
flist = PyShellFileList(top)
|
|
|
|
text = Text(top, height=5)
|
2014-05-29 02:46:26 -03:00
|
|
|
text.pack()
|
|
|
|
|
|
|
|
def show_grep_dialog():
|
|
|
|
text.tag_add(SEL, "1.0", END)
|
|
|
|
grep(text, flist=flist)
|
|
|
|
text.tag_remove(SEL, "1.0", END)
|
|
|
|
|
2016-06-21 19:41:38 -03:00
|
|
|
button = Button(top, text="Show GrepDialog", command=show_grep_dialog)
|
2014-05-29 02:46:26 -03:00
|
|
|
button.pack()
|
|
|
|
|
2013-06-22 19:26:51 -03:00
|
|
|
if __name__ == "__main__":
|
|
|
|
import unittest
|
|
|
|
unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
|
2014-05-29 02:46:26 -03:00
|
|
|
|
|
|
|
from idlelib.idle_test.htest import run
|
|
|
|
run(_grep_dialog)
|