blob: a93d68311f5d61bed049123db51b3b3026c5df96 [file] [log] [blame]
Jack Jansen73019a62003-02-11 23:15:33 +00001import W
2import Wapplication
3from Carbon import Evt
4import EasyDialogs
5import FrameWork
6
7import sys
8import string
9import os
10
11import pimp
12
13ELIPSES = '...'
14
Jack Jansen113af982003-02-12 12:47:56 +000015class PackageManagerMain(Wapplication.Application):
Jack Jansen73019a62003-02-11 23:15:33 +000016
17 def __init__(self):
Jack Jansen113af982003-02-12 12:47:56 +000018 self.preffilepath = os.path.join("Python", "Package Install Manager Prefs")
Jack Jansen73019a62003-02-11 23:15:33 +000019 Wapplication.Application.__init__(self, 'Pimp')
20 from Carbon import AE
21 from Carbon import AppleEvents
22
23 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEOpenApplication,
24 self.ignoreevent)
25 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEReopenApplication,
26 self.ignoreevent)
27 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEPrintDocuments,
28 self.ignoreevent)
29 AE.AEInstallEventHandler(AppleEvents.kCoreEventClass, AppleEvents.kAEQuitApplication,
30 self.quitevent)
Jack Jansen113af982003-02-12 12:47:56 +000031 if 1:
Jack Jansen73019a62003-02-11 23:15:33 +000032 import PyConsole
33 # With -D option (OSX command line only) keep stderr, for debugging the IDE
34 # itself.
35 debug_stderr = None
36 if len(sys.argv) >= 2 and sys.argv[1] == '-D':
37 debug_stderr = sys.stderr
38 del sys.argv[1]
39 PyConsole.installoutput()
Jack Jansen73019a62003-02-11 23:15:33 +000040 if debug_stderr:
41 sys.stderr = debug_stderr
42 self.opendoc(None)
43 self.mainloop()
44
45 def makeusermenus(self):
46 m = Wapplication.Menu(self.menubar, "File")
Jack Jansen113af982003-02-12 12:47:56 +000047## newitem = FrameWork.MenuItem(m, "Open Standard Database", "N", 'openstandard')
Jack Jansen73019a62003-02-11 23:15:33 +000048## openitem = FrameWork.MenuItem(m, "Open"+ELIPSES, "O", 'open')
49## openbynameitem = FrameWork.MenuItem(m, "Open URL"+ELIPSES, "D", 'openbyname')
50 FrameWork.Separator(m)
51 closeitem = FrameWork.MenuItem(m, "Close", "W", 'close')
52## saveitem = FrameWork.MenuItem(m, "Save", "S", 'save')
Jack Jansen113af982003-02-12 12:47:56 +000053## saveasitem = FrameWork.MenuItem(m, "Save as"+ELIPSES, None, 'save_as')
Jack Jansen73019a62003-02-11 23:15:33 +000054 FrameWork.Separator(m)
55
56 m = Wapplication.Menu(self.menubar, "Edit")
57 undoitem = FrameWork.MenuItem(m, "Undo", 'Z', "undo")
58 FrameWork.Separator(m)
59 cutitem = FrameWork.MenuItem(m, "Cut", 'X', "cut")
60 copyitem = FrameWork.MenuItem(m, "Copy", "C", "copy")
61 pasteitem = FrameWork.MenuItem(m, "Paste", "V", "paste")
62 FrameWork.MenuItem(m, "Clear", None, "clear")
63 FrameWork.Separator(m)
64 selallitem = FrameWork.MenuItem(m, "Select all", "A", "selectall")
65
66 m = Wapplication.Menu(self.menubar, "Package")
67 runitem = FrameWork.MenuItem(m, "Install", "I", 'install')
68 homepageitem = FrameWork.MenuItem(m, "Visit Homepage", None, 'homepage')
69
70 self.openwindowsmenu = Wapplication.Menu(self.menubar, 'Windows')
71 self.makeopenwindowsmenu()
72 self._menustocheck = [closeitem, saveasitem,
73 undoitem, cutitem, copyitem, pasteitem,
74 selallitem,
75 runitem, homepageitem]
76
77 def quitevent(self, theAppleEvent, theReply):
78 from Carbon import AE
79 AE.AEInteractWithUser(50000000)
80 self._quit()
81
82 def ignoreevent(self, theAppleEvent, theReply):
83 pass
84
85 def opendocsevent(self, theAppleEvent, theReply):
86 W.SetCursor('watch')
87 import aetools
88 parameters, args = aetools.unpackevent(theAppleEvent)
89 docs = parameters['----']
90 if type(docs) <> type([]):
91 docs = [docs]
92 for doc in docs:
93 fsr, a = doc.FSResolveAlias(None)
94 path = fsr.as_pathname()
95 path = urllib.pathname2url(path)
96 self.opendoc(path)
97
98 def opendoc(self, url):
99 PackageBrowser(url)
100
101 def getabouttext(self):
Jack Jansen113af982003-02-12 12:47:56 +0000102 return "About Package Manager"+ELIPSES
Jack Jansen73019a62003-02-11 23:15:33 +0000103
104 def do_about(self, id, item, window, event):
Jack Jansen113af982003-02-12 12:47:56 +0000105 EasyDialogs.Message("Package Install Manager for Python")
Jack Jansen73019a62003-02-11 23:15:33 +0000106
107 def domenu_open(self, *args):
108 filename = EasyDialogs.AskFileForOpen(typeList=("TEXT",))
109 if filename:
110 filename = urllib.pathname2url(filename)
111 self.opendoc(filename)
112
113 def domenu_openbyname(self, *args):
114 url = EasyDialogs.AskString("Open URL:", ok="Open")
115 if url:
116 self.opendoc(url)
117
118 def makeopenwindowsmenu(self):
119 for i in range(len(self.openwindowsmenu.items)):
120 self.openwindowsmenu.menu.DeleteMenuItem(1)
121 self.openwindowsmenu.items = []
122 windows = []
123 self._openwindows = {}
124 for window in self._windows.keys():
125 title = window.GetWTitle()
126 if not title:
127 title = "<no title>"
128 windows.append((title, window))
129 windows.sort()
130 for title, window in windows:
131 shortcut = None
132 item = FrameWork.MenuItem(self.openwindowsmenu, title, shortcut, callback = self.domenu_openwindows)
133 self._openwindows[item.item] = window
134 self._openwindowscheckmark = 0
135 self.checkopenwindowsmenu()
136
137 def domenu_openwindows(self, id, item, window, event):
138 w = self._openwindows[item]
139 w.ShowWindow()
140 w.SelectWindow()
141
142 def domenu_quit(self):
143 self._quit()
144
145 def domenu_save(self, *args):
146 print "Save"
147
148 def _quit(self):
149## import PyConsole, PyEdit
150 for window in self._windows.values():
151 try:
152 rv = window.close() # ignore any errors while quitting
153 except:
154 rv = 0 # (otherwise, we can get stuck!)
155 if rv and rv > 0:
156 return
157## try:
158## PyConsole.console.writeprefs()
159## PyConsole.output.writeprefs()
160## PyEdit.searchengine.writeprefs()
161## except:
162## # Write to __stderr__ so the msg end up in Console.app and has
163## # at least _some_ chance of getting read...
164## # But: this is a workaround for way more serious problems with
165## # the Python 2.2 Jaguar addon.
166## sys.__stderr__.write("*** PythonIDE: Can't write preferences ***\n")
167 self.quitting = 1
168
169class PimpInterface:
170
171 def setuppimp(self, url):
172 self.pimpprefs = pimp.PimpPreferences()
173 self.pimpdb = pimp.PimpDatabase(self.pimpprefs)
Jack Jansen113af982003-02-12 12:47:56 +0000174 self.pimpinstaller = pimp.PimpInstaller(self.pimpdb)
Jack Jansen73019a62003-02-11 23:15:33 +0000175 if not url:
176 url = self.pimpprefs.pimpDatabase
177 self.pimpdb.appendURL(url)
178
179 def getbrowserdata(self):
180 self.packages = self.pimpdb.list()
181 rv = []
182 for pkg in self.packages:
183 name = pkg.fullname()
184 status, _ = pkg.installed()
185 description = pkg.description()
186 rv.append((status, name, description))
187 return rv
188
189 def getstatus(self, number):
190 pkg = self.packages[number]
191 return pkg.installed()
Jack Jansen113af982003-02-12 12:47:56 +0000192
193 def installpackage(self, sel, output, recursive, force):
194 pkg = self.packages[sel]
195 list, messages = self.pimpinstaller.prepareInstall(pkg, force, recursive)
196 if messages:
197 return messages
198 messages = self.pimpinstaller.install(list, output)
199 return messages
Jack Jansen73019a62003-02-11 23:15:33 +0000200
201class PackageBrowser(PimpInterface):
202
203 def __init__(self, url = None):
204 self.ic = None
205 self.setuppimp(url)
206 self.setupwidgets()
207 self.updatestatus()
208
209 def setupwidgets(self):
210 self.w = W.Window((580, 400), "Python Install Manager", minsize = (300, 200), tabbable = 0)
211## self.w.divline = W.HorizontalLine((0, 20, 0, 0))
212 self.w.titlebar = W.TextBox((4, 4, 40, 12), 'Packages:')
213 data = self.getbrowserdata()
214 self.w.packagebrowser = W.MultiList((4, 20, 0, -70), data, self.listhit, cols=3)
215 self.w.installed_l = W.TextBox((4, -66, 60, 12), 'Installed:')
216 self.w.installed = W.TextBox((64, -66, 0, 12), '')
217 self.w.message_l = W.TextBox((4, -48, 60, 12), 'Status:')
218 self.w.message = W.TextBox((64, -48, 0, 12), '')
219 self.w.homepage_button = W.Button((4, -28, 96, 18), 'View homepage', self.do_homepage)
Jack Jansen113af982003-02-12 12:47:56 +0000220 self.w.verbose_button = W.CheckBox((-288, -26, 60, 18), 'Verbose')
221 self.w.recursive_button = W.CheckBox((-224, -26, 80, 18), 'Recursive', self.updatestatus)
222 self.w.recursive_button.set(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000223 self.w.force_button = W.CheckBox((-140, -26, 60, 18), 'Force', self.updatestatus)
224 self.w.install_button = W.Button((-76, -28, 56, 18), 'Install', self.do_install)
225 self.w.open()
226
227 def updatestatus(self):
228 sel = self.w.packagebrowser.getselection()
Jack Jansen113af982003-02-12 12:47:56 +0000229 data = self.getbrowserdata()
230 self.w.packagebrowser.setitems(data)
Jack Jansen73019a62003-02-11 23:15:33 +0000231 if len(sel) != 1:
232 self.w.installed.set('')
233 self.w.message.set('')
234 self.w.install_button.enable(0)
235 self.w.homepage_button.enable(0)
236 self.w.verbose_button.enable(0)
Jack Jansen113af982003-02-12 12:47:56 +0000237 self.w.recursive_button.enable(0)
Jack Jansen73019a62003-02-11 23:15:33 +0000238 self.w.force_button.enable(0)
239 else:
240 sel = sel[0]
Jack Jansen113af982003-02-12 12:47:56 +0000241 self.w.packagebrowser.setselection([sel])
Jack Jansen73019a62003-02-11 23:15:33 +0000242 installed, message = self.getstatus(sel)
243 self.w.installed.set(installed)
244 self.w.message.set(message)
245 self.w.install_button.enable(installed != "yes" or self.w.force_button.get())
246 self.w.homepage_button.enable(not not self.packages[sel].homepage())
247 self.w.verbose_button.enable(1)
Jack Jansen113af982003-02-12 12:47:56 +0000248 self.w.recursive_button.enable(1)
Jack Jansen73019a62003-02-11 23:15:33 +0000249 self.w.force_button.enable(1)
250
251 def listhit(self, *args, **kwargs):
252 self.updatestatus()
253
254 def do_install(self):
Jack Jansen113af982003-02-12 12:47:56 +0000255 sel = self.w.packagebrowser.getselection()[0]
256 if self.w.verbose_button.get():
257 output = sys.stdout
258 else:
259 output = None
260 recursive = self.w.recursive_button.get()
261 force = self.w.force_button.get()
262 messages = self.installpackage(sel, output, recursive, force)
263 self.updatestatus()
264 if messages:
265 EasyDialogs.Message('\n'.join(messages))
Jack Jansen73019a62003-02-11 23:15:33 +0000266
267 def do_homepage(self):
268 sel = self.w.packagebrowser.getselection()[0]
269 if not self.ic:
270 import ic
271
272 self.ic = ic.IC()
273 self.ic.launchurl(self.packages[sel].homepage())
274
275if __name__ == '__main__':
Jack Jansen113af982003-02-12 12:47:56 +0000276 PackageManagerMain()