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

Python code coverage for Lib/idlelib/percolator.py

#countcontent
1n/afrom idlelib.delegator import Delegator
2n/afrom idlelib.redirector import WidgetRedirector
3n/a
4n/a
5n/aclass Percolator:
6n/a
7n/a def __init__(self, text):
8n/a # XXX would be nice to inherit from Delegator
9n/a self.text = text
10n/a self.redir = WidgetRedirector(text)
11n/a self.top = self.bottom = Delegator(text)
12n/a self.bottom.insert = self.redir.register("insert", self.insert)
13n/a self.bottom.delete = self.redir.register("delete", self.delete)
14n/a self.filters = []
15n/a
16n/a def close(self):
17n/a while self.top is not self.bottom:
18n/a self.removefilter(self.top)
19n/a self.top = None
20n/a self.bottom.setdelegate(None)
21n/a self.bottom = None
22n/a self.redir.close()
23n/a self.redir = None
24n/a self.text = None
25n/a
26n/a def insert(self, index, chars, tags=None):
27n/a # Could go away if inheriting from Delegator
28n/a self.top.insert(index, chars, tags)
29n/a
30n/a def delete(self, index1, index2=None):
31n/a # Could go away if inheriting from Delegator
32n/a self.top.delete(index1, index2)
33n/a
34n/a def insertfilter(self, filter):
35n/a # Perhaps rename to pushfilter()?
36n/a assert isinstance(filter, Delegator)
37n/a assert filter.delegate is None
38n/a filter.setdelegate(self.top)
39n/a self.top = filter
40n/a
41n/a def removefilter(self, filter):
42n/a # XXX Perhaps should only support popfilter()?
43n/a assert isinstance(filter, Delegator)
44n/a assert filter.delegate is not None
45n/a f = self.top
46n/a if f is filter:
47n/a self.top = filter.delegate
48n/a filter.setdelegate(None)
49n/a else:
50n/a while f.delegate is not filter:
51n/a assert f is not self.bottom
52n/a f.resetcache()
53n/a f = f.delegate
54n/a f.setdelegate(filter.delegate)
55n/a filter.setdelegate(None)
56n/a
57n/a
58n/adef _percolator(parent): # htest #
59n/a import tkinter as tk
60n/a
61n/a class Tracer(Delegator):
62n/a def __init__(self, name):
63n/a self.name = name
64n/a Delegator.__init__(self, None)
65n/a
66n/a def insert(self, *args):
67n/a print(self.name, ": insert", args)
68n/a self.delegate.insert(*args)
69n/a
70n/a def delete(self, *args):
71n/a print(self.name, ": delete", args)
72n/a self.delegate.delete(*args)
73n/a
74n/a box = tk.Toplevel(parent)
75n/a box.title("Test Percolator")
76n/a x, y = map(int, parent.geometry().split('+')[1:])
77n/a box.geometry("+%d+%d" % (x, y + 175))
78n/a text = tk.Text(box)
79n/a p = Percolator(text)
80n/a pin = p.insertfilter
81n/a pout = p.removefilter
82n/a t1 = Tracer("t1")
83n/a t2 = Tracer("t2")
84n/a
85n/a def toggle1():
86n/a (pin if var1.get() else pout)(t1)
87n/a def toggle2():
88n/a (pin if var2.get() else pout)(t2)
89n/a
90n/a text.pack()
91n/a var1 = tk.IntVar(parent)
92n/a cb1 = tk.Checkbutton(box, text="Tracer1", command=toggle1, variable=var1)
93n/a cb1.pack()
94n/a var2 = tk.IntVar(parent)
95n/a cb2 = tk.Checkbutton(box, text="Tracer2", command=toggle2, variable=var2)
96n/a cb2.pack()
97n/a
98n/aif __name__ == "__main__":
99n/a import unittest
100n/a unittest.main('idlelib.idle_test.test_percolator', verbosity=2,
101n/a exit=False)
102n/a
103n/a from idlelib.idle_test.htest import run
104n/a run(_percolator)