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