1 | n/a | """Class browser. |
---|
2 | n/a | |
---|
3 | n/a | XXX TO DO: |
---|
4 | n/a | |
---|
5 | n/a | - reparse when source changed (maybe just a button would be OK?) |
---|
6 | n/a | (or recheck on window popup) |
---|
7 | n/a | - add popup menu with more options (e.g. doc strings, base classes, imports) |
---|
8 | n/a | - show function argument list? (have to do pattern matching on source) |
---|
9 | n/a | - should the classes and methods lists also be in the module's menu bar? |
---|
10 | n/a | - add base classes to class browser tree |
---|
11 | n/a | """ |
---|
12 | n/a | |
---|
13 | n/a | import os |
---|
14 | n/a | import pyclbr |
---|
15 | n/a | import sys |
---|
16 | n/a | |
---|
17 | n/a | from idlelib.config import idleConf |
---|
18 | n/a | from idlelib import pyshell |
---|
19 | n/a | from idlelib.tree import TreeNode, TreeItem, ScrolledCanvas |
---|
20 | n/a | from idlelib.windows import ListedToplevel |
---|
21 | n/a | |
---|
22 | n/a | file_open = None # Method...Item and Class...Item use this. |
---|
23 | n/a | # Normally pyshell.flist.open, but there is no pyshell.flist for htest. |
---|
24 | n/a | |
---|
25 | n/a | class ClassBrowser: |
---|
26 | n/a | |
---|
27 | n/a | def __init__(self, flist, name, path, _htest=False): |
---|
28 | n/a | # XXX This API should change, if the file doesn't end in ".py" |
---|
29 | n/a | # XXX the code here is bogus! |
---|
30 | n/a | """ |
---|
31 | n/a | _htest - bool, change box when location running htest. |
---|
32 | n/a | """ |
---|
33 | n/a | global file_open |
---|
34 | n/a | if not _htest: |
---|
35 | n/a | file_open = pyshell.flist.open |
---|
36 | n/a | self.name = name |
---|
37 | n/a | self.file = os.path.join(path[0], self.name + ".py") |
---|
38 | n/a | self._htest = _htest |
---|
39 | n/a | self.init(flist) |
---|
40 | n/a | |
---|
41 | n/a | def close(self, event=None): |
---|
42 | n/a | self.top.destroy() |
---|
43 | n/a | self.node.destroy() |
---|
44 | n/a | |
---|
45 | n/a | def init(self, flist): |
---|
46 | n/a | self.flist = flist |
---|
47 | n/a | # reset pyclbr |
---|
48 | n/a | pyclbr._modules.clear() |
---|
49 | n/a | # create top |
---|
50 | n/a | self.top = top = ListedToplevel(flist.root) |
---|
51 | n/a | top.protocol("WM_DELETE_WINDOW", self.close) |
---|
52 | n/a | top.bind("<Escape>", self.close) |
---|
53 | n/a | if self._htest: # place dialog below parent if running htest |
---|
54 | n/a | top.geometry("+%d+%d" % |
---|
55 | n/a | (flist.root.winfo_rootx(), flist.root.winfo_rooty() + 200)) |
---|
56 | n/a | self.settitle() |
---|
57 | n/a | top.focus_set() |
---|
58 | n/a | # create scrolled canvas |
---|
59 | n/a | theme = idleConf.CurrentTheme() |
---|
60 | n/a | background = idleConf.GetHighlight(theme, 'normal')['background'] |
---|
61 | n/a | sc = ScrolledCanvas(top, bg=background, highlightthickness=0, takefocus=1) |
---|
62 | n/a | sc.frame.pack(expand=1, fill="both") |
---|
63 | n/a | item = self.rootnode() |
---|
64 | n/a | self.node = node = TreeNode(sc.canvas, None, item) |
---|
65 | n/a | node.update() |
---|
66 | n/a | node.expand() |
---|
67 | n/a | |
---|
68 | n/a | def settitle(self): |
---|
69 | n/a | self.top.wm_title("Class Browser - " + self.name) |
---|
70 | n/a | self.top.wm_iconname("Class Browser") |
---|
71 | n/a | |
---|
72 | n/a | def rootnode(self): |
---|
73 | n/a | return ModuleBrowserTreeItem(self.file) |
---|
74 | n/a | |
---|
75 | n/a | class ModuleBrowserTreeItem(TreeItem): |
---|
76 | n/a | |
---|
77 | n/a | def __init__(self, file): |
---|
78 | n/a | self.file = file |
---|
79 | n/a | |
---|
80 | n/a | def GetText(self): |
---|
81 | n/a | return os.path.basename(self.file) |
---|
82 | n/a | |
---|
83 | n/a | def GetIconName(self): |
---|
84 | n/a | return "python" |
---|
85 | n/a | |
---|
86 | n/a | def GetSubList(self): |
---|
87 | n/a | sublist = [] |
---|
88 | n/a | for name in self.listclasses(): |
---|
89 | n/a | item = ClassBrowserTreeItem(name, self.classes, self.file) |
---|
90 | n/a | sublist.append(item) |
---|
91 | n/a | return sublist |
---|
92 | n/a | |
---|
93 | n/a | def OnDoubleClick(self): |
---|
94 | n/a | if os.path.normcase(self.file[-3:]) != ".py": |
---|
95 | n/a | return |
---|
96 | n/a | if not os.path.exists(self.file): |
---|
97 | n/a | return |
---|
98 | n/a | pyshell.flist.open(self.file) |
---|
99 | n/a | |
---|
100 | n/a | def IsExpandable(self): |
---|
101 | n/a | return os.path.normcase(self.file[-3:]) == ".py" |
---|
102 | n/a | |
---|
103 | n/a | def listclasses(self): |
---|
104 | n/a | dir, file = os.path.split(self.file) |
---|
105 | n/a | name, ext = os.path.splitext(file) |
---|
106 | n/a | if os.path.normcase(ext) != ".py": |
---|
107 | n/a | return [] |
---|
108 | n/a | try: |
---|
109 | n/a | dict = pyclbr.readmodule_ex(name, [dir] + sys.path) |
---|
110 | n/a | except ImportError: |
---|
111 | n/a | return [] |
---|
112 | n/a | items = [] |
---|
113 | n/a | self.classes = {} |
---|
114 | n/a | for key, cl in dict.items(): |
---|
115 | n/a | if cl.module == name: |
---|
116 | n/a | s = key |
---|
117 | n/a | if hasattr(cl, 'super') and cl.super: |
---|
118 | n/a | supers = [] |
---|
119 | n/a | for sup in cl.super: |
---|
120 | n/a | if type(sup) is type(''): |
---|
121 | n/a | sname = sup |
---|
122 | n/a | else: |
---|
123 | n/a | sname = sup.name |
---|
124 | n/a | if sup.module != cl.module: |
---|
125 | n/a | sname = "%s.%s" % (sup.module, sname) |
---|
126 | n/a | supers.append(sname) |
---|
127 | n/a | s = s + "(%s)" % ", ".join(supers) |
---|
128 | n/a | items.append((cl.lineno, s)) |
---|
129 | n/a | self.classes[s] = cl |
---|
130 | n/a | items.sort() |
---|
131 | n/a | list = [] |
---|
132 | n/a | for item, s in items: |
---|
133 | n/a | list.append(s) |
---|
134 | n/a | return list |
---|
135 | n/a | |
---|
136 | n/a | class ClassBrowserTreeItem(TreeItem): |
---|
137 | n/a | |
---|
138 | n/a | def __init__(self, name, classes, file): |
---|
139 | n/a | self.name = name |
---|
140 | n/a | self.classes = classes |
---|
141 | n/a | self.file = file |
---|
142 | n/a | try: |
---|
143 | n/a | self.cl = self.classes[self.name] |
---|
144 | n/a | except (IndexError, KeyError): |
---|
145 | n/a | self.cl = None |
---|
146 | n/a | self.isfunction = isinstance(self.cl, pyclbr.Function) |
---|
147 | n/a | |
---|
148 | n/a | def GetText(self): |
---|
149 | n/a | if self.isfunction: |
---|
150 | n/a | return "def " + self.name + "(...)" |
---|
151 | n/a | else: |
---|
152 | n/a | return "class " + self.name |
---|
153 | n/a | |
---|
154 | n/a | def GetIconName(self): |
---|
155 | n/a | if self.isfunction: |
---|
156 | n/a | return "python" |
---|
157 | n/a | else: |
---|
158 | n/a | return "folder" |
---|
159 | n/a | |
---|
160 | n/a | def IsExpandable(self): |
---|
161 | n/a | if self.cl: |
---|
162 | n/a | try: |
---|
163 | n/a | return not not self.cl.methods |
---|
164 | n/a | except AttributeError: |
---|
165 | n/a | return False |
---|
166 | n/a | |
---|
167 | n/a | def GetSubList(self): |
---|
168 | n/a | if not self.cl: |
---|
169 | n/a | return [] |
---|
170 | n/a | sublist = [] |
---|
171 | n/a | for name in self.listmethods(): |
---|
172 | n/a | item = MethodBrowserTreeItem(name, self.cl, self.file) |
---|
173 | n/a | sublist.append(item) |
---|
174 | n/a | return sublist |
---|
175 | n/a | |
---|
176 | n/a | def OnDoubleClick(self): |
---|
177 | n/a | if not os.path.exists(self.file): |
---|
178 | n/a | return |
---|
179 | n/a | edit = file_open(self.file) |
---|
180 | n/a | if hasattr(self.cl, 'lineno'): |
---|
181 | n/a | lineno = self.cl.lineno |
---|
182 | n/a | edit.gotoline(lineno) |
---|
183 | n/a | |
---|
184 | n/a | def listmethods(self): |
---|
185 | n/a | if not self.cl: |
---|
186 | n/a | return [] |
---|
187 | n/a | items = [] |
---|
188 | n/a | for name, lineno in self.cl.methods.items(): |
---|
189 | n/a | items.append((lineno, name)) |
---|
190 | n/a | items.sort() |
---|
191 | n/a | list = [] |
---|
192 | n/a | for item, name in items: |
---|
193 | n/a | list.append(name) |
---|
194 | n/a | return list |
---|
195 | n/a | |
---|
196 | n/a | class MethodBrowserTreeItem(TreeItem): |
---|
197 | n/a | |
---|
198 | n/a | def __init__(self, name, cl, file): |
---|
199 | n/a | self.name = name |
---|
200 | n/a | self.cl = cl |
---|
201 | n/a | self.file = file |
---|
202 | n/a | |
---|
203 | n/a | def GetText(self): |
---|
204 | n/a | return "def " + self.name + "(...)" |
---|
205 | n/a | |
---|
206 | n/a | def GetIconName(self): |
---|
207 | n/a | return "python" # XXX |
---|
208 | n/a | |
---|
209 | n/a | def IsExpandable(self): |
---|
210 | n/a | return 0 |
---|
211 | n/a | |
---|
212 | n/a | def OnDoubleClick(self): |
---|
213 | n/a | if not os.path.exists(self.file): |
---|
214 | n/a | return |
---|
215 | n/a | edit = file_open(self.file) |
---|
216 | n/a | edit.gotoline(self.cl.methods[self.name]) |
---|
217 | n/a | |
---|
218 | n/a | def _class_browser(parent): #Wrapper for htest |
---|
219 | n/a | try: |
---|
220 | n/a | file = __file__ |
---|
221 | n/a | except NameError: |
---|
222 | n/a | file = sys.argv[0] |
---|
223 | n/a | if sys.argv[1:]: |
---|
224 | n/a | file = sys.argv[1] |
---|
225 | n/a | else: |
---|
226 | n/a | file = sys.argv[0] |
---|
227 | n/a | dir, file = os.path.split(file) |
---|
228 | n/a | name = os.path.splitext(file)[0] |
---|
229 | n/a | flist = pyshell.PyShellFileList(parent) |
---|
230 | n/a | global file_open |
---|
231 | n/a | file_open = flist.open |
---|
232 | n/a | ClassBrowser(flist, name, [dir], _htest=True) |
---|
233 | n/a | |
---|
234 | n/a | if __name__ == "__main__": |
---|
235 | n/a | from idlelib.idle_test.htest import run |
---|
236 | n/a | run(_class_browser) |
---|