B „ fÛã@sjddlmZGdd„dƒZGdd„dƒZdd„Zedkrfdd lmZed d d d �ddlm Z e eƒdS)é)ÚTclErrorc@s@eZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dS)ÚWidgetRedirectoraõSupport for redirecting arbitrary widget subcommands. Some Tk operations don't normally pass through tkinter. For example, if a character is inserted into a Text widget by pressing a key, a default Tk binding to the widget's 'insert' operation is activated, and the Tk library processes the insert without calling back into tkinter. Although a binding to could be made via tkinter, what we really want to do is to hook the Tk 'insert' operation itself. For one thing, we want a text.insert call in idle code to have the same effect as a key press. When a widget is instantiated, a Tcl command is created whose name is the same as the pathname widget._w. This command is used to invoke the various widget operations, e.g. insert (for a Text widget). We are going to hook this command and provide a facility ('register') to intercept the widget operation. We will also intercept method calls on the tkinter class instance that represents the tk widget. In IDLE, WidgetRedirector is used in Percolator to intercept Text commands. The function being registered provides access to the top of a Percolator chain. At the bottom of the chain is a call to the original Tk widget operation. cCsJi|_||_|j|_}|j}|d|_| d||j¡| ||j¡dS)aÀInitialize attributes and setup redirection. _operations: dict mapping operation name to new function. widget: the widget whose tcl command is to be intercepted. tk: widget.tk, a convenience attribute, probably not needed. orig: new name of the original tcl command. Since renaming to orig fails with TclError when orig already exists, only one WidgetDirector can exist for a given widget. Z_origÚrenameN)Ú _operationsÚwidgetÚtkÚ_wÚorigÚcallZ createcommandÚdispatch)ÚselfrrÚw©rú7/opt/alt/python37/lib64/python3.7/idlelib/redirector.pyÚ__init__s   zWidgetRedirector.__init__cCsd|jj|jjj|jjfS)Nz %s(%s<%s>))Ú __class__Ú__name__rr)r rrrÚ__repr__1szWidgetRedirector.__repr__cCsVxt|jƒD]}| |¡q W|j}|j}|j}| |¡| d|j|¡|`|`dS)zBUnregister operations and revert redirection created by .__init__.rN) ÚlistrÚ unregisterrrrZ deletecommandr r )r Ú operationrrr rrrÚclose6s zWidgetRedirector.closecCs"||j|<t|j||ƒt||ƒS)a¶Return OriginalCommand(operation) after registering function. Registration adds an operation: function pair to ._operations. It also adds a widget function attribute that masks the tkinter class instance method. Method masking operates independently from command dispatch. If a second function is registered for the same operation, the first function is replaced in both places. )rÚsetattrrÚOriginalCommand)r rÚfunctionrrrÚregisterCs zWidgetRedirector.registercCsN||jkrF|j|}|j|=yt|j|ƒWntk r@YnX|SdSdS)z~Return the function for the operation, or None. Deleting the instance attribute unmasks the class attribute. N)rÚdelattrrÚAttributeError)r rrrrrrRs  zWidgetRedirector.unregistercGsL|j |¡}y&|r||ŽS|j |j|f|¡SWntk rFdSXdS)aÿCallback from Tcl which runs when the widget is referenced. If an operation has been registered in self._operations, apply the associated function to the args passed into Tcl. Otherwise, pass the operation through to Tk via the original Tcl function. Note that if a registered function is called, the operation is not passed through to Tk. Apply the function returned by self.register() to *args to accomplish that. For an example, see colorizer.py. ÚN)rÚgetrr r r)r rÚargsÚmrrrr bs zWidgetRedirector.dispatchN) rÚ __module__Ú __qualname__Ú__doc__rrrrrr rrrrrs rc@s(eZdZdZdd„Zdd„Zdd„ZdS) ra7Callable for original tk command that has been redirected. Returned by .register; can be used in the function registered. redir = WidgetRedirector(text) def my_insert(*args): print("insert", args) original_insert(*args) original_insert = redir.register("insert", my_insert) cCs6||_||_|j|_|j|_|jj|_|j|f|_dS)zÏCreate .tk_call and .orig_and_operation for .__call__ method. .redir and .operation store the input args for __repr__. .tk and .orig copy attributes of .redir (probably not needed). N)Úredirrrr r Útk_callÚorig_and_operation)r r%rrrrrƒs  zOriginalCommand.__init__cCsd|jj|j|jfS)Nz %s(%r, %r))rrr%r)r rrrr‘szOriginalCommand.__repr__cGs| |j|¡S)N)r&r')r r rrrÚ__call__•szOriginalCommand.__call__N)rr"r#r$rrr(rrrrrxs rc s”ddlm}m}||ƒ}| d¡tt| ¡ d¡dd…ƒ\}}| d||df¡||ƒ}| ¡|  ¡t |ƒ}‡fdd „}|  d |¡‰dS) Nr)ÚToplevelÚTextzTest WidgetRedirectorú+éz+%d+%dé¯cstd|ƒˆ|ŽdS)NÚinsert)Úprint)r )Úoriginal_insertrrÚ my_insert¤s z%_widget_redirector..my_insertr.) Útkinterr)r*ÚtitleÚmapÚintZgeometryÚsplitZpackZ focus_setrr) Úparentr)r*ÚtopÚxÚyÚtextr%r1r)r0rÚ_widget_redirector™s   r<Ú__main__)Úmainz!idlelib.idle_test.test_redirectoréF)Ú verbosityÚexit)ÚrunN) r2rrrr<rZunittestr>Zidlelib.idle_test.htestrBrrrrÚs u!