ยปCore Development>Code coverage>Tools/demo/redemo.py

Python code coverage for Tools/demo/redemo.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/a"""Basic regular expression demonstration facility (Perl style syntax)."""
4n/a
5n/afrom tkinter import *
6n/aimport re
7n/a
8n/aclass ReDemo:
9n/a
10n/a def __init__(self, master):
11n/a self.master = master
12n/a
13n/a self.promptdisplay = Label(self.master, anchor=W,
14n/a text="Enter a Perl-style regular expression:")
15n/a self.promptdisplay.pack(side=TOP, fill=X)
16n/a
17n/a self.regexdisplay = Entry(self.master)
18n/a self.regexdisplay.pack(fill=X)
19n/a self.regexdisplay.focus_set()
20n/a
21n/a self.addoptions()
22n/a
23n/a self.statusdisplay = Label(self.master, text="", anchor=W)
24n/a self.statusdisplay.pack(side=TOP, fill=X)
25n/a
26n/a self.labeldisplay = Label(self.master, anchor=W,
27n/a text="Enter a string to search:")
28n/a self.labeldisplay.pack(fill=X)
29n/a self.labeldisplay.pack(fill=X)
30n/a
31n/a self.showframe = Frame(master)
32n/a self.showframe.pack(fill=X, anchor=W)
33n/a
34n/a self.showvar = StringVar(master)
35n/a self.showvar.set("first")
36n/a
37n/a self.showfirstradio = Radiobutton(self.showframe,
38n/a text="Highlight first match",
39n/a variable=self.showvar,
40n/a value="first",
41n/a command=self.recompile)
42n/a self.showfirstradio.pack(side=LEFT)
43n/a
44n/a self.showallradio = Radiobutton(self.showframe,
45n/a text="Highlight all matches",
46n/a variable=self.showvar,
47n/a value="all",
48n/a command=self.recompile)
49n/a self.showallradio.pack(side=LEFT)
50n/a
51n/a self.stringdisplay = Text(self.master, width=60, height=4)
52n/a self.stringdisplay.pack(fill=BOTH, expand=1)
53n/a self.stringdisplay.tag_configure("hit", background="yellow")
54n/a
55n/a self.grouplabel = Label(self.master, text="Groups:", anchor=W)
56n/a self.grouplabel.pack(fill=X)
57n/a
58n/a self.grouplist = Listbox(self.master)
59n/a self.grouplist.pack(expand=1, fill=BOTH)
60n/a
61n/a self.regexdisplay.bind('<Key>', self.recompile)
62n/a self.stringdisplay.bind('<Key>', self.reevaluate)
63n/a
64n/a self.compiled = None
65n/a self.recompile()
66n/a
67n/a btags = self.regexdisplay.bindtags()
68n/a self.regexdisplay.bindtags(btags[1:] + btags[:1])
69n/a
70n/a btags = self.stringdisplay.bindtags()
71n/a self.stringdisplay.bindtags(btags[1:] + btags[:1])
72n/a
73n/a def addoptions(self):
74n/a self.frames = []
75n/a self.boxes = []
76n/a self.vars = []
77n/a for name in ('IGNORECASE',
78n/a 'LOCALE',
79n/a 'MULTILINE',
80n/a 'DOTALL',
81n/a 'VERBOSE'):
82n/a if len(self.boxes) % 3 == 0:
83n/a frame = Frame(self.master)
84n/a frame.pack(fill=X)
85n/a self.frames.append(frame)
86n/a val = getattr(re, name)
87n/a var = IntVar()
88n/a box = Checkbutton(frame,
89n/a variable=var, text=name,
90n/a offvalue=0, onvalue=val,
91n/a command=self.recompile)
92n/a box.pack(side=LEFT)
93n/a self.boxes.append(box)
94n/a self.vars.append(var)
95n/a
96n/a def getflags(self):
97n/a flags = 0
98n/a for var in self.vars:
99n/a flags = flags | var.get()
100n/a flags = flags
101n/a return flags
102n/a
103n/a def recompile(self, event=None):
104n/a try:
105n/a self.compiled = re.compile(self.regexdisplay.get(),
106n/a self.getflags())
107n/a bg = self.promptdisplay['background']
108n/a self.statusdisplay.config(text="", background=bg)
109n/a except re.error as msg:
110n/a self.compiled = None
111n/a self.statusdisplay.config(
112n/a text="re.error: %s" % str(msg),
113n/a background="red")
114n/a self.reevaluate()
115n/a
116n/a def reevaluate(self, event=None):
117n/a try:
118n/a self.stringdisplay.tag_remove("hit", "1.0", END)
119n/a except TclError:
120n/a pass
121n/a try:
122n/a self.stringdisplay.tag_remove("hit0", "1.0", END)
123n/a except TclError:
124n/a pass
125n/a self.grouplist.delete(0, END)
126n/a if not self.compiled:
127n/a return
128n/a self.stringdisplay.tag_configure("hit", background="yellow")
129n/a self.stringdisplay.tag_configure("hit0", background="orange")
130n/a text = self.stringdisplay.get("1.0", END)
131n/a last = 0
132n/a nmatches = 0
133n/a while last <= len(text):
134n/a m = self.compiled.search(text, last)
135n/a if m is None:
136n/a break
137n/a first, last = m.span()
138n/a if last == first:
139n/a last = first+1
140n/a tag = "hit0"
141n/a else:
142n/a tag = "hit"
143n/a pfirst = "1.0 + %d chars" % first
144n/a plast = "1.0 + %d chars" % last
145n/a self.stringdisplay.tag_add(tag, pfirst, plast)
146n/a if nmatches == 0:
147n/a self.stringdisplay.yview_pickplace(pfirst)
148n/a groups = list(m.groups())
149n/a groups.insert(0, m.group())
150n/a for i in range(len(groups)):
151n/a g = "%2d: %r" % (i, groups[i])
152n/a self.grouplist.insert(END, g)
153n/a nmatches = nmatches + 1
154n/a if self.showvar.get() == "first":
155n/a break
156n/a
157n/a if nmatches == 0:
158n/a self.statusdisplay.config(text="(no match)",
159n/a background="yellow")
160n/a else:
161n/a self.statusdisplay.config(text="")
162n/a
163n/a
164n/a# Main function, run when invoked as a stand-alone Python program.
165n/a
166n/adef main():
167n/a root = Tk()
168n/a demo = ReDemo(root)
169n/a root.protocol('WM_DELETE_WINDOW', root.quit)
170n/a root.mainloop()
171n/a
172n/aif __name__ == '__main__':
173n/a main()