summaryrefslogtreecommitdiffstats
path: root/Tools/idle/PathBrowser.py
blob: b24977f4a691781c297e1dbe7741df2d44e3ede8 (plain)
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
import os
import sys
import imp
import string
import tkMessageBox

from MultiScrolledLists import MultiScrolledLists

class PathBrowser(MultiScrolledLists):
    
    def __init__(self, flist):
        self.flist = flist
        MultiScrolledLists.__init__(self, flist.root, 4)
    
    def longtitle(self):
        return "Path Browser"
    
    def width(self, i):
        return 30
    
    def height(self, i):
        return 20
    
    def subtitle(self, i):
        if i == 0:
            return "Path Entries (sys.path)"
        if i-1 >= len(self.path):
            return ""
        if i == 1:
            return self.path[0]
        if i == 2:
            return "Classes in " + self.path[1]
        if i == 3:
            s = self.path[2]
            i = string.find(s, "(")
            if i > 0:
                s = s[:i]
            return "Methods of " + s
        return ""
    
    def items(self, i):
        if i == 0:
            return sys.path
        if i == 1:
            return self.listmodules()
        if i == 2:
            return self.listclasses()
        if i == 3:
            return self.listmethods()
    
    def listmodules(self):
        dir = self.path[0] or os.curdir
        modules = {}
        suffixes = imp.get_suffixes()
        allnames = os.listdir(dir)
        sorted = []
        for suff, mode, flag in suffixes:
            i = -len(suff)
            for name in allnames:
                normed_name = os.path.normcase(name)
                if normed_name[i:] == suff:
                    mod_name = name[:i]
                    if not modules.has_key(mod_name):
                        modules[mod_name] = None
                        sorted.append((normed_name, name))
        sorted.sort()
        names = []
        for nn, name in sorted:
            names.append(name)
        return names
    
    def listclasses(self):
        import pyclbr
        dir = self.path[0]
        file = self.path[1]
        name, ext = os.path.splitext(file)
        if os.path.normcase(ext) != ".py":
            self.top.bell()
            return []
        try:
            self.top.configure(cursor="watch")
            self.top.update_idletasks()
            try:
                dict = pyclbr.readmodule(name, [dir] + sys.path)
            finally:
                self.top.configure(cursor="")
        except ImportError, msg:
            tkMessageBox.showerror("Import error", str(msg), parent=root)
            return []
        items = []
        self.classes = {}
        for key, cl in dict.items():
            if cl.module == name:
                s = key
                if cl.super:
                    supers = []
                    for sup in cl.super:
                        if type(sup) is type(''):
                            sname = sup
                        else:
                            sname = sup.name
                            if sup.module != cl.module:
                                sname = "%s.%s" % (sup.module, sname)
                        supers.append(sname)
                    s = s + "(%s)" % string.join(supers, ", ")
                items.append((cl.lineno, s))
                self.classes[s] = cl
        items.sort()
        list = []
        for item, s in items:
            list.append(s)
        return list
    
    def listmethods(self):
        try:
            cl = self.classes[self.path[2]]
        except (IndexError, KeyError):
            return []
        items = []
        for name, lineno in cl.methods.items():
            items.append((lineno, name))
        items.sort()
        list = []
        for item, name in items:
            list.append(name)
        return list
    
    def on_double(self, index, i):
        if i == 0:
            return
        if i >= 1:
            dir = self.path[0]
            file = self.path[1]
            name, ext = os.path.splitext(file)
            if os.path.normcase(ext) != ".py":
                self.top.bell()
                return
            fullname = os.path.join(dir, file)
            edit = self.flist.open(fullname)
            if i >= 2:
                classname = self.path[2]
                try:
                    cl = self.classes[classname]
                except KeyError:
                    cl = None
                else:
                    if i == 2:
                        edit.gotoline(cl.lineno)
                    else:
                        methodname = self.path[3]
                        edit.gotoline(cl.methods[methodname])


def main():
    import PyShell
    PathBrowser(PyShell.flist)

if __name__ == "__main__":
    main()