blob: 833e60789b312bc7069190a9b758001d3d1b587d [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
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00006from Carbon.Dlg import *
7from Carbon.Events import *
8from Carbon.Res import *
9from Carbon import Controls
Jack Jansen7571f301995-07-29 13:48:41 +000010import string
11import struct
12import macfs
13import MacOS
14import os
15import sys
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000016from Carbon import Res # For Res.Error
Jack Jansen3b3a2871997-09-08 13:19:42 +000017import pythonprefs
Jack Jansen3c06b9a2001-08-27 21:41:23 +000018import macresource
Jack Jansen3b3a2871997-09-08 13:19:42 +000019import EasyDialogs
Jack Jansen1bbf6ff2001-01-29 14:59:33 +000020try:
Jack Jansen5a6fdcd2001-08-25 12:15:04 +000021 from Carbon import Help
Jack Jansen1bbf6ff2001-01-29 14:59:33 +000022except ImportError:
23 Help = None
Jack Jansen84872291996-10-22 15:33:02 +000024
Jack Jansen7571f301995-07-29 13:48:41 +000025# resource IDs in our own resources (dialogs, etc)
26MESSAGE_ID = 256
27
Jack Jansen145c92d1996-10-11 11:30:26 +000028DIALOG_ID = 511
Jack Jansen7571f301995-07-29 13:48:41 +000029TEXT_ITEM = 1
30OK_ITEM = 2
31CANCEL_ITEM = 3
Jack Jansendb0bace1996-04-04 15:38:44 +000032DIR_ITEM = 4
33TITLE_ITEM = 5
Jack Jansen822a30b1996-04-10 14:52:18 +000034OPTIONS_ITEM = 7
Jack Jansenb95901e1997-09-09 13:58:19 +000035HELP_ITEM = 9
Jack Jansen822a30b1996-04-10 14:52:18 +000036
37# The options dialog. There is a correspondence between
38# the dialog item numbers and the option.
Jack Jansen145c92d1996-10-11 11:30:26 +000039OPT_DIALOG_ID = 510
Jack Jansen3b3a2871997-09-08 13:19:42 +000040
41# Map dialog item numbers to option names (and the reverse)
42opt_dialog_map = [
43 None,
Jack Jansen4a5eb962000-09-22 21:50:11 +000044 None,
Jack Jansen3e3eb3e2000-10-19 21:50:54 +000045 None,
Jack Jansen3b3a2871997-09-08 13:19:42 +000046 "inspect",
47 "verbose",
48 "optimize",
49 "unbuffered",
50 "debugging",
Jack Jansen4a5eb962000-09-22 21:50:11 +000051 "tabwarn",
52 "nosite",
53 "nonavservice",
Jack Jansen3b3a2871997-09-08 13:19:42 +000054 "nointopt",
55 "noargs",
Jack Jansenb95901e1997-09-09 13:58:19 +000056 "delayconsole",
Jack Jansen4a5eb962000-09-22 21:50:11 +000057 ]
Jack Jansen3b3a2871997-09-08 13:19:42 +000058opt_dialog_dict = {}
59for i in range(len(opt_dialog_map)):
60 if opt_dialog_map[i]:
61 opt_dialog_dict[opt_dialog_map[i]] = i
62# 1 thru 10 are the options
Jack Jansen6c4e9871996-09-06 22:25:34 +000063# The GUSI creator/type and delay-console
Jack Jansen4a5eb962000-09-22 21:50:11 +000064OD_CREATOR_ITEM = 18
65OD_TYPE_ITEM = 19
66OD_OK_ITEM = 1
67OD_CANCEL_ITEM = 2
68OD_HELP_ITEM = 20
69OD_KEEPALWAYS_ITEM = 14
70OD_KEEPOUTPUT_ITEM = 15
71OD_KEEPERROR_ITEM = 16
72OD_KEEPNEVER_ITEM = 17
Jack Jansen7571f301995-07-29 13:48:41 +000073
Jack Jansen3b3a2871997-09-08 13:19:42 +000074def optinteract(options):
Jack Jansen822a30b1996-04-10 14:52:18 +000075 """Let the user interact with the options dialog"""
Jack Jansen822a30b1996-04-10 14:52:18 +000076 d = GetNewDialog(OPT_DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +000077 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
78 SetDialogItemText(htext, options['creator'])
79 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
80 SetDialogItemText(htext, options['type'])
Jack Jansen822a30b1996-04-10 14:52:18 +000081 d.SetDialogDefaultItem(OD_OK_ITEM)
82 d.SetDialogCancelItem(OD_CANCEL_ITEM)
Jack Jansen1bbf6ff2001-01-29 14:59:33 +000083 if not Help:
84 d.HideDialogItem(OD_HELP_ITEM)
Jack Jansen822a30b1996-04-10 14:52:18 +000085 while 1:
Jack Jansen3b3a2871997-09-08 13:19:42 +000086 for name in opt_dialog_dict.keys():
87 num = opt_dialog_dict[name]
Jack Jansen6a6db071999-12-23 14:34:07 +000088 ctl = d.GetDialogItemAsControl(num)
89 ctl.SetControlValue(options[name])
Jack Jansen4a5eb962000-09-22 21:50:11 +000090 ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM)
91 ctl.SetControlValue(options['keep_console'] == 3)
92 ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM)
93 ctl.SetControlValue(options['keep_console'] == 1)
94 ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM)
95 ctl.SetControlValue(options['keep_console'] == 2)
96 ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM)
97 ctl.SetControlValue(options['keep_console'] == 0)
Jack Jansen822a30b1996-04-10 14:52:18 +000098 n = ModalDialog(None)
99 if n == OD_OK_ITEM:
Jack Jansen6a6db071999-12-23 14:34:07 +0000100 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
101 ncreator = GetDialogItemText(htext)
102 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
103 ntype = GetDialogItemText(htext)
Jack Jansen6c4e9871996-09-06 22:25:34 +0000104 if len(ncreator) == 4 and len(ntype) == 4:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000105 options['creator'] = ncreator
106 options['type'] = ntype
107 return options
Jack Jansen6c4e9871996-09-06 22:25:34 +0000108 else:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000109 MacOS.SysBeep()
Jack Jansen822a30b1996-04-10 14:52:18 +0000110 elif n == OD_CANCEL_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000111 return
Jack Jansen6c4e9871996-09-06 22:25:34 +0000112 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
113 pass
Jack Jansen4a5eb962000-09-22 21:50:11 +0000114 elif n == OD_KEEPALWAYS_ITEM:
115 options['keep_console'] = 3;
116 elif n == OD_KEEPOUTPUT_ITEM:
117 options['keep_console'] = 1;
118 elif n == OD_KEEPERROR_ITEM:
119 options['keep_console'] = 2;
120 elif n == OD_KEEPNEVER_ITEM:
121 options['keep_console'] = 0;
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000122 elif n == OD_HELP_ITEM and Help:
Jack Jansenb95901e1997-09-09 13:58:19 +0000123 onoff = Help.HMGetBalloons()
124 Help.HMSetBalloons(not onoff)
Jack Jansen3b3a2871997-09-08 13:19:42 +0000125 elif 1 <= n <= len(opt_dialog_map):
126 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
Jack Jansen822a30b1996-04-10 14:52:18 +0000127
128
Jack Jansen3b3a2871997-09-08 13:19:42 +0000129def interact(options, title):
Jack Jansen7571f301995-07-29 13:48:41 +0000130 """Let the user interact with the dialog"""
Jack Jansen7571f301995-07-29 13:48:41 +0000131 try:
132 # Try to go to the "correct" dir for GetDirectory
Jack Jansen3b3a2871997-09-08 13:19:42 +0000133 os.chdir(options['dir'].as_pathname())
Jack Jansen7571f301995-07-29 13:48:41 +0000134 except os.error:
135 pass
136 d = GetNewDialog(DIALOG_ID, -1)
Jack Jansen6a6db071999-12-23 14:34:07 +0000137 htext = d.GetDialogItemAsControl(TITLE_ITEM)
138 SetDialogItemText(htext, title)
Jack Jansen8242c9e2000-01-13 16:22:12 +0000139 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
Jack Jansen6a6db071999-12-23 14:34:07 +0000140 data = string.joinfields(options['path'], '\r')
Jack Jansen8242c9e2000-01-13 16:22:12 +0000141 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
Jack Jansen6a6db071999-12-23 14:34:07 +0000142
Jack Jansena918b8c1996-11-20 14:55:26 +0000143 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
144 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000145## d.SetDialogDefaultItem(OK_ITEM)
146 d.SetDialogCancelItem(CANCEL_ITEM)
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000147 if not Help:
148 d.HideDialogItem(HELP_ITEM)
Jack Jansena918b8c1996-11-20 14:55:26 +0000149 d.GetDialogWindow().ShowWindow()
150 d.DrawDialog()
Jack Jansen7571f301995-07-29 13:48:41 +0000151 while 1:
152 n = ModalDialog(None)
153 if n == OK_ITEM:
154 break
155 if n == CANCEL_ITEM:
156 return None
Jack Jansendb0bace1996-04-04 15:38:44 +0000157## if n == REVERT_ITEM:
158## return [], pythondir
Jack Jansen7571f301995-07-29 13:48:41 +0000159 if n == DIR_ITEM:
Jack Jansen9062fa21995-08-14 12:21:12 +0000160 fss, ok = macfs.GetDirectory('Select python home folder:')
Jack Jansen7571f301995-07-29 13:48:41 +0000161 if ok:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000162 options['dir'] = fss
Jack Jansen1bbf6ff2001-01-29 14:59:33 +0000163 elif n == HELP_ITEM and Help:
Jack Jansenb95901e1997-09-09 13:58:19 +0000164 onoff = Help.HMGetBalloons()
165 Help.HMSetBalloons(not onoff)
Jack Jansen822a30b1996-04-10 14:52:18 +0000166 if n == OPTIONS_ITEM:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000167 noptions = options
168 for k in options.keys():
169 noptions[k] = options[k]
170 noptions = optinteract(noptions)
171 if noptions:
172 options = noptions
Jack Jansen8242c9e2000-01-13 16:22:12 +0000173 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
174 tmp = string.splitfields(data, '\r')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000175 newpath = []
Jack Jansen7571f301995-07-29 13:48:41 +0000176 for i in tmp:
177 if i:
Jack Jansen3b3a2871997-09-08 13:19:42 +0000178 newpath.append(i)
179 options['path'] = newpath
180 return options
Jack Jansen7571f301995-07-29 13:48:41 +0000181
Jack Jansendb0bace1996-04-04 15:38:44 +0000182
183def edit_preferences():
Jack Jansen3b3a2871997-09-08 13:19:42 +0000184 handler = pythonprefs.PythonOptions()
Jack Jansen43fd1f71999-12-02 22:52:12 +0000185 options = handler.load()
186 if options['noargs']:
187 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
188 result = interact(options, 'System-wide preferences')
Jack Jansen3b3a2871997-09-08 13:19:42 +0000189 if result:
190 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000191
192def edit_applet(name):
Jack Jansen3b3a2871997-09-08 13:19:42 +0000193 handler = pythonprefs.AppletOptions(name)
194 result = interact(handler.load(), os.path.split(name)[1])
195 if result:
196 handler.save(result)
Jack Jansendb0bace1996-04-04 15:38:44 +0000197
198def main():
Jack Jansen3c06b9a2001-08-27 21:41:23 +0000199 macresource.need('DLOG', DIALOG_ID, 'EditPythonPrefs.rsrc')
Jack Jansendb0bace1996-04-04 15:38:44 +0000200
Jack Jansen8c94d5e2000-10-13 23:34:06 +0000201 MacOS.SchedParams(1, 0)
Jack Jansendb0bace1996-04-04 15:38:44 +0000202 if len(sys.argv) <= 1:
203 edit_preferences()
204 else:
205 for appl in sys.argv[1:]:
206 edit_applet(appl)
207
Jack Jansen7571f301995-07-29 13:48:41 +0000208
209if __name__ == '__main__':
Jack Jansen7571f301995-07-29 13:48:41 +0000210 main()