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