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