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