5
5
- reparse when source changed (maybe just a button would be OK?)
6
6
(or recheck on window popup)
7
7
- add popup menu with more options (e.g. doc strings, base classes, imports)
8
- - show function argument list? (have to do pattern matching on source)
9
- - should the classes and methods lists also be in the module's menu bar?
10
8
- add base classes to class browser tree
9
+ - finish removing limitation to x.py files (ModuleBrowserTreeItem)
11
10
"""
12
11
13
12
import os
@@ -58,19 +57,18 @@ def transform_children(child_dict, modname=None):
58
57
class ModuleBrowser :
59
58
"""Browse module classes and functions in IDLE.
60
59
"""
61
- # This class is the base class for pathbrowser.PathBrowser.
60
+ # This class is also the base class for pathbrowser.PathBrowser.
62
61
# Init and close are inherited, other methods are overriden.
62
+ # PathBrowser.__init__ does not call __init__ below.
63
63
64
- def __init__ (self , flist , name , path , * , _htest = False , _utest = False ):
65
- # XXX This API should change, if the file doesn't end in ".py"
66
- # XXX the code here is bogus!
64
+ def __init__ (self , master , path , * , _htest = False , _utest = False ):
67
65
"""Create a window for browsing a module's structure.
68
66
69
67
Args:
70
- flist: filelist.FileList instance used as the root for the window .
71
- name: Python module to parse .
72
- path: Module search path .
73
- _htest - bool, change box when location running htest .
68
+ master: parent for widgets .
69
+ path: full path of file to browse .
70
+ _htest - bool; change box location when running htest .
71
+ -utest - bool; suppress contents when running unittest .
74
72
75
73
Global variables:
76
74
file_open: Function used for opening a file.
@@ -84,35 +82,36 @@ def __init__(self, flist, name, path, *, _htest=False, _utest=False):
84
82
global file_open
85
83
if not (_htest or _utest ):
86
84
file_open = pyshell .flist .open
87
- self .name = name
88
- self .file = os . path . join ( path [ 0 ], self . name + ".py" )
85
+ self .master = master
86
+ self .path = path
89
87
self ._htest = _htest
90
88
self ._utest = _utest
91
- self .init (flist )
89
+ self .init ()
92
90
93
91
def close (self , event = None ):
94
92
"Dismiss the window and the tree nodes."
95
93
self .top .destroy ()
96
94
self .node .destroy ()
97
95
98
- def init (self , flist ):
96
+ def init (self ):
99
97
"Create browser tkinter widgets, including the tree."
100
- self . flist = flist
98
+ root = self . master
101
99
# reset pyclbr
102
100
pyclbr ._modules .clear ()
103
101
# create top
104
- self .top = top = ListedToplevel (flist . root )
102
+ self .top = top = ListedToplevel (root )
105
103
top .protocol ("WM_DELETE_WINDOW" , self .close )
106
104
top .bind ("<Escape>" , self .close )
107
105
if self ._htest : # place dialog below parent if running htest
108
106
top .geometry ("+%d+%d" %
109
- (flist . root .winfo_rootx (), flist . root .winfo_rooty () + 200 ))
107
+ (root .winfo_rootx (), root .winfo_rooty () + 200 ))
110
108
self .settitle ()
111
109
top .focus_set ()
112
110
# create scrolled canvas
113
111
theme = idleConf .CurrentTheme ()
114
112
background = idleConf .GetHighlight (theme , 'normal' )['background' ]
115
- sc = ScrolledCanvas (top , bg = background , highlightthickness = 0 , takefocus = 1 )
113
+ sc = ScrolledCanvas (top , bg = background , highlightthickness = 0 ,
114
+ takefocus = 1 )
116
115
sc .frame .pack (expand = 1 , fill = "both" )
117
116
item = self .rootnode ()
118
117
self .node = node = TreeNode (sc .canvas , None , item )
@@ -122,18 +121,19 @@ def init(self, flist):
122
121
123
122
def settitle (self ):
124
123
"Set the window title."
125
- self .top .wm_title ("Module Browser - " + self .name )
124
+ self .top .wm_title ("Module Browser - " + os . path . basename ( self .path ) )
126
125
self .top .wm_iconname ("Module Browser" )
127
126
128
127
def rootnode (self ):
129
128
"Return a ModuleBrowserTreeItem as the root of the tree."
130
- return ModuleBrowserTreeItem (self .file )
129
+ return ModuleBrowserTreeItem (self .path )
131
130
132
131
133
132
class ModuleBrowserTreeItem (TreeItem ):
134
133
"""Browser tree for Python module.
135
134
136
135
Uses TreeItem as the basis for the structure of the tree.
136
+ Used by both browsers.
137
137
"""
138
138
139
139
def __init__ (self , file ):
@@ -170,8 +170,8 @@ def IsExpandable(self):
170
170
171
171
def listchildren (self ):
172
172
"Return sequenced classes and functions in the module."
173
- dir , file = os .path .split (self .file )
174
- name , ext = os .path .splitext (file )
173
+ dir , base = os .path .split (self .file )
174
+ name , ext = os .path .splitext (base )
175
175
if os .path .normcase (ext ) != ".py" :
176
176
return []
177
177
try :
@@ -227,25 +227,22 @@ def OnDoubleClick(self):
227
227
228
228
229
229
def _module_browser (parent ): # htest #
230
- try :
231
- file = sys .argv [1 ] # If pass file on command line
232
- # If this succeeds, unittest will fail.
233
- except IndexError :
230
+ if len (sys .argv ) > 1 : # If pass file on command line.
231
+ file = sys .argv [1 ]
232
+ else :
234
233
file = __file__
235
- # Add objects for htest
234
+ # Add nested objects for htest.
236
235
class Nested_in_func (TreeNode ):
237
236
def nested_in_class (): pass
238
237
def closure ():
239
238
class Nested_in_closure : pass
240
- dir , file = os .path .split (file )
241
- name = os .path .splitext (file )[0 ]
242
- flist = pyshell .PyShellFileList (parent )
243
239
global file_open
244
- file_open = flist .open
245
- ModuleBrowser (flist , name , [ dir ] , _htest = True )
240
+ file_open = pyshell . PyShellFileList ( parent ) .open
241
+ ModuleBrowser (parent , file , _htest = True )
246
242
247
243
if __name__ == "__main__" :
248
- from unittest import main
249
- main ('idlelib.idle_test.test_browser' , verbosity = 2 , exit = False )
244
+ if len (sys .argv ) == 1 : # If pass file on command line, unittest fails.
245
+ from unittest import main
246
+ main ('idlelib.idle_test.test_browser' , verbosity = 2 , exit = False )
250
247
from idlelib .idle_test .htest import run
251
248
run (_module_browser )
0 commit comments