ยปCore Development>Code coverage>Lib/idlelib/grep.py

Python code coverage for Lib/idlelib/grep.py

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