This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Mac / scripts / EditPythonPrefs.py
bloba03ae78119a4f85493d48f22fbefff2f9d3e5e07
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 Dlg import *
7 from Events import *
8 from Res import *
9 import Controls
10 import string
11 import struct
12 import macfs
13 import MacOS
14 import os
15 import sys
16 import Res # For Res.Error
17 import pythonprefs
18 import EasyDialogs
19 try:
20 import Help
21 except ImportError:
22 Help = None
24 # resource IDs in our own resources (dialogs, etc)
25 MESSAGE_ID = 256
27 DIALOG_ID = 511
28 TEXT_ITEM = 1
29 OK_ITEM = 2
30 CANCEL_ITEM = 3
31 DIR_ITEM = 4
32 TITLE_ITEM = 5
33 OPTIONS_ITEM = 7
34 HELP_ITEM = 9
36 # The options dialog. There is a correspondence between
37 # the dialog item numbers and the option.
38 OPT_DIALOG_ID = 510
40 # Map dialog item numbers to option names (and the reverse)
41 opt_dialog_map = [
42 None,
43 None,
44 None,
45 "inspect",
46 "verbose",
47 "optimize",
48 "unbuffered",
49 "debugging",
50 "tabwarn",
51 "nosite",
52 "nonavservice",
53 "nointopt",
54 "noargs",
55 "delayconsole",
57 opt_dialog_dict = {}
58 for 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
62 # The GUSI creator/type and delay-console
63 OD_CREATOR_ITEM = 18
64 OD_TYPE_ITEM = 19
65 OD_OK_ITEM = 1
66 OD_CANCEL_ITEM = 2
67 OD_HELP_ITEM = 20
68 OD_KEEPALWAYS_ITEM = 14
69 OD_KEEPOUTPUT_ITEM = 15
70 OD_KEEPERROR_ITEM = 16
71 OD_KEEPNEVER_ITEM = 17
73 def optinteract(options):
74 """Let the user interact with the options dialog"""
75 d = GetNewDialog(OPT_DIALOG_ID, -1)
76 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
77 SetDialogItemText(htext, options['creator'])
78 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
79 SetDialogItemText(htext, options['type'])
80 d.SetDialogDefaultItem(OD_OK_ITEM)
81 d.SetDialogCancelItem(OD_CANCEL_ITEM)
82 if not Help:
83 d.HideDialogItem(OD_HELP_ITEM)
84 while 1:
85 for name in opt_dialog_dict.keys():
86 num = opt_dialog_dict[name]
87 ctl = d.GetDialogItemAsControl(num)
88 ctl.SetControlValue(options[name])
89 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)
97 n = ModalDialog(None)
98 if n == OD_OK_ITEM:
99 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
100 ncreator = GetDialogItemText(htext)
101 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
102 ntype = GetDialogItemText(htext)
103 if len(ncreator) == 4 and len(ntype) == 4:
104 options['creator'] = ncreator
105 options['type'] = ntype
106 return options
107 else:
108 MacOS.SysBeep()
109 elif n == OD_CANCEL_ITEM:
110 return
111 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
112 pass
113 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;
121 elif n == OD_HELP_ITEM and Help:
122 onoff = Help.HMGetBalloons()
123 Help.HMSetBalloons(not onoff)
124 elif 1 <= n <= len(opt_dialog_map):
125 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
128 def interact(options, title):
129 """Let the user interact with the dialog"""
130 try:
131 # Try to go to the "correct" dir for GetDirectory
132 os.chdir(options['dir'].as_pathname())
133 except os.error:
134 pass
135 d = GetNewDialog(DIALOG_ID, -1)
136 htext = d.GetDialogItemAsControl(TITLE_ITEM)
137 SetDialogItemText(htext, title)
138 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
139 data = string.joinfields(options['path'], '\r')
140 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
142 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
143 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
144 ## d.SetDialogDefaultItem(OK_ITEM)
145 d.SetDialogCancelItem(CANCEL_ITEM)
146 if not Help:
147 d.HideDialogItem(HELP_ITEM)
148 d.GetDialogWindow().ShowWindow()
149 d.DrawDialog()
150 while 1:
151 n = ModalDialog(None)
152 if n == OK_ITEM:
153 break
154 if n == CANCEL_ITEM:
155 return None
156 ## if n == REVERT_ITEM:
157 ## return [], pythondir
158 if n == DIR_ITEM:
159 fss, ok = macfs.GetDirectory('Select python home folder:')
160 if ok:
161 options['dir'] = fss
162 elif n == HELP_ITEM and Help:
163 onoff = Help.HMGetBalloons()
164 Help.HMSetBalloons(not onoff)
165 if n == OPTIONS_ITEM:
166 noptions = options
167 for k in options.keys():
168 noptions[k] = options[k]
169 noptions = optinteract(noptions)
170 if noptions:
171 options = noptions
172 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
173 tmp = string.splitfields(data, '\r')
174 newpath = []
175 for i in tmp:
176 if i:
177 newpath.append(i)
178 options['path'] = newpath
179 return options
182 def edit_preferences():
183 handler = pythonprefs.PythonOptions()
184 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')
188 if result:
189 handler.save(result)
191 def edit_applet(name):
192 handler = pythonprefs.AppletOptions(name)
193 result = interact(handler.load(), os.path.split(name)[1])
194 if result:
195 handler.save(result)
197 def main():
198 try:
199 h = FSpOpenResFile('EditPythonPrefs.rsrc', 1)
200 except Res.Error:
201 pass # Assume we already have acces to our own resource
203 MacOS.SchedParams(1, 0)
204 if len(sys.argv) <= 1:
205 edit_preferences()
206 else:
207 for appl in sys.argv[1:]:
208 edit_applet(appl)
211 if __name__ == '__main__':
212 main()