comparison Mac/scripts/EditPythonPrefs.py @ 3484:ce27e0328462 legacy-trunk

Added support for editing override preferences in applets
author Jack Jansen <jack.jansen@cwi.nl>
date Thu, 04 Apr 1996 15:38:44 +0000
parents 8b3f2a8620f5
children e8d501c88a3e
comparison
equal deleted inserted replaced
3483:fed93d2d88fc 3484:ce27e0328462
16 16
17 DIALOG_ID = 512 17 DIALOG_ID = 512
18 TEXT_ITEM = 1 18 TEXT_ITEM = 1
19 OK_ITEM = 2 19 OK_ITEM = 2
20 CANCEL_ITEM = 3 20 CANCEL_ITEM = 3
21 REVERT_ITEM = 4 21 DIR_ITEM = 4
22 DIR_ITEM = 5 22 TITLE_ITEM = 5
23 23
24 # Resource IDs in the preferences file 24 # Resource IDs in the preferences file
25 PATH_STRINGS_ID = 128 25 PATH_STRINGS_ID = 128
26 DIRECTORY_ID = 128 26 DIRECTORY_ID = 128
27 27 OPTIONS_ID = 128
28
29 # Override IDs (in the applet)
30 OVERRIDE_PATH_STRINGS_ID = 129
31 OVERRIDE_DIRECTORY_ID = 129
32 OVERRIDE_OPTIONS_ID = 129
33
34 READ = 1
28 WRITE = 2 35 WRITE = 2
29 smAllScripts = -3 36 smAllScripts = -3
30 kOnSystemDisk = 0x8000 37 kOnSystemDisk = 0x8000
31 38
32 def restolist(data): 39 def restolist(data):
59 SetDialogItemText(h, str) 66 SetDialogItemText(h, str)
60 while 1: 67 while 1:
61 n = ModalDialog(None) 68 n = ModalDialog(None)
62 if n == 1: break 69 if n == 1: break
63 70
64 def interact(list, pythondir): 71 def interact(list, pythondir, title):
65 """Let the user interact with the dialog""" 72 """Let the user interact with the dialog"""
66 opythondir = pythondir 73 opythondir = pythondir
67 try: 74 try:
68 # Try to go to the "correct" dir for GetDirectory 75 # Try to go to the "correct" dir for GetDirectory
69 os.chdir(pythondir.as_pathname()) 76 os.chdir(pythondir.as_pathname())
70 except os.error: 77 except os.error:
71 pass 78 pass
72 d = GetNewDialog(DIALOG_ID, -1) 79 d = GetNewDialog(DIALOG_ID, -1)
73 tp, h, rect = d.GetDialogItem(1) 80 tp, h, rect = d.GetDialogItem(TITLE_ITEM)
81 SetDialogItemText(h, title)
82 tp, h, rect = d.GetDialogItem(TEXT_ITEM)
74 SetDialogItemText(h, string.joinfields(list, '\r')) 83 SetDialogItemText(h, string.joinfields(list, '\r'))
84 ## d.SetDialogDefaultItem(OK_ITEM)
85 d.SetDialogCancelItem(CANCEL_ITEM)
75 while 1: 86 while 1:
76 n = ModalDialog(None) 87 n = ModalDialog(None)
77 if n == OK_ITEM: 88 if n == OK_ITEM:
78 break 89 break
79 if n == CANCEL_ITEM: 90 if n == CANCEL_ITEM:
80 return None 91 return None
81 if n == REVERT_ITEM: 92 ## if n == REVERT_ITEM:
82 return [], pythondir 93 ## return [], pythondir
83 if n == DIR_ITEM: 94 if n == DIR_ITEM:
84 fss, ok = macfs.GetDirectory('Select python home folder:') 95 fss, ok = macfs.GetDirectory('Select python home folder:')
85 if ok: 96 if ok:
86 pythondir = fss 97 pythondir = fss
87 tmp = string.splitfields(GetDialogItemText(h), '\r') 98 tmp = string.splitfields(GetDialogItemText(h), '\r')
89 for i in tmp: 100 for i in tmp:
90 if i: 101 if i:
91 rv.append(i) 102 rv.append(i)
92 return rv, pythondir 103 return rv, pythondir
93 104
94 def main(): 105 def getprefpath(id):
95 try: 106 # Load the path and directory resources
96 h = OpenResFile('EditPythonPrefs.rsrc') 107 try:
97 except Res.Error: 108 sr = GetResource('STR#', id)
98 pass # Assume we already have acces to our own resource 109 except (MacOS.Error, Res.Error):
99 110 return None, None
111 d = sr.data
112 l = restolist(d)
113 return l, sr
114
115 def getprefdir(id):
116 try:
117 dr = GetResource('alis', id)
118 fss, fss_changed = macfs.RawAlias(dr.data).Resolve()
119 except (MacOS.Error, Res.Error):
120 return None, None, 1
121 return fss, dr, fss_changed
122
123 def openpreffile(rw):
100 # Find the preferences folder and our prefs file, create if needed. 124 # Find the preferences folder and our prefs file, create if needed.
101 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0) 125 vrefnum, dirid = macfs.FindFolder(kOnSystemDisk, 'pref', 0)
102 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences')) 126 preff_fss = macfs.FSSpec((vrefnum, dirid, 'Python Preferences'))
103 try: 127 try:
104 preff_handle = FSpOpenResFile(preff_fss, WRITE) 128 preff_handle = FSpOpenResFile(preff_fss, rw)
105 except Res.Error: 129 except Res.Error:
106 # Create it 130 # Create it
107 message('No preferences file, creating one...') 131 message('No preferences file, creating one...')
108 FSpCreateResFile(preff_fss, 'Pyth', 'pref', smAllScripts) 132 FSpCreateResFile(preff_fss, 'Pyth', 'pref', smAllScripts)
109 preff_handle = FSpOpenResFile(preff_fss, WRITE) 133 preff_handle = FSpOpenResFile(preff_fss, rw)
110 134 return preff_handle
111 # Load the path and directory resources 135
112 try: 136 def openapplet(name):
113 sr = GetResource('STR#', PATH_STRINGS_ID) 137 fss = macfs.FSSpec(name)
114 except (MacOS.Error, Res.Error): 138 try:
139 app_handle = FSpOpenResFile(fss, WRITE)
140 except Res.Error:
141 message('File does not have a resource fork.')
142 sys.exit(0)
143 return app_handle
144
145
146 def edit_preferences():
147 preff_handle = openpreffile(WRITE)
148
149 l, sr = getprefpath(PATH_STRINGS_ID)
150 if l == None:
115 message('Cannot find any sys.path resource! (Old python?)') 151 message('Cannot find any sys.path resource! (Old python?)')
116 sys.exit(0) 152 sys.exit(0)
117 d = sr.data 153
118 l = restolist(d) 154 fss, dr, fss_changed = getprefdir(DIRECTORY_ID)
119 155 if fss == None:
120 try:
121 dr = GetResource('alis', DIRECTORY_ID)
122 fss, fss_changed = macfs.RawAlias(dr.data).Resolve()
123 except (MacOS.Error, Res.Error):
124 dr = None
125 fss = macfs.FSSpec(os.getcwd()) 156 fss = macfs.FSSpec(os.getcwd())
126 fss_changed = 1 157 fss_changed = 1
127 158
128 # Let the user play away 159 # Let the user play away
129 result = interact(l, fss) 160 result = interact(l, fss, 'System-wide preferences')
130 161
131 # See what we have to update, and how 162 # See what we have to update, and how
132 if result == None: 163 if result == None:
133 sys.exit(0) 164 sys.exit(0)
134 165
156 else: 187 else:
157 sr = Resource(listtores(pathlist)) 188 sr = Resource(listtores(pathlist))
158 sr.AddResource('STR#', PATH_STRINGS_ID, '') 189 sr.AddResource('STR#', PATH_STRINGS_ID, '')
159 190
160 CloseResFile(preff_handle) 191 CloseResFile(preff_handle)
192
193 def edit_applet(name):
194 pref_handle = openpreffile(READ)
195 app_handle = openapplet(name)
196
197 notfound = ''
198 l, sr = getprefpath(OVERRIDE_PATH_STRINGS_ID)
199 if l == None:
200 notfound = 'path'
201
202 l, dummy = getprefpath(PATH_STRINGS_ID)
203 if l == None:
204 message('Cannot find any sys.path resource! (Old python?)')
205 sys.exit(0)
206
207 fss, dr, fss_changed = getprefdir(OVERRIDE_DIRECTORY_ID)
208 if fss == None:
209 if notfound:
210 notfound = notfound + ' and ' + 'directory'
211 else:
212 notfound = 'directory'
213 fss, dummy, dummy2 = getprefdir(DIRECTORY_ID)
214 if fss == None:
215 fss = macfs.FSSpec(os.getcwd())
216 fss_changed = 1
217
218 dummy = dummy2 = None # Discard them.
219
220 if notfound:
221 message('Warning: initial %s taken from system-wide defaults'%notfound)
222 # Let the user play away
223 result = interact(l, fss, name)
224
225 # See what we have to update, and how
226 if result == None:
227 sys.exit(0)
228
229 pathlist, nfss = result
230 if nfss != fss:
231 fss_changed = 1
232
233 if fss_changed or pathlist != l:
234 if fss_changed:
235 alias = nfss.NewAlias()
236 if dr:
237 dr.data = alias.data
238 dr.ChangedResource()
239 else:
240 dr = Resource(alias.data)
241 dr.AddResource('alis', OVERRIDE_DIRECTORY_ID, '')
242
243 if pathlist != l:
244 if pathlist == []:
245 if sr.HomeResFile() == app_handle:
246 sr.RemoveResource()
247 elif sr and sr.HomeResFile() == app_handle:
248 sr.data = listtores(pathlist)
249 sr.ChangedResource()
250 else:
251 sr = Resource(listtores(pathlist))
252 sr.AddResource('STR#', OVERRIDE_PATH_STRINGS_ID, '')
253
254 CloseResFile(app_handle)
255
256 def main():
257 try:
258 h = OpenResFile('EditPythonPrefs.rsrc')
259 except Res.Error:
260 pass # Assume we already have acces to our own resource
261
262 if len(sys.argv) <= 1:
263 edit_preferences()
264 else:
265 for appl in sys.argv[1:]:
266 edit_applet(appl)
267
161 268
162 if __name__ == '__main__': 269 if __name__ == '__main__':
163 print # Stupid, to init toolboxes... 270 print # Stupid, to init toolboxes...
164 main() 271 main()