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

Python code coverage for Lib/idlelib/searchbase.py

#countcontent
1n/a'''Define SearchDialogBase used by Search, Replace, and Grep dialogs.'''
2n/a
3n/afrom tkinter import Toplevel, Frame
4n/afrom tkinter.ttk import Entry, Label, Button, Checkbutton, Radiobutton
5n/a
6n/a
7n/aclass SearchDialogBase:
8n/a '''Create most of a 3 or 4 row, 3 column search dialog.
9n/a
10n/a The left and wide middle column contain:
11n/a 1 or 2 labeled text entry lines (make_entry, create_entries);
12n/a a row of standard Checkbuttons (make_frame, create_option_buttons),
13n/a each of which corresponds to a search engine Variable;
14n/a a row of dialog-specific Check/Radiobuttons (create_other_buttons).
15n/a
16n/a The narrow right column contains command buttons
17n/a (make_button, create_command_buttons).
18n/a These are bound to functions that execute the command.
19n/a
20n/a Except for command buttons, this base class is not limited to items
21n/a common to all three subclasses. Rather, it is the Find dialog minus
22n/a the "Find Next" command, its execution function, and the
23n/a default_command attribute needed in create_widgets. The other
24n/a dialogs override attributes and methods, the latter to replace and
25n/a add widgets.
26n/a '''
27n/a
28n/a title = "Search Dialog" # replace in subclasses
29n/a icon = "Search"
30n/a needwrapbutton = 1 # not in Find in Files
31n/a
32n/a def __init__(self, root, engine):
33n/a '''Initialize root, engine, and top attributes.
34n/a
35n/a top (level widget): set in create_widgets() called from open().
36n/a text (Text searched): set in open(), only used in subclasses().
37n/a ent (ry): created in make_entry() called from create_entry().
38n/a row (of grid): 0 in create_widgets(), +1 in make_entry/frame().
39n/a default_command: set in subclasses, used in create_widgers().
40n/a
41n/a title (of dialog): class attribute, override in subclasses.
42n/a icon (of dialog): ditto, use unclear if cannot minimize dialog.
43n/a '''
44n/a self.root = root
45n/a self.engine = engine
46n/a self.top = None
47n/a
48n/a def open(self, text, searchphrase=None):
49n/a "Make dialog visible on top of others and ready to use."
50n/a self.text = text
51n/a if not self.top:
52n/a self.create_widgets()
53n/a else:
54n/a self.top.deiconify()
55n/a self.top.tkraise()
56n/a if searchphrase:
57n/a self.ent.delete(0,"end")
58n/a self.ent.insert("end",searchphrase)
59n/a self.ent.focus_set()
60n/a self.ent.selection_range(0, "end")
61n/a self.ent.icursor(0)
62n/a self.top.grab_set()
63n/a
64n/a def close(self, event=None):
65n/a "Put dialog away for later use."
66n/a if self.top:
67n/a self.top.grab_release()
68n/a self.top.withdraw()
69n/a
70n/a def create_widgets(self):
71n/a '''Create basic 3 row x 3 col search (find) dialog.
72n/a
73n/a Other dialogs override subsidiary create_x methods as needed.
74n/a Replace and Find-in-Files add another entry row.
75n/a '''
76n/a top = Toplevel(self.root)
77n/a top.bind("<Return>", self.default_command)
78n/a top.bind("<Escape>", self.close)
79n/a top.protocol("WM_DELETE_WINDOW", self.close)
80n/a top.wm_title(self.title)
81n/a top.wm_iconname(self.icon)
82n/a self.top = top
83n/a self.bell = top.bell
84n/a
85n/a self.row = 0
86n/a self.top.grid_columnconfigure(0, pad=2, weight=0)
87n/a self.top.grid_columnconfigure(1, pad=2, minsize=100, weight=100)
88n/a
89n/a self.create_entries() # row 0 (and maybe 1), cols 0, 1
90n/a self.create_option_buttons() # next row, cols 0, 1
91n/a self.create_other_buttons() # next row, cols 0, 1
92n/a self.create_command_buttons() # col 2, all rows
93n/a
94n/a def make_entry(self, label_text, var):
95n/a '''Return (entry, label), .
96n/a
97n/a entry - gridded labeled Entry for text entry.
98n/a label - Label widget, returned for testing.
99n/a '''
100n/a label = Label(self.top, text=label_text)
101n/a label.grid(row=self.row, column=0, sticky="nw")
102n/a entry = Entry(self.top, textvariable=var, exportselection=0)
103n/a entry.grid(row=self.row, column=1, sticky="nwe")
104n/a self.row = self.row + 1
105n/a return entry, label
106n/a
107n/a def create_entries(self):
108n/a "Create one or more entry lines with make_entry."
109n/a self.ent = self.make_entry("Find:", self.engine.patvar)[0]
110n/a
111n/a def make_frame(self,labeltext=None):
112n/a '''Return (frame, label).
113n/a
114n/a frame - gridded labeled Frame for option or other buttons.
115n/a label - Label widget, returned for testing.
116n/a '''
117n/a if labeltext:
118n/a label = Label(self.top, text=labeltext)
119n/a label.grid(row=self.row, column=0, sticky="nw")
120n/a else:
121n/a label = ''
122n/a frame = Frame(self.top)
123n/a frame.grid(row=self.row, column=1, columnspan=1, sticky="nwe")
124n/a self.row = self.row + 1
125n/a return frame, label
126n/a
127n/a def create_option_buttons(self):
128n/a '''Return (filled frame, options) for testing.
129n/a
130n/a Options is a list of searchengine booleanvar, label pairs.
131n/a A gridded frame from make_frame is filled with a Checkbutton
132n/a for each pair, bound to the var, with the corresponding label.
133n/a '''
134n/a frame = self.make_frame("Options")[0]
135n/a engine = self.engine
136n/a options = [(engine.revar, "Regular expression"),
137n/a (engine.casevar, "Match case"),
138n/a (engine.wordvar, "Whole word")]
139n/a if self.needwrapbutton:
140n/a options.append((engine.wrapvar, "Wrap around"))
141n/a for var, label in options:
142n/a btn = Checkbutton(frame, variable=var, text=label)
143n/a btn.pack(side="left", fill="both")
144n/a return frame, options
145n/a
146n/a def create_other_buttons(self):
147n/a '''Return (frame, others) for testing.
148n/a
149n/a Others is a list of value, label pairs.
150n/a A gridded frame from make_frame is filled with radio buttons.
151n/a '''
152n/a frame = self.make_frame("Direction")[0]
153n/a var = self.engine.backvar
154n/a others = [(1, 'Up'), (0, 'Down')]
155n/a for val, label in others:
156n/a btn = Radiobutton(frame, variable=var, value=val, text=label)
157n/a btn.pack(side="left", fill="both")
158n/a return frame, others
159n/a
160n/a def make_button(self, label, command, isdef=0):
161n/a "Return command button gridded in command frame."
162n/a b = Button(self.buttonframe,
163n/a text=label, command=command,
164n/a default=isdef and "active" or "normal")
165n/a cols,rows=self.buttonframe.grid_size()
166n/a b.grid(pady=1,row=rows,column=0,sticky="ew")
167n/a self.buttonframe.grid(rowspan=rows+1)
168n/a return b
169n/a
170n/a def create_command_buttons(self):
171n/a "Place buttons in vertical command frame gridded on right."
172n/a f = self.buttonframe = Frame(self.top)
173n/a f.grid(row=0,column=2,padx=2,pady=2,ipadx=2,ipady=2)
174n/a
175n/a b = self.make_button("close", self.close)
176n/a b.lower()
177n/a
178n/a
179n/aclass _searchbase(SearchDialogBase): # htest #
180n/a "Create auto-opening dialog with no text connection."
181n/a
182n/a def __init__(self, parent):
183n/a import re
184n/a from idlelib import searchengine
185n/a
186n/a self.root = parent
187n/a self.engine = searchengine.get(parent)
188n/a self.create_widgets()
189n/a print(parent.geometry())
190n/a width,height, x,y = list(map(int, re.split('[x+]', parent.geometry())))
191n/a self.top.geometry("+%d+%d" % (x + 40, y + 175))
192n/a
193n/a def default_command(self, dummy): pass
194n/a
195n/aif __name__ == '__main__':
196n/a import unittest
197n/a unittest.main('idlelib.idle_test.test_searchbase', verbosity=2, exit=False)
198n/a
199n/a from idlelib.idle_test.htest import run
200n/a run(_searchbase)