<s> from . WidgetRedirector import WidgetRedirector <EOL> from . Delegator import Delegator <EOL> class Percolator : <EOL> def __init__ ( self , text ) : <EOL> self . text = text <EOL> self . redir = WidgetRedirector ( text ) <EOL> self . top = self . bottom = Delegator ( text ) <EOL> self . bottom . insert = self . redir . register ( "<STR_LIT>" , self . insert ) <EOL> self . bottom . delete = self . redir . register ( "<STR_LIT>" , self . delete ) <EOL> self . filters = [ ] <EOL> def close ( self ) : <EOL> while self . top is not self . bottom : <EOL> self . removefilter ( self . top ) <EOL> self . top = None <EOL> self . bottom . setdelegate ( None ) ; self . bottom = None <EOL> self . redir . close ( ) ; self . redir = None <EOL> self . text = None <EOL> def insert ( self , index , chars , tags = None ) : <EOL> self . top . insert ( index , chars , tags ) <EOL> def delete ( self , index1 , index2 = None ) : <EOL> self . top . delete ( index1 , index2 ) <EOL> def insertfilter ( self , filter ) : <EOL> assert isinstance ( filter , Delegator ) <EOL> assert filter . delegate is None <EOL> filter . setdelegate ( self . top ) <EOL> self . top = filter <EOL> def removefilter ( self , filter ) : <EOL> assert isinstance ( filter , Delegator ) <EOL> assert filter . delegate is not None <EOL> f = self . top <EOL> if f is filter : <EOL> self . top = filter . delegate <EOL> filter . setdelegate ( None ) <EOL> else : <EOL> while f . delegate is not filter : <EOL> assert f is not self . bottom <EOL> f . resetcache ( ) <EOL> f = f . delegate <EOL> f . setdelegate ( filter . delegate ) <EOL> filter . setdelegate ( None ) <EOL> def main ( ) : <EOL> import tkinter as Tk <EOL> class Tracer ( Delegator ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> Delegator . __init__ ( self , None ) <EOL> def insert ( self , * args ) : <EOL> print ( self . name , "<STR_LIT>" , args ) <EOL> self . delegate . insert ( * args ) <EOL> def delete ( self , * args ) : <EOL> print ( self . name , "<STR_LIT>" , args ) <EOL> self . delegate . delete ( * args ) <EOL> root = Tk . Tk ( ) <EOL> root . wm_protocol ( "<STR_LIT>" , root . quit ) <EOL> text = Tk . Text ( ) <EOL> text . pack ( ) <EOL> text . focus_set ( ) <EOL> p = Percolator ( text ) <EOL> t1 = Tracer ( "<STR_LIT>" ) <EOL> t2 = Tracer ( "<STR_LIT>" ) <EOL> p . insertfilter ( t1 ) <EOL> p . insertfilter ( t2 ) <EOL> root . mainloop ( ) <EOL> p . removefilter ( t2 ) <EOL> root . mainloop ( ) <EOL> p . insertfilter ( t2 ) <EOL> p . removefilter ( t1 ) <EOL> root . mainloop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
