csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 1 | """Grep dialog for Find in Files functionality. |
| 2 | |
| 3 | Inherits from SearchDialogBase for GUI and uses searchengine |
| 4 | to prepare search pattern. |
| 5 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 6 | import fnmatch |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 7 | import os |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 8 | import sys |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 9 | |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 10 | from tkinter import StringVar, BooleanVar |
Terry Jan Reedy | aff0ada | 2019-01-02 22:04:06 -0500 | [diff] [blame] | 11 | from tkinter.ttk import Checkbutton # Frame imported in ...Base |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 12 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 13 | from idlelib.searchbase import SearchDialogBase |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 14 | from idlelib import searchengine |
| 15 | |
| 16 | # Importing OutputWindow here fails due to import loop |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 17 | # EditorWindow -> GrepDialog -> OutputWindow -> EditorWindow |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 18 | |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 19 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 20 | def grep(text, io=None, flist=None): |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 21 | """Open the Find in Files dialog. |
| 22 | |
| 23 | Module-level function to access the singleton GrepDialog |
| 24 | instance and open the dialog. If text is selected, it is |
| 25 | used as the search phrase; otherwise, the previous entry |
| 26 | is used. |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 27 | |
| 28 | Args: |
| 29 | text: Text widget that contains the selected text for |
| 30 | default search phrase. |
| 31 | io: iomenu.IOBinding instance with default path to search. |
| 32 | flist: filelist.FileList instance for OutputWindow parent. |
| 33 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 34 | root = text._root() |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 35 | engine = searchengine.get(root) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 36 | if not hasattr(engine, "_grepdialog"): |
| 37 | engine._grepdialog = GrepDialog(root, engine, flist) |
| 38 | dialog = engine._grepdialog |
Kurt B. Kaiser | ef58adf | 2003-06-07 03:21:17 +0000 | [diff] [blame] | 39 | searchphrase = text.get("sel.first", "sel.last") |
| 40 | dialog.open(text, searchphrase, io) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 41 | |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 42 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 43 | class GrepDialog(SearchDialogBase): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 44 | "Dialog for searching multiple files." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 45 | |
| 46 | title = "Find in Files Dialog" |
| 47 | icon = "Grep" |
| 48 | needwrapbutton = 0 |
| 49 | |
| 50 | def __init__(self, root, engine, flist): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 51 | """Create search dialog for searching for a phrase in the file system. |
| 52 | |
| 53 | Uses SearchDialogBase as the basis for the GUI and a |
| 54 | searchengine instance to prepare the search. |
| 55 | |
| 56 | Attributes: |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 57 | flist: filelist.Filelist instance for OutputWindow parent. |
| 58 | globvar: String value of Entry widget for path to search. |
| 59 | globent: Entry widget for globvar. Created in |
| 60 | create_entries(). |
| 61 | recvar: Boolean value of Checkbutton widget for |
| 62 | traversing through subdirectories. |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 63 | """ |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 64 | super().__init__(root, engine) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 65 | self.flist = flist |
| 66 | self.globvar = StringVar(root) |
| 67 | self.recvar = BooleanVar(root) |
| 68 | |
Kurt B. Kaiser | ef58adf | 2003-06-07 03:21:17 +0000 | [diff] [blame] | 69 | def open(self, text, searchphrase, io=None): |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 70 | """Make dialog visible on top of others and ready to use. |
| 71 | |
| 72 | Extend the SearchDialogBase open() to set the initial value |
| 73 | for globvar. |
| 74 | |
| 75 | Args: |
| 76 | text: Multicall object containing the text information. |
| 77 | searchphrase: String phrase to search. |
| 78 | io: iomenu.IOBinding instance containing file path. |
| 79 | """ |
Kurt B. Kaiser | ef58adf | 2003-06-07 03:21:17 +0000 | [diff] [blame] | 80 | SearchDialogBase.open(self, text, searchphrase) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 81 | if io: |
| 82 | path = io.filename or "" |
| 83 | else: |
| 84 | path = "" |
| 85 | dir, base = os.path.split(path) |
| 86 | head, tail = os.path.splitext(base) |
| 87 | if not tail: |
| 88 | tail = ".py" |
| 89 | self.globvar.set(os.path.join(dir, "*" + tail)) |
| 90 | |
| 91 | def create_entries(self): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 92 | "Create base entry widgets and add widget for search path." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 93 | SearchDialogBase.create_entries(self) |
Terry Jan Reedy | 5283c4e | 2014-07-13 17:27:26 -0400 | [diff] [blame] | 94 | self.globent = self.make_entry("In files:", self.globvar)[0] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 95 | |
| 96 | def create_other_buttons(self): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 97 | "Add check button to recurse down subdirectories." |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 98 | btn = Checkbutton( |
| 99 | self.make_frame()[0], variable=self.recvar, |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 100 | text="Recurse down subdirectories") |
| 101 | btn.pack(side="top", fill="both") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | |
| 103 | def create_command_buttons(self): |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 104 | "Create base command buttons and add button for Search Files." |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | SearchDialogBase.create_command_buttons(self) |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 106 | self.make_button("Search Files", self.default_command, isdef=True) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 107 | |
| 108 | def default_command(self, event=None): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 109 | """Grep for search pattern in file path. The default command is bound |
| 110 | to <Return>. |
| 111 | |
| 112 | If entry values are populated, set OutputWindow as stdout |
| 113 | and perform search. The search dialog is closed automatically |
| 114 | when the search begins. |
| 115 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 116 | prog = self.engine.getprog() |
| 117 | if not prog: |
| 118 | return |
| 119 | path = self.globvar.get() |
| 120 | if not path: |
| 121 | self.top.bell() |
| 122 | return |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 123 | from idlelib.outwin import OutputWindow # leave here! |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 124 | save = sys.stdout |
| 125 | try: |
| 126 | sys.stdout = OutputWindow(self.flist) |
| 127 | self.grep_it(prog, path) |
| 128 | finally: |
| 129 | sys.stdout = save |
| 130 | |
| 131 | def grep_it(self, prog, path): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 132 | """Search for prog within the lines of the files in path. |
| 133 | |
| 134 | For the each file in the path directory, open the file and |
| 135 | search each line for the matching pattern. If the pattern is |
| 136 | found, write the file and line information to stdout (which |
| 137 | is an OutputWindow). |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 138 | |
| 139 | Args: |
| 140 | prog: The compiled, cooked search pattern. |
| 141 | path: String containing the search path. |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 142 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 143 | dir, base = os.path.split(path) |
| 144 | list = self.findfiles(dir, base, self.recvar.get()) |
| 145 | list.sort() |
| 146 | self.close() |
| 147 | pat = self.engine.getpat() |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 148 | print(f"Searching {pat!r} in {path} ...") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 149 | hits = 0 |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 150 | try: |
| 151 | for fn in list: |
| 152 | try: |
| 153 | with open(fn, errors='replace') as f: |
| 154 | for lineno, line in enumerate(f, 1): |
| 155 | if line[-1:] == '\n': |
| 156 | line = line[:-1] |
| 157 | if prog.search(line): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 158 | sys.stdout.write(f"{fn}: {lineno}: {line}\n") |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 159 | hits += 1 |
| 160 | except OSError as msg: |
| 161 | print(msg) |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 162 | print(f"Hits found: {hits}\n(Hint: right-click to open locations.)" |
| 163 | if hits else "No hits.") |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 164 | except AttributeError: |
| 165 | # Tk window has been closed, OutputWindow.text = None, |
| 166 | # so in OW.write, OW.text.insert fails. |
| 167 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 168 | |
| 169 | def findfiles(self, dir, base, rec): |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 170 | """Return list of files in the dir that match the base pattern. |
| 171 | |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 172 | Use the current directory if dir has no value. |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 173 | If rec is True, recursively iterate through subdirectories. |
Cheryl Sabella | 0bb5e75 | 2019-03-16 19:29:33 -0400 | [diff] [blame^] | 174 | |
| 175 | Args: |
| 176 | dir: Directory path to search. |
| 177 | base: File search pattern. |
| 178 | rec: Boolean for recursive search through subdirectories. |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 179 | """ |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 180 | try: |
| 181 | names = os.listdir(dir or os.curdir) |
Terry Jan Reedy | c3111fc | 2014-02-23 00:37:16 -0500 | [diff] [blame] | 182 | except OSError as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 183 | print(msg) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 184 | return [] |
| 185 | list = [] |
| 186 | subdirs = [] |
| 187 | for name in names: |
| 188 | fn = os.path.join(dir, name) |
| 189 | if os.path.isdir(fn): |
| 190 | subdirs.append(fn) |
| 191 | else: |
| 192 | if fnmatch.fnmatch(name, base): |
| 193 | list.append(fn) |
| 194 | if rec: |
| 195 | for subdir in subdirs: |
| 196 | list.extend(self.findfiles(subdir, base, rec)) |
| 197 | return list |
| 198 | |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 199 | |
Terry Jan Reedy | cd56736 | 2014-10-17 01:31:35 -0400 | [diff] [blame] | 200 | def _grep_dialog(parent): # htest # |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 201 | from tkinter import Toplevel, Text, SEL, END |
Terry Jan Reedy | aff0ada | 2019-01-02 22:04:06 -0500 | [diff] [blame] | 202 | from tkinter.ttk import Frame, Button |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 203 | from idlelib.pyshell import PyShellFileList |
Terry Jan Reedy | aff0ada | 2019-01-02 22:04:06 -0500 | [diff] [blame] | 204 | |
Terry Jan Reedy | b60adc5 | 2016-06-21 18:41:38 -0400 | [diff] [blame] | 205 | top = Toplevel(parent) |
| 206 | top.title("Test GrepDialog") |
Terry Jan Reedy | a748032 | 2016-07-10 17:28:10 -0400 | [diff] [blame] | 207 | x, y = map(int, parent.geometry().split('+')[1:]) |
csabella | 65474b9 | 2017-06-27 02:41:08 -0400 | [diff] [blame] | 208 | top.geometry(f"+{x}+{y + 175}") |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 209 | |
Terry Jan Reedy | b60adc5 | 2016-06-21 18:41:38 -0400 | [diff] [blame] | 210 | flist = PyShellFileList(top) |
Terry Jan Reedy | aff0ada | 2019-01-02 22:04:06 -0500 | [diff] [blame] | 211 | frame = Frame(top) |
| 212 | frame.pack() |
| 213 | text = Text(frame, height=5) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 214 | text.pack() |
| 215 | |
| 216 | def show_grep_dialog(): |
| 217 | text.tag_add(SEL, "1.0", END) |
| 218 | grep(text, flist=flist) |
| 219 | text.tag_remove(SEL, "1.0", END) |
| 220 | |
Terry Jan Reedy | aff0ada | 2019-01-02 22:04:06 -0500 | [diff] [blame] | 221 | button = Button(frame, text="Show GrepDialog", command=show_grep_dialog) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 222 | button.pack() |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 223 | |
Terry Jan Reedy | de3beb2 | 2013-06-22 18:26:51 -0400 | [diff] [blame] | 224 | if __name__ == "__main__": |
Terry Jan Reedy | ee5ef30 | 2018-06-15 18:20:55 -0400 | [diff] [blame] | 225 | from unittest import main |
| 226 | main('idlelib.idle_test.test_grep', verbosity=2, exit=False) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 227 | |
| 228 | from idlelib.idle_test.htest import run |
| 229 | run(_grep_dialog) |