comparison Mac/scripts/EditPythonPrefs.py @ 3504:e8d501c88a3e legacy-trunk

Added region arg to update calls. And binhex files rebinhexed.
author Jack Jansen <jack.jansen@cwi.nl>
date Wed, 10 Apr 1996 14:52:18 +0000
parents ce27e0328462
children 41a52ab263da
comparison
equal deleted inserted replaced
3503:899aefea801c 3504:e8d501c88a3e
1 """Edit the Python Preferences file.""" 1 """Edit the Python Preferences file."""
2 #
3 # This program is getting more and more clunky. It should really
4 # be rewritten in a modeless way some time soon.
2 5
3 from Dlg import * 6 from Dlg import *
4 from Events import * 7 from Events import *
5 from Res import * 8 from Res import *
6 import string 9 import string
18 TEXT_ITEM = 1 21 TEXT_ITEM = 1
19 OK_ITEM = 2 22 OK_ITEM = 2
20 CANCEL_ITEM = 3 23 CANCEL_ITEM = 3
21 DIR_ITEM = 4 24 DIR_ITEM = 4
22 TITLE_ITEM = 5 25 TITLE_ITEM = 5
26 OPTIONS_ITEM = 7
27
28 # The options dialog. There is a correspondence between
29 # the dialog item numbers and the option.
30 OPT_DIALOG_ID = 513
31 # 1 thru 7 are the options
32 OD_OK_ITEM = 8
33 OD_CANCEL_ITEM = 9
23 34
24 # Resource IDs in the preferences file 35 # Resource IDs in the preferences file
25 PATH_STRINGS_ID = 128 36 PATH_STRINGS_ID = 128
26 DIRECTORY_ID = 128 37 DIRECTORY_ID = 128
27 OPTIONS_ID = 128 38 OPTIONS_ID = 128
59 return rv 70 return rv
60 71
61 def message(str = "Hello, world!", id = MESSAGE_ID): 72 def message(str = "Hello, world!", id = MESSAGE_ID):
62 """Show a simple alert with a text message""" 73 """Show a simple alert with a text message"""
63 d = GetNewDialog(id, -1) 74 d = GetNewDialog(id, -1)
75 d.SetDialogDefaultItem(1)
64 print 'd=', d 76 print 'd=', d
65 tp, h, rect = d.GetDialogItem(2) 77 tp, h, rect = d.GetDialogItem(2)
66 SetDialogItemText(h, str) 78 SetDialogItemText(h, str)
67 while 1: 79 while 1:
68 n = ModalDialog(None) 80 n = ModalDialog(None)
69 if n == 1: break 81 if n == 1: break
70 82
71 def interact(list, pythondir, title): 83 def optinteract(options):
84 """Let the user interact with the options dialog"""
85 old_options = options[:]
86 d = GetNewDialog(OPT_DIALOG_ID, -1)
87 d.SetDialogDefaultItem(OD_OK_ITEM)
88 d.SetDialogCancelItem(OD_CANCEL_ITEM)
89 while 1:
90 for i in range(len(options)):
91 tp, h, rect = d.GetDialogItem(i+1)
92 h.as_Control().SetControlValue(options[i])
93 n = ModalDialog(None)
94 if n == OD_OK_ITEM:
95 return options
96 elif n == OD_CANCEL_ITEM:
97 return old_options
98 elif 1 <= n <= len(options):
99 options[n-1] = (not options[n-1])
100
101
102 def interact(list, pythondir, options, title):
72 """Let the user interact with the dialog""" 103 """Let the user interact with the dialog"""
73 opythondir = pythondir 104 opythondir = pythondir
74 try: 105 try:
75 # Try to go to the "correct" dir for GetDirectory 106 # Try to go to the "correct" dir for GetDirectory
76 os.chdir(pythondir.as_pathname()) 107 os.chdir(pythondir.as_pathname())
93 ## return [], pythondir 124 ## return [], pythondir
94 if n == DIR_ITEM: 125 if n == DIR_ITEM:
95 fss, ok = macfs.GetDirectory('Select python home folder:') 126 fss, ok = macfs.GetDirectory('Select python home folder:')
96 if ok: 127 if ok:
97 pythondir = fss 128 pythondir = fss
129 if n == OPTIONS_ITEM:
130 options = optinteract(options)
98 tmp = string.splitfields(GetDialogItemText(h), '\r') 131 tmp = string.splitfields(GetDialogItemText(h), '\r')
99 rv = [] 132 rv = []
100 for i in tmp: 133 for i in tmp:
101 if i: 134 if i:
102 rv.append(i) 135 rv.append(i)
103 return rv, pythondir 136 return rv, pythondir, options
104 137
105 def getprefpath(id): 138 def getprefpath(id):
106 # Load the path and directory resources 139 # Load the path and directory resources
107 try: 140 try:
108 sr = GetResource('STR#', id) 141 sr = GetResource('STR#', id)
117 dr = GetResource('alis', id) 150 dr = GetResource('alis', id)
118 fss, fss_changed = macfs.RawAlias(dr.data).Resolve() 151 fss, fss_changed = macfs.RawAlias(dr.data).Resolve()
119 except (MacOS.Error, Res.Error): 152 except (MacOS.Error, Res.Error):
120 return None, None, 1 153 return None, None, 1
121 return fss, dr, fss_changed 154 return fss, dr, fss_changed
155
156 def getoptions(id):
157 try:
158 opr = GetResource('Popt', id)
159 except (MacOS.Error, Res.Error):
160 return [0]*7, None
161 return map(lambda x: ord(x), opr.data), opr
122 162
123 def openpreffile(rw): 163 def openpreffile(rw):
124 # Find the preferences folder and our prefs file, create if needed. 164 # Find the preferences folder and our prefs file, create if needed.
125 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0) 165 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0)
126 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences')) 166 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences'))
153 193
154 fss, dr, fss_changed = getprefdir(DIRECTORY_ID) 194 fss, dr, fss_changed = getprefdir(DIRECTORY_ID)
155 if fss == None: 195 if fss == None:
156 fss = macfs.FSSpec(os.getcwd()) 196 fss = macfs.FSSpec(os.getcwd())
157 fss_changed = 1 197 fss_changed = 1
198
199 options, opr = getoptions(OPTIONS_ID)
200 saved_options = options[:]
158 201
159 # Let the user play away 202 # Let the user play away
160 result = interact(l, fss, 'System-wide preferences') 203 result = interact(l, fss, options, 'System-wide preferences')
161 204
162 # See what we have to update, and how 205 # See what we have to update, and how
163 if result == None: 206 if result == None:
164 sys.exit(0) 207 sys.exit(0)
165 208
166 pathlist, nfss = result 209 pathlist, nfss, options = result
167 if nfss != fss: 210 if nfss != fss:
168 fss_changed = 1 211 fss_changed = 1
169 212
170 if fss_changed or pathlist != l: 213 if fss_changed:
171 if fss_changed: 214 alias = nfss.NewAlias()
172 alias = nfss.NewAlias() 215 if dr:
173 if dr: 216 dr.data = alias.data
174 dr.data = alias.data 217 dr.ChangedResource()
175 dr.ChangedResource() 218 else:
176 else: 219 dr = Resource(alias.data)
177 dr = Resource(alias.data) 220 dr.AddResource('alis', DIRECTORY_ID, '')
178 dr.AddResource('alis', DIRECTORY_ID, '') 221
179 222 if pathlist != l:
180 if pathlist != l: 223 if pathlist == []:
181 if pathlist == []: 224 if sr.HomeResFile() == preff_handle:
182 if sr.HomeResFile() == preff_handle: 225 sr.RemoveResource()
183 sr.RemoveResource() 226 elif sr.HomeResFile() == preff_handle:
184 elif sr.HomeResFile() == preff_handle: 227 sr.data = listtores(pathlist)
185 sr.data = listtores(pathlist) 228 sr.ChangedResource()
186 sr.ChangedResource() 229 else:
187 else: 230 sr = Resource(listtores(pathlist))
188 sr = Resource(listtores(pathlist)) 231 sr.AddResource('STR#', PATH_STRINGS_ID, '')
189 sr.AddResource('STR#', PATH_STRINGS_ID, '') 232
233 if options != saved_options:
234 newdata = reduce(lambda x, y: x+chr(y), options, '')
235 if opr and opr.HomeResFile() == preff_handle:
236 opr.data = newdata
237 opr.ChangedResource()
238 else:
239 opr = Resource(newdata)
240 opr.AddResource('Popt', OPTIONS_ID, '')
190 241
191 CloseResFile(preff_handle) 242 CloseResFile(preff_handle)
192 243
193 def edit_applet(name): 244 def edit_applet(name):
194 pref_handle = openpreffile(READ) 245 pref_handle = openpreffile(READ)
205 sys.exit(0) 256 sys.exit(0)
206 257
207 fss, dr, fss_changed = getprefdir(OVERRIDE_DIRECTORY_ID) 258 fss, dr, fss_changed = getprefdir(OVERRIDE_DIRECTORY_ID)
208 if fss == None: 259 if fss == None:
209 if notfound: 260 if notfound:
210 notfound = notfound + ' and ' + 'directory' 261 notfound = notfound + ', directory'
211 else: 262 else:
212 notfound = 'directory' 263 notfound = 'directory'
213 fss, dummy, dummy2 = getprefdir(DIRECTORY_ID) 264 fss, dummy, dummy2 = getprefdir(DIRECTORY_ID)
214 if fss == None: 265 if fss == None:
215 fss = macfs.FSSpec(os.getcwd()) 266 fss = macfs.FSSpec(os.getcwd())
216 fss_changed = 1 267 fss_changed = 1
268
269 options, opr = getoptions(OVERRIDE_OPTIONS_ID)
270 if not opr:
271 if notfound:
272 notfound = notfound + ', options'
273 else:
274 notfound = 'options'
275 options, dummy = getoptions(OPTIONS_ID)
276 saved_options = options[:]
217 277
218 dummy = dummy2 = None # Discard them. 278 dummy = dummy2 = None # Discard them.
219 279
220 if notfound: 280 if notfound:
221 message('Warning: initial %s taken from system-wide defaults'%notfound) 281 message('Warning: initial %s taken from system-wide defaults'%notfound)
222 # Let the user play away 282 # Let the user play away
223 result = interact(l, fss, name) 283 result = interact(l, fss, options, name)
224 284
225 # See what we have to update, and how 285 # See what we have to update, and how
226 if result == None: 286 if result == None:
227 sys.exit(0) 287 sys.exit(0)
228 288
229 pathlist, nfss = result 289 pathlist, nfss, options = result
230 if nfss != fss: 290 if nfss != fss:
231 fss_changed = 1 291 fss_changed = 1
232 292
233 if fss_changed or pathlist != l: 293 if fss_changed:
234 if fss_changed: 294 alias = nfss.NewAlias()
235 alias = nfss.NewAlias() 295 if dr:
236 if dr: 296 dr.data = alias.data
237 dr.data = alias.data 297 dr.ChangedResource()
238 dr.ChangedResource() 298 else:
239 else: 299 dr = Resource(alias.data)
240 dr = Resource(alias.data) 300 dr.AddResource('alis', OVERRIDE_DIRECTORY_ID, '')
241 dr.AddResource('alis', OVERRIDE_DIRECTORY_ID, '') 301
242 302 if pathlist != l:
243 if pathlist != l: 303 if pathlist == []:
244 if pathlist == []: 304 if sr.HomeResFile() == app_handle:
245 if sr.HomeResFile() == app_handle: 305 sr.RemoveResource()
246 sr.RemoveResource() 306 elif sr and sr.HomeResFile() == app_handle:
247 elif sr and sr.HomeResFile() == app_handle: 307 sr.data = listtores(pathlist)
248 sr.data = listtores(pathlist) 308 sr.ChangedResource()
249 sr.ChangedResource() 309 else:
250 else: 310 sr = Resource(listtores(pathlist))
251 sr = Resource(listtores(pathlist)) 311 sr.AddResource('STR#', OVERRIDE_PATH_STRINGS_ID, '')
252 sr.AddResource('STR#', OVERRIDE_PATH_STRINGS_ID, '') 312
313 if options != saved_options:
314 newdata = reduce(lambda x, y: x+chr(y), options, '')
315 if opr and opr.HomeResFile() == app_handle:
316 opr.data = newdata
317 opr.ChangedResource()
318 else:
319 opr = Resource(newdata)
320 opr.AddResource('Popt', OVERRIDE_OPTIONS_ID, '')
253 321
254 CloseResFile(app_handle) 322 CloseResFile(app_handle)
255 323
256 def main(): 324 def main():
257 try: 325 try: