blob: a03ae78119a4f85493d48f22fbefff2f9d3e5e07 [file] [log] [blame]
Jack Jansen7571f301995-07-29 13:48:41 +00001"""Edit the Python Preferences file."""
Jack Jansen822a30b1996-04-10 14:52:18 +00002#
3# This program is getting more and more clunky. It should really
4# be rewritten in a modeless way some time soon.
Jack Jansen7571f301995-07-29 13:48:41 +00005
6from Dlg import *
7from Events import *
8from Res import *
Jack Jansen6a6db071999-12-23 14:34:07 +00009import Controls
Jack Jansen7571f301995-07-29 13:48:41 +000010import string
11import struct
12import macfs
13import MacOS
14import os
15import sys
16import Res # For Res.Error
Jack Jansen3b3a2871997-09-08 13:19:42 +000017import pythonprefs
18import EasyDialogs
Jack Jansen1bbf6ff2001-01-29 14:59:33 +000019try:
20 import Help
21except ImportError:
22 Help = None
Jack Jansen84872291996-10-22 15:33:02 +000023
Jack Jansen7571f301995-07-29 13:48:41 +000024# resource IDs in our own resources (dialogs, etc)
25MESSAGE_ID = 256
26
Jack Jansen145c92d1996-10-11 11:30:26 +000027DIALOG_ID = 511
Jack Jansen7571f301995-07-29 13:48:41 +000028TEXT_ITEM = 1
29OK_ITEM = 2
30CANCEL_ITEM = 3
Jack Jansendb0bace1996-04-04 15:38:44 +000031DIR_ITEM = 4
32TITLE_ITEM = 5
Jack Jansen822a30b1996-04-10 14:52:18 +000033OPTIONS_ITEM = 7
Jack Jansenb95901e1997-09-09 13:58:19 +000034HELP_ITEM = 9
Jack Jansen822a30b1996-04-10 14:52:18 +000035
36# The options dialog. There is a correspondence between
37# the dialog item numbers and the option.
Jack Jansen145c92d1996-10-11 11:30:26 +000038OPT_DIALOG_ID = 510
Jack Jansen3b3a2871997-09-08 13:19:42 +000039
40# Map dialog item numbers to option names (and the reverse)
41opt_dialog_map = [
42 None,
Jack Jansen4a5eb962000-09-22 21:50:11 +000043 None,
Jack Jansen3e3eb3e2000-10-19 21:50:54 +000044 None,
Jack Jansen3b3a2871997-09-08 13:19:42 +000045 "inspect",
46 "verbose",
47 "optimize",
48 "unbuffered",
49 "debugging",
Jack Jansen4a5eb962000-09-22 21:50:11 +000050 "tabwarn",
51 "nosite",
52 "nonavservice",
Jack Jansen3b3a2871997-09-08 13:19:42 +000053 "nointopt",
54 "noargs",
Jack Jansenb95901e1997-09-09 13:58:19 +000055 "delayconsole",
Jack Jansen4a5eb962000-09-22 21:50:11 +000056 ]
Jack Jansen3b3a2871997-09-08 13:19:42 +000057opt_dialog_dict = {}
58for i in range(len(opt_dialog_map)):
59 if opt_dialog_map[i]:
60 opt_dialog_dict[opt_dialog_map[i]] = i
61# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000062# The GUSI creator/type and delay-console
Jack Jansen4a5eb962000-09-22 21:50:11 +000063OD_CREATOR_ITEM = 18
64OD_TYPE_ITEM = 19
65OD_OK_ITEM = 1
66OD_CANCEL_ITEM = 2
67OD_HELP_ITEM = 20
68OD_KEEPALWAYS_ITEM = 14
69OD_KEEPOUTPUT_ITEM = 15
70OD_KEEPERROR_ITEM = 16
71OD_KEEPNEVER_ITEM = 17
Jack Jansen7571f301995-07-29 13:48:41 +000072
Jack Jansen3b3a2871997-09-08 13:19:42 +000073def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000074 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000075 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +000076 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
77 SetDialogItemText(htext, options['creator'])
78 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
79 SetDialogItemText(htext, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000080 d.SetDialogDefaultItem(OD_OK_ITEM)
81 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen1bbf6ff2001-01-29 14:59:33 +000082 if not Help:
83 d.HideDialogItem(OD_HELP_ITEM)
Jack Jansen822a30b1996-04-10 14:52:18 +000084 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000085 for name in opt_dialog_dict.keys():
86 num = opt_dialog_dict[name]
Jack Jansen6a6db071999-12-23 14:34:07 +000087 ctl = d.GetDialogItemAsControl(num)
88 ctl.SetControlValue(options[name])
Jack Jansen4a5eb962000-09-22 21:50:11 +000089 ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM)
90 ctl.SetControlValue(options['keep_console'] == 3)
91 ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM)
92 ctl.SetControlValue(options['keep_console'] == 1)
93 ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM)
94 ctl.SetControlValue(options['keep_console'] == 2)
95 ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM)
96 ctl.SetControlValue(options['keep_console'] == 0)
Jack Jansen822a30b1996-04-10 14:52:18 +000097 n = ModalDialog(None)
98 if n == OD_OK_ITEM:
Jack Jansen6a6db071999-12-23 14:34:07 +000099 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
100 ncreator = GetDialogItemText(htext)
101 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
102 ntype = GetDialogItemText(htext)
Jack Jansen6c4e9871996-09-06 22:25:34 +0000103 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000104 options['creator'] = ncreator
105 options['type'] = ntype
106 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +0000107 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000108 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +0000109 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000110 return
Jack Jansen6c4e9871996-09-06 22:25:34 +0000111 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
112 pass
Jack Jansen4a5eb962000-09-22 21:50:11 +0000113 elif n == OD_KEEPALWAYS_ITEM:
114 options['keep_console'] = 3;
115 elif n == OD_KEEPOUTPUT_ITEM:
116 options['keep_console'] = 1;
117 elif n == OD_KEEPERROR_ITEM:
118 options['keep_console'] = 2;
119 elif n == OD_KEEPNEVER_ITEM:
120 options['keep_console'] = 0;
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000121 elif n == OD_HELP_ITEM and Help:
Jack Jansenb95901e1997-09-09 13:58:19 +0000122 onoff = Help.HMGetBalloons()
123 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +0000124 elif 1 <= n <= len(opt_dialog_map):
125 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000126
127
Jack Jansen3b3a2871997-09-08 13:19:42 +0000128def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000129 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000130 try:
131 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000132 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000133 except os.error:
134 pass
135 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +0000136 htext = d.GetDialogItemAsControl(TITLE_ITEM)
137 SetDialogItemText(htext, title)
Jack Jansen8242c9e2000-01-13 16:22:12 +0000138 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
Jack Jansen6a6db071999-12-23 14:34:07 +0000139 data = string.joinfields(options['path'], '\r')
Jack Jansen8242c9e2000-01-13 16:22:12 +0000140 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
Jack Jansen6a6db071999-12-23 14:34:07 +0000141
Jack Jansena918b8c1996-11-20 14:55:26 +0000142 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
143 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000144## d.SetDialogDefaultItem(OK_ITEM)
145 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000146 if not Help:
147 d.HideDialogItem(HELP_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000148 d.GetDialogWindow().ShowWindow()
149 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000150 while 1:
151 n = ModalDialog(None)
152 if n == OK_ITEM:
153 break
154 if n == CANCEL_ITEM:
155 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000156## if n == REVERT_ITEM:
157## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000158 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000159 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000160 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000161 options['dir'] = fss
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000162 elif n == HELP_ITEM and Help:
Jack Jansenb95901e1997-09-09 13:58:19 +0000163 onoff = Help.HMGetBalloons()
164 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000165 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000166 noptions = options
167 for k in options.keys():
168 noptions[k] = options[k]
169 noptions = optinteract(noptions)
170 if noptions:
171 options = noptions
Jack Jansen8242c9e2000-01-13 16:22:12 +0000172 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
173 tmp = string.splitfields(data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000174 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000175 for i in tmp:
176 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000177 newpath.append(i)
178 options['path'] = newpath
179 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000180
Jack Jansendb0bace1996-04-04 15:38:44 +0000181
182def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000183 handler = pythonprefs.PythonOptions()
Jack Jansen43fd1f71999-12-02 22:52:12 +0000184 options = handler.load()
185 if options['noargs']:
186 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
187 result = interact(options, 'System-wide preferences')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000188 if result:
189 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000190
191def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000192 handler = pythonprefs.AppletOptions(name)
193 result = interact(handler.load(), os.path.split(name)[1])
194 if result:
195 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000196
197def main():
198 try:
Jack Jansend13c3852000-06-20 21:59:25 +0000199 h = FSpOpenResFile('EditPythonPrefs.rsrc', 1)
Jack Jansendb0bace1996-04-04 15:38:44 +0000200 except Res.Error:
201 pass # Assume we already have acces to our own resource
202
Jack Jansen8c94d5e2000-10-13 23:34:06 +0000203 MacOS.SchedParams(1, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000204 if len(sys.argv) <= 1:
205 edit_preferences()
206 else:
207 for appl in sys.argv[1:]:
208 edit_applet(appl)
209
Jack Jansen7571f301995-07-29 13:48:41 +0000210
211if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000212 main()