Skip to content

bpo-30674: IDLE: add docstrings to grep.py #2213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 27, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
IDLE: add docstrings to grep.py
  • Loading branch information
csabella committed Jun 16, 2017
commit 1580835fb54c2bb63f1405ef46ff705d9ced7a0d
41 changes: 17 additions & 24 deletions Lib/idlelib/grep.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Grep dialog for Find in File functionality.
"""Grep dialog for Find in Files functionality.

Inherits from SearchDialogBase for GUI and uses searchengine
to prepare search pattern.
Expand All @@ -23,8 +23,8 @@ def grep(text, io=None, flist=None):
Args:
text: Text widget that contains the selected text for
default search phrase.
io: IOBinding with default path to search.
flist: FileList for OutputWindow parent.
io: iomenu.IOBinding instance with default path to search.
flist: filelist.FileList instance for OutputWindow parent.
"""

root = text._root()
Expand All @@ -37,14 +37,14 @@ def grep(text, io=None, flist=None):


class GrepDialog(SearchDialogBase):
"Search Dialog for Find in Files."
"Dialog for searching multiple files."

title = "Find in Files Dialog"
icon = "Grep"
needwrapbutton = 0

def __init__(self, root, engine, flist):
"""Create Search Dialog for searching for a phrase in the file system.
"""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.
Expand Down Expand Up @@ -90,33 +90,32 @@ def create_command_buttons(self):
self.make_button("Search Files", self.default_command, 1)

def default_command(self, event=None):
"""Grep for search pattern in file path.

The default command is bound to <Return>.
"""Grep for search pattern in file path. The default command is bound
to <Return>.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need new paragraph here.

If entry values are populated, set OutputWindow as stdout
and perform search.
and perform search. The search dialog is closed automatically
when the search begins.
"""
prog = self.engine.getprog()
if not prog:
return None
return
path = self.globvar.get()
if not path:
self.top.bell()
return None
return
from idlelib.outwin import OutputWindow # leave here!
save = sys.stdout
try:
sys.stdout = OutputWindow(self.flist)
self.grep_it(prog, path)
finally:
sys.stdout = save
return None

def grep_it(self, prog, path):
"""Search for prog within the lines of the files in path.

For the each file in the path directory , open the file and
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).
Expand All @@ -126,7 +125,7 @@ def grep_it(self, prog, path):
list.sort()
self.close()
pat = self.engine.getpat()
print("Searching %r in %s ..." % (pat, path))
print(f"Searching {pat!r} in {path} ...")
hits = 0
try:
for fn in list:
Expand All @@ -136,14 +135,12 @@ def grep_it(self, prog, path):
if line[-1:] == '\n':
line = line[:-1]
if prog.search(line):
sys.stdout.write("%s: %s: %s\n" %
(fn, lineno, line))
sys.stdout.write(f"{fn}: {lineno}: {line}\n")
hits += 1
except OSError as msg:
print(msg)
print(("Hits found: %s\n"
"(Hint: right-click to open locations.)"
% hits) if hits else "No hits.")
print(f"Hits found: {hits}\n(Hint: right-click to open locations.)"
if hits else "No hits.")
except AttributeError:
# Tk window has been closed, OutputWindow.text = None,
# so in OW.write, OW.text.insert fails.
Expand Down Expand Up @@ -173,10 +170,6 @@ def findfiles(self, dir, base, rec):
list.extend(self.findfiles(subdir, base, rec))
return list

def close(self, event=None):
"Close the dialog."
SearchDialogBase.close(self, event)


def _grep_dialog(parent): # htest #
from tkinter import Toplevel, Text, SEL, END
Expand All @@ -185,7 +178,7 @@ def _grep_dialog(parent): # htest #
top = Toplevel(parent)
top.title("Test GrepDialog")
x, y = map(int, parent.geometry().split('+')[1:])
top.geometry("+%d+%d" % (x, y + 175))
top.geometry(f"+{x}+{y + 175}")

flist = PyShellFileList(top)
text = Text(top, height=5)
Expand Down