3 ^c@sjddlmZGdddZGdddZddZedkrfdd lmZed d d d ddlm Z e edS))TclErrorc@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)WidgetRedirectoraSupport 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|_|jd||j|j||jdS)aInitialize 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_origrenameN) _operationswidgettk_worigcallZ createcommanddispatch)selfrrwr */usr/lib64/python3.6/idlelib/redirector.py__init__s   zWidgetRedirector.__init__cCsd|jj|jjj|jjfS)Nz %s(%s<%s>)) __class____name__rrr r r r__repr__1szWidgetRedirector.__repr__cCsVxt|jD]}|j|q W|j}|j}|j}|j||jd|j||`|`dS)zBUnregister operations and revert redirection created by .__init__.rN) Zlistr unregisterrrrZ deletecommandr r)r operationrrr r r rclose6s zWidgetRedirector.closecCs"||j|<t|j||t||S)aReturn 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. )rZsetattrrOriginalCommandr rZfunctionr r rregisterCs zWidgetRedirector.registerc CsN||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)rZdelattrrZAttributeErrorrr r rrRs  zWidgetRedirector.unregisterc GsL|jj|}y&|r||S|jj|j|f|SWntk rFdSXdS)aCallback 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. ZN)rZgetrr rr)r rargsZmr r rr bs zWidgetRedirector.dispatchN) r __module__ __qualname____doc__rrrrrr r r r rrs rc@s(eZdZdZddZddZddZdS) 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)zCreate .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)redirrrrr tk_callorig_and_operation)r rrr r rrs  zOriginalCommand.__init__cCsd|jj|j|jfS)Nz %s(%r, %r))rrrrrr r rrszOriginalCommand.__repr__cGs|j|j|S)N)rr )r rr r r__call__szOriginalCommand.__call__N)rrrrrrr!r r r rrxs rc sddlm}m}||}|jdtt|jjddd\}}|jd||df||}|j|j t |}fdd }|j d |dS) Nr)ToplevelTextzTest WidgetRedirectorz+iz+%d+%dicstd||dS)Ninsert)Zprint)rZoriginal_insertr r my_inserts z%_widget_redirector..my_insertr$) tkinterr"r#ZtitleZmapZintZgeometryZsplitZpackZ focus_setrr) Zparentr"r#ZtopZxZyZtextrr&r r%r_widget_redirectors   r(Z__main__)mainz!idlelib.idle_test.test_redirectoriF)Z verbosityZexit)runN) r'rrrr(rZunittestr)Zidlelib.idle_test.htestr*r r r rZs u!