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