Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
d11dde47
Commit
d11dde47
authored
Jul 10, 2014
by
Terry Jan Reedy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #21940: add docstrings to idlelib.WidgetRedirector.
parent
f97376f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
59 additions
and
18 deletions
+59
-18
Lib/idlelib/WidgetRedirector.py
Lib/idlelib/WidgetRedirector.py
+59
-18
No files found.
Lib/idlelib/WidgetRedirector.py
View file @
d11dde47
from
Tkinter
import
*
from
Tkinter
import
TclError
class
WidgetRedirector
:
class
WidgetRedirector
:
"""Support for redirecting arbitrary widget subcommands.
"""Support for redirecting arbitrary widget subcommands.
Some Tk operations don't normally pass through Tkinter. For example, if a
Some Tk operations don't normally pass through Tkinter. For example, if a
...
@@ -18,12 +17,22 @@ class WidgetRedirector:
...
@@ -18,12 +17,22 @@ class WidgetRedirector:
this command and provide a facility ('register') to intercept the widget
this command and provide a facility ('register') to intercept the widget
operation.
operation.
In IDLE,
the function being registered provides access to the top of a
In IDLE,
WidgetRedirector is used in Percolator to intercept Text
Percolator chain. At the bottom of the chain is a call to the original
commands. The function being registered provides access to the top
Tk widget operation.
of a Percolator chain. At the bottom of the chain is a call to the
original Tk widget operation.
"""
"""
def
__init__
(
self
,
widget
):
def
__init__
(
self
,
widget
):
'''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.
'''
self
.
_operations
=
{}
self
.
_operations
=
{}
self
.
widget
=
widget
# widget instance
self
.
widget
=
widget
# widget instance
self
.
tk
=
tk
=
widget
.
tk
# widget's root
self
.
tk
=
tk
=
widget
.
tk
# widget's root
...
@@ -40,22 +49,34 @@ class WidgetRedirector:
...
@@ -40,22 +49,34 @@ class WidgetRedirector:
self
.
widget
.
_w
)
self
.
widget
.
_w
)
def
close
(
self
):
def
close
(
self
):
"Unregister operations and revert redirection created by .__init__."
for
operation
in
list
(
self
.
_operations
):
for
operation
in
list
(
self
.
_operations
):
self
.
unregister
(
operation
)
self
.
unregister
(
operation
)
widget
=
self
.
widget
;
del
self
.
widget
widget
=
self
.
widget
orig
=
self
.
orig
;
del
self
.
orig
tk
=
widget
.
tk
tk
=
widget
.
tk
w
=
widget
.
_w
w
=
widget
.
_w
# Restore the original widget Tcl command.
tk
.
deletecommand
(
w
)
tk
.
deletecommand
(
w
)
# restore the original widget Tcl command:
tk
.
call
(
"rename"
,
self
.
orig
,
w
)
tk
.
call
(
"rename"
,
orig
,
w
)
del
self
.
widget
,
self
.
tk
# Should not be needed
# if instance is deleted after close, as in Percolator.
def
register
(
self
,
operation
,
function
):
def
register
(
self
,
operation
,
function
):
'''Return OriginalCommand(operation) after registering function.
Registration adds an instance function attribute that masks the
class instance method attribute. If a second function is
registered for the same operation, the first function is replaced.
'''
self
.
_operations
[
operation
]
=
function
self
.
_operations
[
operation
]
=
function
setattr
(
self
.
widget
,
operation
,
function
)
setattr
(
self
.
widget
,
operation
,
function
)
return
OriginalCommand
(
self
,
operation
)
return
OriginalCommand
(
self
,
operation
)
def
unregister
(
self
,
operation
):
def
unregister
(
self
,
operation
):
'''Return the function for the operation, or None.
Deleting the instance attribute unmasks the class attribute.
'''
if
operation
in
self
.
_operations
:
if
operation
in
self
.
_operations
:
function
=
self
.
_operations
[
operation
]
function
=
self
.
_operations
[
operation
]
del
self
.
_operations
[
operation
]
del
self
.
_operations
[
operation
]
...
@@ -88,14 +109,29 @@ class WidgetRedirector:
...
@@ -88,14 +109,29 @@ class WidgetRedirector:
class
OriginalCommand
:
class
OriginalCommand
:
'''Callable 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)
'''
def
__init__
(
self
,
redir
,
operation
):
def
__init__
(
self
,
redir
,
operation
):
'''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).
'''
self
.
redir
=
redir
self
.
redir
=
redir
self
.
operation
=
operation
self
.
operation
=
operation
self
.
tk
=
redir
.
tk
self
.
tk
=
redir
.
tk
# redundant with self.redir
self
.
orig
=
redir
.
orig
self
.
orig
=
redir
.
orig
# redundant with self.redir
self
.
tk_call
=
self
.
tk
.
call
# These two could be deleted after checking recipient code.
self
.
orig_and_operation
=
(
self
.
orig
,
self
.
operation
)
self
.
tk_call
=
redir
.
tk
.
call
self
.
orig_and_operation
=
(
redir
.
orig
,
operation
)
def
__repr__
(
self
):
def
__repr__
(
self
):
return
"OriginalCommand(%r, %r)"
%
(
self
.
redir
,
self
.
operation
)
return
"OriginalCommand(%r, %r)"
%
(
self
.
redir
,
self
.
operation
)
...
@@ -104,7 +140,10 @@ class OriginalCommand:
...
@@ -104,7 +140,10 @@ class OriginalCommand:
return
self
.
tk_call
(
self
.
orig_and_operation
+
args
)
return
self
.
tk_call
(
self
.
orig_and_operation
+
args
)
def
_widget_redirector
(
parent
):
def
_widget_redirector
(
parent
):
# htest #
from
Tkinter
import
Tk
,
Text
import
re
root
=
Tk
()
root
=
Tk
()
root
.
title
(
"Test WidgetRedirector"
)
root
.
title
(
"Test WidgetRedirector"
)
width
,
height
,
x
,
y
=
list
(
map
(
int
,
re
.
split
(
'[x+]'
,
parent
.
geometry
())))
width
,
height
,
x
,
y
=
list
(
map
(
int
,
re
.
split
(
'[x+]'
,
parent
.
geometry
())))
...
@@ -113,13 +152,15 @@ def _widget_redirector(parent):
...
@@ -113,13 +152,15 @@ def _widget_redirector(parent):
text
.
pack
()
text
.
pack
()
text
.
focus_set
()
text
.
focus_set
()
redir
=
WidgetRedirector
(
text
)
redir
=
WidgetRedirector
(
text
)
global
previous_tcl_fcn
def
my_insert
(
*
args
):
def
my_insert
(
*
args
):
print
"insert"
,
args
print
"insert"
,
args
previous_tcl_fcn
(
*
args
)
original_insert
(
*
args
)
previous_tcl_fcn
=
redir
.
register
(
"insert"
,
my_insert
)
original_insert
=
redir
.
register
(
"insert"
,
my_insert
)
root
.
mainloop
()
root
.
mainloop
()
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
import
unittest
## unittest.main('idlelib.idle_test.test_widgetredir',
## verbosity=2, exit=False)
from
idlelib.idle_test.htest
import
run
from
idlelib.idle_test.htest
import
run
run
(
_widget_redirector
)
run
(
_widget_redirector
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment