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