From 38d8e8c0ab31f6dd512fd03c6d48b6a185c920f6 Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Thu, 15 Jun 2017 08:28:00 -0400 Subject: [PATCH 1/3] bpo-30674: IDLE: add docstrings to grep.py --- Lib/idlelib/grep.py | 63 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/Lib/idlelib/grep.py b/Lib/idlelib/grep.py index 64ba28d94a5c14..fcfe2d2de60acc 100644 --- a/Lib/idlelib/grep.py +++ b/Lib/idlelib/grep.py @@ -1,3 +1,8 @@ +"""Grep dialog for Find in File functionality. + + Inherits from SearchDialogBase for GUI and uses searchengine + to prepare search pattern. +""" import fnmatch import os import sys @@ -11,7 +16,17 @@ # Importing OutputWindow here fails due to import loop # EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow + def grep(text, io=None, flist=None): + """Create or find singleton GrepDialog instance. + + 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. + """ + root = text._root() engine = searchengine.get(root) if not hasattr(engine, "_grepdialog"): @@ -20,19 +35,32 @@ def grep(text, io=None, flist=None): searchphrase = text.get("sel.first", "sel.last") dialog.open(text, searchphrase, io) + class GrepDialog(SearchDialogBase): + "Search Dialog for Find in 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. + + 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. + """ SearchDialogBase.__init__(self, root, engine) self.flist = flist self.globvar = StringVar(root) self.recvar = BooleanVar(root) def open(self, text, searchphrase, io=None): + "Make dialog visible on top of others and ready to use." SearchDialogBase.open(self, text, searchphrase) if io: path = io.filename or "" @@ -45,27 +73,37 @@ def open(self, text, searchphrase, io=None): self.globvar.set(os.path.join(dir, "*" + tail)) def create_entries(self): + "Create base entry widgets and add widget for search path." SearchDialogBase.create_entries(self) self.globent = self.make_entry("In files:", self.globvar)[0] def create_other_buttons(self): + "Add check button to recurse down subdirectories." btn = Checkbutton( self.make_frame()[0], variable=self.recvar, text="Recurse down subdirectories") btn.pack(side="top", fill="both") def create_command_buttons(self): + "Create base command buttons and add button for search." SearchDialogBase.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 . + + If entry values are populated, set OutputWindow as stdout + and perform search. + """ prog = self.engine.getprog() if not prog: - return + return None path = self.globvar.get() if not path: self.top.bell() - return + return None from idlelib.outwin import OutputWindow # leave here! save = sys.stdout try: @@ -73,8 +111,16 @@ def default_command(self, event=None): 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 + search each line for the matching pattern. If the pattern is + found, write the file and line information to stdout (which + is an OutputWindow). + """ dir, base = os.path.split(path) list = self.findfiles(dir, base, self.recvar.get()) list.sort() @@ -96,14 +142,18 @@ def grep_it(self, prog, path): except OSError as msg: print(msg) print(("Hits found: %s\n" - "(Hint: right-click to open locations.)" - % hits) if hits else "No hits.") + "(Hint: right-click to open locations.)" + % hits) if hits else "No hits.") 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): + """Return list of files in the dir that match the base pattern. + + If rec is True, recursively iterate through subdirectories. + """ try: names = os.listdir(dir or os.curdir) except OSError as msg: @@ -124,9 +174,8 @@ def findfiles(self, dir, base, rec): return list def close(self, event=None): - if self.top: - self.top.grab_release() - self.top.withdraw() + "Close the dialog." + SearchDialogBase.close(self, event) def _grep_dialog(parent): # htest # From 1580835fb54c2bb63f1405ef46ff705d9ced7a0d Mon Sep 17 00:00:00 2001 From: Cheryl Sabella Date: Fri, 16 Jun 2017 07:39:34 -0400 Subject: [PATCH 2/3] IDLE: add docstrings to grep.py --- Lib/idlelib/grep.py | 41 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 24 deletions(-) diff --git a/Lib/idlelib/grep.py b/Lib/idlelib/grep.py index fcfe2d2de60acc..c55c48cf2d69bc 100644 --- a/Lib/idlelib/grep.py +++ b/Lib/idlelib/grep.py @@ -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. @@ -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() @@ -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. @@ -90,20 +90,20 @@ 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 . + """Grep for search pattern in file path. The default command is bound + to . 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: @@ -111,12 +111,11 @@ def default_command(self, event=None): 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). @@ -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: @@ -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. @@ -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 @@ -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) From 9668ba2052dfa95db24a8e03b63a76116358774e Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Tue, 27 Jun 2017 01:40:57 -0400 Subject: [PATCH 3/3] add news item --- Misc/NEWS.d/next/IDLE/2017-06-27-01-40-34.bpo-30674.ppK_q8.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/IDLE/2017-06-27-01-40-34.bpo-30674.ppK_q8.rst diff --git a/Misc/NEWS.d/next/IDLE/2017-06-27-01-40-34.bpo-30674.ppK_q8.rst b/Misc/NEWS.d/next/IDLE/2017-06-27-01-40-34.bpo-30674.ppK_q8.rst new file mode 100644 index 00000000000000..4b718fef017799 --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2017-06-27-01-40-34.bpo-30674.ppK_q8.rst @@ -0,0 +1 @@ +IDLE: add docstrings to grep module. Patch by Cheryl Sabella