1998-10-13 16:31:03 +00:00
|
|
|
"""Primitive class browser.
|
|
|
|
|
|
|
|
XXX TO DO:
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1999-04-08 20:28:42 +00:00
|
|
|
- reparse when source changed
|
1998-10-13 16:31:03 +00:00
|
|
|
- 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 23:27:08 +00:00
|
|
|
import os
|
1998-10-13 03:59:57 +00:00
|
|
|
import string
|
|
|
|
import pyclbr
|
1998-10-13 16:31:03 +00:00
|
|
|
from Tkinter import *
|
|
|
|
import tkMessageBox
|
1999-01-02 21:28:54 +00:00
|
|
|
from WindowList import ListedToplevel
|
1999-01-11 14:46:06 +00:00
|
|
|
from Separator import HSeparator
|
1998-10-13 03:59:57 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
from ScrolledList import ScrolledList
|
|
|
|
|
|
|
|
|
1998-10-13 03:59:57 +00:00
|
|
|
class ClassBrowser:
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-12-18 15:52:54 +00:00
|
|
|
def __init__(self, flist, name, path=[]):
|
1998-10-13 16:31:03 +00:00
|
|
|
root = flist.root
|
1998-10-13 03:59:57 +00:00
|
|
|
try:
|
1998-12-18 15:52:54 +00:00
|
|
|
dict = pyclbr.readmodule(name, path)
|
1998-10-13 03:59:57 +00:00
|
|
|
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 16:31:03 +00:00
|
|
|
self.flist = flist
|
|
|
|
self.dict = dict
|
1998-10-13 03:59:57 +00:00
|
|
|
self.root = root
|
1999-01-02 21:28:54 +00:00
|
|
|
self.top = top = ListedToplevel(root)
|
1998-10-13 16:31:03 +00:00
|
|
|
self.top.protocol("WM_DELETE_WINDOW", self.close)
|
1999-01-11 14:46:06 +00:00
|
|
|
self.top.bind("<Escape>", self.close)
|
1999-01-02 21:28:54 +00:00
|
|
|
top.wm_title("Class Browser - " + name)
|
|
|
|
top.wm_iconname("ClBrowser")
|
1999-01-11 14:46:06 +00:00
|
|
|
self.sepa = HSeparator(top)
|
|
|
|
leftframe, rightframe = self.sepa.parts()
|
|
|
|
self.leftframe = leftframe
|
|
|
|
self.rightframe = rightframe
|
|
|
|
leftframe.pack(side="left", fill="both", expand=1)
|
1998-10-13 03:59:57 +00:00
|
|
|
# Create help label
|
1998-10-15 23:27:08 +00:00
|
|
|
self.helplabel = Label(leftframe, text="Module %s" % name,
|
|
|
|
relief="groove", borderwidth=2)
|
1998-10-13 03:59:57 +00:00
|
|
|
self.helplabel.pack(fill="x")
|
|
|
|
# Create top frame, with scrollbar and listbox
|
1998-10-15 23:27:08 +00:00
|
|
|
self.classviewer = ClassViewer(
|
|
|
|
self.leftframe, self.flist, self)
|
1998-10-13 03:59:57 +00:00
|
|
|
# Load the classes
|
1998-10-15 23:27:08 +00:00
|
|
|
self.load_classes(dict, name)
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1999-01-11 14:46:06 +00:00
|
|
|
def close(self, event=None):
|
1998-10-15 23:27:08 +00:00
|
|
|
self.classviewer = None
|
|
|
|
self.methodviewer = None
|
1998-10-13 16:31:03 +00:00
|
|
|
self.top.destroy()
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def load_classes(self, dict, module):
|
|
|
|
self.classviewer.load_classes(dict, module)
|
1999-01-11 14:46:06 +00:00
|
|
|
if self.methodframe:
|
|
|
|
self.methodframe.destroy()
|
|
|
|
self.methodframe = None
|
1998-10-15 23:27:08 +00:00
|
|
|
self.methodviewer = None
|
|
|
|
|
1999-01-11 14:46:06 +00:00
|
|
|
methodframe = None
|
1998-10-15 23:27:08 +00:00
|
|
|
methodhelplabel = None
|
|
|
|
methodviewer = None
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def show_methods(self, cl):
|
1999-01-11 14:46:06 +00:00
|
|
|
if not self.methodframe:
|
|
|
|
self.methodframe = Frame(self.rightframe)
|
|
|
|
self.methodframe.pack(side="right", expand=1, fill="both")
|
|
|
|
self.methodhelplabel = Label(self.methodframe,
|
1998-10-15 23:27:08 +00:00
|
|
|
relief="groove", borderwidth=2)
|
|
|
|
self.methodhelplabel.pack(fill="x")
|
1999-01-11 14:46:06 +00:00
|
|
|
self.methodviewer = MethodViewer(self.methodframe, self.flist)
|
1998-10-15 23:27:08 +00:00
|
|
|
self.methodhelplabel.config(text="Class %s" % cl.name)
|
|
|
|
self.methodviewer.load_methods(cl)
|
|
|
|
|
|
|
|
|
|
|
|
class ClassViewer(ScrolledList):
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def __init__(self, master, flist, browser):
|
1999-01-11 14:46:06 +00:00
|
|
|
ScrolledList.__init__(self, master, width=40)
|
1998-10-15 23:27:08 +00:00
|
|
|
self.flist = flist
|
|
|
|
self.browser = browser
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def load_classes(self, dict, module):
|
|
|
|
self.clear()
|
|
|
|
self.dict = dict
|
1998-10-13 16:31:03 +00:00
|
|
|
items = []
|
|
|
|
for key, value in dict.items():
|
|
|
|
if value.module == module:
|
|
|
|
items.append((value.lineno, key, value))
|
1998-10-13 03:59:57 +00:00
|
|
|
items.sort()
|
1998-10-13 16:31:03 +00:00
|
|
|
for lineno, key, value in items:
|
1998-10-13 03:59:57 +00: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 23:27:08 +00:00
|
|
|
self.append(s)
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def getname(self, index):
|
1998-10-13 16:31:03 +00:00
|
|
|
name = self.listbox.get(index)
|
|
|
|
i = string.find(name, '(')
|
|
|
|
if i >= 0:
|
|
|
|
name = name[:i]
|
1998-10-15 23:27:08 +00:00
|
|
|
return name
|
1998-10-13 16:31:03 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def getclass(self, index):
|
|
|
|
return self.dict[self.getname(index)]
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def on_select(self, index):
|
|
|
|
self.show_methods(index)
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def on_double(self, index):
|
|
|
|
self.show_source(index)
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-13 16:31:03 +00:00
|
|
|
def show_methods(self, index):
|
1998-10-15 23:27:08 +00:00
|
|
|
cl = self.getclass(index)
|
|
|
|
self.browser.show_methods(cl)
|
1998-10-13 16:31:03 +00:00
|
|
|
|
1998-10-15 23:27:08 +00: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):
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def __init__(self, master, flist):
|
|
|
|
ScrolledList.__init__(self, master)
|
1998-10-13 16:31:03 +00:00
|
|
|
self.flist = flist
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-13 16:31:03 +00:00
|
|
|
classinfo = None
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def load_methods(self, cl):
|
1998-10-13 16:31:03 +00:00
|
|
|
self.classinfo = cl
|
1998-10-15 23:27:08 +00:00
|
|
|
self.clear()
|
1998-10-13 16:31:03 +00:00
|
|
|
items = []
|
|
|
|
for name, lineno in cl.methods.items():
|
|
|
|
items.append((lineno, name))
|
|
|
|
items.sort()
|
|
|
|
for item, name in items:
|
1998-10-15 23:27:08 +00:00
|
|
|
self.append(name)
|
1998-10-13 16:31:03 +00:00
|
|
|
|
|
|
|
def click_event(self, event):
|
1998-10-13 03:59:57 +00:00
|
|
|
pass
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00:00
|
|
|
def on_double(self, index):
|
|
|
|
self.show_source(self.get(index))
|
1999-01-02 21:28:54 +00:00
|
|
|
|
1998-10-15 23:27:08 +00: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])
|