1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
"""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?
"""
import string
import pyclbr
from Tkinter import *
import tkMessageBox
class ClassBrowser:
def __init__(self, flist, name):
root = flist.root
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
self.flist = flist
self.dict = dict
self.root = root
self.top = top = Toplevel(root)
self.top.protocol("WM_DELETE_WINDOW", self.close)
self.leftframe = leftframe = Frame(top)
self.leftframe.pack(side="left", fill="both", expand=1)
top.wm_title("Class browser")
# Create help label
self.helplabel = Label(leftframe,
text="Classes in module %s" % name,
borderwidth=2, relief="groove")
self.helplabel.pack(fill="x")
# Create top frame, with scrollbar and listbox
self.topframe = Frame(leftframe)
self.topframe.pack(fill="both", expand=1)
self.vbar = Scrollbar(self.topframe, name="vbar")
self.vbar.pack(side="right", fill="y")
self.listbox = Listbox(self.topframe, exportselection=0,
takefocus=1, width=30)
self.listbox.pack(expand=1, fill="both")
# Tie listbox and scrollbar together
self.vbar["command"] = self.listbox.yview
self.listbox["yscrollcommand"] = self.vbar.set
# Bind events to the list box
self.listbox.bind("<ButtonRelease-1>", self.click_event)
self.listbox.bind("<Double-ButtonRelease-1>", self.double_click_event)
##self.listbox.bind("<ButtonPress-3>", self.popup_event)
self.listbox.bind("<Key-Up>", self.up_event)
self.listbox.bind("<Key-Down>", self.down_event)
# Load the classes
self.loadclasses(dict, name)
def close(self):
self.top.destroy()
def loadclasses(self, dict, module):
items = []
for key, value in dict.items():
if value.module == module:
items.append((value.lineno, key, value))
items.sort()
l = self.listbox
for lineno, key, value in items:
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, ", ")
l.insert("end", s)
l.focus_set()
l.selection_clear(0, "end")
if self.botframe:
self.botframe.destroy()
self.botframe = None
self.methodviewer = None
def click_event(self, event):
self.listbox.activate("@%d,%d" % (event.x, event.y))
index = self.listbox.index("active")
self.show_methods(index)
def double_click_event(self, event):
self.listbox.activate("@%d,%d" % (event.x, event.y))
index = self.listbox.index("active")
self.show_source(index)
def up_event(self, event):
index = self.listbox.index("active") - 1
if index < 0:
self.top.bell()
return "break"
self.show_methods(index)
return "break"
def down_event(self, event):
index = self.listbox.index("active") + 1
if index >= self.listbox.index("end"):
self.top.bell()
return "break"
self.show_methods(index)
return "break"
def show_source(self, index):
name = self.listbox.get(index)
i = string.find(name, '(')
if i >= 0:
name = name[:i]
cl = self.dict[name]
edit = self.flist.open(cl.file)
edit.gotoline(cl.lineno)
botframe = None
methodviewer = None
def show_methods(self, index):
self.listbox.selection_clear(0, "end")
self.listbox.selection_set(index)
self.listbox.activate(index)
self.listbox.see(index)
self.listbox.focus_set()
name = self.listbox.get(index)
i = string.find(name, '(')
if i >= 0:
name = name[:i]
cl = self.dict[name]
if not self.botframe:
self.botframe = Frame(self.top)
self.botframe.pack(expand=1, fill="both")
if not self.methodviewer:
self.methodviewer = MethodViewer(self.botframe, self.flist)
self.methodviewer.loadmethods(cl)
class MethodViewer:
# XXX There's a pattern emerging here...
def __init__(self, frame, flist):
self.frame = frame
self.flist = flist
# Create help label
self.helplabel = Label(frame,
text="Methods", borderwidth=2, relief="groove")
self.helplabel.pack(fill="x")
# Create top frame, with scrollbar and listbox
self.topframe = Frame(frame)
self.topframe.pack(fill="both", expand=1)
self.vbar = Scrollbar(self.topframe, name="vbar")
self.vbar.pack(side="right", fill="y")
self.listbox = Listbox(self.topframe, exportselection=0,
takefocus=1, width=30)
self.listbox.pack(expand=1, fill="both")
# Tie listbox and scrollbar together
self.vbar["command"] = self.listbox.yview
self.listbox["yscrollcommand"] = self.vbar.set
# Bind events to the list box
self.listbox.bind("<ButtonRelease-1>", self.click_event)
self.listbox.bind("<Double-ButtonRelease-1>", self.double_click_event)
##self.listbox.bind("<ButtonPress-3>", self.popup_event)
self.listbox.bind("<Key-Up>", self.up_event)
self.listbox.bind("<Key-Down>", self.down_event)
classinfo = None
def loadmethods(self, cl):
self.classinfo = cl
self.helplabel.config(text="Methods of class %s" % cl.name)
l = self.listbox
l.delete(0, "end")
l.selection_clear(0, "end")
items = []
for name, lineno in cl.methods.items():
items.append((lineno, name))
items.sort()
for item, name in items:
l.insert("end", name)
def click_event(self, event):
pass
def double_click_event(self, event):
self.listbox.activate("@%d,%d" % (event.x, event.y))
index = self.listbox.index("active")
self.show_source(index)
def up_event(self, event):
pass
def down_event(self, event):
pass
def show_source(self, index):
name = self.listbox.get(index)
i = string.find(name, '(')
if i >= 0:
name = name[:i]
edit = self.flist.open(self.classinfo.file)
edit.gotoline(self.classinfo.methods[name])
|