23
23
# Normally pyshell.flist.open, but there is no pyshell.flist for htest.
24
24
25
25
class ClassBrowser :
26
+ """Browse module classes and functions in IDLE.
27
+ """
26
28
27
29
def __init__ (self , flist , name , path , _htest = False ):
28
30
# XXX This API should change, if the file doesn't end in ".py"
29
31
# XXX the code here is bogus!
30
- """
31
- _htest - bool, change box when location running htest.
32
+ """Create a window for browsing a module's structure.
33
+
34
+ Args:
35
+ flist: filelist.FileList instance used as the root for the window.
36
+ name: Python module to parse.
37
+ path: Module search path.
38
+ _htest - bool, change box when location running htest.
39
+
40
+ Global variables:
41
+ file_open: Function used for opening a file.
42
+
43
+ Instance variables:
44
+ name: Module name.
45
+ file: Full path and module with .py extension. Used in
46
+ creating ModuleBrowserTreeItem as the rootnode for
47
+ the tree and subsequently in the children.
32
48
"""
33
49
global file_open
34
50
if not _htest :
@@ -39,10 +55,12 @@ def __init__(self, flist, name, path, _htest=False):
39
55
self .init (flist )
40
56
41
57
def close (self , event = None ):
58
+ "Dismiss the window and the tree nodes."
42
59
self .top .destroy ()
43
60
self .node .destroy ()
44
61
45
62
def init (self , flist ):
63
+ "Create browser tkinter widgets, including the tree."
46
64
self .flist = flist
47
65
# reset pyclbr
48
66
pyclbr ._modules .clear ()
@@ -66,41 +84,71 @@ def init(self, flist):
66
84
node .expand ()
67
85
68
86
def settitle (self ):
87
+ "Set the window title."
69
88
self .top .wm_title ("Class Browser - " + self .name )
70
89
self .top .wm_iconname ("Class Browser" )
71
90
72
91
def rootnode (self ):
92
+ "Return a ModuleBrowserTreeItem as the root of the tree."
73
93
return ModuleBrowserTreeItem (self .file )
74
94
75
95
class ModuleBrowserTreeItem (TreeItem ):
96
+ """Browser tree for Python module.
97
+
98
+ Uses TreeItem as the basis for the structure of the tree.
99
+ """
76
100
77
101
def __init__ (self , file ):
102
+ """Create a TreeItem for the file.
103
+
104
+ Args:
105
+ file: Full path and module name.
106
+ """
78
107
self .file = file
79
108
80
109
def GetText (self ):
110
+ "Return the module name as the text string to display."
81
111
return os .path .basename (self .file )
82
112
83
113
def GetIconName (self ):
114
+ "Return the name of the icon to display."
84
115
return "python"
85
116
86
117
def GetSubList (self ):
118
+ """Return the list of ClassBrowserTreeItem items.
119
+
120
+ Each item returned from listclasses is the first level of
121
+ classes/functions within the module.
122
+ """
87
123
sublist = []
88
124
for name in self .listclasses ():
89
125
item = ClassBrowserTreeItem (name , self .classes , self .file )
90
126
sublist .append (item )
91
127
return sublist
92
128
93
129
def OnDoubleClick (self ):
130
+ "Open a module in an editor window when double clicked."
94
131
if os .path .normcase (self .file [- 3 :]) != ".py" :
95
132
return
96
133
if not os .path .exists (self .file ):
97
134
return
98
135
pyshell .flist .open (self .file )
99
136
100
137
def IsExpandable (self ):
138
+ "Return True if Python (.py) file."
101
139
return os .path .normcase (self .file [- 3 :]) == ".py"
102
140
103
141
def listclasses (self ):
142
+ """Return list of classes and functions in the module.
143
+
144
+ The dictionary output from pyclbr is re-written as a
145
+ list of tuples in the form (lineno, name) and
146
+ then sorted so that the classes and functions are
147
+ processed in line number order. The returned list only
148
+ contains the name and not the line number. An instance
149
+ variable self.classes contains the pyclbr dictionary values,
150
+ which are instances of Class and Function.
151
+ """
104
152
dir , file = os .path .split (self .file )
105
153
name , ext = os .path .splitext (file )
106
154
if os .path .normcase (ext ) != ".py" :
@@ -134,9 +182,25 @@ def listclasses(self):
134
182
return list
135
183
136
184
class ClassBrowserTreeItem (TreeItem ):
185
+ """Browser tree for classes within a module.
186
+
187
+ Uses TreeItem as the basis for the structure of the tree.
188
+ """
137
189
138
190
def __init__ (self , name , classes , file ):
191
+ """Create a TreeItem for the class/function.
192
+
193
+ Args:
194
+ name: Name of the class/function.
195
+ classes: Dictonary of Class/Function instances from pyclbr.
196
+ file: Full path and module name.
197
+
198
+ Instance variables:
199
+ self.cl: Class/Function instance for the class/function name.
200
+ self.isfunction: True if self.cl is a Function.
201
+ """
139
202
self .name = name
203
+ # XXX - Does classes need to be an instance variable?
140
204
self .classes = classes
141
205
self .file = file
142
206
try :
@@ -146,25 +210,33 @@ def __init__(self, name, classes, file):
146
210
self .isfunction = isinstance (self .cl , pyclbr .Function )
147
211
148
212
def GetText (self ):
213
+ "Return the name of the function/class to display."
149
214
if self .isfunction :
150
215
return "def " + self .name + "(...)"
151
216
else :
152
217
return "class " + self .name
153
218
154
219
def GetIconName (self ):
220
+ "Return the name of the icon to display."
155
221
if self .isfunction :
156
222
return "python"
157
223
else :
158
224
return "folder"
159
225
160
226
def IsExpandable (self ):
227
+ "Return True if this class has methods."
161
228
if self .cl :
162
229
try :
163
230
return not not self .cl .methods
164
231
except AttributeError :
165
232
return False
233
+ return None
166
234
167
235
def GetSubList (self ):
236
+ """Return Class methods as a list of MethodBrowserTreeItem items.
237
+
238
+ Each item is a method within the class.
239
+ """
168
240
if not self .cl :
169
241
return []
170
242
sublist = []
@@ -174,6 +246,7 @@ def GetSubList(self):
174
246
return sublist
175
247
176
248
def OnDoubleClick (self ):
249
+ "Open module with file_open and position to lineno, if it exists."
177
250
if not os .path .exists (self .file ):
178
251
return
179
252
edit = file_open (self .file )
@@ -182,6 +255,7 @@ def OnDoubleClick(self):
182
255
edit .gotoline (lineno )
183
256
184
257
def listmethods (self ):
258
+ "Return list of methods within a class sorted by lineno."
185
259
if not self .cl :
186
260
return []
187
261
items = []
@@ -194,22 +268,37 @@ def listmethods(self):
194
268
return list
195
269
196
270
class MethodBrowserTreeItem (TreeItem ):
271
+ """Browser tree for methods within a class.
272
+
273
+ Uses TreeItem as the basis for the structure of the tree.
274
+ """
197
275
198
276
def __init__ (self , name , cl , file ):
277
+ """Create a TreeItem for the methods.
278
+
279
+ Args:
280
+ name: Name of the class/function.
281
+ cl: pyclbr.Class instance for name.
282
+ file: Full path and module name.
283
+ """
199
284
self .name = name
200
285
self .cl = cl
201
286
self .file = file
202
287
203
288
def GetText (self ):
289
+ "Return the method name to display."
204
290
return "def " + self .name + "(...)"
205
291
206
292
def GetIconName (self ):
207
- return "python" # XXX
293
+ "Return the name of the icon to display."
294
+ return "python"
208
295
209
296
def IsExpandable (self ):
210
- return 0
297
+ "Return False as there are no tree items after methods."
298
+ return False
211
299
212
300
def OnDoubleClick (self ):
301
+ "Open module with file_open and position at the method start."
213
302
if not os .path .exists (self .file ):
214
303
return
215
304
edit = file_open (self .file )
0 commit comments