blob: 0e8850fc8a22648c101aaa39fef7cf20b728a57d [file] [log] [blame]
Just van Rossum40f9b7b1999-01-30 22:39:17 +00001import W
2import sys
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00003from Carbon import Qd
Just van Rossum40f9b7b1999-01-30 22:39:17 +00004
5__version__ = "0.2"
6__author__ = "jvr"
7
8class _modulebrowser:
9
10 def __init__(self):
11 self.editmodules = []
12 self.modules = []
Just van Rossum6b45b1e2001-11-02 22:55:15 +000013 self.window = W.Window((210, 1000), "Module Browser", minsize = (210, 160), maxsize = (340, 20000))
Just van Rossum40f9b7b1999-01-30 22:39:17 +000014
Just van Rossum6b45b1e2001-11-02 22:55:15 +000015 self.window.openbutton = W.Button((10, 8, 90, 16), "Open", self.openbuttonhit)
16 self.window.browsebutton = W.Button((110, 8, 90, 16), "Browse\xc9", self.browsebuttonhit)
17 self.window.reloadbutton = W.Button((10, 32, 90, 16), "Reload", self.reloadbuttonhit)
18 self.window.openotherbutton = W.Button((110, 32, 90, 16), "Open other\xc9", self.openother)
Just van Rossum40f9b7b1999-01-30 22:39:17 +000019
20 self.window.openbutton.enable(0)
21 self.window.reloadbutton.enable(0)
22 self.window.browsebutton.enable(0)
23 self.window.setdefaultbutton(self.window.browsebutton)
24
25 self.window.bind("cmdr", self.window.reloadbutton.push)
26 self.window.bind("cmdb", self.window.browsebutton.push)
27
28 self.window.bind("<activate>", self.activate)
29 self.window.bind("<close>", self.close)
30
31 self.window.list = W.List((-1, 56, 1, -14), [], self.listhit)
32
33 self.window.open()
34 self.checkbuttons()
35
36 def close(self):
37 global _browser
38 _browser = None
39
40 def activate(self, onoff):
41 if onoff:
42 self.makelist()
43
44 def listhit(self, isdbl):
45 self.checkbuttons()
46 if isdbl:
47 if self.window._defaultbutton:
48 self.window._defaultbutton.push()
49
50 def checkbuttons(self):
51 sel = self.window.list.getselection()
52 if sel:
53 for i in sel:
54 if self.editmodules[i]:
55 self.window.openbutton.enable(1)
56 self.window.reloadbutton.enable(1)
57 self.window.setdefaultbutton(self.window.openbutton)
58 break
59 else:
60 self.window.openbutton.enable(0)
61 self.window.reloadbutton.enable(0)
62 self.window.setdefaultbutton(self.window.browsebutton)
63 self.window.browsebutton.enable(1)
64 else:
65 #self.window.setdefaultbutton(self.window.browsebutton)
66 self.window.openbutton.enable(0)
67 self.window.reloadbutton.enable(0)
68 self.window.browsebutton.enable(0)
69
70 def openbuttonhit(self):
71 import imp
72 sel = self.window.list.getselection()
73 W.SetCursor("watch")
74 for i in sel:
75 modname = self.window.list[i]
76 try:
77 self.openscript(sys.modules[modname].__file__, modname)
78 except IOError:
79 try:
80 file, path, description = imp.find_module(modname)
81 except ImportError:
82 W.SetCursor("arrow")
Just van Rossumdc3c6172001-06-19 21:37:33 +000083 W.Message("Can't find file for module '%s'."
Just van Rossum40f9b7b1999-01-30 22:39:17 +000084 % modname)
85 else:
86 self.openscript(path, modname)
87
88 def openscript(self, path, modname):
89 import os
90 if path[-3:] == '.py':
91 W.getapplication().openscript(path, modname=modname)
92 elif path[-4:] in ['.pyc', '.pyo']:
93 W.getapplication().openscript(path[:-1], modname=modname)
94 else:
Just van Rossumdc3c6172001-06-19 21:37:33 +000095 W.Message("Can't edit '%s'; it might be a shared library or a .pyc file."
Just van Rossum40f9b7b1999-01-30 22:39:17 +000096 % modname)
97
98 def openother(self):
99 import imp
100 import EasyDialogs
101
102 modname = EasyDialogs.AskString("Open module:")
103 if modname:
104 try:
105 file, path, description = imp.find_module(modname)
106 except ImportError:
107 if modname in sys.builtin_module_names:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000108 alerttext = "'%s' is a builtin module, which you can't edit." % modname
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000109 else:
Just van Rossumdc3c6172001-06-19 21:37:33 +0000110 alerttext = "No module named '%s'." % modname
Just van Rossum40f9b7b1999-01-30 22:39:17 +0000111 raise W.AlertError, alerttext
112 self.openscript(path, modname)
113
114 def reloadbuttonhit(self):
115 sel = self.window.list.getselection()
116 W.SetCursor("watch")
117 for i in sel:
118 mname = self.window.list[i]
119 m = sys.modules[mname]
120 # Set the __name__ attribute of the module to its real name.
121 # reload() complains if it's __main__, which is true
122 # when it recently has been run as a script with "Run as __main__"
123 # enabled.
124 m.__name__ = mname
125 reload(m)
126
127 def browsebuttonhit(self):
128 sel = self.window.list.getselection()
129 if not sel:
130 return
131 import PyBrowser
132 for i in sel:
133 PyBrowser.Browser(sys.modules[self.window.list[i]])
134
135 def makelist(self):
136 editmodules, modules = getmoduleslist()
137 if modules == self.modules:
138 return
139 self.editmodules, self.modules = editmodules, modules
140 self.window.list.setdrawingmode(0)
141 sel = self.window.list.getselectedobjects()
142 self.window.list.set(self.modules)
143 self.window.list.setselectedobjects(sel)
144 self.window.list.setdrawingmode(1)
145
146
147def getmoduleslist():
148 import PyBrowser # for caselesssort function
149 moduleitems = sys.modules.items()
150 moduleitems = filter(lambda (name, module): module is not None, moduleitems)
151 modules = map(lambda (name, module): name, moduleitems)
152 modules = PyBrowser.caselesssort(modules)
153 editmodules = []
154 sysmodules = sys.modules
155 modulesappend = editmodules.append
156 for m in modules:
157 module = sysmodules[m]
158 try:
159 if sysmodules[m].__file__[-3:] == '.py' or \
160 sysmodules[m].__file__[-4:] in ['.pyc', '.pyo']:
161 modulesappend(1)
162 else:
163 modulesappend(0)
164 except AttributeError:
165 modulesappend(0)
166 return editmodules, modules
167
168
169
170_browser = None
171
172def ModuleBrowser():
173 global _browser
174 if _browser is not None:
175 _browser.window.select()
176 else:
177 _browser = _modulebrowser()