1998-10-13 13:31:03 -03:00
|
|
|
"""Primitive class browser.
|
|
|
|
|
|
|
|
XXX TO DO:
|
|
|
|
|
|
|
|
- generalize the scrolling listbox with some behavior into a base class
|
|
|
|
- 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?
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
import os
|
1998-10-13 00:59:57 -03:00
|
|
|
import string
|
|
|
|
import pyclbr
|
1998-10-13 13:31:03 -03:00
|
|
|
from Tkinter import *
|
|
|
|
import tkMessageBox
|
1998-10-13 00:59:57 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
from ScrolledList import ScrolledList
|
|
|
|
|
|
|
|
|
1998-10-13 00:59:57 -03:00
|
|
|
class ClassBrowser:
|
|
|
|
|
1998-10-13 13:31:03 -03:00
|
|
|
def __init__(self, flist, name):
|
|
|
|
root = flist.root
|
1998-10-13 00:59:57 -03:00
|
|
|
try:
|
|
|
|
dict = pyclbr.readmodule(name)
|
|
|
|
except ImportError, msg:
|
|
|
|
tkMessageBox.showerror("Import error", str(msg), parent=root)
|
|
|
|
return
|
|
|
|
if not dict:
|
|
|
|
tkMessageBox.showerror("Nothing to browse",
|
|
|
|
"Module %s defines no classes" % name, parent=root)
|
|
|
|
return
|
1998-10-13 13:31:03 -03:00
|
|
|
self.flist = flist
|
|
|
|
self.dict = dict
|
1998-10-13 00:59:57 -03:00
|
|
|
self.root = root
|
|
|
|
self.top = top = Toplevel(root)
|
1998-10-13 13:31:03 -03:00
|
|
|
self.top.protocol("WM_DELETE_WINDOW", self.close)
|
1998-10-15 20:27:08 -03:00
|
|
|
top.wm_title("Class browser")
|
1998-10-13 13:31:03 -03:00
|
|
|
self.leftframe = leftframe = Frame(top)
|
|
|
|
self.leftframe.pack(side="left", fill="both", expand=1)
|
1998-10-13 00:59:57 -03:00
|
|
|
# Create help label
|
1998-10-15 20:27:08 -03:00
|
|
|
self.helplabel = Label(leftframe, text="Module %s" % name,
|
|
|
|
relief="groove", borderwidth=2)
|
1998-10-13 00:59:57 -03:00
|
|
|
self.helplabel.pack(fill="x")
|
|
|
|
# Create top frame, with scrollbar and listbox
|
1998-10-15 20:27:08 -03:00
|
|
|
self.classviewer = ClassViewer(
|
|
|
|
self.leftframe, self.flist, self)
|
1998-10-13 00:59:57 -03:00
|
|
|
# Load the classes
|
1998-10-15 20:27:08 -03:00
|
|
|
self.load_classes(dict, name)
|
1998-10-13 13:31:03 -03:00
|
|
|
|
|
|
|
def close(self):
|
1998-10-15 20:27:08 -03:00
|
|
|
self.classviewer = None
|
|
|
|
self.methodviewer = None
|
1998-10-13 13:31:03 -03:00
|
|
|
self.top.destroy()
|
1998-10-13 00:59:57 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def load_classes(self, dict, module):
|
|
|
|
self.classviewer.load_classes(dict, module)
|
|
|
|
if self.botframe:
|
|
|
|
self.botframe.destroy()
|
|
|
|
self.botframe = None
|
|
|
|
self.methodviewer = None
|
|
|
|
|
|
|
|
botframe = None
|
|
|
|
methodhelplabel = None
|
|
|
|
methodviewer = None
|
|
|
|
|
|
|
|
def show_methods(self, cl):
|
|
|
|
if not self.botframe:
|
|
|
|
self.botframe = Frame(self.top)
|
|
|
|
self.botframe.pack(side="right", expand=1, fill="both")
|
|
|
|
self.methodhelplabel = Label(self.botframe,
|
|
|
|
relief="groove", borderwidth=2)
|
|
|
|
self.methodhelplabel.pack(fill="x")
|
|
|
|
self.methodviewer = MethodViewer(self.botframe, self.flist)
|
|
|
|
self.methodhelplabel.config(text="Class %s" % cl.name)
|
|
|
|
self.methodviewer.load_methods(cl)
|
|
|
|
|
|
|
|
|
|
|
|
class ClassViewer(ScrolledList):
|
|
|
|
|
|
|
|
def __init__(self, master, flist, browser):
|
|
|
|
ScrolledList.__init__(self, master)
|
|
|
|
self.flist = flist
|
|
|
|
self.browser = browser
|
|
|
|
|
|
|
|
def load_classes(self, dict, module):
|
|
|
|
self.clear()
|
|
|
|
self.dict = dict
|
1998-10-13 13:31:03 -03:00
|
|
|
items = []
|
|
|
|
for key, value in dict.items():
|
|
|
|
if value.module == module:
|
|
|
|
items.append((value.lineno, key, value))
|
1998-10-13 00:59:57 -03:00
|
|
|
items.sort()
|
1998-10-13 13:31:03 -03:00
|
|
|
for lineno, key, value in items:
|
1998-10-13 00:59:57 -03:00
|
|
|
s = key
|
|
|
|
if value.super:
|
|
|
|
super = []
|
|
|
|
for sup in value.super:
|
|
|
|
name = sup.name
|
|
|
|
if sup.module != value.module:
|
|
|
|
name = "%s.%s" % (sup.module, name)
|
|
|
|
super.append(name)
|
|
|
|
s = s + "(%s)" % string.join(super, ", ")
|
1998-10-15 20:27:08 -03:00
|
|
|
self.append(s)
|
1998-10-13 13:31:03 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def getname(self, index):
|
1998-10-13 13:31:03 -03:00
|
|
|
name = self.listbox.get(index)
|
|
|
|
i = string.find(name, '(')
|
|
|
|
if i >= 0:
|
|
|
|
name = name[:i]
|
1998-10-15 20:27:08 -03:00
|
|
|
return name
|
1998-10-13 13:31:03 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def getclass(self, index):
|
|
|
|
return self.dict[self.getname(index)]
|
|
|
|
|
|
|
|
def on_select(self, index):
|
|
|
|
self.show_methods(index)
|
|
|
|
|
|
|
|
def on_double(self, index):
|
|
|
|
self.show_source(index)
|
1998-10-13 13:31:03 -03:00
|
|
|
|
|
|
|
def show_methods(self, index):
|
1998-10-15 20:27:08 -03:00
|
|
|
cl = self.getclass(index)
|
|
|
|
self.browser.show_methods(cl)
|
1998-10-13 13:31:03 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def show_source(self, index):
|
|
|
|
cl = self.getclass(index)
|
|
|
|
if os.path.isfile(cl.file):
|
|
|
|
edit = self.flist.open(cl.file)
|
|
|
|
edit.gotoline(cl.lineno)
|
|
|
|
|
|
|
|
|
|
|
|
class MethodViewer(ScrolledList):
|
1998-10-13 13:31:03 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def __init__(self, master, flist):
|
|
|
|
ScrolledList.__init__(self, master)
|
1998-10-13 13:31:03 -03:00
|
|
|
self.flist = flist
|
|
|
|
|
|
|
|
classinfo = None
|
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def load_methods(self, cl):
|
1998-10-13 13:31:03 -03:00
|
|
|
self.classinfo = cl
|
1998-10-15 20:27:08 -03:00
|
|
|
self.clear()
|
1998-10-13 13:31:03 -03:00
|
|
|
items = []
|
|
|
|
for name, lineno in cl.methods.items():
|
|
|
|
items.append((lineno, name))
|
|
|
|
items.sort()
|
|
|
|
for item, name in items:
|
1998-10-15 20:27:08 -03:00
|
|
|
self.append(name)
|
1998-10-13 13:31:03 -03:00
|
|
|
|
|
|
|
def click_event(self, event):
|
1998-10-13 00:59:57 -03:00
|
|
|
pass
|
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def on_double(self, index):
|
|
|
|
self.show_source(self.get(index))
|
1998-10-13 13:31:03 -03:00
|
|
|
|
1998-10-15 20:27:08 -03:00
|
|
|
def show_source(self, name):
|
|
|
|
if os.path.isfile(self.classinfo.file):
|
|
|
|
edit = self.flist.open(self.classinfo.file)
|
|
|
|
edit.gotoline(self.classinfo.methods[name])
|