Update for release.
[python/dscho.git] / Mac / scripts / EditPythonPrefs.py
blob64c5218c6a33eca2d77defd0e13f89e2f4b0c64d
1 """Edit the Python Preferences file."""
3 # This program is getting more and more clunky. It should really
4 # be rewritten in a modeless way some time soon.
6 from Carbon.Dlg import *
7 from Carbon.Events import *
8 from Carbon.Res import *
9 from Carbon import Controls
10 import string
11 import struct
12 import macfs
13 import MacOS
14 import os
15 import sys
16 from Carbon import Res # For Res.Error
17 import pythonprefs
18 import macresource
19 import EasyDialogs
20 try:
21 from Carbon import Help
22 except ImportError:
23 Help = None
25 # resource IDs in our own resources (dialogs, etc)
26 MESSAGE_ID = 256
28 DIALOG_ID = 511
29 TEXT_ITEM = 1
30 OK_ITEM = 2
31 CANCEL_ITEM = 3
32 DIR_ITEM = 4
33 TITLE_ITEM = 5
34 OPTIONS_ITEM = 7
35 HELP_ITEM = 9
37 # The options dialog. There is a correspondence between
38 # the dialog item numbers and the option.
39 OPT_DIALOG_ID = 510
41 # Map dialog item numbers to option names (and the reverse)
42 opt_dialog_map = [
43 None,
44 None,
45 None,
46 "inspect",
47 "verbose",
48 "optimize",
49 "unbuffered",
50 "debugging",
51 "tabwarn",
52 "nosite",
53 "nonavservice",
54 "nointopt",
55 "noargs",
56 "delayconsole",
57 "divisionwarn",
58 "unixnewlines",
60 opt_dialog_dict = {}
61 for i in range(len(opt_dialog_map)):
62 if opt_dialog_map[i]:
63 opt_dialog_dict[opt_dialog_map[i]] = i
64 # 1 thru 10 are the options
65 # The GUSI creator/type and delay-console
66 OD_CREATOR_ITEM = 20
67 OD_TYPE_ITEM = 21
68 OD_OK_ITEM = 1
69 OD_CANCEL_ITEM = 2
70 OD_HELP_ITEM = 22
71 OD_KEEPALWAYS_ITEM = 16
72 OD_KEEPOUTPUT_ITEM = 17
73 OD_KEEPERROR_ITEM = 18
74 OD_KEEPNEVER_ITEM = 19
76 def optinteract(options):
77 """Let the user interact with the options dialog"""
78 d = GetNewDialog(OPT_DIALOG_ID, -1)
79 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
80 SetDialogItemText(htext, options['creator'])
81 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
82 SetDialogItemText(htext, options['type'])
83 d.SetDialogDefaultItem(OD_OK_ITEM)
84 d.SetDialogCancelItem(OD_CANCEL_ITEM)
85 if not Help:
86 d.HideDialogItem(OD_HELP_ITEM)
87 while 1:
88 for name in opt_dialog_dict.keys():
89 num = opt_dialog_dict[name]
90 ctl = d.GetDialogItemAsControl(num)
91 ctl.SetControlValue(options[name])
92 ctl = d.GetDialogItemAsControl(OD_KEEPALWAYS_ITEM)
93 ctl.SetControlValue(options['keep_console'] == 3)
94 ctl = d.GetDialogItemAsControl(OD_KEEPOUTPUT_ITEM)
95 ctl.SetControlValue(options['keep_console'] == 1)
96 ctl = d.GetDialogItemAsControl(OD_KEEPERROR_ITEM)
97 ctl.SetControlValue(options['keep_console'] == 2)
98 ctl = d.GetDialogItemAsControl(OD_KEEPNEVER_ITEM)
99 ctl.SetControlValue(options['keep_console'] == 0)
100 n = ModalDialog(None)
101 if n == OD_OK_ITEM:
102 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
103 ncreator = GetDialogItemText(htext)
104 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
105 ntype = GetDialogItemText(htext)
106 if len(ncreator) == 4 and len(ntype) == 4:
107 options['creator'] = ncreator
108 options['type'] = ntype
109 return options
110 else:
111 MacOS.SysBeep()
112 elif n == OD_CANCEL_ITEM:
113 return
114 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
115 pass
116 elif n == OD_KEEPALWAYS_ITEM:
117 options['keep_console'] = 3;
118 elif n == OD_KEEPOUTPUT_ITEM:
119 options['keep_console'] = 1;
120 elif n == OD_KEEPERROR_ITEM:
121 options['keep_console'] = 2;
122 elif n == OD_KEEPNEVER_ITEM:
123 options['keep_console'] = 0;
124 elif n == OD_HELP_ITEM and Help:
125 onoff = Help.HMGetBalloons()
126 Help.HMSetBalloons(not onoff)
127 elif 1 <= n <= len(opt_dialog_map):
128 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
131 def interact(options, title):
132 """Let the user interact with the dialog"""
133 try:
134 # Try to go to the "correct" dir for GetDirectory
135 os.chdir(options['dir'].as_pathname())
136 except os.error:
137 pass
138 d = GetNewDialog(DIALOG_ID, -1)
139 htext = d.GetDialogItemAsControl(TITLE_ITEM)
140 SetDialogItemText(htext, title)
141 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
142 data = string.joinfields(options['path'], '\r')
143 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
145 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
146 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
147 ## d.SetDialogDefaultItem(OK_ITEM)
148 d.SetDialogCancelItem(CANCEL_ITEM)
149 if not Help:
150 d.HideDialogItem(HELP_ITEM)
151 d.GetDialogWindow().ShowWindow()
152 d.DrawDialog()
153 while 1:
154 n = ModalDialog(None)
155 if n == OK_ITEM:
156 break
157 if n == CANCEL_ITEM:
158 return None
159 ## if n == REVERT_ITEM:
160 ## return [], pythondir
161 if n == DIR_ITEM:
162 fss = EasyDialogs.AskFolder(message='Select python home folder:',
163 wanted=macfs.FSSpec)
164 if fss:
165 options['dir'] = fss
166 elif n == HELP_ITEM and Help:
167 onoff = Help.HMGetBalloons()
168 Help.HMSetBalloons(not onoff)
169 if n == OPTIONS_ITEM:
170 noptions = options
171 for k in options.keys():
172 noptions[k] = options[k]
173 noptions = optinteract(noptions)
174 if noptions:
175 options = noptions
176 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
177 tmp = string.splitfields(data, '\r')
178 newpath = []
179 for i in tmp:
180 if i:
181 newpath.append(i)
182 options['path'] = newpath
183 return options
186 def edit_preferences():
187 handler = pythonprefs.PythonOptions()
188 options = handler.load()
189 if options['noargs']:
190 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
191 result = interact(options, 'System-wide preferences')
192 if result:
193 handler.save(result)
195 def edit_applet(name):
196 handler = pythonprefs.AppletOptions(name)
197 result = interact(handler.load(), os.path.split(name)[1])
198 if result:
199 handler.save(result)
201 def main():
202 macresource.need('DLOG', DIALOG_ID, 'EditPythonPrefs.rsrc')
204 MacOS.SchedParams(1, 0)
205 if len(sys.argv) <= 1:
206 edit_preferences()
207 else:
208 for appl in sys.argv[1:]:
209 edit_applet(appl)
212 if __name__ == '__main__':
213 main()