Quick update to the README file. For intros and books we now point to
[python/dscho.git] / Mac / scripts / EditPythonPrefs.py
blob343ea222d40f14bab226bff2bb629b0a3bb3c4c2
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 import Help
21 # resource IDs in our own resources (dialogs, etc)
22 MESSAGE_ID = 256
24 DIALOG_ID = 511
25 TEXT_ITEM = 1
26 OK_ITEM = 2
27 CANCEL_ITEM = 3
28 DIR_ITEM = 4
29 TITLE_ITEM = 5
30 OPTIONS_ITEM = 7
31 HELP_ITEM = 9
33 # The options dialog. There is a correspondence between
34 # the dialog item numbers and the option.
35 OPT_DIALOG_ID = 510
37 # Map dialog item numbers to option names (and the reverse)
38 opt_dialog_map = [
39 None,
40 "inspect",
41 "verbose",
42 "optimize",
43 "unbuffered",
44 "debugging",
45 "keepopen",
46 "keeperror",
47 "nointopt",
48 "noargs",
49 "delayconsole",
50 None, None, None, None, None, None, None, None, # 11-18 are different
51 "oldexc",
52 "nosite"]
53 opt_dialog_dict = {}
54 for i in range(len(opt_dialog_map)):
55 if opt_dialog_map[i]:
56 opt_dialog_dict[opt_dialog_map[i]] = i
57 # 1 thru 10 are the options
58 # The GUSI creator/type and delay-console
59 OD_CREATOR_ITEM = 11
60 OD_TYPE_ITEM = 12
61 OD_OK_ITEM = 13
62 OD_CANCEL_ITEM = 14
63 OD_HELP_ITEM = 22
65 def optinteract(options):
66 """Let the user interact with the options dialog"""
67 d = GetNewDialog(OPT_DIALOG_ID, -1)
68 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
69 SetDialogItemText(htext, options['creator'])
70 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
71 SetDialogItemText(htext, options['type'])
72 d.SetDialogDefaultItem(OD_OK_ITEM)
73 d.SetDialogCancelItem(OD_CANCEL_ITEM)
75 while 1:
76 for name in opt_dialog_dict.keys():
77 num = opt_dialog_dict[name]
78 ctl = d.GetDialogItemAsControl(num)
79 ctl.SetControlValue(options[name])
80 n = ModalDialog(None)
81 if n == OD_OK_ITEM:
82 htext = d.GetDialogItemAsControl(OD_CREATOR_ITEM)
83 ncreator = GetDialogItemText(htext)
84 htext = d.GetDialogItemAsControl(OD_TYPE_ITEM)
85 ntype = GetDialogItemText(htext)
86 if len(ncreator) == 4 and len(ntype) == 4:
87 options['creator'] = ncreator
88 options['type'] = ntype
89 return options
90 else:
91 MacOS.SysBeep()
92 elif n == OD_CANCEL_ITEM:
93 return
94 elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
95 pass
96 elif n == OD_HELP_ITEM:
97 onoff = Help.HMGetBalloons()
98 Help.HMSetBalloons(not onoff)
99 elif 1 <= n <= len(opt_dialog_map):
100 options[opt_dialog_map[n]] = (not options[opt_dialog_map[n]])
103 def interact(options, title):
104 """Let the user interact with the dialog"""
105 try:
106 # Try to go to the "correct" dir for GetDirectory
107 os.chdir(options['dir'].as_pathname())
108 except os.error:
109 pass
110 d = GetNewDialog(DIALOG_ID, -1)
111 htext = d.GetDialogItemAsControl(TITLE_ITEM)
112 SetDialogItemText(htext, title)
113 path_ctl = d.GetDialogItemAsControl(TEXT_ITEM)
114 data = string.joinfields(options['path'], '\r')
115 path_ctl.SetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag, data)
117 d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
118 d.SelectDialogItemText(TEXT_ITEM, 0, 0)
119 ## d.SetDialogDefaultItem(OK_ITEM)
120 d.SetDialogCancelItem(CANCEL_ITEM)
121 d.GetDialogWindow().ShowWindow()
122 d.DrawDialog()
123 while 1:
124 n = ModalDialog(None)
125 if n == OK_ITEM:
126 break
127 if n == CANCEL_ITEM:
128 return None
129 ## if n == REVERT_ITEM:
130 ## return [], pythondir
131 if n == DIR_ITEM:
132 fss, ok = macfs.GetDirectory('Select python home folder:')
133 if ok:
134 options['dir'] = fss
135 elif n == HELP_ITEM:
136 onoff = Help.HMGetBalloons()
137 Help.HMSetBalloons(not onoff)
138 if n == OPTIONS_ITEM:
139 noptions = options
140 for k in options.keys():
141 noptions[k] = options[k]
142 noptions = optinteract(noptions)
143 if noptions:
144 options = noptions
145 data = path_ctl.GetControlData(Controls.kControlEditTextPart, Controls.kControlEditTextTextTag)
146 tmp = string.splitfields(data, '\r')
147 newpath = []
148 for i in tmp:
149 if i:
150 newpath.append(i)
151 options['path'] = newpath
152 return options
155 def edit_preferences():
156 handler = pythonprefs.PythonOptions()
157 options = handler.load()
158 if options['noargs']:
159 EasyDialogs.Message('Warning: system-wide sys.argv processing is off.\nIf you dropped an applet I have not seen it.')
160 result = interact(options, 'System-wide preferences')
161 if result:
162 handler.save(result)
164 def edit_applet(name):
165 handler = pythonprefs.AppletOptions(name)
166 result = interact(handler.load(), os.path.split(name)[1])
167 if result:
168 handler.save(result)
170 def main():
171 try:
172 h = OpenResFile('EditPythonPrefs.rsrc')
173 except Res.Error:
174 pass # Assume we already have acces to our own resource
176 if len(sys.argv) <= 1:
177 edit_preferences()
178 else:
179 for appl in sys.argv[1:]:
180 edit_applet(appl)
183 if __name__ == '__main__':
184 main()