Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4431b0f

Browse files
committed
Adapt to the new pyclbr's support of listing top-level functions. If
this functionality is not present (e.g. when used with a vintage Python 1.5.2 installation) top-level functions are not listed. (Hmm... Any distribution of IDLE 0.5 should probably include a copy of the new pyclbr.py!)
1 parent 7a62128 commit 4431b0f

1 file changed

Lines changed: 34 additions & 33 deletions

File tree

Tools/idle/ClassBrowser.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
- show function argument list? (have to do pattern matching on source)
99
- should the classes and methods lists also be in the module's menu bar?
1010
- add base classes to class browser tree
11-
- make methodless classes inexpandable
12-
- make classless modules inexpandable
13-
14-
1511
"""
1612

1713
import os
1814
import sys
1915
import string
2016
import pyclbr
2117

18+
# XXX Patch pyclbr with dummies if it's vintage Python 1.5.2:
19+
if not hasattr(pyclbr, "readmodule_ex"):
20+
pyclbr.readmodule_ex = pyclbr.readmodule
21+
if not hasattr(pyclbr, "Function"):
22+
class Function(pyclbr.Class):
23+
pass
24+
pyclbr.Function = Function
25+
2226
import PyShell
2327
from WindowList import ListedToplevel
2428
from TreeWidget import TreeNode, TreeItem, ScrolledCanvas
@@ -92,7 +96,7 @@ def listclasses(self):
9296
if os.path.normcase(ext) != ".py":
9397
return []
9498
try:
95-
dict = pyclbr.readmodule(name, [dir] + sys.path)
99+
dict = pyclbr.readmodule_ex(name, [dir] + sys.path)
96100
except ImportError, msg:
97101
return []
98102
items = []
@@ -125,53 +129,50 @@ def __init__(self, name, classes, file):
125129
self.name = name
126130
self.classes = classes
127131
self.file = file
132+
try:
133+
self.cl = self.classes[self.name]
134+
except (IndexError, KeyError):
135+
self.cl = None
136+
self.isfunction = isinstance(self.cl, pyclbr.Function)
128137

129138
def GetText(self):
130-
return "class " + self.name
139+
if self.isfunction:
140+
return "def " + self.name + "(...)"
141+
else:
142+
return "class " + self.name
131143

132-
def IsExpandable(self):
133-
try:
134-
cl = self.classes[self.name]
135-
except (IndexError, KeyError):
136-
return 0
144+
def GetIconName(self):
145+
if self.isfunction:
146+
return "python"
137147
else:
138-
return not not cl.methods
148+
return "folder"
149+
150+
def IsExpandable(self):
151+
if self.cl:
152+
return not not self.cl.methods
139153

140154
def GetSubList(self):
155+
if not self.cl:
156+
return []
141157
sublist = []
142158
for name in self.listmethods():
143-
item = MethodBrowserTreeItem(
144-
name, self.classes[self.name], self.file)
159+
item = MethodBrowserTreeItem(name, self.cl, self.file)
145160
sublist.append(item)
146161
return sublist
147162

148163
def OnDoubleClick(self):
149164
if not os.path.exists(self.file):
150165
return
151166
edit = PyShell.flist.open(self.file)
152-
if self.classes.has_key(self.name):
153-
cl = self.classes[self.name]
154-
else:
155-
name = self.name
156-
i = string.find(name, '(')
157-
if i < 0:
158-
return
159-
name = name[:i]
160-
if not self.classes.has_key(name):
161-
return
162-
cl = self.classes[name]
163-
if not hasattr(cl, 'lineno'):
164-
return
165-
lineno = cl.lineno
166-
edit.gotoline(lineno)
167+
if hasattr(self.cl, 'lineno'):
168+
lineno = self.cl.lineno
169+
edit.gotoline(lineno)
167170

168171
def listmethods(self):
169-
try:
170-
cl = self.classes[self.name]
171-
except (IndexError, KeyError):
172+
if not self.cl:
172173
return []
173174
items = []
174-
for name, lineno in cl.methods.items():
175+
for name, lineno in self.cl.methods.items():
175176
items.append((lineno, name))
176177
items.sort()
177178
list = []

0 commit comments

Comments
 (0)