- reparse when source changed (maybe just a button would be OK?)
(or recheck on window popup)
- add popup menu with more options (e.g. doc strings, base classes, imports)
- show function argument list? (have to do pattern matching on source)
- should the classes and methods lists also be in the module's menu bar?
- add base classes to class browser tree
from idlelib import PyShell
from idlelib.WindowList import ListedToplevel
from idlelib.TreeWidget import TreeNode, TreeItem, ScrolledCanvas
from idlelib.configHandler import idleConf
file_open = None # Method...Item and Class...Item use this.
# Normally PyShell.flist.open, but there is no PyShell.flist for htest.
def __init__(self, flist, name, path, _htest=False):
# XXX This API should change, if the file doesn't end in ".py"
# XXX the code here is bogus!
_htest - bool, change box when location running htest.
file_open = PyShell.flist.open
self.file = os.path.join(path[0], self.name + ".py")
def close(self, event=None):
self.top = top = ListedToplevel(flist.root)
top.protocol("WM_DELETE_WINDOW", self.close)
top.bind("<Escape>", self.close)
if self._htest: # place dialog below parent if running htest
(flist.root.winfo_rootx(), flist.root.winfo_rooty() + 200))
theme = idleConf.CurrentTheme()
background = idleConf.GetHighlight(theme, 'normal')['background']
sc = ScrolledCanvas(top, bg=background, highlightthickness=0, takefocus=1)
sc.frame.pack(expand=1, fill="both")
self.node = node = TreeNode(sc.canvas, None, item)
self.top.wm_title("Class Browser - " + self.name)
self.top.wm_iconname("Class Browser")
return ModuleBrowserTreeItem(self.file)
class ModuleBrowserTreeItem(TreeItem):
def __init__(self, file):
return os.path.basename(self.file)
for name in self.listclasses():
item = ClassBrowserTreeItem(name, self.classes, self.file)
if os.path.normcase(self.file[-3:]) != ".py":
if not os.path.exists(self.file):
PyShell.flist.open(self.file)
return os.path.normcase(self.file[-3:]) == ".py"
dir, file = os.path.split(self.file)
name, ext = os.path.splitext(file)
if os.path.normcase(ext) != ".py":
dict = pyclbr.readmodule_ex(name, [dir] + sys.path)
for key, cl in dict.items():
if hasattr(cl, 'super') and cl.super:
if type(sup) is type(''):
if sup.module != cl.module:
sname = "%s.%s" % (sup.module, sname)
s = s + "(%s)" % ", ".join(supers)
items.append((cl.lineno, s))
class ClassBrowserTreeItem(TreeItem):
def __init__(self, name, classes, file):
self.cl = self.classes[self.name]
except (IndexError, KeyError):
self.isfunction = isinstance(self.cl, pyclbr.Function)
return "def " + self.name + "(...)"
return "class " + self.name
return not not self.cl.methods
for name in self.listmethods():
item = MethodBrowserTreeItem(name, self.cl, self.file)
if not os.path.exists(self.file):
edit = file_open(self.file)
if hasattr(self.cl, 'lineno'):
for name, lineno in self.cl.methods.items():
items.append((lineno, name))
class MethodBrowserTreeItem(TreeItem):
def __init__(self, name, cl, file):
return "def " + self.name + "(...)"
if not os.path.exists(self.file):
edit = file_open(self.file)
edit.gotoline(self.cl.methods[self.name])
def _class_browser(parent): #Wrapper for htest
dir, file = os.path.split(file)
name = os.path.splitext(file)[0]
flist = PyShell.PyShellFileList(parent)
ClassBrowser(flist, name, [dir], _htest=True)
if __name__ == "__main__":
from idlelib.idle_test.htest import run