| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | """Edit the Python Preferences file.""" | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # This program is getting more and more clunky. It should really | 
					
						
							|  |  |  | # be rewritten in a modeless way some time soon. | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from Dlg import * | 
					
						
							|  |  |  | from Events import * | 
					
						
							|  |  |  | from Res import * | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | import Controls | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | import string | 
					
						
							|  |  |  | import struct | 
					
						
							|  |  |  | import macfs | 
					
						
							|  |  |  | import MacOS | 
					
						
							|  |  |  | import os | 
					
						
							|  |  |  | import sys | 
					
						
							|  |  |  | import Res # For Res.Error | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | import pythonprefs | 
					
						
							|  |  |  | import EasyDialogs | 
					
						
							| 
									
										
										
										
											2001-01-29 14:59:33 +00:00
										 |  |  | try: | 
					
						
							|  |  |  | 	import Help | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  | 	Help = None | 
					
						
							| 
									
										
										
										
											1996-10-22 15:33:02 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | # resource IDs in our own resources (dialogs, etc) | 
					
						
							|  |  |  | MESSAGE_ID = 256 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-10-11 11:30:26 +00:00
										 |  |  | DIALOG_ID = 511 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | TEXT_ITEM = 1 | 
					
						
							|  |  |  | OK_ITEM = 2 | 
					
						
							|  |  |  | CANCEL_ITEM = 3 | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | DIR_ITEM = 4 | 
					
						
							|  |  |  | TITLE_ITEM = 5 | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | OPTIONS_ITEM = 7 | 
					
						
							| 
									
										
										
										
											1997-09-09 13:58:19 +00:00
										 |  |  | HELP_ITEM = 9 | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # The options dialog. There is a correspondence between | 
					
						
							|  |  |  | # the dialog item numbers and the option. | 
					
						
							| 
									
										
										
										
											1996-10-11 11:30:26 +00:00
										 |  |  | OPT_DIALOG_ID = 510 | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | # Map dialog item numbers to option names (and the reverse) | 
					
						
							|  |  |  | opt_dialog_map = [ | 
					
						
							| 
									
										
										
										
											2000-10-19 21:50:54 +00:00
										 |  |  | 	None, | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | 	None, | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	None, | 
					
						
							|  |  |  | 	"inspect", | 
					
						
							|  |  |  | 	"verbose", | 
					
						
							|  |  |  | 	"optimize", | 
					
						
							|  |  |  | 	"unbuffered", | 
					
						
							|  |  |  | 	"debugging", | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | 	"tabwarn", | 
					
						
							|  |  |  | 	"nosite", | 
					
						
							|  |  |  | 	"nonavservice", | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	"nointopt", | 
					
						
							|  |  |  | 	"noargs", | 
					
						
							| 
									
										
										
										
											1997-09-09 13:58:19 +00:00
										 |  |  | 	"delayconsole", | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | 	] | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | opt_dialog_dict = {} | 
					
						
							|  |  |  | for i in range(len(opt_dialog_map)): | 
					
						
							|  |  |  | 	if opt_dialog_map[i]: | 
					
						
							|  |  |  | 		opt_dialog_dict[opt_dialog_map[i]] = i | 
					
						
							|  |  |  | # 1 thru 10 are the options | 
					
						
							| 
									
										
										
										
											1996-09-06 22:25:34 +00:00
										 |  |  | # The GUSI creator/type and delay-console | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | OD_CREATOR_ITEM = 18 | 
					
						
							|  |  |  | OD_TYPE_ITEM = 19 | 
					
						
							|  |  |  | OD_OK_ITEM = 1 | 
					
						
							|  |  |  | OD_CANCEL_ITEM = 2 | 
					
						
							|  |  |  | OD_HELP_ITEM = 20 | 
					
						
							|  |  |  | OD_KEEPALWAYS_ITEM = 14 | 
					
						
							|  |  |  | OD_KEEPOUTPUT_ITEM = 15 | 
					
						
							|  |  |  | OD_KEEPERROR_ITEM = 16 | 
					
						
							|  |  |  | OD_KEEPNEVER_ITEM = 17 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | def optinteract(options): | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 	"""Let the user interact with the options dialog""" | 
					
						
							|  |  |  | 	d = GetNewDialog(OPT_DIALOG_ID, -1) | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 	htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) | 
					
						
							|  |  |  | 	SetDialogItemText(htext, options['creator']) | 
					
						
							|  |  |  | 	htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) | 
					
						
							|  |  |  | 	SetDialogItemText(htext, options['type']) | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 	d.SetDialogDefaultItem(OD_OK_ITEM) | 
					
						
							|  |  |  | 	d.SetDialogCancelItem(OD_CANCEL_ITEM) | 
					
						
							| 
									
										
										
										
											2001-01-29 14:59:33 +00:00
										 |  |  | 	if not Help: | 
					
						
							|  |  |  | 		d.HideDialogItem(OD_HELP_ITEM) | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 	while 1: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 		for name in opt_dialog_dict.keys(): | 
					
						
							|  |  |  | 			num = opt_dialog_dict[name] | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 			ctl = d.GetDialogItemAsControl(num) | 
					
						
							|  |  |  | 			ctl.SetControlValue(options[name]) | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | 		ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM) | 
					
						
							|  |  |  | 		ctl.SetControlValue(options['keep_console'] == 3) | 
					
						
							|  |  |  | 		ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM) | 
					
						
							|  |  |  | 		ctl.SetControlValue(options['keep_console'] == 1) | 
					
						
							|  |  |  | 		ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM) | 
					
						
							|  |  |  | 		ctl.SetControlValue(options['keep_console'] == 2) | 
					
						
							|  |  |  | 		ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM) | 
					
						
							|  |  |  | 		ctl.SetControlValue(options['keep_console'] == 0) | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 		n = ModalDialog(None) | 
					
						
							|  |  |  | 		if n == OD_OK_ITEM: | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 			htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM) | 
					
						
							|  |  |  | 			ncreator = GetDialogItemText(htext) | 
					
						
							|  |  |  | 			htext = d.GetDialogItemAsControl(OD_TYPE_ITEM) | 
					
						
							|  |  |  | 			ntype = GetDialogItemText(htext) | 
					
						
							| 
									
										
										
										
											1996-09-06 22:25:34 +00:00
										 |  |  | 			if len(ncreator) == 4 and len(ntype) == 4: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 				options['creator'] = ncreator | 
					
						
							|  |  |  | 				options['type'] = ntype | 
					
						
							|  |  |  | 				return options | 
					
						
							| 
									
										
										
										
											1996-09-06 22:25:34 +00:00
										 |  |  | 			else: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 				MacOS.SysBeep() | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 		elif n == OD_CANCEL_ITEM: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 			return | 
					
						
							| 
									
										
										
										
											1996-09-06 22:25:34 +00:00
										 |  |  | 		elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM): | 
					
						
							|  |  |  | 			pass | 
					
						
							| 
									
										
										
										
											2000-09-22 21:50:11 +00:00
										 |  |  | 		elif n == OD_KEEPALWAYS_ITEM: | 
					
						
							|  |  |  | 			options['keep_console'] = 3; | 
					
						
							|  |  |  | 		elif n == OD_KEEPOUTPUT_ITEM: | 
					
						
							|  |  |  | 			options['keep_console'] = 1; | 
					
						
							|  |  |  | 		elif n == OD_KEEPERROR_ITEM: | 
					
						
							|  |  |  | 			options['keep_console'] = 2; | 
					
						
							|  |  |  | 		elif n == OD_KEEPNEVER_ITEM: | 
					
						
							|  |  |  | 			options['keep_console'] = 0; | 
					
						
							| 
									
										
										
										
											2001-01-29 14:59:33 +00:00
										 |  |  | 		elif n == OD_HELP_ITEM and Help: | 
					
						
							| 
									
										
										
										
											1997-09-09 13:58:19 +00:00
										 |  |  | 			onoff = Help.HMGetBalloons() | 
					
						
							|  |  |  | 			Help.HMSetBalloons(not onoff) | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 		elif 1 <= n <= len(opt_dialog_map): | 
					
						
							|  |  |  | 			options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]]) | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | def interact(options, title): | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 	"""Let the user interact with the dialog""" | 
					
						
							|  |  |  | 	try: | 
					
						
							|  |  |  | 		# Try to go to the "correct" dir for GetDirectory | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 		os.chdir(options['dir'].as_pathname()) | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 	except os.error: | 
					
						
							|  |  |  | 		pass | 
					
						
							|  |  |  | 	d = GetNewDialog(DIALOG_ID, -1) | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 	htext = d.GetDialogItemAsControl(TITLE_ITEM) | 
					
						
							|  |  |  | 	SetDialogItemText(htext, title) | 
					
						
							| 
									
										
										
										
											2000-01-13 16:22:12 +00:00
										 |  |  | 	path_ctl = d.GetDialogItemAsControl(TEXT_ITEM) | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 	data = string.joinfields(options['path'], '\r') | 
					
						
							| 
									
										
										
										
											2000-01-13 16:22:12 +00:00
										 |  |  | 	path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data) | 
					
						
							| 
									
										
										
										
											1999-12-23 14:34:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-11-20 14:55:26 +00:00
										 |  |  | 	d.SelectDialogItemText(TEXT_ITEM, 0, 32767) | 
					
						
							|  |  |  | 	d.SelectDialogItemText(TEXT_ITEM, 0, 0) | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | ##	d.SetDialogDefaultItem(OK_ITEM) | 
					
						
							|  |  |  | 	d.SetDialogCancelItem(CANCEL_ITEM) | 
					
						
							| 
									
										
										
										
											2001-01-29 14:59:33 +00:00
										 |  |  | 	if not Help: | 
					
						
							|  |  |  | 		d.HideDialogItem(HELP_ITEM) | 
					
						
							| 
									
										
										
										
											1996-11-20 14:55:26 +00:00
										 |  |  | 	d.GetDialogWindow().ShowWindow() | 
					
						
							|  |  |  | 	d.DrawDialog() | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 	while 1: | 
					
						
							|  |  |  | 		n = ModalDialog(None) | 
					
						
							|  |  |  | 		if n == OK_ITEM: | 
					
						
							|  |  |  | 			break | 
					
						
							|  |  |  | 		if n == CANCEL_ITEM: | 
					
						
							|  |  |  | 			return None | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | ##		if n == REVERT_ITEM: | 
					
						
							|  |  |  | ##			return [], pythondir | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 		if n == DIR_ITEM: | 
					
						
							| 
									
										
										
										
											1995-08-14 12:21:12 +00:00
										 |  |  | 			fss, ok = macfs.GetDirectory('Select python home folder:') | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 			if ok: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 				options['dir'] = fss | 
					
						
							| 
									
										
										
										
											2001-01-29 14:59:33 +00:00
										 |  |  | 		elif n == HELP_ITEM and Help: | 
					
						
							| 
									
										
										
										
											1997-09-09 13:58:19 +00:00
										 |  |  | 			onoff = Help.HMGetBalloons() | 
					
						
							|  |  |  | 			Help.HMSetBalloons(not onoff) | 
					
						
							| 
									
										
										
										
											1996-04-10 14:52:18 +00:00
										 |  |  | 		if n == OPTIONS_ITEM: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 			noptions = options | 
					
						
							|  |  |  | 			for k in options.keys(): | 
					
						
							|  |  |  | 				noptions[k] = options[k] | 
					
						
							|  |  |  | 			noptions = optinteract(noptions) | 
					
						
							|  |  |  | 			if noptions: | 
					
						
							|  |  |  | 				options = noptions | 
					
						
							| 
									
										
										
										
											2000-01-13 16:22:12 +00:00
										 |  |  | 	data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag) | 
					
						
							|  |  |  | 	tmp = string.splitfields(data, '\r') | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	newpath = [] | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 	for i in tmp: | 
					
						
							|  |  |  | 		if i: | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 			newpath.append(i) | 
					
						
							|  |  |  | 	options['path'] = newpath | 
					
						
							|  |  |  | 	return options | 
					
						
							| 
									
										
										
										
											1996-09-06 22:25:34 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | def edit_preferences(): | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	handler = pythonprefs.PythonOptions() | 
					
						
							| 
									
										
										
										
											1999-12-02 22:52:12 +00:00
										 |  |  | 	options = handler.load() | 
					
						
							|  |  |  | 	if options['noargs']: | 
					
						
							|  |  |  | 		EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.') | 
					
						
							|  |  |  | 	result = interact(options, 'System-wide preferences') | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	if result: | 
					
						
							|  |  |  | 		handler.save(result) | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | def edit_applet(name): | 
					
						
							| 
									
										
										
										
											1997-09-08 13:19:42 +00:00
										 |  |  | 	handler = pythonprefs.AppletOptions(name) | 
					
						
							|  |  |  | 	result = interact(handler.load(), os.path.split(name)[1]) | 
					
						
							|  |  |  | 	if result: | 
					
						
							|  |  |  | 		handler.save(result) | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  | 	try: | 
					
						
							| 
									
										
										
										
											2000-06-20 21:59:25 +00:00
										 |  |  | 		h = FSpOpenResFile('EditPythonPrefs.rsrc', 1) | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | 	except Res.Error: | 
					
						
							|  |  |  | 		pass	# Assume we already have acces to our own resource | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2000-10-13 23:34:06 +00:00
										 |  |  | 	MacOS.SchedParams(1, 0) | 
					
						
							| 
									
										
										
										
											1996-04-04 15:38:44 +00:00
										 |  |  | 	if len(sys.argv) <= 1: | 
					
						
							|  |  |  | 		edit_preferences() | 
					
						
							|  |  |  | 	else: | 
					
						
							|  |  |  | 		for appl in sys.argv[1:]: | 
					
						
							|  |  |  | 			edit_applet(appl) | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											1995-07-29 13:48:41 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  | 	main() |