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

Python code coverage for Lib/idlelib/GrepDialog.py

#countcontent
1n/aimport os
2n/aimport fnmatch
3n/aimport sys
4n/afrom tkinter import *
5n/afrom idlelib import SearchEngine
6n/afrom idlelib.SearchDialogBase import SearchDialogBase
7n/a
8n/adef grep(text, io=None, flist=None):
9n/a root = text._root()
10n/a engine = SearchEngine.get(root)
11n/a if not hasattr(engine, "_grepdialog"):
12n/a engine._grepdialog = GrepDialog(root, engine, flist)
13n/a dialog = engine._grepdialog
14n/a searchphrase = text.get("sel.first", "sel.last")
15n/a dialog.open(text, searchphrase, io)
16n/a
17n/aclass GrepDialog(SearchDialogBase):
18n/a
19n/a title = "Find in Files Dialog"
20n/a icon = "Grep"
21n/a needwrapbutton = 0
22n/a
23n/a def __init__(self, root, engine, flist):
24n/a SearchDialogBase.__init__(self, root, engine)
25n/a self.flist = flist
26n/a self.globvar = StringVar(root)
27n/a self.recvar = BooleanVar(root)
28n/a
29n/a def open(self, text, searchphrase, io=None):
30n/a SearchDialogBase.open(self, text, searchphrase)
31n/a if io:
32n/a path = io.filename or ""
33n/a else:
34n/a path = ""
35n/a dir, base = os.path.split(path)
36n/a head, tail = os.path.splitext(base)
37n/a if not tail:
38n/a tail = ".py"
39n/a self.globvar.set(os.path.join(dir, "*" + tail))
40n/a
41n/a def create_entries(self):
42n/a SearchDialogBase.create_entries(self)
43n/a self.globent = self.make_entry("In files:", self.globvar)
44n/a
45n/a def create_other_buttons(self):
46n/a f = self.make_frame()
47n/a
48n/a btn = Checkbutton(f, anchor="w",
49n/a variable=self.recvar,
50n/a text="Recurse down subdirectories")
51n/a btn.pack(side="top", fill="both")
52n/a btn.select()
53n/a
54n/a def create_command_buttons(self):
55n/a SearchDialogBase.create_command_buttons(self)
56n/a self.make_button("Search Files", self.default_command, 1)
57n/a
58n/a def default_command(self, event=None):
59n/a prog = self.engine.getprog()
60n/a if not prog:
61n/a return
62n/a path = self.globvar.get()
63n/a if not path:
64n/a self.top.bell()
65n/a return
66n/a from idlelib.OutputWindow import OutputWindow
67n/a save = sys.stdout
68n/a try:
69n/a sys.stdout = OutputWindow(self.flist)
70n/a self.grep_it(prog, path)
71n/a finally:
72n/a sys.stdout = save
73n/a
74n/a def grep_it(self, prog, path):
75n/a dir, base = os.path.split(path)
76n/a list = self.findfiles(dir, base, self.recvar.get())
77n/a list.sort()
78n/a self.close()
79n/a pat = self.engine.getpat()
80n/a print("Searching %r in %s ..." % (pat, path))
81n/a hits = 0
82n/a for fn in list:
83n/a try:
84n/a with open(fn, errors='replace') as f:
85n/a for lineno, line in enumerate(f, 1):
86n/a if line[-1:] == '\n':
87n/a line = line[:-1]
88n/a if prog.search(line):
89n/a sys.stdout.write("%s: %s: %s\n" %
90n/a (fn, lineno, line))
91n/a hits += 1
92n/a except OSError as msg:
93n/a print(msg)
94n/a print(("Hits found: %s\n"
95n/a "(Hint: right-click to open locations.)"
96n/a % hits) if hits else "No hits.")
97n/a
98n/a def findfiles(self, dir, base, rec):
99n/a try:
100n/a names = os.listdir(dir or os.curdir)
101n/a except OSerror as msg:
102n/a print(msg)
103n/a return []
104n/a list = []
105n/a subdirs = []
106n/a for name in names:
107n/a fn = os.path.join(dir, name)
108n/a if os.path.isdir(fn):
109n/a subdirs.append(fn)
110n/a else:
111n/a if fnmatch.fnmatch(name, base):
112n/a list.append(fn)
113n/a if rec:
114n/a for subdir in subdirs:
115n/a list.extend(self.findfiles(subdir, base, rec))
116n/a return list
117n/a
118n/a def close(self, event=None):
119n/a if self.top:
120n/a self.top.grab_release()
121n/a self.top.withdraw()
122n/a
123n/aif __name__ == "__main__":
124n/a # A human test is a bit tricky since EditorWindow() imports this module.
125n/a # Hence Idle must be restarted after editing this file for a live test.
126n/a import unittest
127n/a unittest.main('idlelib.idle_test.test_grep', verbosity=2, exit=False)