David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | import fnmatch |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 2 | import os |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 3 | import sys |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 4 | |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 5 | from tkinter import StringVar, BooleanVar |
| 6 | from tkinter.ttk import Checkbutton |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 7 | |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 8 | from idlelib.searchbase import SearchDialogBase |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 9 | from idlelib import searchengine |
| 10 | |
| 11 | # Importing OutputWindow here fails due to import loop |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 12 | # EditorWindow -> GrepDialop -> OutputWindow -> EditorWindow |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 13 | |
| 14 | def grep(text, io=None, flist=None): |
| 15 | root = text._root() |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 16 | engine = searchengine.get(root) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 17 | if not hasattr(engine, "_grepdialog"): |
| 18 | engine._grepdialog = GrepDialog(root, engine, flist) |
| 19 | dialog = engine._grepdialog |
Kurt B. Kaiser | ef58adf | 2003-06-07 03:21:17 +0000 | [diff] [blame] | 20 | searchphrase = text.get("sel.first", "sel.last") |
| 21 | dialog.open(text, searchphrase, io) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 22 | |
| 23 | class GrepDialog(SearchDialogBase): |
| 24 | |
| 25 | title = "Find in Files Dialog" |
| 26 | icon = "Grep" |
| 27 | needwrapbutton = 0 |
| 28 | |
| 29 | def __init__(self, root, engine, flist): |
| 30 | SearchDialogBase.__init__(self, root, engine) |
| 31 | self.flist = flist |
| 32 | self.globvar = StringVar(root) |
| 33 | self.recvar = BooleanVar(root) |
| 34 | |
Kurt B. Kaiser | ef58adf | 2003-06-07 03:21:17 +0000 | [diff] [blame] | 35 | def open(self, text, searchphrase, io=None): |
| 36 | SearchDialogBase.open(self, text, searchphrase) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 37 | if io: |
| 38 | path = io.filename or "" |
| 39 | else: |
| 40 | path = "" |
| 41 | dir, base = os.path.split(path) |
| 42 | head, tail = os.path.splitext(base) |
| 43 | if not tail: |
| 44 | tail = ".py" |
| 45 | self.globvar.set(os.path.join(dir, "*" + tail)) |
| 46 | |
| 47 | def create_entries(self): |
| 48 | SearchDialogBase.create_entries(self) |
Terry Jan Reedy | 5283c4e | 2014-07-13 17:27:26 -0400 | [diff] [blame] | 49 | self.globent = self.make_entry("In files:", self.globvar)[0] |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 50 | |
| 51 | def create_other_buttons(self): |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 52 | btn = Checkbutton( |
| 53 | self.make_frame()[0], variable=self.recvar, |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 54 | text="Recurse down subdirectories") |
| 55 | btn.pack(side="top", fill="both") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 56 | |
| 57 | def create_command_buttons(self): |
| 58 | SearchDialogBase.create_command_buttons(self) |
| 59 | self.make_button("Search Files", self.default_command, 1) |
| 60 | |
| 61 | def default_command(self, event=None): |
| 62 | prog = self.engine.getprog() |
| 63 | if not prog: |
| 64 | return |
| 65 | path = self.globvar.get() |
| 66 | if not path: |
| 67 | self.top.bell() |
| 68 | return |
Terry Jan Reedy | 6fa5bdc | 2016-05-28 13:22:31 -0400 | [diff] [blame] | 69 | from idlelib.outwin import OutputWindow # leave here! |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 70 | save = sys.stdout |
| 71 | try: |
| 72 | sys.stdout = OutputWindow(self.flist) |
| 73 | self.grep_it(prog, path) |
| 74 | finally: |
| 75 | sys.stdout = save |
| 76 | |
| 77 | def grep_it(self, prog, path): |
| 78 | dir, base = os.path.split(path) |
| 79 | list = self.findfiles(dir, base, self.recvar.get()) |
| 80 | list.sort() |
| 81 | self.close() |
| 82 | pat = self.engine.getpat() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 83 | print("Searching %r in %s ..." % (pat, path)) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 84 | hits = 0 |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 85 | try: |
| 86 | for fn in list: |
| 87 | try: |
| 88 | with open(fn, errors='replace') as f: |
| 89 | for lineno, line in enumerate(f, 1): |
| 90 | if line[-1:] == '\n': |
| 91 | line = line[:-1] |
| 92 | if prog.search(line): |
| 93 | sys.stdout.write("%s: %s: %s\n" % |
| 94 | (fn, lineno, line)) |
| 95 | hits += 1 |
| 96 | except OSError as msg: |
| 97 | print(msg) |
| 98 | print(("Hits found: %s\n" |
| 99 | "(Hint: right-click to open locations.)" |
| 100 | % hits) if hits else "No hits.") |
| 101 | except AttributeError: |
| 102 | # Tk window has been closed, OutputWindow.text = None, |
| 103 | # so in OW.write, OW.text.insert fails. |
| 104 | pass |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 105 | |
| 106 | def findfiles(self, dir, base, rec): |
| 107 | try: |
| 108 | names = os.listdir(dir or os.curdir) |
Terry Jan Reedy | c3111fc | 2014-02-23 00:37:16 -0500 | [diff] [blame] | 109 | except OSError as msg: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 110 | print(msg) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 111 | return [] |
| 112 | list = [] |
| 113 | subdirs = [] |
| 114 | for name in names: |
| 115 | fn = os.path.join(dir, name) |
| 116 | if os.path.isdir(fn): |
| 117 | subdirs.append(fn) |
| 118 | else: |
| 119 | if fnmatch.fnmatch(name, base): |
| 120 | list.append(fn) |
| 121 | if rec: |
| 122 | for subdir in subdirs: |
| 123 | list.extend(self.findfiles(subdir, base, rec)) |
| 124 | return list |
| 125 | |
| 126 | def close(self, event=None): |
| 127 | if self.top: |
| 128 | self.top.grab_release() |
| 129 | self.top.withdraw() |
Terry Jan Reedy | de3beb2 | 2013-06-22 18:26:51 -0400 | [diff] [blame] | 130 | |
Terry Jan Reedy | 4762382 | 2014-06-10 02:49:35 -0400 | [diff] [blame] | 131 | |
Terry Jan Reedy | cd56736 | 2014-10-17 01:31:35 -0400 | [diff] [blame] | 132 | def _grep_dialog(parent): # htest # |
Terry Jan Reedy | 6f7b0f5 | 2016-07-10 20:21:31 -0400 | [diff] [blame] | 133 | from tkinter import Toplevel, Text, SEL, END |
| 134 | from tkinter.ttk import Button |
Terry Jan Reedy | bfbaa6b | 2016-08-31 00:50:55 -0400 | [diff] [blame] | 135 | from idlelib.pyshell import PyShellFileList |
Terry Jan Reedy | b60adc5 | 2016-06-21 18:41:38 -0400 | [diff] [blame] | 136 | top = Toplevel(parent) |
| 137 | top.title("Test GrepDialog") |
Terry Jan Reedy | a748032 | 2016-07-10 17:28:10 -0400 | [diff] [blame] | 138 | x, y = map(int, parent.geometry().split('+')[1:]) |
| 139 | top.geometry("+%d+%d" % (x, y + 175)) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 140 | |
Terry Jan Reedy | b60adc5 | 2016-06-21 18:41:38 -0400 | [diff] [blame] | 141 | flist = PyShellFileList(top) |
| 142 | text = Text(top, height=5) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 143 | text.pack() |
| 144 | |
| 145 | def show_grep_dialog(): |
| 146 | text.tag_add(SEL, "1.0", END) |
| 147 | grep(text, flist=flist) |
| 148 | text.tag_remove(SEL, "1.0", END) |
| 149 | |
Terry Jan Reedy | b60adc5 | 2016-06-21 18:41:38 -0400 | [diff] [blame] | 150 | button = Button(top, text="Show GrepDialog", command=show_grep_dialog) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 151 | button.pack() |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 152 | |
Terry Jan Reedy | de3beb2 | 2013-06-22 18:26:51 -0400 | [diff] [blame] | 153 | if __name__ == "__main__": |
Terry Jan Reedy | de3beb2 | 2013-06-22 18:26:51 -0400 | [diff] [blame] | 154 | import unittest |
| 155 | unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False) |
Terry Jan Reedy | 2e8234a | 2014-05-29 01:46:26 -0400 | [diff] [blame] | 156 | |
| 157 | from idlelib.idle_test.htest import run |
| 158 | run(_grep_dialog) |