summaryrefslogtreecommitdiffstats
path: root/Mac/scripts/EditPythonPrefs.py
blob: bc7e2dabd2f5af7111e7aa4927fa5d589e82d28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
"""Edit the Python Preferences file."""
#
# This program is getting more and more clunky. It should really
# be rewritten in a modeless way some time soon.

from Dlg import *
from Events import *
from Res import *
import string
import struct
import macfs
import MacOS
import os
import sys
import Res # For Res.Error

# Resource in the Python resource chain
PREFNAME_NAME="PythonPreferenceFileName"

# resource IDs in our own resources (dialogs, etc)
MESSAGE_ID = 256

DIALOG_ID = 511
TEXT_ITEM = 1
OK_ITEM = 2
CANCEL_ITEM = 3
DIR_ITEM = 4
TITLE_ITEM = 5
OPTIONS_ITEM = 7

# The options dialog. There is a correspondence between
# the dialog item numbers and the option.
OPT_DIALOG_ID = 510
# 1 thru 9 are the options
# The GUSI creator/type and delay-console
OD_CREATOR_ITEM = 10
OD_TYPE_ITEM = 11
OD_DELAYCONSOLE_ITEM = 12
OD_OK_ITEM = 13
OD_CANCEL_ITEM = 14

# Resource IDs in the preferences file
PATH_STRINGS_ID = 128
DIRECTORY_ID = 128
OPTIONS_ID = 128
GUSI_ID = 10240

# Override IDs (in the applet)
OVERRIDE_PATH_STRINGS_ID = 129
OVERRIDE_DIRECTORY_ID = 129
OVERRIDE_OPTIONS_ID = 129
OVERRIDE_GUSI_ID = 10241

# Things we know about the GUSI resource. Note the code knows these too.
GUSIPOS_TYPE=0
GUSIPOS_CREATOR=4
GUSIPOS_SKIP=8
GUSIPOS_FLAGS=9
GUSIPOS_VERSION=10
GUSIVERSION='0181'
GUSIFLAGS_DELAY=0x20 # Mask

READ = 1
WRITE = 2
smAllScripts = -3
kOnSystemDisk = 0x8000

def restolist(data):
	"""Convert STR# resource data to a list of strings"""
	if not data:
		return []
	num, = struct.unpack('h', data[:2])
	data = data[2:]
	rv = []
	for i in range(num):
		strlen = ord(data[0])
		if strlen < 0: strlen = strlen + 256
		str = data[1:strlen+1]
		data = data[strlen+1:]
		rv.append(str)
	return rv
	
def listtores(list):
	"""Convert a list of strings to STR# resource data"""
	rv = struct.pack('h', len(list))
	for str in list:
		rv = rv + chr(len(str)) + str
	return rv

def message(str = "Hello, world!", id = MESSAGE_ID):
	"""Show a simple alert with a text message"""
	d = GetNewDialog(id, -1)
	d.SetDialogDefaultItem(1)
	tp, h, rect = d.GetDialogItem(2)
	SetDialogItemText(h, str)
	while 1:
		n = ModalDialog(None)
		if n == 1: break
		
def optinteract((options, creator, type, delaycons)):
	"""Let the user interact with the options dialog"""
	old_options = (options[:], creator, type, delaycons)
	d = GetNewDialog(OPT_DIALOG_ID, -1)
	tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
	SetDialogItemText(h, creator)
	tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
	SetDialogItemText(h, type)
	d.SetDialogDefaultItem(OD_OK_ITEM)
	d.SetDialogCancelItem(OD_CANCEL_ITEM)
	while 1:
		for i in range(len(options)):
			tp, h, rect = d.GetDialogItem(i+1)
			h.as_Control().SetControlValue(options[i])
		tp, h, rect = d.GetDialogItem(OD_DELAYCONSOLE_ITEM)
		h.as_Control().SetControlValue(delaycons)
		n = ModalDialog(None)
		if n == OD_OK_ITEM:
			tp, h, rect = d.GetDialogItem(OD_CREATOR_ITEM)
			ncreator = GetDialogItemText(h)
			tp, h, rect = d.GetDialogItem(OD_TYPE_ITEM)
			ntype = GetDialogItemText(h)
			if len(ncreator) == 4 and len(ntype) == 4:
				return options, ncreator, ntype, delaycons
			else:
				sys.stderr.write('\007')
		elif n == OD_CANCEL_ITEM:
			return old_options
		elif n in (OD_CREATOR_ITEM, OD_TYPE_ITEM):
			pass
		elif n == OD_DELAYCONSOLE_ITEM:
			delaycons = (not delaycons)
		elif 1 <= n <= len(options):
			options[n-1] = (not options[n-1])

			
def interact(list, pythondir, options, title):
	"""Let the user interact with the dialog"""
	opythondir = pythondir
	try:
		# Try to go to the "correct" dir for GetDirectory
		os.chdir(pythondir.as_pathname())
	except os.error:
		pass
	d = GetNewDialog(DIALOG_ID, -1)
	tp, h, rect = d.GetDialogItem(TITLE_ITEM)
	SetDialogItemText(h, title)
	tp, h, rect = d.GetDialogItem(TEXT_ITEM)
##	SetDialogItemText(h, string.joinfields(list, '\r'))
	h.data = string.joinfields(list, '\r')
	d.SelectDialogItemText(TEXT_ITEM, 0, 32767)
	d.SelectDialogItemText(TEXT_ITEM, 0, 0)
##	d.SetDialogDefaultItem(OK_ITEM)
	d.SetDialogCancelItem(CANCEL_ITEM)
	d.GetDialogWindow().ShowWindow()
	d.DrawDialog()
	while 1:
		n = ModalDialog(None)
		if n == OK_ITEM:
			break
		if n == CANCEL_ITEM:
			return None
##		if n == REVERT_ITEM:
##			return [], pythondir
		if n == DIR_ITEM:
			fss, ok = macfs.GetDirectory('Select python home folder:')
			if ok:
				pythondir = fss
		if n == OPTIONS_ITEM:
			options = optinteract(options)
	tmp = string.splitfields(h.data, '\r')
	rv = []
	for i in tmp:
		if i:
			rv.append(i)
	return rv, pythondir, options
	
def getprefpath(id):
	# Load the path and directory resources
	try:
		sr = GetResource('STR#', id)
	except (MacOS.Error, Res.Error):
		return None, None
	d = sr.data
	l = restolist(d)
	return l, sr

def getprefdir(id):
	try:
		dr = GetResource('alis', id)
		fss, fss_changed = macfs.RawAlias(dr.data).Resolve()
	except (MacOS.Error, Res.Error):
		return None, None, 1
	return fss, dr, fss_changed

def getoptions(id):
	try:
		opr = GetResource('Popt', id)
	except (MacOS.Error, Res.Error):
		return [0]*9, None
	options = map(lambda x: ord(x), opr.data)
	while len(options) < 9:
		options = options + [0]
	return options, opr
	
def getgusioptions(id):
	try:
		opr = GetResource('GU\267I', id)
	except (MacOS.Error, Res.Error):
		return '????', '????', 0, None
	data = opr.data
	type = data[GUSIPOS_TYPE:GUSIPOS_TYPE+4]
	creator = data[GUSIPOS_CREATOR:GUSIPOS_CREATOR+4]
	flags = ord(data[GUSIPOS_FLAGS])
	version = data[GUSIPOS_VERSION:GUSIPOS_VERSION+4]
	if version <> GUSIVERSION:
		message('GU\267I resource version "%s", fixing to "%s"'%(version, GUSIVERSION))
		flags = 0
	delay = (not not (flags & GUSIFLAGS_DELAY))
	return creator, type, delay, opr
	
def setgusioptions(opr, creator, type, delay):
	data = opr.data
	flags = ord(data[GUSIPOS_FLAGS])
	version = data[GUSIPOS_VERSION:GUSIPOS_VERSION+4]
	if version <> GUSIVERSION:
		flags = 0x88
		version = GUSIVERSION
	if delay:
		flags = flags | GUSIFLAGS_DELAY
	else:
		flags = flags & ~GUSIFLAGS_DELAY
	data = type + creator + data[GUSIPOS_SKIP] + chr(flags) + GUSIVERSION + data[GUSIPOS_VERSION+4:]
	return data
	
def openpreffile(rw):
	# Find the preferences folder and our prefs file, create if needed.	
	vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0)
	try:
		pnhandle = GetNamedResource('STR ', PREFNAME_NAME)
	except Res.Error:
		message("No %s resource (old Python?)"%PREFNAME_NAME)
		sys.exit(1)
	prefname = pnhandle.data[1:]
	preff_fss = macfs.FSSpec((vrefnum, dirid, prefname))
	try:
		preff_handle = FSpOpenResFile(preff_fss, rw)
	except Res.Error:
		# Create it
		message('No preferences file, creating one...')
		FSpCreateResFile(preff_fss, 'Pyth', 'pref', smAllScripts)
		preff_handle = FSpOpenResFile(preff_fss, rw)
	return preff_handle
	
def openapplet(name):
	fss = macfs.FSSpec(name)
	try:
		app_handle = FSpOpenResFile(fss, WRITE)
	except Res.Error:
		message('File does not have a resource fork.')
		sys.exit(0)
	return app_handle
		
	
def edit_preferences():
	preff_handle = openpreffile(WRITE)
	
	l, sr = getprefpath(PATH_STRINGS_ID)
	if l == None:	
		message('Cannot find any sys.path resource! (Old python?)')
		sys.exit(0)
		
	fss, dr, fss_changed = getprefdir(DIRECTORY_ID)
	if fss == None:
		fss = macfs.FSSpec(os.getcwd())
		fss_changed = 1
		
	options, opr = getoptions(OPTIONS_ID)
	saved_options = options[:]
	
	creator, type, delaycons, gusi_opr = getgusioptions(GUSI_ID)
	saved_gusi_options = creator, type, delaycons
	
	# Let the user play away
	result = interact(l, fss, (options, creator, type, delaycons),
			 'System-wide preferences')
	
	# See what we have to update, and how
	if result == None:
		sys.exit(0)
		
	pathlist, nfss, (options, creator, type, delaycons) = result
	if nfss != fss:
		fss_changed = 1
		
	if fss_changed:
		alias = nfss.NewAlias()
		if dr:
			dr.data = alias.data
			dr.ChangedResource()
		else:
			dr = Resource(alias.data)
			dr.AddResource('alis', DIRECTORY_ID, '')
			
	if pathlist != l:
		if pathlist == []:
			if sr.HomeResFile() == preff_handle:
				sr.RemoveResource()
		elif sr.HomeResFile() == preff_handle:
			sr.data = listtores(pathlist)
			sr.ChangedResource()
		else:
			sr = Resource(listtores(pathlist))
			sr.AddResource('STR#', PATH_STRINGS_ID, '')
			
	if options != saved_options:
		newdata = reduce(lambda x, y: x+chr(y), options, '')
		if opr and opr.HomeResFile() == preff_handle:
			opr.data = newdata
			opr.ChangedResource()
		else:
			opr = Resource(newdata)
			opr.AddResource('Popt', OPTIONS_ID, '')
			
	if (creator, type, delaycons) != saved_gusi_options:
		newdata = setgusioptions(gusi_opr, creator, type, delaycons)
		if gusi_opr.HomeResFile() == preff_handle:
			gusi_opr.data = newdata
			gusi_opr.ChangedResource()
		else:
			ngusi_opr = Resource(newdata)
			ngusi_opr.AddResource('GU\267I', GUSI_ID, '')
				
	CloseResFile(preff_handle)
	
def edit_applet(name):
	pref_handle = openpreffile(READ)
	app_handle = openapplet(name)
	
	notfound = ''
	l, sr = getprefpath(OVERRIDE_PATH_STRINGS_ID)
	if l == None:
		notfound = 'path'
		
		l, dummy = getprefpath(PATH_STRINGS_ID)
		if l == None:	
			message('Cannot find any sys.path resource! (Old python?)')
			sys.exit(0)
		
	fss, dr, fss_changed = getprefdir(OVERRIDE_DIRECTORY_ID)
	if fss == None:
		if notfound:
			notfound = notfound + ', directory'
		else:
			notfound = 'directory'
		fss, dummy, dummy2 = getprefdir(DIRECTORY_ID)
		if fss == None:
			fss = macfs.FSSpec(os.getcwd())
			fss_changed = 1

	options, opr = getoptions(OVERRIDE_OPTIONS_ID)
	if not opr:
		if notfound:
			notfound = notfound + ', options'
		else:
			notfound = 'options'
		options, dummy = getoptions(OPTIONS_ID)
	saved_options = options[:]
	
	creator, type, delaycons, gusi_opr = getgusioptions(OVERRIDE_GUSI_ID)
	if not gusi_opr:
		if notfound:
			notfound = notfound + ', GUSI options'
		else:
			notfound = 'GUSI options'
		creator, type, delaycons, gusi_opr = getgusioptions(GUSI_ID)
	saved_gusi_options = creator, type, delaycons
	
	dummy = dummy2 = None # Discard them.
	
	if notfound:
		message('Warning: initial %s taken from system-wide defaults'%notfound)
	# Let the user play away
	result = interact(l, fss, (options, creator, type, delaycons), name)
	
	# See what we have to update, and how
	if result == None:
		sys.exit(0)
		
	pathlist, nfss, (options, creator, type, delaycons) = result
	if nfss != fss:
		fss_changed = 1
		
	if fss_changed:
		alias = nfss.NewAlias()
		if dr:
			dr.data = alias.data
			dr.ChangedResource()
		else:
			dr = Resource(alias.data)
			dr.AddResource('alis', OVERRIDE_DIRECTORY_ID, '')
			
	if pathlist != l:
		if pathlist == []:
			if sr.HomeResFile() == app_handle:
				sr.RemoveResource()
		elif sr and sr.HomeResFile() == app_handle:
			sr.data = listtores(pathlist)
			sr.ChangedResource()
		else:
			sr = Resource(listtores(pathlist))
			sr.AddResource('STR#', OVERRIDE_PATH_STRINGS_ID, '')
			
	if options != saved_options:
		newdata = reduce(lambda x, y: x+chr(y), options, '')
		if opr and opr.HomeResFile() == app_handle:
			opr.data = newdata
			opr.ChangedResource()
		else:
			opr = Resource(newdata)
			opr.AddResource('Popt', OVERRIDE_OPTIONS_ID, '')
			
	if (creator, type, delaycons) != saved_gusi_options:
		newdata = setgusioptions(gusi_opr, creator, type, delaycons)
		id, type, name = gusi_opr.GetResInfo()
		if gusi_opr.HomeResFile() == app_handle and id == OVERRIDE_GUSI_ID:
			gusi_opr.data = newdata
			gusi_opr.ChangedResource()
		else:
			ngusi_opr = Resource(newdata)
			ngusi_opr.AddResource('GU\267I', OVERRIDE_GUSI_ID, '')
			
	CloseResFile(app_handle)

def main():
	try:
		h = OpenResFile('EditPythonPrefs.rsrc')
	except Res.Error:
		pass	# Assume we already have acces to our own resource
	
	if len(sys.argv) <= 1:
		edit_preferences()
	else:
		for appl in sys.argv[1:]:
			edit_applet(appl)
		

if __name__ == '__main__':
	main()