openmedialibrary_platform_d.../lib/python3.7/idlelib/grep.py

201 lines
6.6 KiB
Python
Raw Normal View History

2018-12-31 23:25:26 +00:00
"""Grep dialog for Find in Files functionality.
Inherits from SearchDialogBase for GUI and uses searchengine
to prepare search pattern.
"""
2016-02-06 09:36:57 +00:00
import fnmatch
2018-12-31 23:25:26 +00:00
import os
2016-02-06 09:36:57 +00:00
import sys
2018-12-31 23:25:26 +00:00
from tkinter import StringVar, BooleanVar
from tkinter.ttk import Checkbutton
from idlelib.searchbase import SearchDialogBase
from idlelib import searchengine
# Importing OutputWindow here fails due to import loop
2016-02-06 09:36:57 +00:00
# EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow
2018-12-31 23:25:26 +00:00
2016-02-06 09:36:57 +00:00
def grep(text, io=None, flist=None):
2018-12-31 23:25:26 +00: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.
"""
2016-02-06 09:36:57 +00:00
root = text._root()
2018-12-31 23:25:26 +00:00
engine = searchengine.get(root)
2016-02-06 09:36:57 +00:00
if not hasattr(engine, "_grepdialog"):
engine._grepdialog = GrepDialog(root, engine, flist)
dialog = engine._grepdialog
searchphrase = text.get("sel.first", "sel.last")
dialog.open(text, searchphrase, io)
2018-12-31 23:25:26 +00:00
2016-02-06 09:36:57 +00:00
class GrepDialog(SearchDialogBase):
2018-12-31 23:25:26 +00:00
"Dialog for searching multiple files."
2016-02-06 09:36:57 +00:00
title = "Find in Files Dialog"
icon = "Grep"
needwrapbutton = 0
def __init__(self, root, engine, flist):
2018-12-31 23:25:26 +00: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.
"""
2016-02-06 09:36:57 +00:00
SearchDialogBase.__init__(self, root, engine)
self.flist = flist
self.globvar = StringVar(root)
self.recvar = BooleanVar(root)
def open(self, text, searchphrase, io=None):
2018-12-31 23:25:26 +00:00
"Make dialog visible on top of others and ready to use."
2016-02-06 09:36:57 +00:00
SearchDialogBase.open(self, text, searchphrase)
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):
2018-12-31 23:25:26 +00:00
"Create base entry widgets and add widget for search path."
2016-02-06 09:36:57 +00:00
SearchDialogBase.create_entries(self)
self.globent = self.make_entry("In files:", self.globvar)[0]
def create_other_buttons(self):
2018-12-31 23:25:26 +00:00
"Add check button to recurse down subdirectories."
btn = Checkbutton(
self.make_frame()[0], variable=self.recvar,
2016-02-06 09:36:57 +00:00
text="Recurse down subdirectories")
btn.pack(side="top", fill="both")
def create_command_buttons(self):
2018-12-31 23:25:26 +00:00
"Create base command buttons and add button for search."
2016-02-06 09:36:57 +00:00
SearchDialogBase.create_command_buttons(self)
self.make_button("Search Files", self.default_command, 1)
def default_command(self, event=None):
2018-12-31 23:25:26 +00: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.
"""
2016-02-06 09:36:57 +00:00
prog = self.engine.getprog()
if not prog:
return
path = self.globvar.get()
if not path:
self.top.bell()
return
2018-12-31 23:25:26 +00:00
from idlelib.outwin import OutputWindow # leave here!
2016-02-06 09:36:57 +00: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):
2018-12-31 23:25:26 +00: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).
"""
2016-02-06 09:36:57 +00:00
dir, base = os.path.split(path)
list = self.findfiles(dir, base, self.recvar.get())
list.sort()
self.close()
pat = self.engine.getpat()
2018-12-31 23:25:26 +00:00
print(f"Searching {pat!r} in {path} ...")
2016-02-06 09:36:57 +00:00
hits = 0
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):
2018-12-31 23:25:26 +00:00
sys.stdout.write(f"{fn}: {lineno}: {line}\n")
2016-02-06 09:36:57 +00:00
hits += 1
except OSError as msg:
print(msg)
2018-12-31 23:25:26 +00:00
print(f"Hits found: {hits}\n(Hint: right-click to open locations.)"
if hits else "No hits.")
2016-02-06 09:36:57 +00:00
except AttributeError:
# Tk window has been closed, OutputWindow.text = None,
# so in OW.write, OW.text.insert fails.
pass
def findfiles(self, dir, base, rec):
2018-12-31 23:25:26 +00:00
"""Return list of files in the dir that match the base pattern.
If rec is True, recursively iterate through subdirectories.
"""
2016-02-06 09:36:57 +00:00
try:
names = os.listdir(dir or os.curdir)
except OSError as msg:
print(msg)
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
def _grep_dialog(parent): # htest #
2018-12-31 23:25:26 +00:00
from tkinter import Toplevel, Text, SEL, END
from tkinter.ttk import Button
from idlelib.pyshell import PyShellFileList
top = Toplevel(parent)
top.title("Test GrepDialog")
x, y = map(int, parent.geometry().split('+')[1:])
top.geometry(f"+{x}+{y + 175}")
flist = PyShellFileList(top)
text = Text(top, height=5)
2016-02-06 09:36:57 +00: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)
2018-12-31 23:25:26 +00:00
button = Button(top, text="Show GrepDialog", command=show_grep_dialog)
2016-02-06 09:36:57 +00:00
button.pack()
if __name__ == "__main__":
2018-12-31 23:25:26 +00:00
from unittest import main
main('idlelib.idle_test.test_grep', verbosity=2, exit=False)
2016-02-06 09:36:57 +00:00
from idlelib.idle_test.htest import run
run(_grep_dialog)